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 3 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
22 changes: 19 additions & 3 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 @@ -76,26 +77,41 @@ export default util.createRule<Options, MessageIds>({

// Check if interface is within ambient declaration
let useAutoFix = true;
let hasSuggest = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I found it confusing how the previous variable starts with use but this starts with has, even though they're pretty much the same thing. Can we make the names more similar?

Suggested change
let hasSuggest = true;
let useSuggestion = true;

Note: after applying the refactor I suggest later in the file, the variable might be unnecessary.

const scope = context.getScope();

if (util.isDefinitionFile(filename)) {
const scope = context.getScope();
if (scope.type === 'tsModule' && scope.block.declare) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Nit] Now that the const scope = ... line is earlier, these two if statements can be combined:

if (
  util.isDefinitionFile(filename) &&
  scope.type === "tsModule" &&
  scope.block.declare
) {
  useAutoFix = false;
}

...so the code can be simplified a bit more, to give useAutoFix an initial value:

let useAutoFix = ...;

I would even suggest making a separate variable for whether it's merged with a class declaration:

const mergedWithClassDeclaration = scope.set
  .get(node.id.name)
  ?.defs?.some((def) => def.node.type === AST_NODE_TYPES.ClassDeclaration);

const useAutoFix =
  !mergedWithClassDeclaration && ...;

Thoughts?

useAutoFix = false;
}
}

// Checks if the interface declaration is merged with class declarations.
const defs = scope.set.get(node.id.name)?.defs;
if (
defs?.some(
def => def.node.type === AST_NODE_TYPES.ClassDeclaration,
)
) {
useAutoFix = false;
hasSuggest = false;
}

context.report({
node: node.id,
messageId: 'noEmptyWithSuper',
...(useAutoFix
? { fix }
: {
: hasSuggest
? {
suggest: [
{
messageId: 'noEmptyWithSuper',
fix,
},
],
}),
}
: null),
});
}
}
Expand Down
20 changes: 20 additions & 0 deletions packages/eslint-plugin/tests/rules/no-empty-interface.test.ts
Expand Up @@ -58,6 +58,26 @@ interface Bar extends Foo {}
},
{
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 {
name: string;
}
Expand Down