Skip to content

Commit 25ea953

Browse files
authored
feat: refactor to split AST specification out as its own module (typescript-eslint#2911)
1 parent 209f6d0 commit 25ea953

File tree

282 files changed

+3647
-1847
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

282 files changed

+3647
-1847
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ coverage
77
__snapshots__
88

99
packages/eslint-plugin-tslint/tests
10+
11+
# Files copied as part of the build
12+
packages/types/src/ast-spec.ts

.eslintrc.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
'import',
88
'eslint-comments',
99
'@typescript-eslint/internal',
10+
'simple-import-sort',
1011
],
1112
env: {
1213
es6: true,
@@ -261,5 +262,27 @@ module.exports = {
261262
'@typescript-eslint/internal/prefer-ast-types-enum': 'off',
262263
},
263264
},
265+
// ast spec specific standardization
266+
{
267+
files: ['packages/ast-spec/src/**/*.ts'],
268+
rules: {
269+
'@typescript-eslint/consistent-type-imports': [
270+
'error',
271+
{ prefer: 'type-imports', disallowTypeAnnotations: true },
272+
],
273+
'@typescript-eslint/no-unused-vars': 'error',
274+
'@typescript-eslint/sort-type-union-intersection-members': 'error',
275+
'import/first': 'error',
276+
'import/newline-after-import': 'error',
277+
'import/no-duplicates': 'error',
278+
'simple-import-sort/imports': 'error',
279+
},
280+
},
281+
{
282+
files: ['rollup.config.ts'],
283+
rules: {
284+
'import/no-default-export': 'off',
285+
},
286+
},
264287
],
265288
};

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ dist
6767
_ts3.4
6868
*.tsbuildinfo
6969
.watchmanconfig
70+
.rollup.cache
71+
72+
# Files copied as part of the build
73+
packages/types/src/ast-spec.ts

.vscode/settings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
// typescript auto-format settings
1515
"typescript.tsdk": "node_modules/typescript/lib",
16-
"javascript.preferences.importModuleSpecifier": "auto",
17-
"typescript.preferences.importModuleSpecifier": "auto",
16+
"javascript.preferences.importModuleSpecifier": "project-relative",
17+
"typescript.preferences.importModuleSpecifier": "project-relative",
1818
"javascript.preferences.quoteStyle": "single",
1919
"typescript.preferences.quoteStyle": "single",
2020
"editor.defaultFormatter": "esbenp.prettier-vscode",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
1818
},
1919
"scripts": {
20-
"build": "lerna run build",
20+
"build": "lerna run build --ignore ast-spec",
2121
"check:clean-workspace-after-install": "git diff --quiet --exit-code",
2222
"check:configs": "lerna run check:configs",
2323
"check:docs": "lerna run check:docs",
@@ -98,6 +98,7 @@
9898
"eslint-plugin-eslint-plugin": "^3.0.0",
9999
"eslint-plugin-import": "^2.22.0",
100100
"eslint-plugin-jest": "^24.1.3",
101+
"eslint-plugin-simple-import-sort": "^7.0.0",
101102
"glob": "^7.1.6",
102103
"husky": "^5.0.9",
103104
"jest": "^26.6.3",

packages/ast-spec/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 TypeScript ESLint and other contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

packages/ast-spec/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<h1 align="center">TypeScript-ESTree AST Specification</h1>
2+
3+
<p align="center">
4+
<img src="https://github.com/typescript-eslint/typescript-eslint/workflows/CI/badge.svg" alt="CI" />
5+
<a href="https://www.npmjs.com/package/@typescript-eslint/ast-spec"><img src="https://img.shields.io/npm/v/@typescript-eslint/ast-spec.svg?style=flat-square" alt="NPM Version" /></a>
6+
<a href="https://www.npmjs.com/package/@typescript-eslint/ast-spec"><img src="https://img.shields.io/npm/dm/@typescript-eslint/ast-spec.svg?style=flat-square" alt="NPM Downloads" /></a>
7+
</p>
8+
9+
This is the complete specification for the TypeScript-ESTree AST.
10+
11+
It includes:
12+
13+
- Node definitions as TypeScript types (the specification)
14+
- Logic for converting from the TypeScript AST to the TypeScript-ESTree AST.
15+
- Tests/Fixtures/Examples for each Node
16+
17+
**You probably don't want to use it directly.**
18+
19+
If you're building an ESLint plugin, consider using [`@typescript-eslint/experimental-utils`](../experimental-utils).
20+
If you're parsing TypeScript code, consider using [`@typescript-eslint/typescript-estree`](../typescript-estree).
21+
22+
## Contributing
23+
24+
[See the contributing guide here](../../CONTRIBUTING.md)

packages/ast-spec/api-extractor.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"mainEntryPointFilePath": "<projectFolder>/dist/index.d.ts",
3+
"apiReport": {
4+
"enabled": false
5+
},
6+
"docModel": {
7+
"enabled": false
8+
},
9+
"dtsRollup": {
10+
"enabled": true,
11+
"untrimmedFilePath": "<projectFolder>/dist/ast-spec.ts"
12+
},
13+
"tsdocMetadata": {
14+
"enabled": false
15+
},
16+
"messages": {
17+
"extractorMessageReporting": {
18+
"default": {
19+
"logLevel": "none"
20+
},
21+
"ae-forgotten-export": {
22+
"logLevel": "none"
23+
}
24+
},
25+
"tsdocMessageReporting": {
26+
"default": {
27+
"logLevel": "none"
28+
}
29+
}
30+
}
31+
}

