Skip to content

docs(ast-spec): add script to generates code strings for the doc files #3248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
'import',
'eslint-comments',
'@typescript-eslint/internal',
'simple-import-sort',
],
env: {
es6: true,
Expand Down Expand Up @@ -255,5 +256,21 @@ module.exports = {
'@typescript-eslint/internal/prefer-ast-types-enum': 'off',
},
},
// ast spec specific standardization
{
files: ['packages/ast-spec/src/**/*.ts'],
rules: {
'@typescript-eslint/consistent-type-imports': [
'error',
{ prefer: 'type-imports', disallowTypeAnnotations: true },
],
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/sort-type-union-intersection-members': 'error',
'import/first': 'error',
'import/newline-after-import': 'error',
'import/no-duplicates': 'error',
'simple-import-sort/imports': 'error',
},
},
],
};
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

// typescript auto-format settings
"typescript.tsdk": "node_modules/typescript/lib",
"javascript.preferences.importModuleSpecifier": "auto",
"typescript.preferences.importModuleSpecifier": "auto",
"javascript.preferences.importModuleSpecifier": "project-relative",
"typescript.preferences.importModuleSpecifier": "project-relative",
"javascript.preferences.quoteStyle": "single",
"typescript.preferences.quoteStyle": "single",
"editor.defaultFormatter": "esbenp.prettier-vscode",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"eslint-plugin-eslint-plugin": "^2.3.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^24.1.3",
"eslint-plugin-simple-import-sort": "^7.0.0",
"glob": "^7.1.6",
"husky": "^5.0.9",
"isomorphic-fetch": "^3.0.0",
Expand Down
21 changes: 21 additions & 0 deletions packages/ast-spec/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 TypeScript ESLint and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
24 changes: 24 additions & 0 deletions packages/ast-spec/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<h1 align="center">TypeScript-ESTree AST Specification</h1>

<p align="center">
<img src="https://github.com/typescript-eslint/typescript-eslint/workflows/CI/badge.svg" alt="CI" />
<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>
<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>
</p>

This is the complete specification for the TypeScript-ESTree AST.

It includes:

- Node definitions as TypeScript types (the specification)
- Logic for converting from the TypeScript AST to the TypeScript-ESTree AST.
- Tests/Fixtures/Examples for each Node

**You probably don't want to use it directly.**

If you're building an ESLint plugin, consider using [`@typescript-eslint/experimental-utils`](../experimental-utils).
If you're parsing TypeScript code, consider using [`@typescript-eslint/typescript-estree`](../typescript-estree).

## Contributing

[See the contributing guide here](../../CONTRIBUTING.md)
20 changes: 20 additions & 0 deletions packages/ast-spec/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

// @ts-check
/** @type {import('@jest/types').Config.InitialOptions} */
module.exports = {
globals: {
'ts-jest': {
isolatedModules: true,
},
},
testEnvironment: 'node',
transform: {
['^.+\\.tsx?$']: 'ts-jest',
},
testRegex: ['./tests/.+\\.test\\.ts$'],
collectCoverage: false,
collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
coverageReporters: ['text-summary', 'lcov'],
};
55 changes: 55 additions & 0 deletions packages/ast-spec/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@typescript-eslint/ast-spec",
"version": "4.20.0",
"description": "Types for the TypeScript-ESTree AST spec",
"keywords": [
"eslint",
"typescript",
"estree"
],
"engines": {
"node": "^8.10.0 || ^10.13.0 || >=11.10.1"
},
"files": [
"dist",
"package.json",
"README.md",
"LICENSE"
],
"repository": {
"type": "git",
"url": "https://github.com/typescript-eslint/typescript-eslint.git",
"directory": "packages/ast-spec"
},
"bugs": {
"url": "https://github.com/typescript-eslint/typescript-eslint/issues"
},
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc -b tsconfig.build.json",
"postbuild": "downlevel-dts dist _ts3.4/dist",
"clean": "tsc -b tsconfig.build.json --clean",
"postclean": "rimraf dist && rimraf _ts3.4 && rimraf coverage",
"format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore",
"lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'",
"typecheck": "tsc -p tsconfig.json --noEmit"
},
"devDependencies": {
"@typescript-eslint/typescript-estree": "4.20.0",
"globby": "*",
"typescript": "*"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/typescript-eslint"
},
"typesVersions": {
"<3.8": {
"*": [
"_ts3.4/*"
]
}
}
}
16 changes: 16 additions & 0 deletions packages/ast-spec/src/__tests__/ast-node-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { AST_NODE_TYPES } from '../ast-node-types';
import type { Node } from '../unions/Node';

type GetKeys<T extends AST_NODE_TYPES> = keyof Extract<Node, { type: T }>;

type AllKeys = {
readonly [T in AST_NODE_TYPES]: GetKeys<T>;
};

type TakesString<T extends Record<string, string>> = T;

