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] disable autofix for declaration merging with class #5920

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
`,
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