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 @@ +