packages/ast-spec/jest.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
// @ts-check
4+
/** @type {import('@jest/types').Config.InitialOptions} */
5+
module.exports = {
6+
globals: {
7+
'ts-jest': {
8+
isolatedModules: true,
9+
},
10+
},
11+
testEnvironment: 'node',
12+
transform: {
13+
['^.+\\.tsx?$']: 'ts-jest',
14+
},
15+
testRegex: ['./tests/.+\\.test\\.ts$'],
16+
collectCoverage: false,
17+
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
18+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
19+
coverageReporters: ['text-summary', 'lcov'],
20+
};

packages/ast-spec/package.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"name": "@typescript-eslint/ast-spec",
3+
"version": "4.20.0",
4+
"description": "TypeScript-ESTree AST spec",
5+
"private": true,
6+
"keywords": [
7+
"eslint",
8+
"typescript",
9+
"estree"
10+
],
11+
"engines": {
12+
"node": "^8.10.0 || ^10.13.0 || >=11.10.1"
13+
},
14+
"files": [
15+
"dist",
16+
"package.json",
17+
"README.md",
18+
"LICENSE"
19+
],
20+
"repository": {
21+
"type": "git",
22+
"url": "https://github.com/typescript-eslint/typescript-eslint.git",
23+
"directory": "packages/ast-spec"
24+
},
25+
"bugs": {
26+
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
27+
},
28+
"license": "MIT",
29+
"main": "dist/index.js",
30+
"types": "dist/index.d.ts",
31+
"scripts": {
32+
"build": "tsc -b tsconfig.build.json && api-extractor run --local",
33+
"clean": "tsc -b tsconfig.build.json --clean",
34+
"postclean": "rimraf dist && rimraf _ts3.4 && rimraf .rollup.cache && rimraf coverage",
35+
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
36+
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
37+
"typecheck": "tsc -p tsconfig.json --noEmit"
38+
},
39+
"funding": {
40+
"type": "opencollective",
41+
"url": "https://opencollective.com/typescript-eslint"
42+
},
43+
"devDependencies": {
44+
"@microsoft/api-extractor": "^7.13.2"
45+
}
46+
}

