From c7aff58e96bc4f0fc65a7d3e0f5eafe8c9767ba5 Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:16:37 +0300 Subject: [PATCH 01/51] chore(eslint-plugin): [no-unused-vars] remove unused nested TSModuleDeclaration rule listener (#8279) --- .../eslint-plugin/src/rules/no-unused-vars.ts | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-unused-vars.ts b/packages/eslint-plugin/src/rules/no-unused-vars.ts index be8fcbe337e5..a0094bee811d 100644 --- a/packages/eslint-plugin/src/rules/no-unused-vars.ts +++ b/packages/eslint-plugin/src/rules/no-unused-vars.ts @@ -312,23 +312,6 @@ export default createRule({ markDeclarationChildAsUsed(node); }, - // module declaration in module declaration should not report unused vars error - // this is workaround as this change should be done in better way - 'TSModuleDeclaration > TSModuleDeclaration'( - node: TSESTree.TSModuleDeclaration, - ): void { - if (node.id.type === AST_NODE_TYPES.Identifier) { - let scope = getScope(context); - if (scope.upper) { - scope = scope.upper; - } - const superVar = scope.set.get(node.id.name); - if (superVar) { - superVar.eslintUsed = true; - } - } - }, - // children of a namespace that is a child of a declared namespace are auto-exported [ambientDeclarationSelector( 'TSModuleDeclaration[declare = true] > TSModuleBlock TSModuleDeclaration > TSModuleBlock', From dae7ddf919b9843fabc7689d052816ee68d8e4f4 Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:16:51 +0300 Subject: [PATCH 02/51] fix(eslint-plugin): [no-unused-vars] don't report on types referenced in export assignment expression (#8265) --- .../no-unused-vars/no-unused-vars.test.ts | 73 +++++++++++++++++++ .../src/referencer/ExportVisitor.ts | 12 ++- .../src/referencer/Referencer.ts | 4 + .../tests/fixtures/export/equals1.ts.shot | 2 +- .../tests/fixtures/export/equals3-type.ts | 4 + .../fixtures/export/equals3-type.ts.shot | 49 +++++++++++++ 6 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 packages/scope-manager/tests/fixtures/export/equals3-type.ts create mode 100644 packages/scope-manager/tests/fixtures/export/equals3-type.ts.shot diff --git a/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts b/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts index baed3954060c..5d39c2a2610e 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts @@ -1111,6 +1111,37 @@ foo &&= 2; let foo = 1; foo ||= 2; `, + ` +const foo = 1; +export = foo; + `, + ` +const Foo = 1; +interface Foo { + bar: string; +} +export = Foo; + `, + ` +interface Foo { + bar: string; +} +export = Foo; + `, + ` +type Foo = 1; +export = Foo; + `, + ` +type Foo = 1; +export = {} as Foo; + `, + ` +declare module 'foo' { + type Foo = 1; + export = Foo; +} + `, ], invalid: [ @@ -1874,5 +1905,47 @@ foo += 1; }, ], }, + { + code: ` +interface Foo { + bar: string; +} +type Bar = 1; +export = Bar; + `, + errors: [ + { + messageId: 'unusedVar', + line: 2, + column: 11, + data: { + varName: 'Foo', + action: 'defined', + additional: '', + }, + }, + ], + }, + { + code: ` +interface Foo { + bar: string; +} +type Bar = 1; +export = Foo; + `, + errors: [ + { + messageId: 'unusedVar', + line: 5, + column: 6, + data: { + varName: 'Bar', + action: 'defined', + additional: '', + }, + }, + ], + }, ], }); diff --git a/packages/scope-manager/src/referencer/ExportVisitor.ts b/packages/scope-manager/src/referencer/ExportVisitor.ts index 1af3a81ca2c6..24d35b49dab2 100644 --- a/packages/scope-manager/src/referencer/ExportVisitor.ts +++ b/packages/scope-manager/src/referencer/ExportVisitor.ts @@ -7,7 +7,8 @@ import { Visitor } from './Visitor'; type ExportNode = | TSESTree.ExportAllDeclaration | TSESTree.ExportDefaultDeclaration - | TSESTree.ExportNamedDeclaration; + | TSESTree.ExportNamedDeclaration + | TSESTree.TSExportAssignment; class ExportVisitor extends Visitor { readonly #referencer: Referencer; @@ -25,7 +26,10 @@ class ExportVisitor extends Visitor { } protected Identifier(node: TSESTree.Identifier): void { - if (this.#exportNode.exportKind === 'type') { + if ( + this.#exportNode.type !== AST_NODE_TYPES.TSExportAssignment && + this.#exportNode.exportKind === 'type' + ) { // export type { T }; // type exports can only reference types this.#referencer.currentScope().referenceType(node); @@ -49,6 +53,10 @@ class ExportVisitor extends Visitor { } } + protected TSExportAssignment(node: TSESTree.TSExportAssignment): void { + this.visit(node.expression); + } + protected ExportNamedDeclaration( node: TSESTree.ExportNamedDeclaration, ): void { diff --git a/packages/scope-manager/src/referencer/Referencer.ts b/packages/scope-manager/src/referencer/Referencer.ts index f5d36f2fa0ba..ddbdf7c61a4c 100644 --- a/packages/scope-manager/src/referencer/Referencer.ts +++ b/packages/scope-manager/src/referencer/Referencer.ts @@ -443,6 +443,10 @@ class Referencer extends Visitor { } } + protected TSExportAssignment(node: TSESTree.TSExportAssignment): void { + ExportVisitor.visit(this, node); + } + protected ExportNamedDeclaration( node: TSESTree.ExportNamedDeclaration, ): void { diff --git a/packages/scope-manager/tests/fixtures/export/equals1.ts.shot b/packages/scope-manager/tests/fixtures/export/equals1.ts.shot index 729abd96f92f..a4afe31abc8c 100644 --- a/packages/scope-manager/tests/fixtures/export/equals1.ts.shot +++ b/packages/scope-manager/tests/fixtures/export/equals1.ts.shot @@ -26,7 +26,7 @@ ScopeManager { Reference$2 { identifier: Identifier<"x">, isRead: true, - isTypeReference: false, + isTypeReference: true, isValueReference: true, isWrite: false, resolved: Variable$2, diff --git a/packages/scope-manager/tests/fixtures/export/equals3-type.ts b/packages/scope-manager/tests/fixtures/export/equals3-type.ts new file mode 100644 index 000000000000..a894a6341de0 --- /dev/null +++ b/packages/scope-manager/tests/fixtures/export/equals3-type.ts @@ -0,0 +1,4 @@ +interface Foo { + bar: 1; +} +export = Foo; diff --git a/packages/scope-manager/tests/fixtures/export/equals3-type.ts.shot b/packages/scope-manager/tests/fixtures/export/equals3-type.ts.shot new file mode 100644 index 000000000000..49ebdfb950af --- /dev/null +++ b/packages/scope-manager/tests/fixtures/export/equals3-type.ts.shot @@ -0,0 +1,49 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`export equals3-type 1`] = ` +ScopeManager { + variables: [ + ImplicitGlobalConstTypeVariable, + Variable$2 { + defs: [ + TypeDefinition$1 { + name: Identifier<"Foo">, + node: TSInterfaceDeclaration$1, + }, + ], + name: "Foo", + references: [ + Reference$1 { + identifier: Identifier<"Foo">, + isRead: true, + isTypeReference: true, + isValueReference: true, + isWrite: false, + resolved: Variable$2, + }, + ], + isValueVariable: false, + isTypeVariable: true, + }, + ], + scopes: [ + GlobalScope$1 { + block: Program$2, + isStrict: false, + references: [ + Reference$1, + ], + set: Map { + "const" => ImplicitGlobalConstTypeVariable, + "Foo" => Variable$2, + }, + type: "global", + upper: null, + variables: [ + ImplicitGlobalConstTypeVariable, + Variable$2, + ], + }, + ], +} +`; From 56149153745dec8cb44ea47397a2dce7b7846315 Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:18:16 +0300 Subject: [PATCH 03/51] docs(eslint-plugin): remove `fixable` and `hasSuggestions` from rules that don't provide them (#8253) --- packages/eslint-plugin/src/rules/class-methods-use-this.ts | 2 -- .../eslint-plugin/src/rules/no-confusing-non-null-assertion.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/class-methods-use-this.ts b/packages/eslint-plugin/src/rules/class-methods-use-this.ts index 42991c40f499..d6240415b066 100644 --- a/packages/eslint-plugin/src/rules/class-methods-use-this.ts +++ b/packages/eslint-plugin/src/rules/class-methods-use-this.ts @@ -28,8 +28,6 @@ export default createRule({ extendsBaseRule: true, requiresTypeChecking: false, }, - fixable: 'code', - hasSuggestions: false, schema: [ { type: 'object', diff --git a/packages/eslint-plugin/src/rules/no-confusing-non-null-assertion.ts b/packages/eslint-plugin/src/rules/no-confusing-non-null-assertion.ts index e54c5f1db2a1..76e60100422c 100644 --- a/packages/eslint-plugin/src/rules/no-confusing-non-null-assertion.ts +++ b/packages/eslint-plugin/src/rules/no-confusing-non-null-assertion.ts @@ -13,7 +13,6 @@ export default createRule({ 'Disallow non-null assertion in locations that may be confusing', recommended: 'stylistic', }, - fixable: 'code', hasSuggestions: true, messages: { confusingEqual: From 7573053ca2db41d45a2bb6af7e60d215f334cf54 Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:21:39 +0300 Subject: [PATCH 04/51] feat(typescript-estree): forbid duplicated accessibility modifiers (#8257) --- .../fixture.ts | 3 +++ .../snapshots/1-TSESTree-Error.shot | 10 +++++++++ .../snapshots/2-Babel-Error.shot | 3 +++ .../snapshots/3-Alignment-Error.shot | 3 +++ .../mixed-accessibility-modifiers/fixture.ts | 3 +++ .../snapshots/1-TSESTree-Error.shot | 10 +++++++++ .../snapshots/2-Babel-Error.shot | 3 +++ .../snapshots/3-Alignment-Error.shot | 3 +++ .../fixture.ts | 3 +++ .../snapshots/1-TSESTree-Error.shot | 10 +++++++++ .../snapshots/2-Babel-Error.shot | 3 +++ .../snapshots/3-Alignment-Error.shot | 3 +++ .../mixed-accessibility-modifiers/fixture.ts | 3 +++ .../snapshots/1-TSESTree-Error.shot | 10 +++++++++ .../snapshots/2-Babel-Error.shot | 3 +++ .../snapshots/3-Alignment-Error.shot | 3 +++ packages/typescript-estree/src/convert.ts | 21 +++++++++++++++++++ 17 files changed, 97 insertions(+) create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot create mode 100644 packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot create mode 100644 packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts new file mode 100644 index 000000000000..29db5140d271 --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts @@ -0,0 +1,3 @@ +class Foo { + public public bar() {}; +} diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot new file mode 100644 index 000000000000..49db5284df5a --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element MethodDefinition _error_ duplicated-accessibility-modifiers TSESTree - Error 1`] = ` +"TSError + 1 | class Foo { +> 2 | public public bar() {}; + | ^^^^^^ Accessibility modifier already seen. + 3 | } + 4 |" +`; diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot new file mode 100644 index 000000000000..e84a8adf3e77 --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element MethodDefinition _error_ duplicated-accessibility-modifiers Babel - Error 1`] = `[SyntaxError: Accessibility modifier already seen. (2:9)]`; diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot new file mode 100644 index 000000000000..d87e3076b189 --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element MethodDefinition _error_ duplicated-accessibility-modifiers Error Alignment 1`] = `"Both errored"`; diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts new file mode 100644 index 000000000000..6d9a757a6c96 --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts @@ -0,0 +1,3 @@ +class Foo { + public protected bar() {}; +} diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot new file mode 100644 index 000000000000..adeb1e6df792 --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element MethodDefinition _error_ mixed-accessibility-modifiers TSESTree - Error 1`] = ` +"TSError + 1 | class Foo { +> 2 | public protected bar() {}; + | ^^^^^^^^^ Accessibility modifier already seen. + 3 | } + 4 |" +`; diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot new file mode 100644 index 000000000000..223e5193276e --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element MethodDefinition _error_ mixed-accessibility-modifiers Babel - Error 1`] = `[SyntaxError: Accessibility modifier already seen. (2:9)]`; diff --git a/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot new file mode 100644 index 000000000000..957afbc76959 --- /dev/null +++ b/packages/ast-spec/src/element/MethodDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element MethodDefinition _error_ mixed-accessibility-modifiers Error Alignment 1`] = `"Both errored"`; diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts new file mode 100644 index 000000000000..cfe758d6b422 --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/fixture.ts @@ -0,0 +1,3 @@ +class Foo { + public public bar; +} diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot new file mode 100644 index 000000000000..6fbf0b6b8290 --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/1-TSESTree-Error.shot @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element PropertyDefinition _error_ duplicated-accessibility-modifiers TSESTree - Error 1`] = ` +"TSError + 1 | class Foo { +> 2 | public public bar; + | ^^^^^^ Accessibility modifier already seen. + 3 | } + 4 |" +`; diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot new file mode 100644 index 000000000000..1285ca65797e --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/2-Babel-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element PropertyDefinition _error_ duplicated-accessibility-modifiers Babel - Error 1`] = `[SyntaxError: Accessibility modifier already seen. (2:9)]`; diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot new file mode 100644 index 000000000000..116d47b88301 --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/duplicated-accessibility-modifiers/snapshots/3-Alignment-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element PropertyDefinition _error_ duplicated-accessibility-modifiers Error Alignment 1`] = `"Both errored"`; diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts new file mode 100644 index 000000000000..a9715175b5ad --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/fixture.ts @@ -0,0 +1,3 @@ +class Foo { + public protected bar; +} diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot new file mode 100644 index 000000000000..0786548487bd --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/1-TSESTree-Error.shot @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element PropertyDefinition _error_ mixed-accessibility-modifiers TSESTree - Error 1`] = ` +"TSError + 1 | class Foo { +> 2 | public protected bar; + | ^^^^^^^^^ Accessibility modifier already seen. + 3 | } + 4 |" +`; diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot new file mode 100644 index 000000000000..309038c6d169 --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/2-Babel-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element PropertyDefinition _error_ mixed-accessibility-modifiers Babel - Error 1`] = `[SyntaxError: Accessibility modifier already seen. (2:9)]`; diff --git a/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot new file mode 100644 index 000000000000..611da1f7d9ca --- /dev/null +++ b/packages/ast-spec/src/element/PropertyDefinition/fixtures/_error_/mixed-accessibility-modifiers/snapshots/3-Alignment-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures element PropertyDefinition _error_ mixed-accessibility-modifiers Error Alignment 1`] = `"Both errored"`; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 2fb094c1b589..3eb898abc8f7 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -3419,6 +3419,27 @@ export class Converter { ); } + // `checkGrammarModifiers` function in `typescript` + if ( + modifier.kind === SyntaxKind.PublicKeyword || + modifier.kind === SyntaxKind.ProtectedKeyword || + modifier.kind === SyntaxKind.PrivateKeyword + ) { + for (const anotherModifier of getModifiers(node) ?? []) { + if ( + anotherModifier !== modifier && + (anotherModifier.kind === SyntaxKind.PublicKeyword || + anotherModifier.kind === SyntaxKind.ProtectedKeyword || + anotherModifier.kind === SyntaxKind.PrivateKeyword) + ) { + this.#throwError( + anotherModifier, + `Accessibility modifier already seen.`, + ); + } + } + } + // `checkParameter` function in `typescript` if ( node.kind === SyntaxKind.Parameter && From a41ad155b5fee9177651439adb1c5131e7e6254f Mon Sep 17 00:00:00 2001 From: auvred <61150013+auvred@users.noreply.github.com> Date: Mon, 29 Jan 2024 20:24:04 +0300 Subject: [PATCH 05/51] test(eslint-plugin): assert that `ts`/`tsx` code blocks in docs are syntactically valid (#8142) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test(eslint-plugin): assert that `ts`/`tsx` code blocks in docs are syntactically valid * revert unintended changes in space-before-blocks.md * chore: shorten examples in consistent-type-assertions.md * refactor: do not parse md file again * docs: more consistent examples for consistent-type-assertions * docs: convert `js` code blocks to `ts` * Update packages/eslint-plugin/tests/docs.test.ts Co-authored-by: Josh Goldberg ✨ * chore: use regex instead of startsWith * chore: fix few codesamples --------- Co-authored-by: Josh Goldberg ✨ --- .../docs/rules/ban-tslint-comment.md | 4 +- .../docs/rules/consistent-type-assertions.md | 38 ++++++++++--------- .../rules/explicit-function-return-type.md | 12 ++---- .../rules/explicit-member-accessibility.md | 16 ++++---- .../docs/rules/member-ordering.md | 2 - .../docs/rules/no-empty-function.md | 3 -- .../docs/rules/no-for-in-array.md | 6 +-- .../eslint-plugin/docs/rules/no-this-alias.md | 4 +- .../eslint-plugin/docs/rules/no-type-alias.md | 32 ++++++++++------ .../rules/no-unnecessary-type-arguments.md | 4 +- .../rules/no-unnecessary-type-constraint.md | 2 +- .../docs/rules/prefer-enum-initializers.md | 2 +- .../eslint-plugin/docs/rules/prefer-for-of.md | 4 +- .../docs/rules/space-before-blocks.md | 7 +++- packages/eslint-plugin/docs/rules/typedef.md | 6 +-- packages/eslint-plugin/tests/docs.test.ts | 27 +++++++++++++ 16 files changed, 102 insertions(+), 67 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/ban-tslint-comment.md b/packages/eslint-plugin/docs/rules/ban-tslint-comment.md index eac42f6cdabf..c2529de6179f 100644 --- a/packages/eslint-plugin/docs/rules/ban-tslint-comment.md +++ b/packages/eslint-plugin/docs/rules/ban-tslint-comment.md @@ -16,7 +16,7 @@ Useful when migrating from TSLint to ESLint. Once TSLint has been removed, this ### ❌ Incorrect -```js +```ts /* tslint:disable */ /* tslint:enable */ /* tslint:disable:rule1 rule2 rule3... */ @@ -28,7 +28,7 @@ someCode(); // tslint:disable-line ### ✅ Correct -```js +```ts // This is a comment that just happens to mention tslint /* This is a multiline comment that just happens to mention tslint */ someCode(); // This is a comment that just happens to mention tslint diff --git a/packages/eslint-plugin/docs/rules/consistent-type-assertions.md b/packages/eslint-plugin/docs/rules/consistent-type-assertions.md index b453e7201aa6..7bd4412470e5 100644 --- a/packages/eslint-plugin/docs/rules/consistent-type-assertions.md +++ b/packages/eslint-plugin/docs/rules/consistent-type-assertions.md @@ -54,22 +54,22 @@ Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'neve #### ❌ Incorrect ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }' -const x = { ... } as T; +const x = { foo: 1 } as T; -function foo() { - return { ... } as T; +function bar() { + return { foo: 1 } as T; } ``` #### ✅ Correct ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }' -const x: T = { ... }; -const y = { ... } as any; -const z = { ... } as unknown; +const x: T = { foo: 1 }; +const y = { foo: 1 } as any; +const z = { foo: 1 } as unknown; -function foo(): T { - return { ... }; +function bar(): T { + return { foo: 1 }; } ``` @@ -82,23 +82,25 @@ Examples of code for `{ assertionStyle: 'as', objectLiteralTypeAssertions: 'allo #### ❌ Incorrect ```ts option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }' -const x = { ... } as T; +const x = { foo: 1 } as T; -function foo() { - return { ... } as T; +function bar() { + return { foo: 1 } as T; } ``` #### ✅ Correct ```tsx option='{ "assertionStyle": "as", "objectLiteralTypeAssertions": "allow-as-parameter" }' -const x: T = { ... }; -const y = { ... } as any; -const z = { ... } as unknown; -foo({ ... } as T); -new Clazz({ ... } as T); -function foo() { throw { bar: 5 } as Foo } -const foo = ; +const x: T = { foo: 1 }; +const y = { foo: 1 } as any; +const z = { foo: 1 } as unknown; +bar({ foo: 1 } as T); +new Clazz({ foo: 1 } as T); +function bar() { + throw { foo: 1 } as Foo; +} +const foo = ; ``` diff --git a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md index ad6b5598e557..2d02e06bdf01 100644 --- a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md +++ b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md @@ -143,7 +143,7 @@ type FuncType = () => string; let arrowFn: FuncType = () => 'test'; -let funcExpr: FuncType = function() { +let funcExpr: FuncType = function () { return 'test'; }; @@ -163,19 +163,15 @@ let objectPropCast = { foo: () => 1, }; -declare functionWithArg(arg: () => number); +declare function functionWithArg(arg: () => number); functionWithArg(() => 1); -declare functionWithObjectArg(arg: { method: () => number }); +declare function functionWithObjectArg(arg: { method: () => number }); functionWithObjectArg({ method() { return 1; }, }); - -const Comp: FC = () => { - return