Skip to content

Commit

Permalink
fix(eslint-plugin): [no-empty-interface] disable autofix for declarat…
Browse files Browse the repository at this point in the history
…ion merging with class (#5920)

* fix(eslint-plugin): [no-empty-interface] disable autofix for declaration merging with class

* fix comment

* fix lint

* Apply review
  • Loading branch information
yeonjuan committed Nov 18, 2022
1 parent 7a10707 commit a4f85b8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
31 changes: 21 additions & 10 deletions packages/eslint-plugin/src/rules/no-empty-interface.ts
@@ -1,4 +1,5 @@
import type { TSESLint } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

import * as util from '../util';

Expand Down Expand Up @@ -73,29 +74,39 @@ export default util.createRule<Options, MessageIds>({
)}${typeParam} = ${sourceCode.getText(extend[0])}`,
);
};
const scope = context.getScope();

// Check if interface is within ambient declaration
let useAutoFix = true;
if (util.isDefinitionFile(filename)) {
const scope = context.getScope();
if (scope.type === 'tsModule' && scope.block.declare) {
useAutoFix = false;
}
}
const mergedWithClassDeclaration = scope.set
.get(node.id.name)
?.defs?.some(
def => def.node.type === AST_NODE_TYPES.ClassDeclaration,
);

const isInAmbientDeclaration = !!(
util.isDefinitionFile(filename) &&
scope.type === 'tsModule' &&
scope.block.declare
);

const useAutoFix = !(
isInAmbientDeclaration || mergedWithClassDeclaration
);

context.report({
node: node.id,
messageId: 'noEmptyWithSuper',
...(useAutoFix
? { fix }
: {
: !mergedWithClassDeclaration
? {
suggest: [
{
messageId: 'noEmptyWithSuper',
fix,
},
],
}),
}
: null),
});
}
}
Expand Down
88 changes: 88 additions & 0 deletions packages/eslint-plugin/tests/rules/no-empty-interface.test.ts
Expand Up @@ -34,6 +34,18 @@ interface Bar extends Foo {}
`,
options: [{ allowSingleExtends: true }],
},
{
code: `
interface Foo {
props: string;
}
interface Bar extends Foo {}
class Bar {}
`,
options: [{ allowSingleExtends: true }],
},
],
invalid: [
{
Expand All @@ -58,6 +70,82 @@ interface Bar extends Foo {}
},
{
code: `
interface Foo {
props: string;
}
interface Bar extends Foo {}
class Baz {}
`,
output: `
interface Foo {
props: string;
}
type Bar = Foo
class Baz {}
`,
options: [{ allowSingleExtends: false }],
errors: [
{
messageId: 'noEmptyWithSuper',
line: 6,
column: 11,
},
],
},
{
code: `
interface Foo {
props: string;
}
interface Bar extends Foo {}
class Bar {}
`,
options: [{ allowSingleExtends: false }],
errors: [
{
messageId: 'noEmptyWithSuper',
line: 6,
column: 11,
},
],
output: null,
},
{
code: `
interface Foo {
props: string;
}
interface Bar extends Foo {}
const bar = class Bar {};
`,
output: `
interface Foo {
props: string;
}
type Bar = Foo
const bar = class Bar {};
`,
options: [{ allowSingleExtends: false }],
errors: [
{
messageId: 'noEmptyWithSuper',
line: 6,
column: 11,
},
],
},
{
code: `
interface Foo {
name: string;
}
Expand Down

0 comments on commit a4f85b8

Please sign in to comment.