8000 fix(eslint-plugin): [no-unused-vars] don't report on types referenced… · danvk/typescript-eslint@bb907e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb907e2

Browse files
auvreddanvk
authored andcommitted
fix(eslint-plugin): [no-unused-vars] don't report on types referenced in export assignment expression (typescript-eslint#8265)
1 parent 1bc3c97 commit bb907e2

File tree

6 files changed

+141
-3
lines changed

6 files changed

+141
-3
lines changed

packages/eslint-plugin/tests/rules/no-unused-vars/no-unused-vars.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,37 @@ foo &&= 2;
11111111
let foo = 1;
11121112
foo ||= 2;
11131113
`,
1114+
`
1115+
const foo = 1;
1116+
export = foo;
1117+
`,
1118+
`
1119+
const Foo = 1;
1120+
interface Foo {
1121+
bar: string;
1122+
}
1123+
export = Foo;
1124+
`,
1125+
`
1126+
interface Foo {
1127+
bar: string;
1128+
}
1129+
export = Foo;
1130+
`,
1131+
`
1132+
type Foo = 1;
1133+
export = Foo;
1134+
`,
1135+
`
1136+
type Foo = 1;
1137+
export = {} as Foo;
1138+
`,
1139+
`
1140+
declare module 'foo' {
1141+
type Foo = 1;
1142+
export = Foo;
1143+
}
1144+
`,
11141145
],
11151146

11161147
invalid: [
@@ -1874,5 +1905,47 @@ foo += 1;
18741905
},
18751906
],
18761907
},
1908+
{
1909+
code: `
1910+
interface Foo {
1911+
bar: string;
1912+
}
1913+
type Bar = 1;
1914+
export = Bar;
1915+
`,
1916+
errors: [
1917+
{
1918+
messageId: 'unusedVar',
1919+
line: 2,
1920+
column: 11,
1921+
data: {
1922+
varName: 'Foo',
1923+
action: 'defined',
1924+
additional: '',
1925+
},
1926+
},
1927+
],
1928+
},
1929+
{
1930+
code: `
1931+
interface Foo {
1932+
bar: string;
1933+
}
1934+
type Bar = 1;
1935+
export = Foo;
1936+
`,
1937+
errors: [
1938+
{
1939+
messageId: 'unusedVar',
1940+
line: 5,
1941+
column: 6,
1942+
data: {
1943+
varName: 'Bar',
1944+
action: 'defined',
1945+
additional: '',
1946+
},
1947+
},
1948+
],
1949+
},
18771950
],
18781951
});

packages/scope-manager/src/referencer/ExportVisitor.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import { Visitor } from './Visitor';
77
type ExportNode =
88
| TSESTree.ExportAllDeclaration
99
| TSESTree.ExportDefaultDeclaration
10-
| TSESTree.ExportNamedDeclaration;
10+
| TSESTree.ExportNamedDeclaration
11+
| TSESTree.TSExportAssignment;
1112

1213
class ExportVisitor extends Visitor {
1314
readonly #referencer: Referencer;
@@ -25,7 +26,10 @@ class ExportVisitor extends Visitor {
2526
}
2627

2728
protected Identifier(node: TSESTree.Identifier): void {
28-
if (this.#exportNode.exportKind === 'type') {
29+
if (
30+
this.#exportNode.type !== AST_NODE_TYPES.TSExportAssignment &&
31+
this.#exportNode.exportKind === 'type'
32+
) {
2933
// export type { T };
3034
// type exports can only reference types
3135
this.#referencer.currentScope().referenceType(node);
@@ -49,6 +53,10 @@ class ExportVisitor extends Visitor {
4953
}
5054
}
5155

56+
protected TSExportAssignment(node: TSESTree.TSExportAssignment): void {
57+
this.visit(node.expression);
58+
}
59+
5260
protected ExportNamedDeclaration(
5361
node: TSESTree.ExportNamedDeclaration,
5462
): void {

packages/scope-manager/src/referencer/Referencer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,10 @@ class Referencer extends Visitor {
443443
}
444444
}
445445

446+
protected TSExportAssignment(node: TSESTree.TSExportAssignment): void {
447+
ExportVisitor.visit(this, node);
448+
}
449+
446450
protected ExportNamedDeclaration(
447451
node: TSESTree.ExportNamedDeclaration,
448452
): void {

packages/scope-manager/tests/fixtures/export/equals1.ts.shot

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface Foo {
2+
bar: 1;
3+
}
4+
export = Foo;

packages/scope-manager/tests/fixtures/export/equals3-type.ts.shot

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0