Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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

Merged
merged 5 commits into from
Nov 18, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions packages/eslint-plugin/src/rules/no-empty-interface.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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