diff --git a/.vscode/launch.json b/.vscode/launch.json index 951609b55e3..6e7e47da1a5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -28,6 +28,8 @@ "${workspaceFolder}/packages/parser/dist/index.js", "${workspaceFolder}/packages/typescript-estree/src/index.ts", "${workspaceFolder}/packages/typescript-estree/dist/index.js", + "${workspaceFolder}/packages/types/src/index.ts", + "${workspaceFolder}/packages/types/dist/index.js", ], }, { @@ -54,6 +56,8 @@ "${workspaceFolder}/packages/parser/dist/index.js", "${workspaceFolder}/packages/typescript-estree/src/index.ts", "${workspaceFolder}/packages/typescript-estree/dist/index.js", + "${workspaceFolder}/packages/types/src/index.ts", + "${workspaceFolder}/packages/types/dist/index.js", ], }, { @@ -80,6 +84,8 @@ "${workspaceFolder}/packages/parser/dist/index.js", "${workspaceFolder}/packages/typescript-estree/src/index.ts", "${workspaceFolder}/packages/typescript-estree/dist/index.js", + "${workspaceFolder}/packages/types/src/index.ts", + "${workspaceFolder}/packages/types/dist/index.js", ], }, { @@ -106,6 +112,8 @@ "${workspaceFolder}/packages/parser/dist/index.js", "${workspaceFolder}/packages/typescript-estree/src/index.ts", "${workspaceFolder}/packages/typescript-estree/dist/index.js", + "${workspaceFolder}/packages/types/src/index.ts", + "${workspaceFolder}/packages/types/dist/index.js", ], }, { @@ -132,6 +140,8 @@ "${workspaceFolder}/packages/parser/dist/index.js", "${workspaceFolder}/packages/typescript-estree/src/index.ts", "${workspaceFolder}/packages/typescript-estree/dist/index.js", + "${workspaceFolder}/packages/types/src/index.ts", + "${workspaceFolder}/packages/types/dist/index.js", ], } ] diff --git a/packages/eslint-plugin-internal/src/rules/no-typescript-estree-import.ts b/packages/eslint-plugin-internal/src/rules/no-typescript-estree-import.ts index 2a879c14209..b3c3a71c60d 100644 --- a/packages/eslint-plugin-internal/src/rules/no-typescript-estree-import.ts +++ b/packages/eslint-plugin-internal/src/rules/no-typescript-estree-import.ts @@ -1,6 +1,7 @@ import { createRule } from '../util'; const TSESTREE_NAME = '@typescript-eslint/typescript-estree'; +const TYPES_NAME = '@typescript-eslint/types'; const UTILS_NAME = '@typescript-eslint/experimental-utils'; /* @@ -14,16 +15,16 @@ export default createRule({ meta: { type: 'problem', docs: { - description: `Enforces that eslint-plugin rules don't require anything from ${TSESTREE_NAME}`, + description: `Enforces that eslint-plugin rules don't require anything from ${TSESTREE_NAME} or ${TYPES_NAME}`, category: 'Possible Errors', recommended: 'error', }, fixable: 'code', schema: [], messages: { - dontImportTSEStree: [ - `Don't import from ${TSESTREE_NAME}. Everything you need should be available in ${UTILS_NAME}.`, - `${TSESTREE_NAME} is an indirect dependency of this package, and thus should not be used directly.`, + dontImportPackage: [ + `Don't import from {{packageName}}. Everything you need should be available in ${UTILS_NAME}.`, + `{{packageName}} is an indirect dependency of this package, and thus should not be used directly.`, ].join('\n'), }, }, @@ -31,13 +32,24 @@ export default createRule({ create(context) { return { ImportDeclaration(node): void { - if ( - typeof node.source.value === 'string' && - node.source.value.startsWith(TSESTREE_NAME) - ) { + if (typeof node.source.value !== 'string') { + return; + } + + let packageName: string | null = null; + if (node.source.value.startsWith(TSESTREE_NAME)) { + packageName = TSESTREE_NAME; + } else if (node.source.value.startsWith(TYPES_NAME)) { + packageName = TYPES_NAME; + } + + if (packageName != null) { context.report({ node, - messageId: 'dontImportTSEStree', + messageId: 'dontImportPackage', + data: { + packageName, + }, fix(fixer) { return fixer.replaceTextRange( [node.source.range[0] + 1, node.source.range[1] - 1], diff --git a/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts b/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts index 74b9478b58a..fe31f051a09 100644 --- a/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts +++ b/packages/eslint-plugin-internal/tests/rules/no-typescript-estree.test.ts @@ -19,25 +19,43 @@ ruleTester.run('no-typescript-estree-import', rule, { import { foo } from '@typescript-eslint/typescript-estree'; import foo from '@typescript-eslint/typescript-estree'; import * as foo from '@typescript-eslint/typescript-estree'; +import { foo } from '@typescript-eslint/types'; +import foo from '@typescript-eslint/types'; +import * as foo from '@typescript-eslint/types'; `, output: ` import { foo } from '@typescript-eslint/experimental-utils'; import foo from '@typescript-eslint/experimental-utils'; +import * as foo from '@typescript-eslint/experimental-utils'; +import { foo } from '@typescript-eslint/experimental-utils'; +import foo from '@typescript-eslint/experimental-utils'; import * as foo from '@typescript-eslint/experimental-utils'; `, errors: [ { - messageId: 'dontImportTSEStree', + messageId: 'dontImportPackage', line: 2, }, { - messageId: 'dontImportTSEStree', + messageId: 'dontImportPackage', line: 3, }, { - messageId: 'dontImportTSEStree', + messageId: 'dontImportPackage', line: 4, }, + { + messageId: 'dontImportPackage', + line: 5, + }, + { + messageId: 'dontImportPackage', + line: 6, + }, + { + messageId: 'dontImportPackage', + line: 7, + }, ], }), }); diff --git a/packages/experimental-utils/README.md b/packages/experimental-utils/README.md index 05f65ecb7da..208578bfc11 100644 --- a/packages/experimental-utils/README.md +++ b/packages/experimental-utils/README.md @@ -20,17 +20,17 @@ Once it is stable, it will be renamed to `@typescript-eslint/util` for a `4.0.0` ## Exports -| Name | Description | -| ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [`ASTUtils`](./src/ast-utils) | Tools for operating on the ESTree AST. Also includes the [`eslint-utils`](https://www.npmjs.com/package/eslint-utils) package, correctly typed to work with the types found in `TSESTree` | -| [`ESLintUtils`](./src/eslint-utils) | Tools for creating ESLint rules with TypeScript. | -| `JSONSchema` | Types from the [`@types/json-schema`](https://www.npmjs.com/package/@types/json-schema) package, re-exported to save you having to manually import them. Also ensures you're using the same version of the types as this package. | -| [`TSESLint`](./src/ts-eslint) | Types for ESLint, correctly typed to work with the types found in `TSESTree`. | -| [`TSESLintScope`](./src/ts-eslint-scope) | The [`eslint-scope`](https://www.npmjs.com/package/eslint-scope) package, correctly typed to work with the types found in both `TSESTree` and `TSESLint` | -| [`TSESTree`](../typescript-estree/src/ts-estree/ts-estree.ts) | Types for the TypeScript flavor of ESTree created by `@typescript-eslint/typescript-estree`. | -| [`AST_NODE_TYPES`](../typescript-estree/src/ts-estree/ast-node-types.ts) | An enum with the names of every single _node_ found in `TSESTree`. | -| [`AST_TOKEN_TYPES`](../typescript-estree/src/ts-estree/ast-node-types.ts) | An enum with the names of every single _token_ found in `TSESTree`. | -| [`ParserServices`](../typescript-estree/src/parser-options.ts) | Typing for the parser services provided when parsing a file using `@typescript-eslint/typescript-estree`. | +| Name | Description | +| -------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| [`ASTUtils`](./src/ast-utils) | Tools for operating on the ESTree AST. Also includes the [`eslint-utils`](https://www.npmjs.com/package/eslint-utils) package, correctly typed to work with the types found in `TSESTree` | +| [`ESLintUtils`](./src/eslint-utils) | Tools for creating ESLint rules with TypeScript. | +| `JSONSchema` | Types from the [`@types/json-schema`](https://www.npmjs.com/package/@types/json-schema) package, re-exported to save you having to manually import them. Also ensures you're using the same version of the types as this package. | +| [`TSESLint`](./src/ts-eslint) | Types for ESLint, correctly typed to work with the types found in `TSESTree`. | +| [`TSESLintScope`](./src/ts-eslint-scope) | The [`eslint-scope`](https://www.npmjs.com/package/eslint-scope) package, correctly typed to work with the types found in both `TSESTree` and `TSESLint` | +| [`TSESTree`](../types/src/ts-estree.ts) | Types for the TypeScript flavor of ESTree created by `@typescript-eslint/typescript-estree`. | +| [`AST_NODE_TYPES`](../types/src/ast-node-types.ts) | An enum with the names of every single _node_ found in `TSESTree`. | +| [`AST_TOKEN_TYPES`](../types/src/ast-token-types.ts) | An enum with the names of every single _token_ found in `TSESTree`. | +| [`ParserServices`](../typescript-estree/src/parser-options.ts) | Typing for the parser services provided when parsing a file using `@typescript-eslint/typescript-estree`. | ## Contributing diff --git a/packages/experimental-utils/package.json b/packages/experimental-utils/package.json index 376b7555923..44dd6cb1c5d 100644 --- a/packages/experimental-utils/package.json +++ b/packages/experimental-utils/package.json @@ -40,6 +40,7 @@ }, "dependencies": { "@types/json-schema": "^7.0.3", + "@typescript-eslint/types": "3.4.0", "@typescript-eslint/typescript-estree": "3.4.0", "eslint-scope": "^5.0.0", "eslint-utils": "^2.0.0" diff --git a/packages/experimental-utils/src/ts-eslint/ParserOptions.ts b/packages/experimental-utils/src/ts-eslint/ParserOptions.ts index c3678eb939d..5a64049f335 100644 --- a/packages/experimental-utils/src/ts-eslint/ParserOptions.ts +++ b/packages/experimental-utils/src/ts-eslint/ParserOptions.ts @@ -1,43 +1,6 @@ -import { TSESTreeOptions } from '@typescript-eslint/typescript-estree'; - -type EcmaVersion = - | 3 - | 5 - | 6 - | 7 - | 8 - | 9 - | 10 - | 11 - | 2015 - | 2016 - | 2017 - | 2018 - | 2019 - | 2020; - -interface ParserOptions { - comment?: boolean; - ecmaFeatures?: { - globalReturn?: boolean; - jsx?: boolean; - }; - ecmaVersion?: EcmaVersion; - // ts-estree specific - debugLevel?: TSESTreeOptions['debugLevel']; - errorOnTypeScriptSyntacticAndSemanticIssues?: boolean; - errorOnUnknownASTType?: boolean; - extraFileExtensions?: string[]; - filePath?: string; - loc?: boolean; - project?: string | string[]; - projectFolderIgnoreList?: (string | RegExp)[]; - range?: boolean; - sourceType?: 'script' | 'module'; - tokens?: boolean; - tsconfigRootDir?: string; - useJSXTextNode?: boolean; - warnOnUnsupportedTypeScriptVersion?: boolean; -} - -export { EcmaVersion, ParserOptions }; +export { + DebugLevel, + EcmaVersion, + ParserOptions, + SourceType, +} from '@typescript-eslint/types'; diff --git a/packages/experimental-utils/src/ts-estree.ts b/packages/experimental-utils/src/ts-estree.ts index a7f18377e37..1138deddceb 100644 --- a/packages/experimental-utils/src/ts-estree.ts +++ b/packages/experimental-utils/src/ts-estree.ts @@ -1,12 +1,13 @@ // for convenience's sake - export the types directly from here so consumers // don't need to reference/install both packages in their code -// NOTE - this uses hard links inside ts-estree to avoid initialization of entire package -// via its main file (which imports typescript at runtime). -// Not every eslint-plugin written in typescript requires typescript at runtime. export { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree, -} from '@typescript-eslint/typescript-estree/dist/ts-estree'; +} from '@typescript-eslint/types'; + +// NOTE - this uses hard links inside ts-estree to avoid initialization of entire package +// via its main file (which imports typescript at runtime). +// Not every eslint-plugin written in typescript requires typescript at runtime. export { ParserServices } from '@typescript-eslint/typescript-estree/dist/parser-options'; diff --git a/packages/experimental-utils/tsconfig.build.json b/packages/experimental-utils/tsconfig.build.json index e1b49976461..cef65159074 100644 --- a/packages/experimental-utils/tsconfig.build.json +++ b/packages/experimental-utils/tsconfig.build.json @@ -7,5 +7,8 @@ "resolveJsonModule": true }, "include": ["src", "typings"], - "references": [{ "path": "../typescript-estree/tsconfig.build.json" }] + "references": [ + { "path": "../types/tsconfig.build.json" }, + { "path": "../typescript-estree/tsconfig.build.json" } + ] } diff --git a/packages/parser/package.json b/packages/parser/package.json index cf28e63968e..c26dd9ad4b7 100644 --- a/packages/parser/package.json +++ b/packages/parser/package.json @@ -45,6 +45,7 @@ "dependencies": { "@types/eslint-visitor-keys": "^1.0.0", "@typescript-eslint/experimental-utils": "3.4.0", + "@typescript-eslint/types": "3.4.0", "@typescript-eslint/typescript-estree": "3.4.0", "eslint-visitor-keys": "^1.1.0" }, diff --git a/packages/parser/src/analyze-scope.ts b/packages/parser/src/analyze-scope.ts index 73d1d900275..2b6a7420c86 100644 --- a/packages/parser/src/analyze-scope.ts +++ b/packages/parser/src/analyze-scope.ts @@ -1,13 +1,10 @@ -import { - TSESTree, - TSESLintScope, - AST_NODE_TYPES, -} from '@typescript-eslint/experimental-utils'; +import { TSESLintScope } from '@typescript-eslint/experimental-utils'; +import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types'; +import { visitorKeys as childVisitorKeys } from '@typescript-eslint/typescript-estree'; import { getKeys as fallback } from 'eslint-visitor-keys'; import { ParserOptions } from './parser-options'; import { ScopeManager } from './scope/scope-manager'; -import { visitorKeys as childVisitorKeys } from '@typescript-eslint/typescript-estree'; /** * Define the override function of `Scope#__define` for global augmentation. diff --git a/packages/parser/src/parser-options.ts b/packages/parser/src/parser-options.ts index 9848d54ba70..025a97ed61b 100644 --- a/packages/parser/src/parser-options.ts +++ b/packages/parser/src/parser-options.ts @@ -1,3 +1 @@ -import { TSESLint } from '@typescript-eslint/experimental-utils'; - -export type ParserOptions = TSESLint.ParserOptions; +export { ParserOptions } from '@typescript-eslint/types'; diff --git a/packages/parser/src/parser.ts b/packages/parser/src/parser.ts index b658c96c82d..64c81253440 100644 --- a/packages/parser/src/parser.ts +++ b/packages/parser/src/parser.ts @@ -1,15 +1,12 @@ -import { TSESLint } from '@typescript-eslint/experimental-utils'; +import { ParserOptions, TSESTree } from '@typescript-eslint/types'; import { parseAndGenerateServices, ParserServices, TSESTreeOptions, - TSESTree, visitorKeys, } from '@typescript-eslint/typescript-estree'; import { analyzeScope } from './analyze-scope'; -type ParserOptions = TSESLint.ParserOptions; - interface ParseForESLintResult { ast: TSESTree.Program & { range?: [number, number]; diff --git a/packages/parser/src/scope/scope-manager.ts b/packages/parser/src/scope/scope-manager.ts index c179bd512d3..ae6fceaf1c6 100644 --- a/packages/parser/src/scope/scope-manager.ts +++ b/packages/parser/src/scope/scope-manager.ts @@ -1,4 +1,5 @@ -import { TSESTree, TSESLintScope } from '@typescript-eslint/experimental-utils'; +import { TSESLintScope } from '@typescript-eslint/experimental-utils'; +import { TSESTree } from '@typescript-eslint/types'; import { EmptyFunctionScope, EnumScope } from './scopes'; /** diff --git a/packages/parser/src/scope/scopes.ts b/packages/parser/src/scope/scopes.ts index 4ddaa297d53..97ef3d60092 100644 --- a/packages/parser/src/scope/scopes.ts +++ b/packages/parser/src/scope/scopes.ts @@ -1,4 +1,5 @@ -import { TSESTree, TSESLintScope } from '@typescript-eslint/experimental-utils'; +import { TSESLintScope } from '@typescript-eslint/experimental-utils'; +import { TSESTree } from '@typescript-eslint/types'; import { ScopeManager } from './scope-manager'; /** The scope class for enum. */ diff --git a/packages/parser/tests/lib/basics.ts b/packages/parser/tests/lib/basics.ts index 0db6e698249..0c90af58905 100644 --- a/packages/parser/tests/lib/basics.ts +++ b/packages/parser/tests/lib/basics.ts @@ -1,7 +1,5 @@ -import { - AST_NODE_TYPES, - TSESLint, -} from '@typescript-eslint/experimental-utils'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; +import { AST_NODE_TYPES } from '@typescript-eslint/types'; import fs from 'fs'; import glob from 'glob'; import * as parser from '../../src/parser'; diff --git a/packages/parser/tsconfig.build.json b/packages/parser/tsconfig.build.json index 2f7c5d79609..4de958191f1 100644 --- a/packages/parser/tsconfig.build.json +++ b/packages/parser/tsconfig.build.json @@ -9,7 +9,8 @@ "include": ["src"], "references": [ { "path": "../experimental-utils/tsconfig.build.json" }, - { "path": "../typescript-estree/tsconfig.build.json" }, - { "path": "../shared-fixtures/tsconfig.build.json" } + { "path": "../shared-fixtures/tsconfig.build.json" }, + { "path": "../types/tsconfig.build.json" }, + { "path": "../typescript-estree/tsconfig.build.json" } ] } diff --git a/packages/types/LICENSE b/packages/types/LICENSE new file mode 100644 index 00000000000..7e7370143b2 --- /dev/null +++ b/packages/types/LICENSE @@ -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. diff --git a/packages/types/README.md b/packages/types/README.md new file mode 100644 index 00000000000..5c91f1687d2 --- /dev/null +++ b/packages/types/README.md @@ -0,0 +1,17 @@ +

