Skip to content

Commit ac397f1

Browse files
authored
fix(eslint-plugin): [no-useless-empty-export] exempt .d.ts (#7718)
1 parent f81a2da commit ac397f1

2 files changed

Lines changed: 58 additions & 12 deletions

File tree

‎packages/eslint-plugin/src/rules/no-useless-empty-export.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,34 +40,38 @@ export default createRule({
4040
},
4141
defaultOptions: [],
4242
create(context) {
43+
// In a definition file, export {} is necessary to make the module properly
44+
// encapsulated, even when there are other exports
45+
// https://github.com/typescript-eslint/typescript-eslint/issues/4975
46+
if (util.isDefinitionFile(context.getFilename())) {
47+
return {};
48+
}
4349
function checkNode(
4450
node: TSESTree.Program | TSESTree.TSModuleDeclaration,
4551
): void {
4652
if (!Array.isArray(node.body)) {
4753
return;
4854
}
4955

50-
let emptyExport: TSESTree.ExportNamedDeclaration | undefined;
56+
const emptyExports: TSESTree.ExportNamedDeclaration[] = [];
5157
let foundOtherExport = false;
5258

5359
for (const statement of node.body) {
5460
if (isEmptyExport(statement)) {
55-
emptyExport = statement;
56-
57-
if (foundOtherExport) {
58-
break;
59-
}
61+
emptyExports.push(statement);
6062
} else if (exportOrImportNodeTypes.has(statement.type)) {
6163
foundOtherExport = true;
6264
}
6365
}
6466

65-
if (emptyExport && foundOtherExport) {
66-
context.report({
67-
fix: fixer => fixer.remove(emptyExport!),
68-
messageId: 'uselessExport',
69-
node: emptyExport,
70-
});
67+
if (foundOtherExport) {
68+
for (const emptyExport of emptyExports) {
69+
context.report({
70+
fix: fixer => fixer.remove(emptyExport),
71+
messageId: 'uselessExport',
72+
node: emptyExport,
73+
});
74+
}
7175
}
7276
}
7377

‎packages/eslint-plugin/tests/rules/no-useless-empty-export.test.ts‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,35 @@ ruleTester.run('no-useless-empty-export', rule, {
3737
`
3838
export {};
3939
`,
40+
// https://github.com/microsoft/TypeScript/issues/38592
41+
{
42+
code: `
43+
export type A = 1;
44+
export {};
45+
`,
46+
filename: 'foo.d.ts',
47+
},
48+
{
49+
code: `
50+
export declare const a = 2;
51+
export {};
52+
`,
53+
filename: 'foo.d.ts',
54+
},
55+
{
56+
code: `
57+
import type { A } from '_';
58+
export {};
59+
`,
60+
filename: 'foo.d.ts',
61+
},
62+
{
63+
code: `
64+
import { A } from '_';
65+
export {};
66+
`,
67+
filename: 'foo.d.ts',
68+
},
4069
],
4170
invalid: [
4271
{
@@ -120,6 +149,19 @@ export {};
120149
output: `
121150
import _ = require('_');
122151
152+
`,
153+
},
154+
{
155+
code: `
156+
import _ = require('_');
157+
export {};
158+
export {};
159+
`,
160+
errors: [error, error],
161+
output: `
162+
import _ = require('_');
163+
164+
123165
`,
124166
},
125167
],

0 commit comments

Comments
 (0)