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): [consistent-type-definitions] remove fixer when the interface is within a global module declaration #2739

Merged
merged 5 commits into from Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
71 changes: 48 additions & 23 deletions packages/eslint-plugin/src/rules/consistent-type-definitions.ts
@@ -1,4 +1,5 @@
import {
AST_NODE_TYPES,
AST_TOKEN_TYPES,
TSESLint,
TSESTree,
Expand Down Expand Up @@ -31,6 +32,21 @@ export default util.createRule({
create(context, [option]) {
const sourceCode = context.getSourceCode();

function isNodeGrandparentGlobalModuleDeclaration(
node: TSESTree.Node,
): boolean {
if (
node.parent?.type === AST_NODE_TYPES.TSModuleBlock &&
node.parent.parent?.type === AST_NODE_TYPES.TSModuleDeclaration &&
node.parent.parent?.declare &&
node.parent.parent?.global
Copy link
Member

Choose a reason for hiding this comment

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

instead of just looking at the grandparent - we could instead look at all the ancestors so we can catch cases like:

declare global {
  namespace Foo {
    interface Bar {}
  }
}

context.getAncestors() will return you an array and you can traverse it from the start to go from the highest parent.

) {
return true;
}

return false;
}

return {
"TSTypeAliasDeclaration[typeAnnotation.type='TSTypeLiteral']"(
node: TSESTree.TSTypeAliasDeclaration,
Expand Down Expand Up @@ -73,32 +89,41 @@ export default util.createRule({
context.report({
node: node.id,
messageId: 'typeOverInterface',
fix(fixer) {
const typeNode = node.typeParameters ?? node.id;
const fixes: TSESLint.RuleFix[] = [];
/**
* remove automatically fix when the interface is within a declare global
* @see {@link https://github.com/typescript-eslint/typescript-eslint/issues/2707}
*/
fix: isNodeGrandparentGlobalModuleDeclaration(node)
? null
: (fixer): TSESLint.RuleFix[] => {
const typeNode = node.typeParameters ?? node.id;
const fixes: TSESLint.RuleFix[] = [];

const firstToken = sourceCode.getFirstToken(node);
if (firstToken) {
fixes.push(fixer.replaceText(firstToken, 'type'));
fixes.push(
fixer.replaceTextRange(
[typeNode.range[1], node.body.range[0]],
' = ',
),
);
}
const firstToken = sourceCode.getFirstToken(node);
if (firstToken) {
fixes.push(fixer.replaceText(firstToken, 'type'));
fixes.push(
fixer.replaceTextRange(
[typeNode.range[1], node.body.range[0]],
' = ',
),
);
}

if (node.extends) {
node.extends.forEach(heritage => {
const typeIdentifier = sourceCode.getText(heritage);
fixes.push(
fixer.insertTextAfter(node.body, ` & ${typeIdentifier}`),
);
});
}
if (node.extends) {
node.extends.forEach(heritage => {
const typeIdentifier = sourceCode.getText(heritage);
fixes.push(
fixer.insertTextAfter(
node.body,
` & ${typeIdentifier}`,
),
);
});
}

return fixes;
},
return fixes;
},
});
}
},
Expand Down
Expand Up @@ -197,5 +197,71 @@ export type W<T> = {
},
],
},
{
code: `
namespace JSX {
interface Array<T> {
foo(x: (x: number) => T): T[];
}
}
`,
output: noFormat`
namespace JSX {
type Array<T> = {
foo(x: (x: number) => T): T[];
}
}
`,
options: ['type'],
errors: [
{
messageId: 'typeOverInterface',
line: 3,
column: 13,
},
],
},
{
code: `
global {
interface Array<T> {
foo(x: (x: number) => T): T[];
}
}
`,
output: noFormat`
global {
type Array<T> = {
foo(x: (x: number) => T): T[];
}
}
`,
options: ['type'],
errors: [
{
messageId: 'typeOverInterface',
line: 3,
column: 13,
},
],
},
{
code: `
declare global {
interface Array<T> {
foo(x: (x: number) => T): T[];
}
}
`,
output: null,
options: ['type'],
errors: [
{
messageId: 'typeOverInterface',
line: 3,
column: 13,
},
],
},
],
});