Skip to content

Commit

Permalink
fix(eslint-plugin): [no-useless-empty-export] exempt .d.ts (#7718)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Oct 11, 2023
1 parent f81a2da commit ac397f1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
28 changes: 16 additions & 12 deletions packages/eslint-plugin/src/rules/no-useless-empty-export.ts
Expand Up @@ -40,34 +40,38 @@ export default createRule({
},
defaultOptions: [],
create(context) {
// In a definition file, export {} is necessary to make the module properly
// encapsulated, even when there are other exports
// https://github.com/typescript-eslint/typescript-eslint/issues/4975
if (util.isDefinitionFile(context.getFilename())) {
return {};
}
function checkNode(
node: TSESTree.Program | TSESTree.TSModuleDeclaration,
): void {
if (!Array.isArray(node.body)) {
return;
}

let emptyExport: TSESTree.ExportNamedDeclaration | undefined;
const emptyExports: TSESTree.ExportNamedDeclaration[] = [];
let foundOtherExport = false;

for (const statement of node.body) {
if (isEmptyExport(statement)) {
emptyExport = statement;

if (foundOtherExport) {
break;
}
emptyExports.push(statement);
} else if (exportOrImportNodeTypes.has(statement.type)) {
foundOtherExport = true;
}
}

if (emptyExport && foundOtherExport) {
context.report({
fix: fixer => fixer.remove(emptyExport!),
messageId: 'uselessExport',
node: emptyExport,
});
if (foundOtherExport) {
for (const emptyExport of emptyExports) {
context.report({
fix: fixer => fixer.remove(emptyExport),
messageId: 'uselessExport',
node: emptyExport,
});
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions packages/eslint-plugin/tests/rules/no-useless-empty-export.test.ts
Expand Up @@ -37,6 +37,35 @@ ruleTester.run('no-useless-empty-export', rule, {
`
export {};
`,
// https://github.com/microsoft/TypeScript/issues/38592
{
code: `
export type A = 1;
export {};
`,
filename: 'foo.d.ts',
},
{
code: `
export declare const a = 2;
export {};
`,
filename: 'foo.d.ts',
},
{
code: `
import type { A } from '_';
export {};
`,
filename: 'foo.d.ts',
},
{
code: `
import { A } from '_';
export {};
`,
filename: 'foo.d.ts',
},
],
invalid: [
{
Expand Down Expand Up @@ -120,6 +149,19 @@ export {};
output: `
import _ = require('_');
`,
},
{
code: `
import _ = require('_');
export {};
export {};
`,
errors: [error, error],
output: `
import _ = require('_');
`,
},
],
Expand Down

0 comments on commit ac397f1

Please sign in to comment.