packages/types/src/ast-node-types.ts renamed to packages/ast-spec/src/ast-node-types.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
enum AST_NODE_TYPES {
1+
export enum AST_NODE_TYPES {
22
ArrayExpression = 'ArrayExpression',
33
ArrayPattern = 'ArrayPattern',
44
ArrowFunctionExpression = 'ArrowFunctionExpression',
@@ -164,23 +164,3 @@ enum AST_NODE_TYPES {
164164
TSUnknownKeyword = 'TSUnknownKeyword',
165165
TSVoidKeyword = 'TSVoidKeyword',
166166
}
167-
168-
export { AST_NODE_TYPES };
169-
170-
// Below is a special type-only test which ensures that we don't accidentally leave unused keys in this enum
171-
// eslint-disable-next-line import/first -- purposely down here to colocate it with this hack of a test
172-
import type { Node } from './ts-estree';
173-
174-
type GetKeys<T extends AST_NODE_TYPES> = keyof Extract<Node, { type: T }>;
175-
176-
type AllKeys = {
177-
readonly [T in AST_NODE_TYPES]: GetKeys<T>;
178-
};
179-
180-
type TakesString<T extends Record<string, string>> = T;
181-
182-
// @ts-expect-error: purposely unused
183-
type _Test =
184-
// forcing the test onto a new line so it isn't covered by the expect error
185-
// If there are any enum members that don't have a corresponding TSESTree.Node, then this line will error with "Type 'string | number | symbol' is not assignable to type 'string'."
186-
void | TakesString<AllKeys>;

packages/types/src/ast-token-types.ts renamed to packages/ast-spec/src/ast-token-types.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
enum AST_TOKEN_TYPES {
1+
export enum AST_TOKEN_TYPES {
22
Boolean = 'Boolean',
33
Identifier = 'Identifier',
44
JSXIdentifier = 'JSXIdentifier',
@@ -15,5 +15,3 @@ enum AST_TOKEN_TYPES {
1515
Block = 'Block',
1616
Line = 'Line',
1717
}
18-
19-
export { AST_TOKEN_TYPES };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Accessibility = 'private' | 'protected' | 'public';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// import type { Node } from '../unions/Node';
2+
import type { Range } from './Range';
3+
import type { SourceLocation } from './SourceLocation';
4+
5+
export interface BaseNode {
6+
/**
7+
* The source location information of the node.
8+
* @see {SourceLocation}
9+
*/
10+
loc: SourceLocation;
11+
/**
12+
* @see {Range}
13+
*/
14+
range: Range;
15+
/**
16+
* The parent node of the current node
17+
*/
18+
// parent?: Node;
19+
20+
// every node *will* have a type, but let the nodes define their own exact string
21+
// type: string;
22+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { BaseNode } from './BaseNode';
2+
3+
/*
4+
* Token and Comment are pseudo-nodes to represent pieces of source code
5+
*/
6+
export interface BaseToken extends BaseNode {
7+
value: string;
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { Expression } from '../unions/Expression';
2+
import type { BaseNode } from './BaseNode';
3+
4+
export interface BinaryExpressionBase extends BaseNode {
5+
operator: string;
6+
left: Expression;
7+
right: Expression;
8+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { Identifier } from '../expression/Identifier/spec';
2+
import type { ClassBody } from '../special/ClassBody/spec';
3+
import type { Decorator } from '../special/Decorator/spec';
4+
import type { TSClassImplements } from '../special/TSClassImplements/spec';
5+
import type { TSTypeParameterDeclaration } from '../special/TSTypeParameterDeclaration/spec';
6+
import type { TSTypeParameterInstantiation } from '../special/TSTypeParameterInstantiation/spec';
7+
import type { LeftHandSideExpression } from '../unions/LeftHandSideExpression';
8+
import type { BaseNode } from './BaseNode';
9+
10+
export interface ClassDeclarationBase extends BaseNode {
11+
typeParameters?: TSTypeParameterDeclaration;
12+
superTypeParameters?: TSTypeParameterInstantiation;
13+
id: Identifier | null;
14+
body: ClassBody;
15+
superClass: LeftHandSideExpression | null;
16+
implements?: TSClassImplements[];
17+
abstract?: boolean;
18+
declare?: boolean;
19+
decorators?: Decorator[];
20+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Decorator } from '../special/Decorator/spec';
2+
import type { TSTypeAnnotation } from '../special/TSTypeAnnotation/spec';
3+
import type { Expression } from '../unions/Expression';
4+
import type {
5+
PropertyName,
6+
PropertyNameComputed,
7+
PropertyNameNonComputed,
8+
} from '../unions/PropertyName';
9+
import type { Accessibility } from './Accessibility';
10+
import type { BaseNode } from './BaseNode';
11+
12+
interface ClassPropertyBase extends BaseNode {
13+
key: PropertyName;
14+
value: Expression | null;
15+
computed: boolean;
16+
static: boolean;
17+
declare: boolean;
18+
readonly?: boolean;
19+
decorators?: Decorator[];
20+
accessibility?: Accessibility;
21+
optional?: boolean;
22+
definite?: boolean;
23+
typeAnnotation?: TSTypeAnnotation;
24+
}
25+
26+
export interface ClassPropertyComputedNameBase extends ClassPropertyBase {
27+
key: PropertyNameComputed;
28+
computed: true;
29+
}
30+
31+
export interface ClassPropertyNonComputedNameBase extends ClassPropertyBase {
32+
key: PropertyNameNonComputed;
33+
computed: false;
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Identifier } from '../expression/Identifier/spec';
2+
import type { TSTypeAnnotation } from '../special/TSTypeAnnotation/spec';
3+
import type { TSTypeParameterDeclaration } from '../special/TSTypeParameterDeclaration/spec';
4+
import type { BlockStatement } from '../statement/BlockStatement/spec';
5+
import type { Parameter } from '../unions/Parameter';
6+
import type { BaseNode } from './BaseNode';
7+
8+
export interface FunctionDeclarationBase extends BaseNode {
9+
id: Identifier | null;
10+
generator: boolean;
11+
expression: boolean;
12+
async: boolean;
13+
params: Parameter[];
14+
body?: BlockStatement | null;
15+
returnType?: TSTypeAnnotation;
16+
typeParameters?: TSTypeParameterDeclaration;
17+
declare?: boolean;
18+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface LineAndColumnData {
2+
/**
3+
* Line number (1-indexed)
4+
*/
5+
line: number;
6+
/**
7+
* Column number on the line (0-indexed)
8+
*/
9+
column: number;
10+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy