Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-useless-empty-export] exempt .d.ts #7718

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 16 additions & 12 deletions packages/eslint-plugin/src/rules/no-useless-empty-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,34 +40,38 @@ export default util.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
Original file line number Diff line number Diff line change
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