// @ts-expect-error: purposely unused
type _Test =
// forcing the test onto a new line so it isn't covered by the expect error
// 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'."
TakesString<AllKeys> | void;
166 changes: 166 additions & 0 deletions packages/ast-spec/src/ast-node-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
export enum AST_NODE_TYPES {
ArrayExpression = 'ArrayExpression',
ArrayPattern = 'ArrayPattern',
ArrowFunctionExpression = 'ArrowFunctionExpression',
AssignmentExpression = 'AssignmentExpression',
AssignmentPattern = 'AssignmentPattern',
AwaitExpression = 'AwaitExpression',
BinaryExpression = 'BinaryExpression',
BlockStatement = 'BlockStatement',
BreakStatement = 'BreakStatement',
CallExpression = 'CallExpression',
CatchClause = 'CatchClause',
ChainExpression = 'ChainExpression',
ClassBody = 'ClassBody',
ClassDeclaration = 'ClassDeclaration',
ClassExpression = 'ClassExpression',
ClassProperty = 'ClassProperty',
ConditionalExpression = 'ConditionalExpression',
ContinueStatement = 'ContinueStatement',
DebuggerStatement = 'DebuggerStatement',
Decorator = 'Decorator',
DoWhileStatement = 'DoWhileStatement',
EmptyStatement = 'EmptyStatement',
ExportAllDeclaration = 'ExportAllDeclaration',
ExportDefaultDeclaration = 'ExportDefaultDeclaration',
ExportNamedDeclaration = 'ExportNamedDeclaration',
ExportSpecifier = 'ExportSpecifier',
ExpressionStatement = 'ExpressionStatement',
ForInStatement = 'ForInStatement',
ForOfStatement = 'ForOfStatement',
ForStatement = 'ForStatement',
FunctionDeclaration = 'FunctionDeclaration',
FunctionExpression = 'FunctionExpression',
Identifier = 'Identifier',
IfStatement = 'IfStatement',
ImportDeclaration = 'ImportDeclaration',
ImportDefaultSpecifier = 'ImportDefaultSpecifier',
ImportExpression = 'ImportExpression',
ImportNamespaceSpecifier = 'ImportNamespaceSpecifier',
ImportSpecifier = 'ImportSpecifier',
JSXAttribute = 'JSXAttribute',
JSXClosingElement = 'JSXClosingElement',
JSXClosingFragment = 'JSXClosingFragment',
JSXElement = 'JSXElement',
JSXEmptyExpression = 'JSXEmptyExpression',
JSXExpressionContainer = 'JSXExpressionContainer',
JSXFragment = 'JSXFragment',
JSXIdentifier = 'JSXIdentifier',
JSXMemberExpression = 'JSXMemberExpression',
JSXNamespacedName = 'JSXNamespacedName',
JSXOpeningElement = 'JSXOpeningElement',
JSXOpeningFragment = 'JSXOpeningFragment',
JSXSpreadAttribute = 'JSXSpreadAttribute',
JSXSpreadChild = 'JSXSpreadChild',
JSXText = 'JSXText',
LabeledStatement = 'LabeledStatement',
Literal = 'Literal',
LogicalExpression = 'LogicalExpression',
MemberExpression = 'MemberExpression',
MetaProperty = 'MetaProperty',
MethodDefinition = 'MethodDefinition',
NewExpression = 'NewExpression',
ObjectExpression = 'ObjectExpression',
ObjectPattern = 'ObjectPattern',
Program = 'Program',
Property = 'Property',
RestElement = 'RestElement',
ReturnStatement = 'ReturnStatement',
SequenceExpression = 'SequenceExpression',
SpreadElement = 'SpreadElement',
Super = 'Super',
SwitchCase = 'SwitchCase',
SwitchStatement = 'SwitchStatement',
TaggedTemplateExpression = 'TaggedTemplateExpression',
TemplateElement = 'TemplateElement',
TemplateLiteral = 'TemplateLiteral',
ThisExpression = 'ThisExpression',
ThrowStatement = 'ThrowStatement',
TryStatement = 'TryStatement',
UnaryExpression = 'UnaryExpression',
UpdateExpression = 'UpdateExpression',
VariableDeclaration = 'VariableDeclaration',
VariableDeclarator = 'VariableDeclarator',
WhileStatement = 'WhileStatement',
WithStatement = 'WithStatement',
YieldExpression = 'YieldExpression',
/**
* TS-prefixed nodes
*/
TSAbstractClassProperty = 'TSAbstractClassProperty',
TSAbstractKeyword = 'TSAbstractKeyword',
TSAbstractMethodDefinition = 'TSAbstractMethodDefinition',
TSAnyKeyword = 'TSAnyKeyword',
TSArrayType = 'TSArrayType',
TSAsExpression = 'TSAsExpression',
TSAsyncKeyword = 'TSAsyncKeyword',
TSBigIntKeyword = 'TSBigIntKeyword',
TSBooleanKeyword = 'TSBooleanKeyword',
TSCallSignatureDeclaration = 'TSCallSignatureDeclaration',
TSClassImplements = 'TSClassImplements',
TSConditionalType = 'TSConditionalType',
TSConstructorType = 'TSConstructorType',
TSConstructSignatureDeclaration = 'TSConstructSignatureDeclaration',
TSDeclareFunction = 'TSDeclareFunction',
TSDeclareKeyword = 'TSDeclareKeyword',
TSEmptyBodyFunctionExpression = 'TSEmptyBodyFunctionExpression',
TSEnumDeclaration = 'TSEnumDeclaration',
TSEnumMember = 'TSEnumMember',
TSExportAssignment = 'TSExportAssignment',
TSExportKeyword = 'TSExportKeyword',
TSExternalModuleReference = 'TSExternalModuleReference',
TSFunctionType = 'TSFunctionType',
TSImportEqualsDeclaration = 'TSImportEqualsDeclaration',
TSImportType = 'TSImportType',
TSIndexedAccessType = 'TSIndexedAccessType',
TSIndexSignature = 'TSIndexSignature',
TSInferType = 'TSInferType',
TSInterfaceBody = 'TSInterfaceBody',
TSInterfaceDeclaration = 'TSInterfaceDeclaration',
TSInterfaceHeritage = 'TSInterfaceHeritage',
TSIntersectionType = 'TSIntersectionType',
TSIntrinsicKeyword = 'TSIntrinsicKeyword',
TSLiteralType = 'TSLiteralType',
TSMappedType = 'TSMappedType',
TSMethodSignature = 'TSMethodSignature',
TSModuleBlock = 'TSModuleBlock',
TSModuleDeclaration = 'TSModuleDeclaration',
TSNamedTupleMember = 'TSNamedTupleMember',
TSNamespaceExportDeclaration = 'TSNamespaceExportDeclaration',
TSNeverKeyword = 'TSNeverKeyword',
TSNonNullExpression = 'TSNonNullExpression',
TSNullKeyword = 'TSNullKeyword',
TSNumberKeyword = 'TSNumberKeyword',
TSObjectKeyword = 'TSObjectKeyword',
TSOptionalType = 'TSOptionalType',
TSParameterProperty = 'TSParameterProperty',
TSParenthesizedType = 'TSParenthesizedType',
TSPrivateKeyword = 'TSPrivateKeyword',
TSPropertySignature = 'TSPropertySignature',
TSProtectedKeyword = 'TSProtectedKeyword',
TSPublicKeyword = 'TSPublicKeyword',
TSQualifiedName = 'TSQualifiedName',
TSReadonlyKeyword = 'TSReadonlyKeyword',
TSRestType = 'TSRestType',
TSStaticKeyword = 'TSStaticKeyword',
TSStringKeyword = 'TSStringKeyword',
TSSymbolKeyword = 'TSSymbolKeyword',
TSTemplateLiteralType = 'TSTemplateLiteralType',
TSThisType = 'TSThisType',
TSTupleType = 'TSTupleType',
TSTypeAliasDeclaration = 'TSTypeAliasDeclaration',
TSTypeAnnotation = 'TSTypeAnnotation',
TSTypeAssertion = 'TSTypeAssertion',
TSTypeLiteral = 'TSTypeLiteral',
TSTypeOperator = 'TSTypeOperator',
TSTypeParameter = 'TSTypeParameter',
TSTypeParameterDeclaration = 'TSTypeParameterDeclaration',
TSTypeParameterInstantiation = 'TSTypeParameterInstantiation',
TSTypePredicate = 'TSTypePredicate',
TSTypeQuery = 'TSTypeQuery',
TSTypeReference = 'TSTypeReference',
TSUndefinedKeyword = 'TSUndefinedKeyword',
TSUnionType = 'TSUnionType',
TSUnknownKeyword = 'TSUnknownKeyword',
TSVoidKeyword = 'TSVoidKeyword',
}
17 changes: 17 additions & 0 deletions packages/ast-spec/src/ast-token-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export enum AST_TOKEN_TYPES {
Boolean = 'Boolean',
Identifier = 'Identifier',
JSXIdentifier = 'JSXIdentifier',
JSXText = 'JSXText',
Keyword = 'Keyword',
Null = 'Null',
Numeric = 'Numeric',
Punctuator = 'Punctuator',
RegularExpression = 'RegularExpression',
String = 'String',
Template = 'Template',

// comment types
Block = 'Block',
Line = 'Line',
}
1 change: 1 addition & 0 deletions packages/ast-spec/src/base/Accessibility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Accessibility = 'private' | 'protected' | 'public';
22 changes: 22 additions & 0 deletions packages/ast-spec/src/base/BaseNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// import type { Node } from '../unions/Node';
import type { Range } from './Range';
import type { SourceLocation } from './SourceLocation';

export interface BaseNode {
/**
* The source location information of the node.
* @see {SourceLocation}
*/
loc: SourceLocation;
/**
* @see {Range}
*/
range: Range;
/**
* The parent node of the current node
*/
// parent?: Node;

// every node *will* have a type, but let the nodes define their own exact string
// type: string;
}
Loading