TypeScript-ESTree Types

+ +

+ CI + NPM Version + NPM Downloads +

+ +This package exists to help us reduce cycles and provide lighter-weight packages at runtime. +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) diff --git a/packages/types/jest.config.js b/packages/types/jest.config.js new file mode 100644 index 00000000000..dd66fc204b3 --- /dev/null +++ b/packages/types/jest.config.js @@ -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?$/.source]: 'ts-jest', + }, + testRegex: [/.\/tests\/.+\.test\.ts$/.source], + collectCoverage: false, + collectCoverageFrom: ['src/**/*.{js,jsx,ts,tsx}'], + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'], + coverageReporters: ['text-summary', 'lcov'], +}; diff --git a/packages/types/package.json b/packages/types/package.json new file mode 100644 index 00000000000..22591b557c9 --- /dev/null +++ b/packages/types/package.json @@ -0,0 +1,42 @@ +{ + "name": "@typescript-eslint/types", + "version": "3.4.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/visitor-keys" + }, + "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", + "clean": "tsc -b tsconfig.build.json --clean", + "format": "prettier --write \"./**/*.{ts,js,json,md}\" --ignore-path ../../.prettierignore", + "lint": "eslint . --ext .js,.ts --ignore-path='../../.eslintignore'", + "test": "jest --coverage", + "typecheck": "tsc -p tsconfig.json --noEmit" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + } +} diff --git a/packages/typescript-estree/src/ts-estree/ast-node-types.ts b/packages/types/src/ast-node-types.ts similarity index 94% rename from packages/typescript-estree/src/ts-estree/ast-node-types.ts rename to packages/types/src/ast-node-types.ts index d630398ec71..cbf72617453 100644 --- a/packages/typescript-estree/src/ts-estree/ast-node-types.ts +++ b/packages/types/src/ast-node-types.ts @@ -1,4 +1,4 @@ -export enum AST_NODE_TYPES { +enum AST_NODE_TYPES { ArrayExpression = 'ArrayExpression', ArrayPattern = 'ArrayPattern', ArrowFunctionExpression = 'ArrowFunctionExpression', @@ -163,20 +163,4 @@ export enum AST_NODE_TYPES { TSVoidKeyword = 'TSVoidKeyword', } -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', -} +export { AST_NODE_TYPES }; diff --git a/packages/types/src/ast-token-types.ts b/packages/types/src/ast-token-types.ts new file mode 100644 index 00000000000..144befece83 --- /dev/null +++ b/packages/types/src/ast-token-types.ts @@ -0,0 +1,19 @@ +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', +} + +export { AST_TOKEN_TYPES }; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts new file mode 100644 index 00000000000..94bbdfab66d --- /dev/null +++ b/packages/types/src/index.ts @@ -0,0 +1,4 @@ +export { AST_NODE_TYPES } from './ast-node-types'; +export { AST_TOKEN_TYPES } from './ast-token-types'; +export * from './parser-options'; +export * as TSESTree from './ts-estree'; diff --git a/packages/types/src/parser-options.ts b/packages/types/src/parser-options.ts new file mode 100644 index 00000000000..61eacf440c7 --- /dev/null +++ b/packages/types/src/parser-options.ts @@ -0,0 +1,46 @@ +type DebugLevel = boolean | ('typescript-eslint' | 'eslint' | 'typescript')[]; + +type EcmaVersion = + | 3 + | 5 + | 6 + | 7 + | 8 + | 9 + | 10 + | 11 + | 2015 + | 2016 + | 2017 + | 2018 + | 2019 + | 2020; + +type SourceType = 'script' | 'module'; + +interface ParserOptions { + comment?: boolean; + ecmaFeatures?: { + globalReturn?: boolean; + jsx?: boolean; + }; + ecmaVersion?: EcmaVersion; + + // typescript-eslint specific + debugLevel?: DebugLevel; + errorOnTypeScriptSyntacticAndSemanticIssues?: boolean; + errorOnUnknownASTType?: boolean; + extraFileExtensions?: string[]; + filePath?: string; + loc?: boolean; + project?: string | string[]; + projectFolderIgnoreList?: (string | RegExp)[]; + range?: boolean; + sourceType?: SourceType; + tokens?: boolean; + tsconfigRootDir?: string; + useJSXTextNode?: boolean; + warnOnUnsupportedTypeScriptVersion?: boolean; +} + +export { DebugLevel, EcmaVersion, ParserOptions, SourceType }; diff --git a/packages/typescript-estree/src/ts-estree/ts-estree.ts b/packages/types/src/ts-estree.ts similarity index 99% rename from packages/typescript-estree/src/ts-estree/ts-estree.ts rename to packages/types/src/ts-estree.ts index 48ce05a6c29..9c1e14bd5b4 100644 --- a/packages/typescript-estree/src/ts-estree/ts-estree.ts +++ b/packages/types/src/ts-estree.ts @@ -1,4 +1,5 @@ -import { AST_NODE_TYPES, AST_TOKEN_TYPES } from './ast-node-types'; +import { AST_NODE_TYPES } from './ast-node-types'; +import { AST_TOKEN_TYPES } from './ast-token-types'; export interface LineAndColumnData { /** diff --git a/packages/types/tsconfig.build.json b/packages/types/tsconfig.build.json new file mode 100644 index 00000000000..215a0282df2 --- /dev/null +++ b/packages/types/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "composite": true, + "outDir": "./dist", + "rootDir": "./src", + "resolveJsonModule": true + }, + "include": ["src", "typings"] +} diff --git a/packages/types/tsconfig.json b/packages/types/tsconfig.json new file mode 100644 index 00000000000..9cea515ba6b --- /dev/null +++ b/packages/types/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.build.json", + "compilerOptions": { + "composite": false, + "rootDir": "." + }, + "include": ["src", "typings", "tests", "tools"] +} diff --git a/packages/typescript-estree/package.json b/packages/typescript-estree/package.json index e0d03e774e1..c520548770c 100644 --- a/packages/typescript-estree/package.json +++ b/packages/typescript-estree/package.json @@ -40,6 +40,7 @@ "typecheck": "tsc -p tsconfig.json --noEmit" }, "dependencies": { + "@typescript-eslint/types": "3.4.0", "debug": "^4.1.1", "eslint-visitor-keys": "^1.1.0", "glob": "^7.1.6", diff --git a/packages/typescript-estree/src/parser-options.ts b/packages/typescript-estree/src/parser-options.ts index 0f423abb0cf..180ac418d7b 100644 --- a/packages/typescript-estree/src/parser-options.ts +++ b/packages/typescript-estree/src/parser-options.ts @@ -1,3 +1,4 @@ +import { DebugLevel } from '@typescript-eslint/types'; import { Program } from 'typescript'; import { TSESTree, TSNode, TSESTreeToTSNode, TSToken } from './ts-estree'; @@ -45,7 +46,7 @@ interface ParseOptions { * - true === ['typescript-eslint'] * - false === [] */ - debugLevel?: boolean | ('typescript-eslint' | 'eslint' | 'typescript')[]; + debugLevel?: DebugLevel; /** * Cause the parser to error if it encounters an unknown AST node type (useful for testing). diff --git a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts index 5585c290284..4d201d49c97 100644 --- a/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts +++ b/packages/typescript-estree/src/ts-estree/estree-to-ts-node-types.ts @@ -1,7 +1,6 @@ -import { TSNode } from './ts-nodes'; -import { AST_NODE_TYPES } from './ast-node-types'; -import { Node } from './ts-estree'; +import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/types'; import * as ts from 'typescript'; +import { TSNode } from './ts-nodes'; export interface EstreeToTsNodeTypes { [AST_NODE_TYPES.ArrayExpression]: ts.ArrayLiteralExpression; @@ -274,7 +273,7 @@ export interface EstreeToTsNodeTypes { * Maps TSESTree AST Node type to the expected TypeScript AST Node type(s). * This mapping is based on the internal logic of the parser. */ -export type TSESTreeToTSNode = Extract< +export type TSESTreeToTSNode = Extract< TSNode | ts.Token, EstreeToTsNodeTypes[T['type']] >; diff --git a/packages/typescript-estree/src/ts-estree/index.ts b/packages/typescript-estree/src/ts-estree/index.ts index 459edb57877..a7f64d91ce6 100644 --- a/packages/typescript-estree/src/ts-estree/index.ts +++ b/packages/typescript-estree/src/ts-estree/index.ts @@ -1,6 +1,8 @@ -import * as TSESTree from './ts-estree'; - -export { TSESTree }; -export * from './ast-node-types'; +// for simplicity and backwards-compatibility +export { + AST_NODE_TYPES, + AST_TOKEN_TYPES, + TSESTree, +} from '@typescript-eslint/types'; export * from './ts-nodes'; export * from './estree-to-ts-node-types'; diff --git a/packages/typescript-estree/tsconfig.json b/packages/typescript-estree/tsconfig.json index 9b219345ac3..77267ff8f9c 100644 --- a/packages/typescript-estree/tsconfig.json +++ b/packages/typescript-estree/tsconfig.json @@ -6,5 +6,8 @@ }, "include": ["src", "typings", "tests", "tools"], "exclude": ["tests/fixtures/**/*"], - "references": [{ "path": "../shared-fixtures/tsconfig.build.json" }] + "references": [ + { "path": "../shared-fixtures/tsconfig.build.json" }, + { "path": "../types/tsconfig.build.json" } + ] } diff --git a/tests/integration/docker-compose.yml b/tests/integration/docker-compose.yml index a65f23c6b38..5f1a1be49d9 100644 --- a/tests/integration/docker-compose.yml +++ b/tests/integration/docker-compose.yml @@ -19,6 +19,8 @@ services: - /usr/eslint-plugin/tests - ../../packages/eslint-plugin-tslint/:/usr/eslint-plugin-tslint - /usr/eslint-plugin-tslint/tests + - ../../packages/types/:/usr/types + - /usr/types/tests # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/typescript-and-tslint-plugins-together:/usr/linked @@ -38,6 +40,8 @@ services: - /usr/experimental-utils/tests - ../../packages/eslint-plugin/:/usr/eslint-plugin - /usr/eslint-plugin/tests + - ../../packages/types/:/usr/types + - /usr/types/tests # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/vue-sfc:/usr/linked @@ -57,6 +61,8 @@ services: - /usr/experimental-utils/tests - ../../packages/eslint-plugin/:/usr/eslint-plugin - /usr/eslint-plugin/tests + - ../../packages/types/:/usr/types + - /usr/types/tests # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/vue-jsx:/usr/linked @@ -76,6 +82,8 @@ services: - /usr/experimental-utils/tests - ../../packages/eslint-plugin/:/usr/eslint-plugin - /usr/eslint-plugin/tests + - ../../packages/types/:/usr/types + - /usr/types/tests # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/recommended-does-not-require-program:/usr/linked @@ -95,6 +103,8 @@ services: - /usr/experimental-utils/tests - ../../packages/eslint-plugin/:/usr/eslint-plugin - /usr/eslint-plugin/tests + - ../../packages/types/:/usr/types + - /usr/types/tests # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/markdown:/usr/linked @@ -114,5 +124,7 @@ services: - /usr/experimental-utils/tests - ../../packages/eslint-plugin/:/usr/eslint-plugin - /usr/eslint-plugin/tests + - ../../packages/types/:/usr/types + - /usr/types/tests # Runtime link to all the specific integration test files, so that most updates don't require a rebuild. - ./fixtures/eslint-v6:/usr/linked diff --git a/tests/integration/fixtures/eslint-v6/test.sh b/tests/integration/fixtures/eslint-v6/test.sh index d1cc755c58a..cf39bca322e 100755 --- a/tests/integration/fixtures/eslint-v6/test.sh +++ b/tests/integration/fixtures/eslint-v6/test.sh @@ -9,6 +9,7 @@ npm install npm install eslint@6.0.0 # Use the local volumes for our own packages +npm install $(npm pack /usr/types | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/markdown/test.sh b/tests/integration/fixtures/markdown/test.sh index 9ff7a57b68c..1da9c082f72 100755 --- a/tests/integration/fixtures/markdown/test.sh +++ b/tests/integration/fixtures/markdown/test.sh @@ -8,6 +8,7 @@ node /usr/utils/generate-package-json.js npm install # Use the local volumes for our own packages +npm install $(npm pack /usr/types | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/recommended-does-not-require-program/test.sh b/tests/integration/fixtures/recommended-does-not-require-program/test.sh index 9e9b2a6917b..058e0e4d575 100755 --- a/tests/integration/fixtures/recommended-does-not-require-program/test.sh +++ b/tests/integration/fixtures/recommended-does-not-require-program/test.sh @@ -8,6 +8,7 @@ node /usr/utils/generate-package-json.js npm install # Use the local volumes for our own packages +npm install $(npm pack /usr/types | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh b/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh index 7afab784b6c..cea5ee07aa8 100755 --- a/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh +++ b/tests/integration/fixtures/typescript-and-tslint-plugins-together/test.sh @@ -8,6 +8,7 @@ node /usr/utils/generate-package-json.js npm install # Use the local volumes for our own packages +npm install $(npm pack /usr/types | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/vue-jsx/test.sh b/tests/integration/fixtures/vue-jsx/test.sh index 7e03aa3dc15..252dfa6e6b1 100755 --- a/tests/integration/fixtures/vue-jsx/test.sh +++ b/tests/integration/fixtures/vue-jsx/test.sh @@ -8,6 +8,7 @@ node /usr/utils/generate-package-json.js npm install # Use the local volumes for our own packages +npm install $(npm pack /usr/types | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1) diff --git a/tests/integration/fixtures/vue-sfc/test.sh b/tests/integration/fixtures/vue-sfc/test.sh index 7e03aa3dc15..252dfa6e6b1 100755 --- a/tests/integration/fixtures/vue-sfc/test.sh +++ b/tests/integration/fixtures/vue-sfc/test.sh @@ -8,6 +8,7 @@ node /usr/utils/generate-package-json.js npm install # Use the local volumes for our own packages +npm install $(npm pack /usr/types | tail -1) npm install $(npm pack /usr/typescript-estree | tail -1) npm install $(npm pack /usr/parser | tail -1) npm install $(npm pack /usr/experimental-utils | tail -1)