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-empty-interface] use suggestion fixer for ambient contexts #1880

Merged
merged 6 commits into from Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
54 changes: 42 additions & 12 deletions packages/eslint-plugin/src/rules/no-empty-interface.ts
@@ -1,4 +1,9 @@
import * as util from '../util';
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import {
RuleFix,
RuleFixer,
} from '@typescript-eslint/experimental-utils/dist/ts-eslint';
greyscaled marked this conversation as resolved.
Show resolved Hide resolved

type Options = [
{
Expand Down Expand Up @@ -43,6 +48,7 @@ export default util.createRule<Options, MessageIds>({
return {
TSInterfaceDeclaration(node): void {
const sourceCode = context.getSourceCode();
const filename = context.getFilename();

if (node.body.body.length !== 0) {
// interface contains members --> Nothing to report
Expand All @@ -58,21 +64,45 @@ export default util.createRule<Options, MessageIds>({
} else if (extend.length === 1) {
// interface extends exactly 1 interface --> Report depending on rule setting
if (!allowSingleExtends) {
const fix = (fixer: RuleFixer): RuleFix => {
let typeParam = '';
if (node.typeParameters) {
typeParam = sourceCode.getText(node.typeParameters);
}
return fixer.replaceText(
node,
`type ${sourceCode.getText(
node.id,
)}${typeParam} = ${sourceCode.getText(extend[0])}`,
);
};

// Check if interface is within ambient declaration
let useAutoFix = true;
if (util.isDefinitionFile(filename)) {
const scope = context.getScope();
if (
scope.block.parent &&
scope.block.parent.type ===
AST_NODE_TYPES.TSModuleDeclaration &&
scope.block.parent.declare
) {
useAutoFix = false;
}
}

context.report({
node: node.id,
messageId: 'noEmptyWithSuper',
fix: fixer => {
let typeParam = '';
if (node.typeParameters) {
typeParam = sourceCode.getText(node.typeParameters);
}
return fixer.replaceText(
node,
`type ${sourceCode.getText(
node.id,
)}${typeParam} = ${sourceCode.getText(extend[0])}`,
);
},
fix: useAutoFix ? fix : undefined,
suggest: useAutoFix
? undefined
: [
{
messageId: 'noEmptyWithSuper',
fix,
},
],
greyscaled marked this conversation as resolved.
Show resolved Hide resolved
});
}
}
Expand Down
22 changes: 22 additions & 0 deletions packages/eslint-plugin/tests/rules/no-empty-interface.test.ts
Expand Up @@ -148,5 +148,27 @@ type Foo<T> = Bar<T>
},
],
},
{
filename: 'test.d.ts',
code: `
declare module FooBar {
type Baz = typeof baz;
export interface Bar extends Baz {}
}
`,
errors: [
{
messageId: 'noEmptyWithSuper',
line: 4,
},
],
greyscaled marked this conversation as resolved.
Show resolved Hide resolved
// output matches input because a suggestion was made
output: `
declare module FooBar {
type Baz = typeof baz;
export interface Bar extends Baz {}
}
`,
},
],
});