From e364b60e58e6e51553f36587fcd4f87875b12d04 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Tue, 30 Jan 2024 16:44:15 +0530 Subject: [PATCH 01/27] chore(eslint-plugin): deprecate no-var-requires in favor of no-require-imports fixes: #8092 --- .../docs/rules/no-var-requires.md | 6 ++ packages/eslint-plugin/src/configs/all.ts | 1 - .../src/rules/no-require-imports.ts | 60 +++++++++++++++---- .../src/rules/no-var-requires.ts | 1 + .../tests/rules/no-require-imports.test.ts | 34 +++++++++++ 5 files changed, 89 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-var-requires.md b/packages/eslint-plugin/docs/rules/no-var-requires.md index 0bd0b2b564b9..a30d6671cbd7 100644 --- a/packages/eslint-plugin/docs/rules/no-var-requires.md +++ b/packages/eslint-plugin/docs/rules/no-var-requires.md @@ -6,6 +6,12 @@ description: 'Disallow `require` statements except in import statements.' > > See **https://typescript-eslint.io/rules/no-var-requires** for documentation. +:::danger Deprecated + +This rule has been deprecated in favour of the [`@typescript-eslint/no-require-imports`](./no-require-imports.md) rule. + +::: + In other words, the use of forms such as `var foo = require("foo")` are banned. Instead use ES6 style imports or `import foo = require("foo")` imports. ## Examples diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index 3a5e6343b197..b7bdaac2ede4 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -112,7 +112,6 @@ export = { '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-empty-export': 'error', '@typescript-eslint/no-useless-template-literals': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', diff --git a/packages/eslint-plugin/src/rules/no-require-imports.ts b/packages/eslint-plugin/src/rules/no-require-imports.ts index c4f20e6c68f7..c157a6ea033e 100644 --- a/packages/eslint-plugin/src/rules/no-require-imports.ts +++ b/packages/eslint-plugin/src/rules/no-require-imports.ts @@ -6,7 +6,8 @@ import * as util from '../util'; type Options = [ { - allow: string[]; + allow?: string[]; + allowAsImport?: boolean; }, ]; type MessageIds = 'noRequireImports'; @@ -27,6 +28,10 @@ export default util.createRule({ items: { type: 'string' }, description: 'Patterns of import paths to allow requiring from.', }, + allowAsImport: { + type: 'boolean', + description: 'Allows `require` statements in import declarations.', + }, }, additionalProperties: false, }, @@ -35,13 +40,14 @@ export default util.createRule({ noRequireImports: 'A `require()` style import is forbidden.', }, }, - defaultOptions: [{ allow: [] }], + defaultOptions: [{ allow: [], allowAsImport: false }], create(context, options) { - const allowPatterns = options[0].allow.map( + let allowAsImport = options[0].allowAsImport; + const allowPatterns = options[0].allow?.map( pattern => new RegExp(pattern, 'u'), ); - function isImportPathAllowed(importPath: string): boolean { - return allowPatterns.some(pattern => importPath.match(pattern)); + function isImportPathAllowed(importPath: string): boolean | undefined { + return allowPatterns?.some(pattern => importPath.match(pattern)); } return { 'CallExpression[callee.name="require"]'( @@ -55,14 +61,38 @@ export default util.createRule({ return; } const variable = ASTUtils.findVariable(getScope(context), 'require'); + let parent = + node.parent.type === AST_NODE_TYPES.ChainExpression + ? node.parent.parent + : node.parent; - // ignore non-global require usage as it's something user-land custom instead - // of the commonjs standard - if (!variable?.identifiers.length) { - context.report({ - node, - messageId: 'noRequireImports', - }); + if (allowAsImport) { + if ( + [ + AST_NODE_TYPES.CallExpression, + AST_NODE_TYPES.MemberExpression, + AST_NODE_TYPES.NewExpression, + AST_NODE_TYPES.TSAsExpression, + AST_NODE_TYPES.TSTypeAssertion, + AST_NODE_TYPES.VariableDeclarator, + ].includes(parent.type) + ) { + if (!variable?.identifiers.length) { + context.report({ + node, + messageId: 'noRequireImports', + }); + } + } + } else { + // ignore non-global require usage as it's something user-land custom instead + // of the commonjs standard + if (!variable?.identifiers.length) { + context.report({ + node, + messageId: 'noRequireImports', + }); + } } }, TSExternalModuleReference(node): void { @@ -73,6 +103,12 @@ export default util.createRule({ ) { return; } + if ( + allowAsImport && + node.parent.type === AST_NODE_TYPES.TSImportEqualsDeclaration + ) { + return; + } context.report({ node, messageId: 'noRequireImports', diff --git a/packages/eslint-plugin/src/rules/no-var-requires.ts b/packages/eslint-plugin/src/rules/no-var-requires.ts index 3605904e655b..00105b5ae90d 100644 --- a/packages/eslint-plugin/src/rules/no-var-requires.ts +++ b/packages/eslint-plugin/src/rules/no-var-requires.ts @@ -14,6 +14,7 @@ type MessageIds = 'noVarReqs'; export default createRule({ name: 'no-var-requires', meta: { + deprecated: true, type: 'problem', docs: { description: 'Disallow `require` statements except in import statements', diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index 42374ccffbf8..b03158d2c015 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -47,6 +47,18 @@ require('remark-preset-prettier'); code: "import pkg = require('some-package');", options: [{ allow: ['^some-package$'] }], }, + { + code: "import foo = require('foo');", + options: [{ allowAsImport: true }], + }, + { + code: 'require(foo);', + options: [{ allowAsImport: true }], + }, + { + code: 'require?.(foo);', + options: [{ allowAsImport: true }], + }, ], invalid: [ { @@ -188,5 +200,27 @@ var lib5 = require?.('lib5'), }, ], }, + { + code: "var foo = require?.('foo');", + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 11, + }, + ], + }, + { + code: "let foo = trick(require?.('foo'));", + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 17, + }, + ], + }, ], }); From 274243371eb5d5bdb13b261592ddf221159d959f Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 1 Feb 2024 19:03:30 +0530 Subject: [PATCH 02/27] chore: address CI failures --- .../docs/rules/no-require-imports.md | 26 ++++++++++++++++++- .../src/rules/no-require-imports.ts | 4 +-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.md b/packages/eslint-plugin/docs/rules/no-require-imports.md index d67d639b9cb8..1ba4c83665cc 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.md +++ b/packages/eslint-plugin/docs/rules/no-require-imports.md @@ -32,7 +32,7 @@ import * as lib3 from 'lib3'; ### `allow` -A array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work. +An array of strings. These strings will be compiled into regular expressions with the `u` flag and be used to test against the imported path. A common use case is to allow importing `package.json`. This is because `package.json` commonly lives outside of the TS root directory, so statically importing it would lead to root directory conflicts, especially with `resolveJsonModule` enabled. You can also use it to allow importing any JSON if your environment doesn't support JSON modules, or use it for other cases where `import` statements cannot work. With `{allow: ['/package\\.json$']}`: @@ -50,6 +50,30 @@ console.log(require('../data.json').version); console.log(require('../package.json').version); ``` +### `allowAsImport` + +A boolean. If it's set to `true`, the require statements with an `import` declaration won't be reported. The default value is `false`. If you want to apply the `no-var-requires` rule, then set it to `true`. + +With `{allowAsImport: true}`: + + + +### ❌ Incorrect + +```ts +var foo = require('foo'); +const foo = require('foo'); +let foo = require('foo'); +``` + +### ✅ Correct + +```ts +import foo = require('foo'); +require('foo'); +import foo from 'foo'; +``` + ## When Not To Use It If your project frequently uses older CommonJS `require`s, then this rule might not be applicable to you. diff --git a/packages/eslint-plugin/src/rules/no-require-imports.ts b/packages/eslint-plugin/src/rules/no-require-imports.ts index c157a6ea033e..90820436ead0 100644 --- a/packages/eslint-plugin/src/rules/no-require-imports.ts +++ b/packages/eslint-plugin/src/rules/no-require-imports.ts @@ -42,7 +42,7 @@ export default util.createRule({ }, defaultOptions: [{ allow: [], allowAsImport: false }], create(context, options) { - let allowAsImport = options[0].allowAsImport; + const allowAsImport = options[0].allowAsImport; const allowPatterns = options[0].allow?.map( pattern => new RegExp(pattern, 'u'), ); @@ -61,7 +61,7 @@ export default util.createRule({ return; } const variable = ASTUtils.findVariable(getScope(context), 'require'); - let parent = + const parent = node.parent.type === AST_NODE_TYPES.ChainExpression ? node.parent.parent : node.parent; From c108ea677588209bfc62cb23d284d5fa8f440cd0 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 1 Feb 2024 19:15:02 +0530 Subject: [PATCH 03/27] chore: more CI failures --- packages/eslint-plugin/src/configs/all.ts | 1 + .../tests/schema-snapshots/no-require-imports.shot | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index b7bdaac2ede4..8e53a2b2c8ec 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -114,6 +114,7 @@ export = { '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', '@typescript-eslint/parameter-properties': 'error', + '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', 'prefer-destructuring': 'off', '@typescript-eslint/prefer-destructuring': 'error', diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot b/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot index 7561bcbc4b3c..2b1da6da4fe9 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot @@ -14,6 +14,10 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos "type": "string" }, "type": "array" + }, + "allowAsImport": { + "description": "Allows `require` statements in import declarations.", + "type": "boolean" } }, "type": "object" @@ -27,6 +31,8 @@ type Options = [ { /** Patterns of import paths to allow requiring from. */ allow?: string[]; + /** Allows `require` statements in import declarations. */ + allowAsImport?: boolean; }, ]; " From 539306293a19b8d10f0c64988e8e7a5c00b0e057 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 1 Feb 2024 19:31:31 +0530 Subject: [PATCH 04/27] chore: small miss --- packages/eslint-plugin/src/configs/all.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index 8e53a2b2c8ec..3a5e6343b197 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -112,9 +112,9 @@ export = { '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-empty-export': 'error', '@typescript-eslint/no-useless-template-literals': 'error', + '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', '@typescript-eslint/parameter-properties': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', 'prefer-destructuring': 'off', '@typescript-eslint/prefer-destructuring': 'error', From a5c03b77df49e057946237ffc8d8e15cc4e54dfd Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 1 Feb 2024 19:51:22 +0530 Subject: [PATCH 05/27] chore: address ci failures --- packages/eslint-plugin/src/configs/all.ts | 1 - packages/eslint-plugin/src/configs/strict-type-checked.ts | 1 - packages/eslint-plugin/src/configs/strict.ts | 1 - .../tests/schema-snapshots/no-require-imports.shot | 4 ++-- 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index 3a5e6343b197..b7bdaac2ede4 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -112,7 +112,6 @@ export = { '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-empty-export': 'error', '@typescript-eslint/no-useless-template-literals': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', diff --git a/packages/eslint-plugin/src/configs/strict-type-checked.ts b/packages/eslint-plugin/src/configs/strict-type-checked.ts index a0f82563b1f3..1216cd64cc7f 100644 --- a/packages/eslint-plugin/src/configs/strict-type-checked.ts +++ b/packages/eslint-plugin/src/configs/strict-type-checked.ts @@ -58,7 +58,6 @@ export = { 'no-useless-constructor': 'off', '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-template-literals': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-includes': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', diff --git a/packages/eslint-plugin/src/configs/strict.ts b/packages/eslint-plugin/src/configs/strict.ts index 98553e52bf72..aff764c59413 100644 --- a/packages/eslint-plugin/src/configs/strict.ts +++ b/packages/eslint-plugin/src/configs/strict.ts @@ -32,7 +32,6 @@ export = { '@typescript-eslint/no-unused-vars': 'error', 'no-useless-constructor': 'off', '@typescript-eslint/no-useless-constructor': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', '@typescript-eslint/prefer-ts-expect-error': 'error', diff --git a/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot b/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot index 2b1da6da4fe9..088b2be7c210 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/no-require-imports.shot @@ -16,7 +16,7 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos "type": "array" }, "allowAsImport": { - "description": "Allows `require` statements in import declarations.", + "description": "Allows \`require\` statements in import declarations.", "type": "boolean" } }, @@ -31,7 +31,7 @@ type Options = [ { /** Patterns of import paths to allow requiring from. */ allow?: string[]; - /** Allows `require` statements in import declarations. */ + /** Allows \`require\` statements in import declarations. */ allowAsImport?: boolean; }, ]; From 03543208c0081b3d40fa3bca7eba4f48f8a0719f Mon Sep 17 00:00:00 2001 From: arka1002 Date: Tue, 6 Feb 2024 19:52:33 +0530 Subject: [PATCH 06/27] chore: few more tests --- .../tests/rules/no-require-imports.test.ts | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index b03158d2c015..256973995f93 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -222,5 +222,68 @@ var lib5 = require?.('lib5'), }, ], }, + { + code: "trick(require('foo'));", + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 7, + }, + ], + }, + { + code: "const foo = require('./foo.json') as Foo;", + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 13, + }, + ], + }, + { + code: "const foo: Foo = require('./foo.json').default;", + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 18, + }, + ], + }, + { + code: "const foo = require('./foo.json');", + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 18, + }, + ], + }, + { + code: ` +const configValidator = new Validator(require('./a.json')); +configValidator.addSchema(require('./a.json')); + `, + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 2, + column: 39, + }, + { + messageId: 'noRequireImports', + line: 3, + column: 27, + }, + ], + }, ], }); From 0ab1f789a155d2b94161b1e0ea9eb26e65b41cc6 Mon Sep 17 00:00:00 2001 From: Arka Pratim Chaudhuri <105232141+arka1002@users.noreply.github.com> Date: Tue, 6 Feb 2024 20:45:14 +0530 Subject: [PATCH 07/27] chore: update docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Josh Goldberg ✨ --- packages/eslint-plugin/docs/rules/no-require-imports.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.md b/packages/eslint-plugin/docs/rules/no-require-imports.md index 1ba4c83665cc..a14543e1c630 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.md +++ b/packages/eslint-plugin/docs/rules/no-require-imports.md @@ -52,7 +52,8 @@ console.log(require('../package.json').version); ### `allowAsImport` -A boolean. If it's set to `true`, the require statements with an `import` declaration won't be reported. The default value is `false`. If you want to apply the `no-var-requires` rule, then set it to `true`. +If it's set to `true`, the require statements with an `import` declaration won't be reported. +This is useful if you're on an older CommonJS-style codebase and haven't been able to transition to ESM-style imports yet. With `{allowAsImport: true}`: From 145c4e5db741e1f1a1a7036a18575a28d016cf56 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 8 Feb 2024 10:59:45 +0530 Subject: [PATCH 08/27] chore: address reviews --- .../src/configs/recommended-type-checked.ts | 1 - .../eslint-plugin/src/configs/recommended.ts | 1 - .../src/rules/no-var-requires.ts | 1 - .../tests/rules/no-require-imports.test.ts | 36 +++++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/configs/recommended-type-checked.ts b/packages/eslint-plugin/src/configs/recommended-type-checked.ts index 6c0a8aff826e..45a430166d91 100644 --- a/packages/eslint-plugin/src/configs/recommended-type-checked.ts +++ b/packages/eslint-plugin/src/configs/recommended-type-checked.ts @@ -43,7 +43,6 @@ export = { '@typescript-eslint/no-unsafe-return': 'error', 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', 'require-await': 'off', '@typescript-eslint/require-await': 'error', diff --git a/packages/eslint-plugin/src/configs/recommended.ts b/packages/eslint-plugin/src/configs/recommended.ts index a4c94992c159..2bc888a1b470 100644 --- a/packages/eslint-plugin/src/configs/recommended.ts +++ b/packages/eslint-plugin/src/configs/recommended.ts @@ -27,7 +27,6 @@ export = { '@typescript-eslint/no-unsafe-declaration-merging': 'error', 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/triple-slash-reference': 'error', }, diff --git a/packages/eslint-plugin/src/rules/no-var-requires.ts b/packages/eslint-plugin/src/rules/no-var-requires.ts index 9b6a13a4bbfd..d2eb3b075d14 100644 --- a/packages/eslint-plugin/src/rules/no-var-requires.ts +++ b/packages/eslint-plugin/src/rules/no-var-requires.ts @@ -17,7 +17,6 @@ export default createRule({ type: 'problem', docs: { description: 'Disallow `require` statements except in import statements', - recommended: 'recommended', }, messages: { noVarReqs: 'Require statement not part of import statement.', diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index 256973995f93..3dc58599285b 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -59,6 +59,42 @@ require('remark-preset-prettier'); code: 'require?.(foo);', options: [{ allowAsImport: true }], }, + { + code: ` +declare function require(path: string): unknown; +trick(require('foo')); + `, + options: [{ allowAsImport: true }], + }, + { + code: ` +declare function require(path: string): unknown; +const foo = require('./foo.json') as Foo; + `, + options: [{ allowAsImport: true }], + }, + { + code: ` +declare function require(path: string): unknown; +const foo: Foo = require('./foo.json').default; + `, + options: [{ allowAsImport: true }], + }, + { + code: ` +declare function require(path: string): unknown; +const foo = require('./foo.json'); + `, + options: [{ allowAsImport: true }], + }, + { + code: ` +declare function require(path: string): unknown; +const configValidator = new Validator(require('./a.json')); +configValidator.addSchema(require('./a.json')); + `, + options: [{ allowAsImport: true }], + }, ], invalid: [ { From 61baa8185883af85ab633b0d996cbbd5de6c73a0 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Mon, 12 Feb 2024 20:34:07 +0530 Subject: [PATCH 09/27] missed one test --- .../eslint-plugin/tests/rules/no-require-imports.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index 3dc58599285b..1b072e4cd6dc 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -95,6 +95,14 @@ configValidator.addSchema(require('./a.json')); `, options: [{ allowAsImport: true }], }, + { + code: ` +import { createRequire } from 'module'; +const require = createRequire(); +require('remark-preset-prettier'); + `, + options: [{ allowAsImport: true }], + }, ], invalid: [ { From 2815ab414c6aaf0b1d534f64819ca1f6875ca95c Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 21 Mar 2024 10:47:20 +0530 Subject: [PATCH 10/27] chore: fix tests --- packages/typescript-eslint/src/configs/all.ts | 1 - .../typescript-eslint/src/configs/recommended-type-checked.ts | 1 - packages/typescript-eslint/src/configs/recommended.ts | 1 - packages/typescript-eslint/src/configs/strict-type-checked.ts | 1 - packages/typescript-eslint/src/configs/strict.ts | 1 - 5 files changed, 5 deletions(-) diff --git a/packages/typescript-eslint/src/configs/all.ts b/packages/typescript-eslint/src/configs/all.ts index ad4a85cd524c..64abffbfebbe 100644 --- a/packages/typescript-eslint/src/configs/all.ts +++ b/packages/typescript-eslint/src/configs/all.ts @@ -124,7 +124,6 @@ export default ( '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-empty-export': 'error', '@typescript-eslint/no-useless-template-literals': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', diff --git a/packages/typescript-eslint/src/configs/recommended-type-checked.ts b/packages/typescript-eslint/src/configs/recommended-type-checked.ts index 77feebb92cba..bccb947ce291 100644 --- a/packages/typescript-eslint/src/configs/recommended-type-checked.ts +++ b/packages/typescript-eslint/src/configs/recommended-type-checked.ts @@ -51,7 +51,6 @@ export default ( '@typescript-eslint/no-unsafe-return': 'error', 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', 'require-await': 'off', '@typescript-eslint/require-await': 'error', diff --git a/packages/typescript-eslint/src/configs/recommended.ts b/packages/typescript-eslint/src/configs/recommended.ts index 042891ebf9b2..893e08bf9468 100644 --- a/packages/typescript-eslint/src/configs/recommended.ts +++ b/packages/typescript-eslint/src/configs/recommended.ts @@ -35,7 +35,6 @@ export default ( '@typescript-eslint/no-unsafe-declaration-merging': 'error', 'no-unused-vars': 'off', '@typescript-eslint/no-unused-vars': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/triple-slash-reference': 'error', }, diff --git a/packages/typescript-eslint/src/configs/strict-type-checked.ts b/packages/typescript-eslint/src/configs/strict-type-checked.ts index e53f57934cea..f4d38ab6bbaf 100644 --- a/packages/typescript-eslint/src/configs/strict-type-checked.ts +++ b/packages/typescript-eslint/src/configs/strict-type-checked.ts @@ -74,7 +74,6 @@ export default ( 'no-useless-constructor': 'off', '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-template-literals': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-includes': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', diff --git a/packages/typescript-eslint/src/configs/strict.ts b/packages/typescript-eslint/src/configs/strict.ts index dabfa2f78a92..612b3c360426 100644 --- a/packages/typescript-eslint/src/configs/strict.ts +++ b/packages/typescript-eslint/src/configs/strict.ts @@ -45,7 +45,6 @@ export default ( '@typescript-eslint/no-unused-vars': 'error', 'no-useless-constructor': 'off', '@typescript-eslint/no-useless-constructor': 'error', - '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', '@typescript-eslint/prefer-ts-expect-error': 'error', From 7f8466c22c5bf557dd24618a66b38d4476050f19 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 21 Mar 2024 11:42:32 +0530 Subject: [PATCH 11/27] chore: add recommended --- packages/eslint-plugin/src/configs/recommended-type-checked.ts | 1 + packages/eslint-plugin/src/configs/recommended.ts | 1 + packages/eslint-plugin/src/configs/strict-type-checked.ts | 1 + packages/eslint-plugin/src/configs/strict.ts | 1 + packages/eslint-plugin/src/rules/no-require-imports.ts | 1 + .../typescript-eslint/src/configs/recommended-type-checked.ts | 1 + packages/typescript-eslint/src/configs/recommended.ts | 1 + packages/typescript-eslint/src/configs/strict-type-checked.ts | 1 + packages/typescript-eslint/src/configs/strict.ts | 1 + 9 files changed, 9 insertions(+) diff --git a/packages/eslint-plugin/src/configs/recommended-type-checked.ts b/packages/eslint-plugin/src/configs/recommended-type-checked.ts index 7b132a2c703c..ff88c5bc5d41 100644 --- a/packages/eslint-plugin/src/configs/recommended-type-checked.ts +++ b/packages/eslint-plugin/src/configs/recommended-type-checked.ts @@ -31,6 +31,7 @@ export = { '@typescript-eslint/no-namespace': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-unnecessary-type-assertion': 'error', '@typescript-eslint/no-unnecessary-type-constraint': 'error', diff --git a/packages/eslint-plugin/src/configs/recommended.ts b/packages/eslint-plugin/src/configs/recommended.ts index 90b0715c781b..bbca526fc7b7 100644 --- a/packages/eslint-plugin/src/configs/recommended.ts +++ b/packages/eslint-plugin/src/configs/recommended.ts @@ -22,6 +22,7 @@ export = { '@typescript-eslint/no-misused-new': 'error', '@typescript-eslint/no-namespace': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-unnecessary-type-constraint': 'error', '@typescript-eslint/no-unsafe-declaration-merging': 'error', diff --git a/packages/eslint-plugin/src/configs/strict-type-checked.ts b/packages/eslint-plugin/src/configs/strict-type-checked.ts index ffeff24d5e33..e94ceb02630f 100644 --- a/packages/eslint-plugin/src/configs/strict-type-checked.ts +++ b/packages/eslint-plugin/src/configs/strict-type-checked.ts @@ -43,6 +43,7 @@ export = { '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', 'no-throw-literal': 'off', '@typescript-eslint/no-throw-literal': 'error', diff --git a/packages/eslint-plugin/src/configs/strict.ts b/packages/eslint-plugin/src/configs/strict.ts index 96ad20782be6..8b05b024833e 100644 --- a/packages/eslint-plugin/src/configs/strict.ts +++ b/packages/eslint-plugin/src/configs/strict.ts @@ -30,6 +30,7 @@ export = { '@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-non-null-assertion': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-unnecessary-type-constraint': 'error', '@typescript-eslint/no-unsafe-declaration-merging': 'error', diff --git a/packages/eslint-plugin/src/rules/no-require-imports.ts b/packages/eslint-plugin/src/rules/no-require-imports.ts index 6ab2a8d0ec2c..758fc3696984 100644 --- a/packages/eslint-plugin/src/rules/no-require-imports.ts +++ b/packages/eslint-plugin/src/rules/no-require-imports.ts @@ -17,6 +17,7 @@ export default util.createRule({ type: 'problem', docs: { description: 'Disallow invocation of `require()`', + recommended: 'recommended', }, schema: [ { diff --git a/packages/typescript-eslint/src/configs/recommended-type-checked.ts b/packages/typescript-eslint/src/configs/recommended-type-checked.ts index bccb947ce291..ce783ca16765 100644 --- a/packages/typescript-eslint/src/configs/recommended-type-checked.ts +++ b/packages/typescript-eslint/src/configs/recommended-type-checked.ts @@ -39,6 +39,7 @@ export default ( '@typescript-eslint/no-namespace': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-unnecessary-type-assertion': 'error', '@typescript-eslint/no-unnecessary-type-constraint': 'error', diff --git a/packages/typescript-eslint/src/configs/recommended.ts b/packages/typescript-eslint/src/configs/recommended.ts index 893e08bf9468..2e8f0bfe4cc7 100644 --- a/packages/typescript-eslint/src/configs/recommended.ts +++ b/packages/typescript-eslint/src/configs/recommended.ts @@ -30,6 +30,7 @@ export default ( '@typescript-eslint/no-misused-new': 'error', '@typescript-eslint/no-namespace': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-unnecessary-type-constraint': 'error', '@typescript-eslint/no-unsafe-declaration-merging': 'error', diff --git a/packages/typescript-eslint/src/configs/strict-type-checked.ts b/packages/typescript-eslint/src/configs/strict-type-checked.ts index f4d38ab6bbaf..178451236c50 100644 --- a/packages/typescript-eslint/src/configs/strict-type-checked.ts +++ b/packages/typescript-eslint/src/configs/strict-type-checked.ts @@ -54,6 +54,7 @@ export default ( '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', 'no-throw-literal': 'off', '@typescript-eslint/no-throw-literal': 'error', diff --git a/packages/typescript-eslint/src/configs/strict.ts b/packages/typescript-eslint/src/configs/strict.ts index 612b3c360426..ee364384c415 100644 --- a/packages/typescript-eslint/src/configs/strict.ts +++ b/packages/typescript-eslint/src/configs/strict.ts @@ -38,6 +38,7 @@ export default ( '@typescript-eslint/no-non-null-asserted-nullish-coalescing': 'error', '@typescript-eslint/no-non-null-asserted-optional-chain': 'error', '@typescript-eslint/no-non-null-assertion': 'error', + '@typescript-eslint/no-require-imports': 'error', '@typescript-eslint/no-this-alias': 'error', '@typescript-eslint/no-unnecessary-type-constraint': 'error', '@typescript-eslint/no-unsafe-declaration-merging': 'error', From 04d7887ca0b74f8d142ed81e56f5b3d676f46ec4 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 21 Mar 2024 12:18:54 +0530 Subject: [PATCH 12/27] chore: lint errors --- eslint.config.mjs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/eslint.config.mjs b/eslint.config.mjs index 01f94ffd0ade..e6c3183629cd 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -169,6 +169,12 @@ export default tseslint.config( ignorePrimitives: true, }, ], + '@typescript-eslint/no-require-imports': [ + 'error', + { + allow: ['/package\\.json$'], + }, + ], // // Internal repo rules From 39cc57ae3f1a42f188afb94e72e02a0a0d0a86b4 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 21 Mar 2024 16:03:42 +0530 Subject: [PATCH 13/27] chore: address reviews --- .../src/rules/no-require-imports.ts | 1 + .../src/rules/no-var-requires.ts | 1 + .../tests/rules/no-require-imports.test.ts | 54 ++++++++++++++----- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-require-imports.ts b/packages/eslint-plugin/src/rules/no-require-imports.ts index 758fc3696984..c9c381c81527 100644 --- a/packages/eslint-plugin/src/rules/no-require-imports.ts +++ b/packages/eslint-plugin/src/rules/no-require-imports.ts @@ -85,6 +85,7 @@ export default util.createRule({ AST_NODE_TYPES.TSAsExpression, AST_NODE_TYPES.TSTypeAssertion, AST_NODE_TYPES.VariableDeclarator, + AST_NODE_TYPES.ExpressionStatement, ].includes(parent.type) ) { if (!variable?.identifiers.length) { diff --git a/packages/eslint-plugin/src/rules/no-var-requires.ts b/packages/eslint-plugin/src/rules/no-var-requires.ts index 9ef12211a2ae..b7c38620168a 100644 --- a/packages/eslint-plugin/src/rules/no-var-requires.ts +++ b/packages/eslint-plugin/src/rules/no-var-requires.ts @@ -14,6 +14,7 @@ export default createRule({ name: 'no-var-requires', meta: { deprecated: true, + replacedBy: ['@typescript-eslint/no-require-imports'], type: 'problem', docs: { description: 'Disallow `require` statements except in import statements', diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index 6c0f8d4505d4..0a9e56fc8520 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -55,45 +55,37 @@ require('remark-preset-prettier'); code: "import foo = require('foo');", options: [{ allowAsImport: true }], }, - { - code: 'require(foo);', - options: [{ allowAsImport: true }], - }, - { - code: 'require?.(foo);', - options: [{ allowAsImport: true }], - }, { code: ` -declare function require(path: string): unknown; +let require = bazz; trick(require('foo')); `, options: [{ allowAsImport: true }], }, { code: ` -declare function require(path: string): unknown; +let require = bazz; const foo = require('./foo.json') as Foo; `, options: [{ allowAsImport: true }], }, { code: ` -declare function require(path: string): unknown; +let require = bazz; const foo: Foo = require('./foo.json').default; `, options: [{ allowAsImport: true }], }, { code: ` -declare function require(path: string): unknown; +let require = bazz; const foo = require('./foo.json'); `, options: [{ allowAsImport: true }], }, { code: ` -declare function require(path: string): unknown; +let require = bazz; const configValidator = new Validator(require('./a.json')); configValidator.addSchema(require('./a.json')); `, @@ -101,6 +93,20 @@ configValidator.addSchema(require('./a.json')); }, { code: ` +let require = bazz; +require('foo'); + `, + options: [{ allowAsImport: true }], + }, + { + code: ` +let require = bazz; +require?.('foo'); + `, + options: [{ allowAsImport: true }], + }, + { + code: ` import { createRequire } from 'module'; const require = createRequire(); require('remark-preset-prettier'); @@ -344,5 +350,27 @@ configValidator.addSchema(require('./a.json')); }, ], }, + { + code: 'require(foo);', + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 1, + }, + ], + }, + { + code: 'require?.(foo);', + options: [{ allowAsImport: true }], + errors: [ + { + messageId: 'noRequireImports', + line: 1, + column: 1, + }, + ], + }, ], }); From c060a3571c49bd9aab7d2d284e5c3381231429f1 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 21 Mar 2024 18:29:41 +0530 Subject: [PATCH 14/27] chore: small miss --- packages/eslint-plugin/docs/rules/no-require-imports.mdx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.mdx b/packages/eslint-plugin/docs/rules/no-require-imports.mdx index 23cc3826d6f8..b8d8ace4deec 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.mdx +++ b/packages/eslint-plugin/docs/rules/no-require-imports.mdx @@ -59,6 +59,7 @@ console.log(require('../package.json').version); + ### `allowAsImport` If it's set to `true`, the require statements with an `import` declaration won't be reported. From b2a008190aab7fe266ddf8927d1f7c666815a4db Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 21 Mar 2024 19:41:45 +0530 Subject: [PATCH 15/27] chore: mistake --- packages/eslint-plugin/docs/rules/no-require-imports.mdx | 1 - packages/eslint-plugin/tests/rules/no-require-imports.test.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.mdx b/packages/eslint-plugin/docs/rules/no-require-imports.mdx index b8d8ace4deec..23cc3826d6f8 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.mdx +++ b/packages/eslint-plugin/docs/rules/no-require-imports.mdx @@ -59,7 +59,6 @@ console.log(require('../package.json').version); - ### `allowAsImport` If it's set to `true`, the require statements with an `import` declaration won't be reported. diff --git a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts index 0a9e56fc8520..4fc743026e56 100644 --- a/packages/eslint-plugin/tests/rules/no-require-imports.test.ts +++ b/packages/eslint-plugin/tests/rules/no-require-imports.test.ts @@ -255,7 +255,7 @@ var lib5 = require?.('lib5'), ], }, { - code: "import pkg = require('./package.json');", + code: 'import pkg = require(`./package.json`);', options: [{ allow: ['^some-package$'] }], errors: [ { From cf12e820fa89c258d1e6163206bd9d727418816b Mon Sep 17 00:00:00 2001 From: arka1002 Date: Sun, 31 Mar 2024 11:18:53 +0530 Subject: [PATCH 16/27] chore: eslint comments --- packages/website-eslint/src/mock/eslint-rules.js | 1 + packages/website-eslint/src/mock/eslint.js | 2 +- packages/website/sidebars/sidebar.rules.js | 1 + packages/website/src/clientModules.js | 1 + packages/website/src/theme/prism-include-languages.js | 3 ++- packages/website/webpack.plugin.js | 3 +++ 6 files changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/website-eslint/src/mock/eslint-rules.js b/packages/website-eslint/src/mock/eslint-rules.js index e38eed4cefbd..65b54e0bc983 100644 --- a/packages/website-eslint/src/mock/eslint-rules.js +++ b/packages/website-eslint/src/mock/eslint-rules.js @@ -1 +1,2 @@ +// eslint-disable-next-line @typescript-eslint/no-require-imports exports.builtinRules = require('vt:eslint/rules'); diff --git a/packages/website-eslint/src/mock/eslint.js b/packages/website-eslint/src/mock/eslint.js index fac5d44d93e6..b5d428738f9a 100644 --- a/packages/website-eslint/src/mock/eslint.js +++ b/packages/website-eslint/src/mock/eslint.js @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-extraneous-class */ class RuleTester {} class SourceCode {} - +// eslint-disable-next-line @typescript-eslint/no-require-imports exports.Linter = require('vt:eslint/linter').Linter; exports.RuleTester = RuleTester; diff --git a/packages/website/sidebars/sidebar.rules.js b/packages/website/sidebars/sidebar.rules.js index 4e6887df8cd9..0815d479b0b4 100644 --- a/packages/website/sidebars/sidebar.rules.js +++ b/packages/website/sidebars/sidebar.rules.js @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-require-imports const plugin = require('@typescript-eslint/eslint-plugin'); const rules = Object.entries(plugin.rules).map(([name, rule]) => { diff --git a/packages/website/src/clientModules.js b/packages/website/src/clientModules.js index b393e94f50f7..8b38aa40ef73 100644 --- a/packages/website/src/clientModules.js +++ b/packages/website/src/clientModules.js @@ -1,5 +1,6 @@ import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment'; if (ExecutionEnvironment.canUseDOM) { + // eslint-disable-next-line @typescript-eslint/no-require-imports require('konamimojisplosion').initializeKonamimojisplosion(); } diff --git a/packages/website/src/theme/prism-include-languages.js b/packages/website/src/theme/prism-include-languages.js index 660275a1af1b..f356adf2a581 100644 --- a/packages/website/src/theme/prism-include-languages.js +++ b/packages/website/src/theme/prism-include-languages.js @@ -8,9 +8,10 @@ export default function prismIncludeLanguages(PrismObject) { globalThis.Prism = PrismObject; additionalLanguages.forEach(lang => { + // eslint-disable-next-line @typescript-eslint/no-require-imports require(`prismjs/components/prism-${lang}`); }); - + // eslint-disable-next-line @typescript-eslint/no-require-imports require(`../prism/language/jsonc`); delete globalThis.Prism; } diff --git a/packages/website/webpack.plugin.js b/packages/website/webpack.plugin.js index fff4c85008be..ab5dd29767fd 100644 --- a/packages/website/webpack.plugin.js +++ b/packages/website/webpack.plugin.js @@ -1,5 +1,8 @@ +// eslint-disable-next-line @typescript-eslint/no-require-imports const webpack = require('webpack'); +// eslint-disable-next-line @typescript-eslint/no-require-imports const path = require('path'); +// eslint-disable-next-line @typescript-eslint/no-require-imports const CopyPlugin = require('copy-webpack-plugin'); module.exports = function (/*context, options*/) { From 92838773a40791c5a908d506099cd69b61db08f2 Mon Sep 17 00:00:00 2001 From: Arka Pratim Chaudhuri <105232141+arka1002@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:44:47 +0530 Subject: [PATCH 17/27] chore: apply suggestions from code review Co-authored-by: auvred <61150013+auvred@users.noreply.github.com> --- packages/eslint-plugin/docs/rules/no-require-imports.mdx | 4 ++-- packages/eslint-plugin/docs/rules/no-var-requires.mdx | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.mdx b/packages/eslint-plugin/docs/rules/no-require-imports.mdx index 23cc3826d6f8..08db7bcbbd42 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.mdx +++ b/packages/eslint-plugin/docs/rules/no-require-imports.mdx @@ -69,7 +69,7 @@ With `{allowAsImport: true}`: -```ts +```ts option='{ "allowAsImport": true }' var foo = require('foo'); const foo = require('foo'); let foo = require('foo'); @@ -78,7 +78,7 @@ let foo = require('foo'); -```ts +```ts option='{ "allowAsImport": true }' import foo = require('foo'); require('foo'); import foo from 'foo'; diff --git a/packages/eslint-plugin/docs/rules/no-var-requires.mdx b/packages/eslint-plugin/docs/rules/no-var-requires.mdx index b1cea0937bee..68579b575a79 100644 --- a/packages/eslint-plugin/docs/rules/no-var-requires.mdx +++ b/packages/eslint-plugin/docs/rules/no-var-requires.mdx @@ -11,7 +11,7 @@ import TabItem from '@theme/TabItem'; :::danger Deprecated -This rule has been deprecated in favour of the [`@typescript-eslint/no-require-imports`](./no-require-imports.md) rule. +This rule has been deprecated in favour of the [`@typescript-eslint/no-require-imports`](./no-require-imports.mdx) rule. ::: From 8253e21926aeaebac329c73f3c009f5d3cfa1fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Mon, 15 Apr 2024 10:56:27 -0400 Subject: [PATCH 18/27] feat(typescript-estree): remove slow deprecated and isolated programs (#8834) * feat(typescript-estree): remove slow deprecated and isolated programs * Update packages/typescript-estree/src/create-program/createProjectProgram.ts Co-authored-by: Brad Zacher --------- Co-authored-by: Brad Zacher --- docs/packages/TypeScript_ESTree.mdx | 6 -- .../create-program/createDefaultProgram.ts | 72 ------------------- .../create-program/createProjectProgram.ts | 7 +- .../src/parseSettings/createParseSettings.ts | 3 - .../src/parseSettings/index.ts | 7 -- .../typescript-estree/src/parser-options.ts | 6 -- packages/typescript-estree/src/parser.ts | 17 +---- .../tests/lib/semanticInfo.test.ts | 9 --- .../website/src/components/linter/config.ts | 1 - 9 files changed, 4 insertions(+), 124 deletions(-) delete mode 100644 packages/typescript-estree/src/create-program/createDefaultProgram.ts diff --git a/docs/packages/TypeScript_ESTree.mdx b/docs/packages/TypeScript_ESTree.mdx index 7269942e84d3..b1993353e2dd 100644 --- a/docs/packages/TypeScript_ESTree.mdx +++ b/docs/packages/TypeScript_ESTree.mdx @@ -235,12 +235,6 @@ interface ParseAndGenerateServicesOptions extends ParseOptions { */ programs?: Program[]; - /** - * @deprecated - this flag will be removed in the next major. - * Do not rely on the behavior provided by this flag. - */ - DEPRECATED__createDefaultProgram?: boolean; - /** * ESLint (and therefore typescript-eslint) is used in both "single run"/one-time contexts, * such as an ESLint CLI invocation, and long-running sessions (such as continuous feedback diff --git a/packages/typescript-estree/src/create-program/createDefaultProgram.ts b/packages/typescript-estree/src/create-program/createDefaultProgram.ts deleted file mode 100644 index b42ec76c678b..000000000000 --- a/packages/typescript-estree/src/create-program/createDefaultProgram.ts +++ /dev/null @@ -1,72 +0,0 @@ -import debug from 'debug'; -import path from 'path'; -import * as ts from 'typescript'; - -import type { ParseSettings } from '../parseSettings'; -import type { ASTAndDefiniteProgram } from './shared'; -import { createDefaultCompilerOptionsFromExtra } from './shared'; - -const log = debug('typescript-eslint:typescript-estree:createDefaultProgram'); - -/** - * @param parseSettings Internal settings for parsing the file - * @returns If found, returns the source file corresponding to the code and the containing program - * @deprecated - * This is a legacy option that comes with severe performance penalties. - * Please do not use it. - */ -function createDefaultProgram( - parseSettings: ParseSettings, -): ASTAndDefiniteProgram | undefined { - log( - 'Getting default program for: %s', - parseSettings.filePath || 'unnamed file', - ); - - if (parseSettings.projects.length !== 1) { - return undefined; - } - - const tsconfigPath = parseSettings.projects[0]; - - const commandLine = ts.getParsedCommandLineOfConfigFile( - tsconfigPath, - createDefaultCompilerOptionsFromExtra(parseSettings), - { - ...ts.sys, - // TODO: file issue on TypeScript to suggest making optional? - // eslint-disable-next-line @typescript-eslint/no-empty-function - onUnRecoverableConfigFileDiagnostic: () => {}, - }, - ); - - if (!commandLine) { - return undefined; - } - - const compilerHost = ts.createCompilerHost( - commandLine.options, - /* setParentNodes */ true, - ); - - const oldReadFile = compilerHost.readFile; - compilerHost.readFile = (fileName: string): string | undefined => - path.normalize(fileName) === path.normalize(parseSettings.filePath) - ? parseSettings.codeFullText - : oldReadFile(fileName); - - const program = ts.createProgram( - [parseSettings.filePath], - { - ...commandLine.options, - jsDocParsingMode: parseSettings.jsDocParsingMode, - }, - compilerHost, - ); - const ast = program.getSourceFile(parseSettings.filePath); - - return ast && { ast, program }; -} - -// eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major -export { createDefaultProgram }; diff --git a/packages/typescript-estree/src/create-program/createProjectProgram.ts b/packages/typescript-estree/src/create-program/createProjectProgram.ts index a58097e3cd73..280e48daff2e 100644 --- a/packages/typescript-estree/src/create-program/createProjectProgram.ts +++ b/packages/typescript-estree/src/create-program/createProjectProgram.ts @@ -28,16 +28,15 @@ const DEFAULT_EXTRA_FILE_EXTENSIONS = [ function createProjectProgram( parseSettings: ParseSettings, programsForProjects: readonly ts.Program[], -): ASTAndDefiniteProgram | undefined { +): ASTAndDefiniteProgram { log('Creating project program for: %s', parseSettings.filePath); const astAndProgram = firstDefined(programsForProjects, currentProgram => getAstFromProgram(currentProgram, parseSettings.filePath), ); - // The file was either matched within the tsconfig, or we allow creating a default program - // eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major - if (astAndProgram || parseSettings.DEPRECATED__createDefaultProgram) { + // The file was matched within the tsconfig + if (astAndProgram) { return astAndProgram; } diff --git a/packages/typescript-estree/src/parseSettings/createParseSettings.ts b/packages/typescript-estree/src/parseSettings/createParseSettings.ts index 49d048391d8f..210da9bcb042 100644 --- a/packages/typescript-estree/src/parseSettings/createParseSettings.ts +++ b/packages/typescript-estree/src/parseSettings/createParseSettings.ts @@ -68,9 +68,6 @@ export function createParseSettings( codeFullText, comment: options.comment === true, comments: [], - DEPRECATED__createDefaultProgram: - // eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major - options.DEPRECATED__createDefaultProgram === true, debugLevel: options.debugLevel === true ? new Set(['typescript-eslint']) diff --git a/packages/typescript-estree/src/parseSettings/index.ts b/packages/typescript-estree/src/parseSettings/index.ts index 1df275901066..65e197ca70af 100644 --- a/packages/typescript-estree/src/parseSettings/index.ts +++ b/packages/typescript-estree/src/parseSettings/index.ts @@ -47,13 +47,6 @@ export interface MutableParseSettings { */ comments: TSESTree.Comment[]; - /** - * @deprecated - * This is a legacy option that comes with severe performance penalties. - * Please do not use it. - */ - DEPRECATED__createDefaultProgram: boolean; - /** * Which debug areas should be logged. */ diff --git a/packages/typescript-estree/src/parser-options.ts b/packages/typescript-estree/src/parser-options.ts index 4347ee29e47e..3c4c3ecd253b 100644 --- a/packages/typescript-estree/src/parser-options.ts +++ b/packages/typescript-estree/src/parser-options.ts @@ -192,12 +192,6 @@ interface ParseAndGenerateServicesOptions extends ParseOptions { */ programs?: ts.Program[] | null; - /** - * @deprecated - this flag will be removed in the next major. - * Do not rely on the behavior provided by this flag. - */ - DEPRECATED__createDefaultProgram?: boolean; - /** * ESLint (and therefore typescript-eslint) is used in both "single run"/one-time contexts, * such as an ESLint CLI invocation, and long-running sessions (such as continuous feedback diff --git a/packages/typescript-estree/src/parser.ts b/packages/typescript-estree/src/parser.ts index ffa0c4212295..4c4b7f61a848 100644 --- a/packages/typescript-estree/src/parser.ts +++ b/packages/typescript-estree/src/parser.ts @@ -3,7 +3,6 @@ import type * as ts from 'typescript'; import { astConverter } from './ast-converter'; import { convertError } from './convert'; -import { createDefaultProgram } from './create-program/createDefaultProgram'; import { createIsolatedProgram } from './create-program/createIsolatedProgram'; import { createProjectProgram } from './create-program/createProjectProgram'; import { @@ -76,24 +75,10 @@ function getProgramAndAST( return createNoProgram(parseSettings); } - const fromProjectProgram = createProjectProgram( + return createProjectProgram( parseSettings, getWatchProgramsForProjects(parseSettings), ); - if (fromProjectProgram) { - return fromProjectProgram; - } - - // eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major - if (parseSettings.DEPRECATED__createDefaultProgram) { - // eslint-disable-next-line deprecation/deprecation -- will be cleaned up with the next major - const fromDefaultProgram = createDefaultProgram(parseSettings); - if (fromDefaultProgram) { - return fromDefaultProgram; - } - } - - return createIsolatedProgram(parseSettings); } // eslint-disable-next-line @typescript-eslint/no-empty-interface diff --git a/packages/typescript-estree/tests/lib/semanticInfo.test.ts b/packages/typescript-estree/tests/lib/semanticInfo.test.ts index f5f78934a880..eac23a5deeab 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo.test.ts @@ -302,15 +302,6 @@ describe('semanticInfo', () => { }); } - it('default program produced with option', () => { - const parseResult = parseCodeAndGenerateServices('var foo = 5;', { - ...createOptions(''), - DEPRECATED__createDefaultProgram: true, - }); - - expectToHaveParserServices(parseResult.services); - }); - it('empty programs array should throw', () => { const fileName = path.resolve(FIXTURES_DIR, 'isolated-file.src.ts'); const badConfig = createOptions(fileName); diff --git a/packages/website/src/components/linter/config.ts b/packages/website/src/components/linter/config.ts index 8262487369c1..fea3cf897b16 100644 --- a/packages/website/src/components/linter/config.ts +++ b/packages/website/src/components/linter/config.ts @@ -10,7 +10,6 @@ export const defaultParseSettings: ParseSettings = { comment: true, comments: [], debugLevel: new Set(), - DEPRECATED__createDefaultProgram: false, errorOnTypeScriptSyntacticAndSemanticIssues: false, errorOnUnknownASTType: false, EXPERIMENTAL_projectService: undefined, From faa74c5e9ea181703d7577fafacbe11634be53a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Mon, 15 Apr 2024 11:20:16 -0400 Subject: [PATCH 19/27] fix(typescript-estree): add TSEnumBody node for TSEnumDeclaration body (#8920) * fix(typescript-estree): add TSEnumBody node for TSEnumDeclaration body * Fixed up tests and their snapshots * More about enums * Indent too * Update packages/ast-spec/src/special/TSEnumBody/spec.ts Co-authored-by: Brad Zacher * parent types touchups * Update packages/visitor-keys/src/visitor-keys.ts Co-authored-by: Brad Zacher --------- Co-authored-by: Brad Zacher --- packages/ast-spec/src/ast-node-types.ts | 1 + .../enum/snapshots/1-TSESTree-AST.shot | 11 +- .../enum/snapshots/5-AST-Alignment-AST.shot | 12 +- .../const/snapshots/1-TSESTree-AST.shot | 11 +- .../const/snapshots/5-AST-Alignment-AST.shot | 12 +- .../declare/snapshots/1-TSESTree-AST.shot | 11 +- .../snapshots/5-AST-Alignment-AST.shot | 12 +- .../empty/snapshots/1-TSESTree-AST.shot | 11 +- .../empty/snapshots/5-AST-Alignment-AST.shot | 12 +- .../snapshots/1-TSESTree-AST.shot | 57 ++--- .../snapshots/5-AST-Alignment-AST.shot | 86 +++++--- .../src/declaration/TSEnumDeclaration/spec.ts | 6 + .../ast-spec/src/element/TSEnumMember/spec.ts | 2 + .../const-enum/snapshots/1-TSESTree-AST.shot | 115 +++++----- .../snapshots/5-AST-Alignment-AST.shot | 159 +++++++++----- .../snapshots/1-TSESTree-AST.shot | 115 +++++----- .../snapshots/5-AST-Alignment-AST.shot | 160 +++++++++----- .../snapshots/1-TSESTree-AST.shot | 115 +++++----- .../snapshots/5-AST-Alignment-AST.shot | 159 +++++++++----- .../snapshots/1-TSESTree-AST.shot | 115 +++++----- .../snapshots/5-AST-Alignment-AST.shot | 158 +++++++++----- .../enum/snapshots/1-TSESTree-AST.shot | 93 ++++---- .../enum/snapshots/5-AST-Alignment-AST.shot | 126 +++++++---- .../ast-spec/src/special/TSEnumBody/spec.ts | 10 + packages/ast-spec/src/special/spec.ts | 1 + packages/ast-spec/src/unions/Node.ts | 2 + .../src/rules/prefer-ast-types-enum.ts | 3 +- .../eslint-plugin/src/rules/brace-style.ts | 2 +- .../eslint-plugin/src/rules/comma-dangle.ts | 2 +- packages/eslint-plugin/src/rules/indent.ts | 5 +- .../src/rules/no-duplicate-enum-values.ts | 2 +- .../eslint-plugin/src/rules/no-mixed-enums.ts | 10 +- .../src/rules/prefer-enum-initializers.ts | 2 +- .../tests/rules/indent/indent.test.ts | 101 --------- .../tests/rules/lines-around-comment.test.ts | 202 ------------------ .../src/referencer/Referencer.ts | 2 +- packages/typescript-estree/src/convert.ts | 66 +++++- .../src/ts-estree/estree-to-ts-node-types.ts | 1 + packages/visitor-keys/src/visitor-keys.ts | 3 +- 39 files changed, 1080 insertions(+), 893 deletions(-) create mode 100644 packages/ast-spec/src/special/TSEnumBody/spec.ts diff --git a/packages/ast-spec/src/ast-node-types.ts b/packages/ast-spec/src/ast-node-types.ts index 7a637489eb0d..c1f8d7b4cc36 100644 --- a/packages/ast-spec/src/ast-node-types.ts +++ b/packages/ast-spec/src/ast-node-types.ts @@ -110,6 +110,7 @@ export enum AST_NODE_TYPES { TSDeclareKeyword = 'TSDeclareKeyword', TSEmptyBodyFunctionExpression = 'TSEmptyBodyFunctionExpression', TSEnumDeclaration = 'TSEnumDeclaration', + TSEnumBody = 'TSEnumBody', TSEnumMember = 'TSEnumMember', TSExportAssignment = 'TSExportAssignment', TSExportKeyword = 'TSExportKeyword', diff --git a/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/1-TSESTree-AST.shot index f3dff0f70ee9..f8860c0534d2 100644 --- a/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/1-TSESTree-AST.shot @@ -9,6 +9,16 @@ Program { attributes: [], declaration: TSEnumDeclaration { type: "TSEnumDeclaration", + body: TSEnumBody { + type: "TSEnumBody", + members: [], + + range: [16, 18], + loc: { + start: { column: 16, line: 1 }, + end: { column: 18, line: 1 }, + }, + }, const: false, declare: false, id: Identifier { @@ -23,7 +33,6 @@ Program { end: { column: 15, line: 1 }, }, }, - members: [], range: [7, 18], loc: { diff --git a/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/5-AST-Alignment-AST.shot index 0f83a6c9f506..9aa4daf97447 100644 --- a/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/ExportNamedDeclaration/fixtures/enum/snapshots/5-AST-Alignment-AST.shot @@ -14,6 +14,16 @@ exports[`AST Fixtures declaration ExportNamedDeclaration enum AST Alignment - AS + assertions: Array [], declaration: TSEnumDeclaration { type: 'TSEnumDeclaration', +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [], +- +- range: [16, 18], +- loc: { +- start: { column: 16, line: 1 }, +- end: { column: 18, line: 1 }, +- }, +- }, - const: false, - declare: false, id: Identifier { @@ -28,7 +38,7 @@ exports[`AST Fixtures declaration ExportNamedDeclaration enum AST Alignment - AS end: { column: 15, line: 1 }, }, }, - members: Array [], ++ members: Array [], range: [7, 18], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/1-TSESTree-AST.shot index 45c1e395be04..30fdcfbc6815 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/1-TSESTree-AST.shot @@ -6,6 +6,16 @@ Program { body: [ TSEnumDeclaration { type: "TSEnumDeclaration", + body: TSEnumBody { + type: "TSEnumBody", + members: [], + + range: [15, 17], + loc: { + start: { column: 15, line: 1 }, + end: { column: 17, line: 1 }, + }, + }, const: true, declare: false, id: Identifier { @@ -20,7 +30,6 @@ Program { end: { column: 14, line: 1 }, }, }, - members: [], range: [0, 17], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/5-AST-Alignment-AST.shot index c9480e42c3a4..a0fe24cb4d5c 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/const/snapshots/5-AST-Alignment-AST.shot @@ -10,6 +10,16 @@ exports[`AST Fixtures declaration TSEnumDeclaration const AST Alignment - AST 1` body: Array [ TSEnumDeclaration { type: 'TSEnumDeclaration', +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [], +- +- range: [15, 17], +- loc: { +- start: { column: 15, line: 1 }, +- end: { column: 17, line: 1 }, +- }, +- }, const: true, - declare: false, id: Identifier { @@ -24,7 +34,7 @@ exports[`AST Fixtures declaration TSEnumDeclaration const AST Alignment - AST 1` end: { column: 14, line: 1 }, }, }, - members: Array [], ++ members: Array [], range: [0, 17], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/1-TSESTree-AST.shot index b4e7d92295e5..e1f611ebd1ba 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/1-TSESTree-AST.shot @@ -6,6 +6,16 @@ Program { body: [ TSEnumDeclaration { type: "TSEnumDeclaration", + body: TSEnumBody { + type: "TSEnumBody", + members: [], + + range: [17, 19], + loc: { + start: { column: 17, line: 1 }, + end: { column: 19, line: 1 }, + }, + }, const: false, declare: true, id: Identifier { @@ -20,7 +30,6 @@ Program { end: { column: 16, line: 1 }, }, }, - members: [], range: [0, 19], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/5-AST-Alignment-AST.shot index 3926e3f88cc8..c942393ad72d 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/declare/snapshots/5-AST-Alignment-AST.shot @@ -10,6 +10,16 @@ exports[`AST Fixtures declaration TSEnumDeclaration declare AST Alignment - AST body: Array [ TSEnumDeclaration { type: 'TSEnumDeclaration', +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [], +- +- range: [17, 19], +- loc: { +- start: { column: 17, line: 1 }, +- end: { column: 19, line: 1 }, +- }, +- }, - const: false, declare: true, id: Identifier { @@ -24,7 +34,7 @@ exports[`AST Fixtures declaration TSEnumDeclaration declare AST Alignment - AST end: { column: 16, line: 1 }, }, }, - members: Array [], ++ members: Array [], range: [0, 19], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/1-TSESTree-AST.shot index 2c454bcfcd82..dd8b9928297b 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/1-TSESTree-AST.shot @@ -6,6 +6,16 @@ Program { body: [ TSEnumDeclaration { type: "TSEnumDeclaration", + body: TSEnumBody { + type: "TSEnumBody", + members: [], + + range: [9, 11], + loc: { + start: { column: 9, line: 1 }, + end: { column: 11, line: 1 }, + }, + }, const: false, declare: false, id: Identifier { @@ -20,7 +30,6 @@ Program { end: { column: 8, line: 1 }, }, }, - members: [], range: [0, 11], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/5-AST-Alignment-AST.shot index 86d2f4844f4a..f6fd91b6e44c 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/empty/snapshots/5-AST-Alignment-AST.shot @@ -10,6 +10,16 @@ exports[`AST Fixtures declaration TSEnumDeclaration empty AST Alignment - AST 1` body: Array [ TSEnumDeclaration { type: 'TSEnumDeclaration', +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [], +- +- range: [9, 11], +- loc: { +- start: { column: 9, line: 1 }, +- end: { column: 11, line: 1 }, +- }, +- }, - const: false, - declare: false, id: Identifier { @@ -24,7 +34,7 @@ exports[`AST Fixtures declaration TSEnumDeclaration empty AST Alignment - AST 1` end: { column: 8, line: 1 }, }, }, - members: Array [], ++ members: Array [], range: [0, 11], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/1-TSESTree-AST.shot index 29f98e4e129e..4d9bc63c978a 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/1-TSESTree-AST.shot @@ -6,6 +6,39 @@ Program { body: [ TSEnumDeclaration { type: "TSEnumDeclaration", + body: TSEnumBody { + type: "TSEnumBody", + members: [ + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "A", + optional: false, + + range: [13, 14], + loc: { + start: { column: 2, line: 2 }, + end: { column: 3, line: 2 }, + }, + }, + + range: [13, 14], + loc: { + start: { column: 2, line: 2 }, + end: { column: 3, line: 2 }, + }, + }, + ], + + range: [9, 17], + loc: { + start: { column: 9, line: 1 }, + end: { column: 1, line: 3 }, + }, + }, const: false, declare: false, id: Identifier { @@ -20,30 +53,6 @@ Program { end: { column: 8, line: 1 }, }, }, - members: [ - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "A", - optional: false, - - range: [13, 14], - loc: { - start: { column: 2, line: 2 }, - end: { column: 3, line: 2 }, - }, - }, - - range: [13, 14], - loc: { - start: { column: 2, line: 2 }, - end: { column: 3, line: 2 }, - }, - }, - ], range: [0, 17], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/5-AST-Alignment-AST.shot index 7137394a11bb..85d1319d1313 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/fixtures/with-member-one/snapshots/5-AST-Alignment-AST.shot @@ -10,29 +10,39 @@ exports[`AST Fixtures declaration TSEnumDeclaration with-member-one AST Alignmen body: Array [ TSEnumDeclaration { type: 'TSEnumDeclaration', -- const: false, -- declare: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Foo', -- optional: false, +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [ +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'A', +- optional: false, ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Foo', - range: [5, 8], - loc: { - start: { column: 5, line: 1 }, - end: { column: 8, line: 1 }, - }, - }, - members: Array [ - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'A', -- optional: false, +- range: [13, 14], +- loc: { +- start: { column: 2, line: 2 }, +- end: { column: 3, line: 2 }, +- }, +- }, ++ range: [5, 8], ++ loc: { ++ start: { column: 5, line: 1 }, ++ end: { column: 8, line: 1 }, ++ }, ++ }, ++ members: Array [ ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'A', range: [13, 14], loc: { @@ -40,14 +50,34 @@ exports[`AST Fixtures declaration TSEnumDeclaration with-member-one AST Alignmen end: { column: 3, line: 2 }, }, }, +- ], - range: [13, 14], - loc: { - start: { column: 2, line: 2 }, - end: { column: 3, line: 2 }, - }, +- range: [9, 17], +- loc: { +- start: { column: 9, line: 1 }, +- end: { column: 1, line: 3 }, ++ range: [13, 14], ++ loc: { ++ start: { column: 2, line: 2 }, ++ end: { column: 3, line: 2 }, ++ }, }, - ], +- }, +- const: false, +- declare: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Foo', +- optional: false, +- +- range: [5, 8], +- loc: { +- start: { column: 5, line: 1 }, +- end: { column: 8, line: 1 }, +- }, +- }, ++ ], range: [0, 17], loc: { diff --git a/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts b/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts index 1625063426c8..9d51d654c47b 100644 --- a/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts +++ b/packages/ast-spec/src/declaration/TSEnumDeclaration/spec.ts @@ -2,9 +2,14 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; import type { BaseNode } from '../../base/BaseNode'; import type { TSEnumMember } from '../../element/TSEnumMember/spec'; import type { Identifier } from '../../expression/Identifier/spec'; +import type { TSEnumBody } from '../../special/TSEnumBody/spec'; export interface TSEnumDeclaration extends BaseNode { type: AST_NODE_TYPES.TSEnumDeclaration; + /** + * The body of the enum. + */ + body: TSEnumBody; /** * Whether this is a `const` enum. * ``` @@ -25,6 +30,7 @@ export interface TSEnumDeclaration extends BaseNode { id: Identifier; /** * The enum members. + * @deprecated Use {@link body} instead. */ members: TSEnumMember[]; } diff --git a/packages/ast-spec/src/element/TSEnumMember/spec.ts b/packages/ast-spec/src/element/TSEnumMember/spec.ts index 9dd1ddeee696..3e506809dab9 100644 --- a/packages/ast-spec/src/element/TSEnumMember/spec.ts +++ b/packages/ast-spec/src/element/TSEnumMember/spec.ts @@ -1,5 +1,6 @@ import type { AST_NODE_TYPES } from '../../ast-node-types'; import type { BaseNode } from '../../base/BaseNode'; +import type { TSEnumBody } from '../../special/spec'; import type { Expression } from '../../unions/Expression'; import type { PropertyNameComputed, @@ -13,6 +14,7 @@ interface TSEnumMemberBase extends BaseNode { | PropertyNameNonComputed; initializer: Expression | undefined; computed: boolean; + parent: TSEnumBody; } /** diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/1-TSESTree-AST.shot index 6afdeed4fd39..e6eeb71a2399 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/1-TSESTree-AST.shot @@ -6,62 +6,57 @@ Program { body: [ TSEnumDeclaration { type: "TSEnumDeclaration", - const: true, - declare: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Foo", - optional: false, + body: TSEnumBody { + type: "TSEnumBody", + members: [ + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "foo", + optional: false, - range: [84, 87], - loc: { - start: { column: 11, line: 3 }, - end: { column: 14, line: 3 }, - }, - }, - members: [ - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "foo", - optional: false, + range: [92, 95], + loc: { + start: { column: 2, line: 4 }, + end: { column: 5, line: 4 }, + }, + }, + initializer: Literal { + type: "Literal", + raw: "1", + value: 1, - range: [92, 95], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, + range: [98, 99], + loc: { + start: { column: 8, line: 4 }, + end: { column: 9, line: 4 }, + }, }, - }, - initializer: Literal { - type: "Literal", - raw: "1", - value: 1, - range: [98, 99], + range: [92, 99], loc: { - start: { column: 8, line: 4 }, + start: { column: 2, line: 4 }, end: { column: 9, line: 4 }, }, }, + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "bar", + optional: false, - range: [92, 99], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "bar", - optional: false, + range: [103, 106], + loc: { + start: { column: 2, line: 5 }, + end: { column: 5, line: 5 }, + }, + }, range: [103, 106], loc: { @@ -69,14 +64,28 @@ Program { end: { column: 5, line: 5 }, }, }, + ], - range: [103, 106], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, + range: [88, 109], + loc: { + start: { column: 15, line: 3 }, + end: { column: 1, line: 6 }, }, - ], + }, + const: true, + declare: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Foo", + optional: false, + + range: [84, 87], + loc: { + start: { column: 11, line: 3 }, + end: { column: 14, line: 3 }, + }, + }, range: [73, 109], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/5-AST-Alignment-AST.shot index 3ca65220e227..edeb8e60fafa 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/const-enum/snapshots/5-AST-Alignment-AST.shot @@ -10,62 +10,95 @@ exports[`AST Fixtures legacy-fixtures basics const-enum AST Alignment - AST 1`] body: Array [ TSEnumDeclaration { type: 'TSEnumDeclaration', - const: true, -- declare: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Foo', -- optional: false, +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [ +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'foo', +- optional: false, ++ const: true, ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Foo', - range: [84, 87], - loc: { - start: { column: 11, line: 3 }, - end: { column: 14, line: 3 }, - }, - }, - members: Array [ - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'foo', -- optional: false, +- range: [92, 95], +- loc: { +- start: { column: 2, line: 4 }, +- end: { column: 5, line: 4 }, +- }, +- }, +- initializer: Literal { +- type: 'Literal', +- raw: '1', +- value: 1, ++ range: [84, 87], ++ loc: { ++ start: { column: 11, line: 3 }, ++ end: { column: 14, line: 3 }, ++ }, ++ }, ++ members: Array [ ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'foo', - range: [92, 95], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, +- range: [98, 99], +- loc: { +- start: { column: 8, line: 4 }, +- end: { column: 9, line: 4 }, +- }, ++ range: [92, 95], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 5, line: 4 }, }, - }, - initializer: Literal { - type: 'Literal', - raw: '1', - value: 1, ++ }, ++ initializer: Literal { ++ type: 'Literal', ++ raw: '1', ++ value: 1, - range: [98, 99], +- range: [92, 99], ++ range: [98, 99], loc: { - start: { column: 8, line: 4 }, +- start: { column: 2, line: 4 }, ++ start: { column: 8, line: 4 }, end: { column: 9, line: 4 }, }, }, +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'bar', +- optional: false, - range: [92, 99], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'bar', -- optional: false, +- range: [103, 106], +- loc: { +- start: { column: 2, line: 5 }, +- end: { column: 5, line: 5 }, +- }, +- }, ++ range: [92, 99], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 9, line: 4 }, ++ }, ++ }, ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'bar', range: [103, 106], loc: { @@ -73,14 +106,34 @@ exports[`AST Fixtures legacy-fixtures basics const-enum AST Alignment - AST 1`] end: { column: 5, line: 5 }, }, }, +- ], - range: [103, 106], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, +- range: [88, 109], +- loc: { +- start: { column: 15, line: 3 }, +- end: { column: 1, line: 6 }, +- }, +- }, +- const: true, +- declare: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Foo', +- optional: false, +- +- range: [84, 87], +- loc: { +- start: { column: 11, line: 3 }, +- end: { column: 14, line: 3 }, ++ range: [103, 106], ++ loc: { ++ start: { column: 2, line: 5 }, ++ end: { column: 5, line: 5 }, ++ }, }, - ], +- }, ++ ], range: [73, 109], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/1-TSESTree-AST.shot index edf76ad24aee..39a9d91b204a 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/1-TSESTree-AST.shot @@ -9,62 +9,57 @@ Program { attributes: [], declaration: TSEnumDeclaration { type: "TSEnumDeclaration", - const: true, - declare: true, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Foo", - optional: false, + body: TSEnumBody { + type: "TSEnumBody", + members: [ + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "foo", + optional: false, - range: [99, 102], - loc: { - start: { column: 26, line: 3 }, - end: { column: 29, line: 3 }, - }, - }, - members: [ - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "foo", - optional: false, + range: [107, 110], + loc: { + start: { column: 2, line: 4 }, + end: { column: 5, line: 4 }, + }, + }, + initializer: Literal { + type: "Literal", + raw: "1", + value: 1, - range: [107, 110], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, + range: [113, 114], + loc: { + start: { column: 8, line: 4 }, + end: { column: 9, line: 4 }, + }, }, - }, - initializer: Literal { - type: "Literal", - raw: "1", - value: 1, - range: [113, 114], + range: [107, 114], loc: { - start: { column: 8, line: 4 }, + start: { column: 2, line: 4 }, end: { column: 9, line: 4 }, }, }, + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "bar", + optional: false, - range: [107, 114], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "bar", - optional: false, + range: [118, 121], + loc: { + start: { column: 2, line: 5 }, + end: { column: 5, line: 5 }, + }, + }, range: [118, 121], loc: { @@ -72,14 +67,28 @@ Program { end: { column: 5, line: 5 }, }, }, + ], - range: [118, 121], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, + range: [103, 124], + loc: { + start: { column: 30, line: 3 }, + end: { column: 1, line: 6 }, }, - ], + }, + const: true, + declare: true, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Foo", + optional: false, + + range: [99, 102], + loc: { + start: { column: 26, line: 3 }, + end: { column: 29, line: 3 }, + }, + }, range: [80, 124], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/5-AST-Alignment-AST.shot index 1795c3300259..7d3830711a9c 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-const-named-enum/snapshots/5-AST-Alignment-AST.shot @@ -14,62 +14,96 @@ exports[`AST Fixtures legacy-fixtures basics export-declare-const-named-enum AST + assertions: Array [], declaration: TSEnumDeclaration { type: 'TSEnumDeclaration', - const: true, - declare: true, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Foo', -- optional: false, +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [ +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'foo', +- optional: false, ++ const: true, ++ declare: true, ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Foo', - range: [99, 102], - loc: { - start: { column: 26, line: 3 }, - end: { column: 29, line: 3 }, - }, - }, - members: Array [ - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'foo', -- optional: false, +- range: [107, 110], +- loc: { +- start: { column: 2, line: 4 }, +- end: { column: 5, line: 4 }, +- }, +- }, +- initializer: Literal { +- type: 'Literal', +- raw: '1', +- value: 1, ++ range: [99, 102], ++ loc: { ++ start: { column: 26, line: 3 }, ++ end: { column: 29, line: 3 }, ++ }, ++ }, ++ members: Array [ ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'foo', - range: [107, 110], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, +- range: [113, 114], +- loc: { +- start: { column: 8, line: 4 }, +- end: { column: 9, line: 4 }, +- }, ++ range: [107, 110], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 5, line: 4 }, }, - }, - initializer: Literal { - type: 'Literal', - raw: '1', - value: 1, ++ }, ++ initializer: Literal { ++ type: 'Literal', ++ raw: '1', ++ value: 1, - range: [113, 114], +- range: [107, 114], ++ range: [113, 114], loc: { - start: { column: 8, line: 4 }, +- start: { column: 2, line: 4 }, ++ start: { column: 8, line: 4 }, end: { column: 9, line: 4 }, }, }, +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'bar', +- optional: false, - range: [107, 114], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'bar', -- optional: false, +- range: [118, 121], +- loc: { +- start: { column: 2, line: 5 }, +- end: { column: 5, line: 5 }, +- }, +- }, ++ range: [107, 114], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 9, line: 4 }, ++ }, ++ }, ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'bar', range: [118, 121], loc: { @@ -77,14 +111,34 @@ exports[`AST Fixtures legacy-fixtures basics export-declare-const-named-enum AST end: { column: 5, line: 5 }, }, }, +- ], - range: [118, 121], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, +- range: [103, 124], +- loc: { +- start: { column: 30, line: 3 }, +- end: { column: 1, line: 6 }, +- }, +- }, +- const: true, +- declare: true, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Foo', +- optional: false, +- +- range: [99, 102], +- loc: { +- start: { column: 26, line: 3 }, +- end: { column: 29, line: 3 }, ++ range: [118, 121], ++ loc: { ++ start: { column: 2, line: 5 }, ++ end: { column: 5, line: 5 }, ++ }, }, - ], +- }, ++ ], range: [80, 124], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/1-TSESTree-AST.shot index 14b710bbca7d..80bdeff4c86d 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/1-TSESTree-AST.shot @@ -9,62 +9,57 @@ Program { attributes: [], declaration: TSEnumDeclaration { type: "TSEnumDeclaration", - const: false, - declare: true, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Foo", - optional: false, + body: TSEnumBody { + type: "TSEnumBody", + members: [ + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "foo", + optional: false, - range: [93, 96], - loc: { - start: { column: 20, line: 3 }, - end: { column: 23, line: 3 }, - }, - }, - members: [ - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "foo", - optional: false, + range: [101, 104], + loc: { + start: { column: 2, line: 4 }, + end: { column: 5, line: 4 }, + }, + }, + initializer: Literal { + type: "Literal", + raw: "1", + value: 1, - range: [101, 104], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, + range: [107, 108], + loc: { + start: { column: 8, line: 4 }, + end: { column: 9, line: 4 }, + }, }, - }, - initializer: Literal { - type: "Literal", - raw: "1", - value: 1, - range: [107, 108], + range: [101, 108], loc: { - start: { column: 8, line: 4 }, + start: { column: 2, line: 4 }, end: { column: 9, line: 4 }, }, }, + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "bar", + optional: false, - range: [101, 108], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "bar", - optional: false, + range: [112, 115], + loc: { + start: { column: 2, line: 5 }, + end: { column: 5, line: 5 }, + }, + }, range: [112, 115], loc: { @@ -72,14 +67,28 @@ Program { end: { column: 5, line: 5 }, }, }, + ], - range: [112, 115], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, + range: [97, 118], + loc: { + start: { column: 24, line: 3 }, + end: { column: 1, line: 6 }, }, - ], + }, + const: false, + declare: true, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Foo", + optional: false, + + range: [93, 96], + loc: { + start: { column: 20, line: 3 }, + end: { column: 23, line: 3 }, + }, + }, range: [80, 118], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/5-AST-Alignment-AST.shot index 90b208e7ad14..913dcb738db1 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-declare-named-enum/snapshots/5-AST-Alignment-AST.shot @@ -14,62 +14,95 @@ exports[`AST Fixtures legacy-fixtures basics export-declare-named-enum AST Align + assertions: Array [], declaration: TSEnumDeclaration { type: 'TSEnumDeclaration', -- const: false, - declare: true, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Foo', -- optional: false, +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [ +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'foo', +- optional: false, ++ declare: true, ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Foo', - range: [93, 96], - loc: { - start: { column: 20, line: 3 }, - end: { column: 23, line: 3 }, - }, - }, - members: Array [ - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'foo', -- optional: false, +- range: [101, 104], +- loc: { +- start: { column: 2, line: 4 }, +- end: { column: 5, line: 4 }, +- }, +- }, +- initializer: Literal { +- type: 'Literal', +- raw: '1', +- value: 1, ++ range: [93, 96], ++ loc: { ++ start: { column: 20, line: 3 }, ++ end: { column: 23, line: 3 }, ++ }, ++ }, ++ members: Array [ ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'foo', - range: [101, 104], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, +- range: [107, 108], +- loc: { +- start: { column: 8, line: 4 }, +- end: { column: 9, line: 4 }, +- }, ++ range: [101, 104], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 5, line: 4 }, }, - }, - initializer: Literal { - type: 'Literal', - raw: '1', - value: 1, ++ }, ++ initializer: Literal { ++ type: 'Literal', ++ raw: '1', ++ value: 1, - range: [107, 108], +- range: [101, 108], ++ range: [107, 108], loc: { - start: { column: 8, line: 4 }, +- start: { column: 2, line: 4 }, ++ start: { column: 8, line: 4 }, end: { column: 9, line: 4 }, }, }, +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'bar', +- optional: false, - range: [101, 108], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'bar', -- optional: false, +- range: [112, 115], +- loc: { +- start: { column: 2, line: 5 }, +- end: { column: 5, line: 5 }, +- }, +- }, ++ range: [101, 108], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 9, line: 4 }, ++ }, ++ }, ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'bar', range: [112, 115], loc: { @@ -77,14 +110,34 @@ exports[`AST Fixtures legacy-fixtures basics export-declare-named-enum AST Align end: { column: 5, line: 5 }, }, }, +- ], +- +- range: [97, 118], +- loc: { +- start: { column: 24, line: 3 }, +- end: { column: 1, line: 6 }, +- }, +- }, +- const: false, +- declare: true, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Foo', +- optional: false, - range: [112, 115], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, +- range: [93, 96], +- loc: { +- start: { column: 20, line: 3 }, +- end: { column: 23, line: 3 }, ++ range: [112, 115], ++ loc: { ++ start: { column: 2, line: 5 }, ++ end: { column: 5, line: 5 }, ++ }, }, - ], +- }, ++ ], range: [80, 118], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/1-TSESTree-AST.shot index 7126562753f5..d0fa7b0eb7ba 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/1-TSESTree-AST.shot @@ -9,62 +9,57 @@ Program { attributes: [], declaration: TSEnumDeclaration { type: "TSEnumDeclaration", - const: false, - declare: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Foo", - optional: false, + body: TSEnumBody { + type: "TSEnumBody", + members: [ + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "foo", + optional: false, - range: [85, 88], - loc: { - start: { column: 12, line: 3 }, - end: { column: 15, line: 3 }, - }, - }, - members: [ - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "foo", - optional: false, + range: [93, 96], + loc: { + start: { column: 2, line: 4 }, + end: { column: 5, line: 4 }, + }, + }, + initializer: Literal { + type: "Literal", + raw: "1", + value: 1, - range: [93, 96], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, + range: [99, 100], + loc: { + start: { column: 8, line: 4 }, + end: { column: 9, line: 4 }, + }, }, - }, - initializer: Literal { - type: "Literal", - raw: "1", - value: 1, - range: [99, 100], + range: [93, 100], loc: { - start: { column: 8, line: 4 }, + start: { column: 2, line: 4 }, end: { column: 9, line: 4 }, }, }, + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "bar", + optional: false, - range: [93, 100], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "bar", - optional: false, + range: [104, 107], + loc: { + start: { column: 2, line: 5 }, + end: { column: 5, line: 5 }, + }, + }, range: [104, 107], loc: { @@ -72,14 +67,28 @@ Program { end: { column: 5, line: 5 }, }, }, + ], - range: [104, 107], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, + range: [89, 110], + loc: { + start: { column: 16, line: 3 }, + end: { column: 1, line: 6 }, }, - ], + }, + const: false, + declare: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Foo", + optional: false, + + range: [85, 88], + loc: { + start: { column: 12, line: 3 }, + end: { column: 15, line: 3 }, + }, + }, range: [80, 110], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/5-AST-Alignment-AST.shot index 020aebc7f3d0..3b1b5eeb9c1a 100644 --- a/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/basics/fixtures/export-named-enum/snapshots/5-AST-Alignment-AST.shot @@ -14,62 +14,94 @@ exports[`AST Fixtures legacy-fixtures basics export-named-enum AST Alignment - A + assertions: Array [], declaration: TSEnumDeclaration { type: 'TSEnumDeclaration', -- const: false, -- declare: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Foo', -- optional: false, +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [ +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'foo', +- optional: false, ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Foo', - range: [85, 88], - loc: { - start: { column: 12, line: 3 }, - end: { column: 15, line: 3 }, - }, - }, - members: Array [ - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'foo', -- optional: false, +- range: [93, 96], +- loc: { +- start: { column: 2, line: 4 }, +- end: { column: 5, line: 4 }, +- }, +- }, +- initializer: Literal { +- type: 'Literal', +- raw: '1', +- value: 1, ++ range: [85, 88], ++ loc: { ++ start: { column: 12, line: 3 }, ++ end: { column: 15, line: 3 }, ++ }, ++ }, ++ members: Array [ ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'foo', - range: [93, 96], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, +- range: [99, 100], +- loc: { +- start: { column: 8, line: 4 }, +- end: { column: 9, line: 4 }, +- }, ++ range: [93, 96], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 5, line: 4 }, }, - }, - initializer: Literal { - type: 'Literal', - raw: '1', - value: 1, ++ }, ++ initializer: Literal { ++ type: 'Literal', ++ raw: '1', ++ value: 1, - range: [99, 100], +- range: [93, 100], ++ range: [99, 100], loc: { - start: { column: 8, line: 4 }, +- start: { column: 2, line: 4 }, ++ start: { column: 8, line: 4 }, end: { column: 9, line: 4 }, }, }, +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'bar', +- optional: false, - range: [93, 100], - loc: { - start: { column: 2, line: 4 }, - end: { column: 9, line: 4 }, - }, - }, - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'bar', -- optional: false, +- range: [104, 107], +- loc: { +- start: { column: 2, line: 5 }, +- end: { column: 5, line: 5 }, +- }, +- }, ++ range: [93, 100], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 9, line: 4 }, ++ }, ++ }, ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'bar', range: [104, 107], loc: { @@ -77,14 +109,34 @@ exports[`AST Fixtures legacy-fixtures basics export-named-enum AST Alignment - A end: { column: 5, line: 5 }, }, }, +- ], - range: [104, 107], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, +- range: [89, 110], +- loc: { +- start: { column: 16, line: 3 }, +- end: { column: 1, line: 6 }, ++ range: [104, 107], ++ loc: { ++ start: { column: 2, line: 5 }, ++ end: { column: 5, line: 5 }, ++ }, }, - ], +- }, +- const: false, +- declare: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Foo', +- optional: false, +- +- range: [85, 88], +- loc: { +- start: { column: 12, line: 3 }, +- end: { column: 15, line: 3 }, +- }, +- }, ++ ], range: [80, 110], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/1-TSESTree-AST.shot b/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/1-TSESTree-AST.shot index 956fec5e84a7..e6515601902c 100644 --- a/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/1-TSESTree-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/1-TSESTree-AST.shot @@ -6,29 +6,24 @@ Program { body: [ TSEnumDeclaration { type: "TSEnumDeclaration", - const: false, - declare: true, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Foo", - optional: false, + body: TSEnumBody { + type: "TSEnumBody", + members: [ + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Bar", + optional: false, - range: [86, 89], - loc: { - start: { column: 13, line: 3 }, - end: { column: 16, line: 3 }, - }, - }, - members: [ - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Bar", - optional: false, + range: [94, 97], + loc: { + start: { column: 2, line: 4 }, + end: { column: 5, line: 4 }, + }, + }, range: [94, 97], loc: { @@ -36,21 +31,21 @@ Program { end: { column: 5, line: 4 }, }, }, + TSEnumMember { + type: "TSEnumMember", + computed: false, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Baz", + optional: false, - range: [94, 97], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, - }, - }, - TSEnumMember { - type: "TSEnumMember", - computed: false, - id: Identifier { - type: "Identifier", - decorators: [], - name: "Baz", - optional: false, + range: [101, 104], + loc: { + start: { column: 2, line: 5 }, + end: { column: 5, line: 5 }, + }, + }, range: [101, 104], loc: { @@ -58,14 +53,28 @@ Program { end: { column: 5, line: 5 }, }, }, + ], - range: [101, 104], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, + range: [90, 107], + loc: { + start: { column: 17, line: 3 }, + end: { column: 1, line: 6 }, }, - ], + }, + const: false, + declare: true, + id: Identifier { + type: "Identifier", + decorators: [], + name: "Foo", + optional: false, + + range: [86, 89], + loc: { + start: { column: 13, line: 3 }, + end: { column: 16, line: 3 }, + }, + }, range: [73, 107], loc: { diff --git a/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/5-AST-Alignment-AST.shot b/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/5-AST-Alignment-AST.shot index 1ef1a9d0f0b7..baf9d0365b3a 100644 --- a/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/5-AST-Alignment-AST.shot +++ b/packages/ast-spec/src/legacy-fixtures/declare/fixtures/enum/snapshots/5-AST-Alignment-AST.shot @@ -10,29 +10,40 @@ exports[`AST Fixtures legacy-fixtures declare enum AST Alignment - AST 1`] = ` body: Array [ TSEnumDeclaration { type: 'TSEnumDeclaration', -- const: false, - declare: true, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Foo', -- optional: false, +- body: TSEnumBody { +- type: 'TSEnumBody', +- members: Array [ +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Bar', +- optional: false, ++ declare: true, ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Foo', - range: [86, 89], - loc: { - start: { column: 13, line: 3 }, - end: { column: 16, line: 3 }, - }, - }, - members: Array [ - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Bar', -- optional: false, +- range: [94, 97], +- loc: { +- start: { column: 2, line: 4 }, +- end: { column: 5, line: 4 }, +- }, +- }, ++ range: [86, 89], ++ loc: { ++ start: { column: 13, line: 3 }, ++ end: { column: 16, line: 3 }, ++ }, ++ }, ++ members: Array [ ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Bar', range: [94, 97], loc: { @@ -40,21 +51,32 @@ exports[`AST Fixtures legacy-fixtures declare enum AST Alignment - AST 1`] = ` end: { column: 5, line: 4 }, }, }, +- TSEnumMember { +- type: 'TSEnumMember', +- computed: false, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Baz', +- optional: false, - range: [94, 97], - loc: { - start: { column: 2, line: 4 }, - end: { column: 5, line: 4 }, - }, - }, - TSEnumMember { - type: 'TSEnumMember', -- computed: false, - id: Identifier { - type: 'Identifier', -- decorators: Array [], - name: 'Baz', -- optional: false, +- range: [101, 104], +- loc: { +- start: { column: 2, line: 5 }, +- end: { column: 5, line: 5 }, +- }, +- }, ++ range: [94, 97], ++ loc: { ++ start: { column: 2, line: 4 }, ++ end: { column: 5, line: 4 }, ++ }, ++ }, ++ TSEnumMember { ++ type: 'TSEnumMember', ++ id: Identifier { ++ type: 'Identifier', ++ name: 'Baz', range: [101, 104], loc: { @@ -62,14 +84,34 @@ exports[`AST Fixtures legacy-fixtures declare enum AST Alignment - AST 1`] = ` end: { column: 5, line: 5 }, }, }, +- ], - range: [101, 104], - loc: { - start: { column: 2, line: 5 }, - end: { column: 5, line: 5 }, - }, +- range: [90, 107], +- loc: { +- start: { column: 17, line: 3 }, +- end: { column: 1, line: 6 }, +- }, +- }, +- const: false, +- declare: true, +- id: Identifier { +- type: 'Identifier', +- decorators: Array [], +- name: 'Foo', +- optional: false, +- +- range: [86, 89], +- loc: { +- start: { column: 13, line: 3 }, +- end: { column: 16, line: 3 }, ++ range: [101, 104], ++ loc: { ++ start: { column: 2, line: 5 }, ++ end: { column: 5, line: 5 }, ++ }, }, - ], +- }, ++ ], range: [73, 107], loc: { diff --git a/packages/ast-spec/src/special/TSEnumBody/spec.ts b/packages/ast-spec/src/special/TSEnumBody/spec.ts new file mode 100644 index 000000000000..f6eef26a1afe --- /dev/null +++ b/packages/ast-spec/src/special/TSEnumBody/spec.ts @@ -0,0 +1,10 @@ +import type { AST_NODE_TYPES } from '../../ast-node-types'; +import type { BaseNode } from '../../base/BaseNode'; +import type { TSEnumDeclaration } from '../../declaration/TSEnumDeclaration/spec'; +import type { TSEnumMember } from '../../element/TSEnumMember/spec'; + +export interface TSEnumBody extends BaseNode { + type: AST_NODE_TYPES.TSEnumBody; + members: TSEnumMember[]; + parent: TSEnumDeclaration; +} diff --git a/packages/ast-spec/src/special/spec.ts b/packages/ast-spec/src/special/spec.ts index c906deb52957..24ef5463f150 100644 --- a/packages/ast-spec/src/special/spec.ts +++ b/packages/ast-spec/src/special/spec.ts @@ -11,6 +11,7 @@ export * from './PrivateIdentifier/spec'; export * from './Program/spec'; export * from './SwitchCase/spec'; export * from './TSClassImplements/spec'; +export * from './TSEnumBody/spec'; export * from './TSExternalModuleReference/spec'; export * from './TSInterfaceBody/spec'; export * from './TSInterfaceHeritage/spec'; diff --git a/packages/ast-spec/src/unions/Node.ts b/packages/ast-spec/src/unions/Node.ts index b0952e0e1863..e224e9711b22 100644 --- a/packages/ast-spec/src/unions/Node.ts +++ b/packages/ast-spec/src/unions/Node.ts @@ -92,6 +92,7 @@ import type { Program } from '../special/Program/spec'; import type { SwitchCase } from '../special/SwitchCase/spec'; import type { TemplateElement } from '../special/TemplateElement/spec'; import type { TSClassImplements } from '../special/TSClassImplements/spec'; +import type { TSEnumBody } from '../special/TSEnumBody/spec'; import type { TSExternalModuleReference } from '../special/TSExternalModuleReference/spec'; import type { TSInterfaceBody } from '../special/TSInterfaceBody/spec'; import type { TSInterfaceHeritage } from '../special/TSInterfaceHeritage/spec'; @@ -272,6 +273,7 @@ export type Node = | TSDeclareFunction | TSDeclareKeyword | TSEmptyBodyFunctionExpression + | TSEnumBody | TSEnumDeclaration | TSEnumMember | TSExportAssignment diff --git a/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts b/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts index 9e97e44d2536..5f70bc7cc414 100755 --- a/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts +++ b/packages/eslint-plugin-internal/src/rules/prefer-ast-types-enum.ts @@ -41,9 +41,8 @@ export default createRule({ Literal(node: TSESTree.Literal): void { if ( node.parent.type === AST_NODE_TYPES.TSEnumMember && - node.parent.parent.type === AST_NODE_TYPES.TSEnumDeclaration && ['AST_NODE_TYPES', 'AST_TOKEN_TYPES', 'DefinitionType'].includes( - node.parent.parent.id.name, + node.parent.parent.parent.id.name, ) ) { return; diff --git a/packages/eslint-plugin/src/rules/brace-style.ts b/packages/eslint-plugin/src/rules/brace-style.ts index 1021c9c9a48c..e38b9240fe34 100644 --- a/packages/eslint-plugin/src/rules/brace-style.ts +++ b/packages/eslint-plugin/src/rules/brace-style.ts @@ -132,7 +132,7 @@ export default createRule({ TSEnumDeclaration(node): void { const closingCurly = context.sourceCode.getLastToken(node)!; const openingCurly = context.sourceCode.getTokenBefore( - node.members.length ? node.members[0] : closingCurly, + node.body.members.length ? node.body.members[0] : closingCurly, )!; validateCurlyPair(openingCurly, closingCurly); diff --git a/packages/eslint-plugin/src/rules/comma-dangle.ts b/packages/eslint-plugin/src/rules/comma-dangle.ts index 60b48ecd0b2c..aad665ad58b1 100644 --- a/packages/eslint-plugin/src/rules/comma-dangle.ts +++ b/packages/eslint-plugin/src/rules/comma-dangle.ts @@ -116,7 +116,7 @@ export default createRule({ function getLastItem(node: TSESTree.Node): TSESTree.Node | null { switch (node.type) { case AST_NODE_TYPES.TSEnumDeclaration: - return last(node.members); + return last(node.body.members); case AST_NODE_TYPES.TSTypeParameterDeclaration: return last(node.params); case AST_NODE_TYPES.TSTupleType: diff --git a/packages/eslint-plugin/src/rules/indent.ts b/packages/eslint-plugin/src/rules/indent.ts index 9ebb23d75844..544e3dbdc826 100644 --- a/packages/eslint-plugin/src/rules/indent.ts +++ b/packages/eslint-plugin/src/rules/indent.ts @@ -249,8 +249,9 @@ export default createRule({ // transform it to an ObjectExpression return rules['ObjectExpression, ObjectPattern']({ type: AST_NODE_TYPES.ObjectExpression, - properties: ( - node.members as (TSESTree.TSEnumMember | TSESTree.TypeElement)[] + properties: (node.type === AST_NODE_TYPES.TSEnumDeclaration + ? node.body.members + : node.members ).map( member => TSPropertySignatureToProperty(member) as TSESTree.Property, diff --git a/packages/eslint-plugin/src/rules/no-duplicate-enum-values.ts b/packages/eslint-plugin/src/rules/no-duplicate-enum-values.ts index f5a58ce14f12..d8ac6586666d 100644 --- a/packages/eslint-plugin/src/rules/no-duplicate-enum-values.ts +++ b/packages/eslint-plugin/src/rules/no-duplicate-enum-values.ts @@ -37,7 +37,7 @@ export default createRule({ return { TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void { - const enumMembers = node.members; + const enumMembers = node.body.members; const seenValues = new Set(); enumMembers.forEach(member => { diff --git a/packages/eslint-plugin/src/rules/no-mixed-enums.ts b/packages/eslint-plugin/src/rules/no-mixed-enums.ts index e8a06d9861e0..d95d4a9058b7 100644 --- a/packages/eslint-plugin/src/rules/no-mixed-enums.ts +++ b/packages/eslint-plugin/src/rules/no-mixed-enums.ts @@ -51,7 +51,7 @@ export default createRule({ if ( definition.node.type === AST_NODE_TYPES.TSEnumDeclaration && definition.node.range[0] < node.range[0] && - definition.node.members.length > 0 + definition.node.body.members.length > 0 ) { found.previousSibling = definition.node; break; @@ -146,7 +146,7 @@ export default createRule({ // enum MyEnum { A } // enum MyEnum { B } if (previousSibling) { - return getMemberType(previousSibling.members[0]); + return getMemberType(previousSibling.body.members[0]); } // Case: Namespace declaration merging @@ -185,12 +185,12 @@ export default createRule({ } // Finally, we default to the type of the first enum member - return getMemberType(node.members[0]); + return getMemberType(node.body.members[0]); } return { TSEnumDeclaration(node): void { - if (!node.members.length) { + if (!node.body.members.length) { return; } @@ -199,7 +199,7 @@ export default createRule({ return; } - for (const member of node.members) { + for (const member of node.body.members) { const currentType = getMemberType(member); if (currentType === AllowedType.Unknown) { return; diff --git a/packages/eslint-plugin/src/rules/prefer-enum-initializers.ts b/packages/eslint-plugin/src/rules/prefer-enum-initializers.ts index 27572b4f8f7f..00dae8cccb96 100644 --- a/packages/eslint-plugin/src/rules/prefer-enum-initializers.ts +++ b/packages/eslint-plugin/src/rules/prefer-enum-initializers.ts @@ -24,7 +24,7 @@ export default createRule<[], MessageIds>({ defaultOptions: [], create(context) { function TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void { - const { members } = node; + const { members } = node.body; members.forEach((member, index) => { if (member.initializer == null) { diff --git a/packages/eslint-plugin/tests/rules/indent/indent.test.ts b/packages/eslint-plugin/tests/rules/indent/indent.test.ts index 5974cb5198ee..3d316d1506b9 100644 --- a/packages/eslint-plugin/tests/rules/indent/indent.test.ts +++ b/packages/eslint-plugin/tests/rules/indent/indent.test.ts @@ -181,17 +181,6 @@ class Foo { c : number } ) -} - `, - ], - }, - { - node: 'TSEnumDeclaration, TSEnumMember', - code: [ - ` -enum Foo { - bar = 1, - baz = 1, } `, ], @@ -1471,96 +1460,6 @@ class Foo {} }, ], }, - { - code: ` -enum Foo { -bar, -baz = 1, -buzz = '', -} - `, - output: ` -enum Foo { - bar, - baz = 1, - buzz = '', -} - `, - errors: [ - { - messageId: 'wrongIndentation', - data: { - expected: '4 spaces', - actual: 0, - }, - line: 3, - column: 1, - }, - { - messageId: 'wrongIndentation', - data: { - expected: '4 spaces', - actual: 0, - }, - line: 4, - column: 1, - }, - { - messageId: 'wrongIndentation', - data: { - expected: '4 spaces', - actual: 0, - }, - line: 5, - column: 1, - }, - ], - }, - { - code: ` -const enum Foo { -bar, -baz = 1, -buzz = '', -} - `, - output: ` -const enum Foo { - bar, - baz = 1, - buzz = '', -} - `, - errors: [ - { - messageId: 'wrongIndentation', - data: { - expected: '4 spaces', - actual: 0, - }, - line: 3, - column: 1, - }, - { - messageId: 'wrongIndentation', - data: { - expected: '4 spaces', - actual: 0, - }, - line: 4, - column: 1, - }, - { - messageId: 'wrongIndentation', - data: { - expected: '4 spaces', - actual: 0, - }, - line: 5, - column: 1, - }, - ], - }, { // eslint-disable-next-line @typescript-eslint/internal/plugin-test-formatting code: ` diff --git a/packages/eslint-plugin/tests/rules/lines-around-comment.test.ts b/packages/eslint-plugin/tests/rules/lines-around-comment.test.ts index 368a5cd05676..489ec4aeefa8 100644 --- a/packages/eslint-plugin/tests/rules/lines-around-comment.test.ts +++ b/packages/eslint-plugin/tests/rules/lines-around-comment.test.ts @@ -131,67 +131,6 @@ type A = { ], }, - // Enum - { - code: unIndent` -enum A { - // line - a, -} -`, - options: [ - { - beforeLineComment: true, - allowEnumStart: true, - }, - ], - }, - { - code: unIndent` -enum A { - /* block - comment */ - a, -} -`, - options: [ - { - beforeBlockComment: true, - allowEnumStart: true, - }, - ], - }, - { - code: unIndent` -enum A { - a, - // line -} -`, - options: [ - { - afterLineComment: true, - allowEnumEnd: true, - }, - ], - }, - { - code: unIndent` -enum A { - a, - /* block - comment */ -} -`, - options: [ - { - beforeBlockComment: false, - afterBlockComment: true, - allowEnumEnd: true, - }, - ], - }, - // TS module { code: unIndent` @@ -676,147 +615,6 @@ type A = { errors: [{ messageId: 'after', type: AST_TOKEN_TYPES.Block, line: 3 }], }, - // Enum - { - code: unIndent` -enum A { - a, - // line -} -`, - output: unIndent` -enum A { - a, - - // line -} -`, - options: [ - { - beforeLineComment: true, - allowEnumStart: true, - }, - ], - errors: [{ messageId: 'before', type: AST_TOKEN_TYPES.Line, line: 3 }], - }, - { - code: unIndent` -enum A { - a, - /* block - comment */ -} -`, - output: unIndent` -enum A { - a, - - /* block - comment */ -} -`, - options: [ - { - beforeBlockComment: true, - allowEnumStart: true, - }, - ], - errors: [{ messageId: 'before', type: AST_TOKEN_TYPES.Block, line: 3 }], - }, - { - code: unIndent` -enum A { - // line - a, -} -`, - output: unIndent` -enum A { - - // line - a, -} -`, - options: [ - { - beforeLineComment: true, - allowEnumStart: false, - }, - ], - errors: [{ messageId: 'before', type: AST_TOKEN_TYPES.Line, line: 2 }], - }, - { - code: unIndent` -enum A { - /* block - comment */ - a, -} -`, - output: unIndent` -enum A { - - /* block - comment */ - a, -} -`, - options: [ - { - beforeBlockComment: true, - allowEnumStart: false, - }, - ], - errors: [{ messageId: 'before', type: AST_TOKEN_TYPES.Block, line: 2 }], - }, - { - code: unIndent` -enum A { - a, - // line -} -`, - output: unIndent` -enum A { - a, - // line - -} -`, - options: [ - { - afterLineComment: true, - allowEnumEnd: false, - }, - ], - errors: [{ messageId: 'after', type: AST_TOKEN_TYPES.Line, line: 3 }], - }, - { - code: unIndent` -enum A { - a, - /* block - comment */ -} -`, - output: unIndent` -enum A { - a, - /* block - comment */ - -} -`, - options: [ - { - beforeBlockComment: false, - afterBlockComment: true, - allowEnumEnd: false, - }, - ], - errors: [{ messageId: 'after', type: AST_TOKEN_TYPES.Block, line: 3 }], - }, - // TS module { code: unIndent` diff --git a/packages/scope-manager/src/referencer/Referencer.ts b/packages/scope-manager/src/referencer/Referencer.ts index dac19c71a2c7..f10263642fa1 100644 --- a/packages/scope-manager/src/referencer/Referencer.ts +++ b/packages/scope-manager/src/referencer/Referencer.ts @@ -676,7 +676,7 @@ class Referencer extends Visitor { new TSEnumNameDefinition(node.id, node), ); - for (const member of node.members) { + for (const member of node.body.members) { // TS resolves literal named members to be actual names // enum Foo { // 'a' = 1, diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 9d068cbf2432..c2cf7c2c8bb2 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -2896,13 +2896,26 @@ export class Converter { } case SyntaxKind.EnumDeclaration: { - const result = this.createNode(node, { - type: AST_NODE_TYPES.TSEnumDeclaration, - const: hasModifier(SyntaxKind.ConstKeyword, node), - declare: hasModifier(SyntaxKind.DeclareKeyword, node), - id: this.convertChild(node.name), - members: node.members.map(el => this.convertChild(el)), - }); + const members = node.members.map(el => this.convertChild(el)); + const result = this.createNode( + node, + this.#withDeprecatedGetter( + { + type: AST_NODE_TYPES.TSEnumDeclaration, + body: this.createNode(node, { + type: AST_NODE_TYPES.TSEnumBody, + members, + range: [node.members.pos - 1, node.end], + }), + const: hasModifier(SyntaxKind.ConstKeyword, node), + declare: hasModifier(SyntaxKind.DeclareKeyword, node), + id: this.convertChild(node.name), + }, + 'members', + 'body.members', + node.members.map(el => this.convertChild(el)), + ), + ); return this.fixExports(node, result); } @@ -3518,6 +3531,45 @@ export class Converter { return node as Properties & Record; } + #withDeprecatedGetter< + Properties extends { type: string }, + Key extends string, + Value, + >( + node: Properties, + deprecatedKey: Key, + preferredKey: string, + value: Value, + ): Properties & Record { + let warned = false; + + Object.defineProperty(node, deprecatedKey, { + configurable: true, + get: this.options.suppressDeprecatedPropertyWarnings + ? (): Value => value + : (): Value => { + if (!warned) { + process.emitWarning( + `The '${deprecatedKey}' property is deprecated on ${node.type} nodes. Use '${preferredKey}' instead. See https://typescript-eslint.io/linting/troubleshooting#the-key-property-is-deprecated-on-type-nodes-use-key-instead-warnings.`, + 'DeprecationWarning', + ); + warned = true; + } + + return value; + }, + set(value): void { + Object.defineProperty(node, deprecatedKey, { + enumerable: true, + writable: true, + value, + }); + }, + }); + + return node as Properties & Record; + } + #throwError(node: ts.Node | number, message: string): asserts node is never { let start; let end; 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 f78ba52fb17d..1797ffd62302 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 @@ -177,6 +177,7 @@ export interface EstreeToTsNodeTypes { [AST_NODE_TYPES.TSConstructorType]: ts.ConstructorTypeNode; [AST_NODE_TYPES.TSConstructSignatureDeclaration]: ts.ConstructSignatureDeclaration; [AST_NODE_TYPES.TSDeclareFunction]: ts.FunctionDeclaration; + [AST_NODE_TYPES.TSEnumBody]: ts.EnumDeclaration; [AST_NODE_TYPES.TSEnumDeclaration]: ts.EnumDeclaration; [AST_NODE_TYPES.TSEnumMember]: ts.EnumMember; [AST_NODE_TYPES.TSExportAssignment]: ts.ExportAssignment; diff --git a/packages/visitor-keys/src/visitor-keys.ts b/packages/visitor-keys/src/visitor-keys.ts index 04aa32e5af20..c849790dbb52 100644 --- a/packages/visitor-keys/src/visitor-keys.ts +++ b/packages/visitor-keys/src/visitor-keys.ts @@ -206,7 +206,8 @@ const additionalKeys: AdditionalKeys = { TSDeclareFunction: SharedVisitorKeys.Function, TSDeclareKeyword: [], TSEmptyBodyFunctionExpression: ['id', ...SharedVisitorKeys.FunctionType], - TSEnumDeclaration: ['id', 'members'], + TSEnumBody: ['members'], + TSEnumDeclaration: ['id', 'body'], TSEnumMember: ['id', 'initializer'], TSExportAssignment: ['expression'], TSExportKeyword: [], From da382b8cfc1904cd72f0463087b34d3926cc1637 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Tue, 16 Apr 2024 23:01:22 +0530 Subject: [PATCH 20/27] chore: snapshots --- .../docs/rules/no-require-imports.mdx | 1 - .../no-require-imports.shot | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.mdx b/packages/eslint-plugin/docs/rules/no-require-imports.mdx index d565f12ed4eb..063708ca26c0 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.mdx +++ b/packages/eslint-plugin/docs/rules/no-require-imports.mdx @@ -80,7 +80,6 @@ let foo = require('foo'); ```ts option='{ "allowAsImport": true }' import foo = require('foo'); -require('foo'); import foo from 'foo'; ``` diff --git a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-require-imports.shot b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-require-imports.shot index 027cb637f45c..54cb8f1ddd2e 100644 --- a/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-require-imports.shot +++ b/packages/eslint-plugin/tests/docs-eslint-output-snapshots/no-require-imports.shot @@ -37,3 +37,25 @@ Options: { "allow": ["/package.json$"] } console.log(require('../package.json').version); " `; + +exports[`Validating rule docs no-require-imports.mdx code examples ESLint output 5`] = ` +"Incorrect +Options: { "allowAsImport": true } + +var foo = require('foo'); + ~~~~~~~~~~~~~~ A \`require()\` style import is forbidden. +const foo = require('foo'); + ~~~~~~~~~~~~~~ A \`require()\` style import is forbidden. +let foo = require('foo'); + ~~~~~~~~~~~~~~ A \`require()\` style import is forbidden. +" +`; + +exports[`Validating rule docs no-require-imports.mdx code examples ESLint output 6`] = ` +"Correct +Options: { "allowAsImport": true } + +import foo = require('foo'); +import foo from 'foo'; +" +`; From 9cafe3157ada7e2be51feba3a5c479bfeda5a02d Mon Sep 17 00:00:00 2001 From: arka1002 Date: Fri, 19 Apr 2024 13:54:49 +0530 Subject: [PATCH 21/27] chore: eslint comments --- packages/ast-spec/tests/fixtures.test.ts | 2 ++ packages/parser/tests/lib/services.test.ts | 1 + packages/rule-tester/src/RuleTester.ts | 2 ++ packages/rule-tester/src/utils/dependencyConstraints.ts | 1 + packages/scope-manager/tests/fixtures.test.ts | 1 + .../src/create-program/createProjectService.ts | 1 + .../typescript-estree/tests/lib/semanticInfo-singleRun.test.ts | 1 + packages/typescript-estree/tests/lib/semanticInfo.test.ts | 1 + 8 files changed, 10 insertions(+) diff --git a/packages/ast-spec/tests/fixtures.test.ts b/packages/ast-spec/tests/fixtures.test.ts index d107cc320255..f61b91fccba3 100644 --- a/packages/ast-spec/tests/fixtures.test.ts +++ b/packages/ast-spec/tests/fixtures.test.ts @@ -1,4 +1,5 @@ import fs from 'fs'; +// eslint-disable-next-line @typescript-eslint/no-require-imports import glob = require('glob'); import makeDir from 'make-dir'; import path from 'path'; @@ -66,6 +67,7 @@ const FIXTURES: readonly Fixture[] = [...VALID_FIXTURES, ...ERROR_FIXTURES].map( absolute, config: ((): ASTFixtureConfig => { try { + // eslint-disable-next-line @typescript-eslint/no-require-imports return require(configPath).default; } catch { return {}; diff --git a/packages/parser/tests/lib/services.test.ts b/packages/parser/tests/lib/services.test.ts index 8532ec979ada..cc893b87d58f 100644 --- a/packages/parser/tests/lib/services.test.ts +++ b/packages/parser/tests/lib/services.test.ts @@ -1,5 +1,6 @@ import { createProgram } from '@typescript-eslint/typescript-estree'; import fs from 'fs'; +// eslint-disable-next-line @typescript-eslint/no-require-imports import glob = require('glob'); import path from 'path'; diff --git a/packages/rule-tester/src/RuleTester.ts b/packages/rule-tester/src/RuleTester.ts index f0ac7ca3dcac..5caf3a8c8770 100644 --- a/packages/rule-tester/src/RuleTester.ts +++ b/packages/rule-tester/src/RuleTester.ts @@ -103,6 +103,7 @@ export class RuleTester extends TestFramework { try { // instead of creating a hard dependency, just use a soft require // a bit weird, but if they're using this tooling, it'll be installed + // eslint-disable-next-line @typescript-eslint/no-require-imports const parser = require(TYPESCRIPT_ESLINT_PARSER) as typeof ParserType; parser.clearCaches(); } catch { @@ -524,6 +525,7 @@ export class RuleTester extends TestFramework { this.#linter.defineParser( config.parser, + // eslint-disable-next-line @typescript-eslint/no-require-imports wrapParser(require(config.parser) as Parser.ParserModule), ); diff --git a/packages/rule-tester/src/utils/dependencyConstraints.ts b/packages/rule-tester/src/utils/dependencyConstraints.ts index e651356587a1..47af38167115 100644 --- a/packages/rule-tester/src/utils/dependencyConstraints.ts +++ b/packages/rule-tester/src/utils/dependencyConstraints.ts @@ -21,6 +21,7 @@ function satisfiesDependencyConstraint( : constraintIn; return semver.satisfies( + // eslint-disable-next-line @typescript-eslint/no-require-imports (require(`${packageName}/package.json`) as { version: string }).version, constraint.range, typeof constraint.options === 'object' diff --git a/packages/scope-manager/tests/fixtures.test.ts b/packages/scope-manager/tests/fixtures.test.ts index 753b241b3383..714db87b25a0 100644 --- a/packages/scope-manager/tests/fixtures.test.ts +++ b/packages/scope-manager/tests/fixtures.test.ts @@ -1,4 +1,5 @@ import fs from 'fs'; +// eslint-disable-next-line @typescript-eslint/no-require-imports import glob = require('glob'); import makeDir from 'make-dir'; import path from 'path'; diff --git a/packages/typescript-estree/src/create-program/createProjectService.ts b/packages/typescript-estree/src/create-program/createProjectService.ts index 1cf7f9d82992..aaa24e79d937 100644 --- a/packages/typescript-estree/src/create-program/createProjectService.ts +++ b/packages/typescript-estree/src/create-program/createProjectService.ts @@ -22,6 +22,7 @@ export function createProjectService( ): ProjectServiceSettings { // We import this lazily to avoid its cost for users who don't use the service // TODO: Once we drop support for TS<5.3 we can import from "typescript" directly + // eslint-disable-next-line @typescript-eslint/no-require-imports const tsserver = require('typescript/lib/tsserverlibrary') as typeof ts; // TODO: see getWatchProgramsForProjects diff --git a/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts b/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts index 1051b8d2f171..a84e32cdfdc5 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-require-imports import glob = require('glob'); import * as path from 'path'; diff --git a/packages/typescript-estree/tests/lib/semanticInfo.test.ts b/packages/typescript-estree/tests/lib/semanticInfo.test.ts index eac23a5deeab..b8cf857373dc 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo.test.ts @@ -1,4 +1,5 @@ import * as fs from 'fs'; +// eslint-disable-next-line @typescript-eslint/no-require-imports import glob = require('glob'); import * as path from 'path'; import * as ts from 'typescript'; From 346fed180985bba2d3fa75aece5af12709a29b37 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Thu, 25 Apr 2024 20:35:39 +0530 Subject: [PATCH 22/27] chore: address reviews --- packages/ast-spec/tests/fixtures.test.ts | 3 +- .../src/rules/no-require-imports.ts | 40 ++++--------------- packages/parser/tests/lib/services.test.ts | 3 +- .../tests/lib/semanticInfo-singleRun.test.ts | 3 +- .../tests/lib/semanticInfo.test.ts | 3 +- 5 files changed, 11 insertions(+), 41 deletions(-) diff --git a/packages/ast-spec/tests/fixtures.test.ts b/packages/ast-spec/tests/fixtures.test.ts index f61b91fccba3..3273b2f65731 100644 --- a/packages/ast-spec/tests/fixtures.test.ts +++ b/packages/ast-spec/tests/fixtures.test.ts @@ -1,6 +1,5 @@ import fs from 'fs'; -// eslint-disable-next-line @typescript-eslint/no-require-imports -import glob = require('glob'); +import * as glob from 'glob'; import makeDir from 'make-dir'; import path from 'path'; diff --git a/packages/eslint-plugin/src/rules/no-require-imports.ts b/packages/eslint-plugin/src/rules/no-require-imports.ts index c9c381c81527..64f626076c32 100644 --- a/packages/eslint-plugin/src/rules/no-require-imports.ts +++ b/packages/eslint-plugin/src/rules/no-require-imports.ts @@ -71,39 +71,13 @@ export default util.createRule({ context.sourceCode.getScope(node), 'require', ); - const parent = - node.parent.type === AST_NODE_TYPES.ChainExpression - ? node.parent.parent - : node.parent; - - if (allowAsImport) { - if ( - [ - AST_NODE_TYPES.CallExpression, - AST_NODE_TYPES.MemberExpression, - AST_NODE_TYPES.NewExpression, - AST_NODE_TYPES.TSAsExpression, - AST_NODE_TYPES.TSTypeAssertion, - AST_NODE_TYPES.VariableDeclarator, - AST_NODE_TYPES.ExpressionStatement, - ].includes(parent.type) - ) { - if (!variable?.identifiers.length) { - context.report({ - node, - messageId: 'noRequireImports', - }); - } - } - } else { - // ignore non-global require usage as it's something user-land custom instead - // of the commonjs standard - if (!variable?.identifiers.length) { - context.report({ - node, - messageId: 'noRequireImports', - }); - } + // ignore non-global require usage as it's something user-land custom instead + // of the commonjs standard + if (!variable?.identifiers.length) { + context.report({ + node, + messageId: 'noRequireImports', + }); } }, TSExternalModuleReference(node): void { diff --git a/packages/parser/tests/lib/services.test.ts b/packages/parser/tests/lib/services.test.ts index cc893b87d58f..e39747196aab 100644 --- a/packages/parser/tests/lib/services.test.ts +++ b/packages/parser/tests/lib/services.test.ts @@ -1,7 +1,6 @@ import { createProgram } from '@typescript-eslint/typescript-estree'; import fs from 'fs'; -// eslint-disable-next-line @typescript-eslint/no-require-imports -import glob = require('glob'); +import * as glob from 'glob'; import path from 'path'; import type { ParserOptions } from '../../src/parser'; diff --git a/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts b/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts index a84e32cdfdc5..9e913aa03f8b 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo-singleRun.test.ts @@ -1,5 +1,4 @@ -// eslint-disable-next-line @typescript-eslint/no-require-imports -import glob = require('glob'); +import * as glob from 'glob'; import * as path from 'path'; import { getCanonicalFileName } from '../../src/create-program/shared'; diff --git a/packages/typescript-estree/tests/lib/semanticInfo.test.ts b/packages/typescript-estree/tests/lib/semanticInfo.test.ts index b8cf857373dc..89aa45214d59 100644 --- a/packages/typescript-estree/tests/lib/semanticInfo.test.ts +++ b/packages/typescript-estree/tests/lib/semanticInfo.test.ts @@ -1,6 +1,5 @@ import * as fs from 'fs'; -// eslint-disable-next-line @typescript-eslint/no-require-imports -import glob = require('glob'); +import * as glob from 'glob'; import * as path from 'path'; import * as ts from 'typescript'; From 9a76d9e3a8119c06f3256e5a96281c0961432e9d Mon Sep 17 00:00:00 2001 From: Arka Pratim Chaudhuri <105232141+arka1002@users.noreply.github.com> Date: Fri, 26 Apr 2024 11:08:19 +0530 Subject: [PATCH 23/27] chore: update docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Josh Goldberg ✨ --- packages/eslint-plugin/docs/rules/no-require-imports.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.mdx b/packages/eslint-plugin/docs/rules/no-require-imports.mdx index 063708ca26c0..81159522cdca 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.mdx +++ b/packages/eslint-plugin/docs/rules/no-require-imports.mdx @@ -61,7 +61,7 @@ console.log(require('../package.json').version); ### `allowAsImport` -If it's set to `true`, the require statements with an `import` declaration won't be reported. +When set to `true`, require statements with an `import` declaration won't be reported. This is useful if you're on an older CommonJS-style codebase and haven't been able to transition to ESM-style imports yet. With `{allowAsImport: true}`: From a6a3307c79191064701d0b4d3d1ff9578e01aba3 Mon Sep 17 00:00:00 2001 From: Arka Pratim Chaudhuri <105232141+arka1002@users.noreply.github.com> Date: Sun, 26 May 2024 16:57:20 +0530 Subject: [PATCH 24/27] chore: docs Co-authored-by: Joshua Chen --- packages/eslint-plugin/docs/rules/no-require-imports.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-require-imports.mdx b/packages/eslint-plugin/docs/rules/no-require-imports.mdx index 81159522cdca..4512c2693d95 100644 --- a/packages/eslint-plugin/docs/rules/no-require-imports.mdx +++ b/packages/eslint-plugin/docs/rules/no-require-imports.mdx @@ -61,8 +61,8 @@ console.log(require('../package.json').version); ### `allowAsImport` -When set to `true`, require statements with an `import` declaration won't be reported. -This is useful if you're on an older CommonJS-style codebase and haven't been able to transition to ESM-style imports yet. +When set to `true`, the `import x = require(...)` declaration won't be reported. +This is useful if you use certain module options that require strict CommonJS interop semantics. With `{allowAsImport: true}`: From 594afbf6f6e1c1732a0c68beff2e33266ae1e534 Mon Sep 17 00:00:00 2001 From: arka1002 Date: Sun, 26 May 2024 17:14:09 +0530 Subject: [PATCH 25/27] chore: refactor --- packages/scope-manager/tests/fixtures.test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/scope-manager/tests/fixtures.test.ts b/packages/scope-manager/tests/fixtures.test.ts index 714db87b25a0..9d05626d6536 100644 --- a/packages/scope-manager/tests/fixtures.test.ts +++ b/packages/scope-manager/tests/fixtures.test.ts @@ -1,6 +1,5 @@ import fs from 'fs'; -// eslint-disable-next-line @typescript-eslint/no-require-imports -import glob = require('glob'); +import * as glob from 'glob'; import makeDir from 'make-dir'; import path from 'path'; From b757092922f26ca9522db06d4766fb29b55337b9 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 27 May 2024 19:30:41 -0700 Subject: [PATCH 26/27] Update pack-packages.ts --- packages/integration-tests/tools/pack-packages.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/integration-tests/tools/pack-packages.ts b/packages/integration-tests/tools/pack-packages.ts index 5755476cf3cf..de9fa275ff6a 100644 --- a/packages/integration-tests/tools/pack-packages.ts +++ b/packages/integration-tests/tools/pack-packages.ts @@ -35,6 +35,7 @@ for (const pkg of PACKAGES) { continue; } + // eslint-disable-next-line @typescript-eslint/no-require-imports -- this file needs to be sync and CJS for jest const packageJson = require(packagePath) as PackageJSON; if (packageJson.private === true) { continue; From 05956d874b07156809dd87a86fabc3077b5b953b Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 27 May 2024 19:40:07 -0700 Subject: [PATCH 27/27] Format pack-packages.ts ugh --- packages/integration-tests/tools/pack-packages.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/integration-tests/tools/pack-packages.ts b/packages/integration-tests/tools/pack-packages.ts index de9fa275ff6a..0f8ee41dce43 100644 --- a/packages/integration-tests/tools/pack-packages.ts +++ b/packages/integration-tests/tools/pack-packages.ts @@ -35,7 +35,7 @@ for (const pkg of PACKAGES) { continue; } - // eslint-disable-next-line @typescript-eslint/no-require-imports -- this file needs to be sync and CJS for jest + // eslint-disable-next-line @typescript-eslint/no-require-imports -- this file needs to be sync and CJS for jest const packageJson = require(packagePath) as PackageJSON; if (packageJson.private === true) { continue; 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