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-extra-parens] handle ESLint 7.19.0 #2993

Merged
merged 4 commits into from Feb 6, 2021
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
69 changes: 52 additions & 17 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
@@ -1,6 +1,7 @@
// any is required to work around manipulating the AST in weird ways
/* eslint-disable @typescript-eslint/no-explicit-any */

import semver from 'semver';
import {
AST_NODE_TYPES,
TSESTree,
Expand Down Expand Up @@ -164,23 +165,7 @@ export default util.createRule<Options, MessageIds>({
return rules.ConditionalExpression(node);
},
// DoWhileStatement
'ForInStatement, ForOfStatement'(
node: TSESTree.ForInStatement | TSESTree.ForOfStatement,
) {
if (util.isTypeAssertion(node.right)) {
// makes the rule skip checking of the right
return rules['ForInStatement, ForOfStatement']({
...node,
type: AST_NODE_TYPES.ForOfStatement as any,
right: {
...node.right,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}

return rules['ForInStatement, ForOfStatement'](node);
},
// ForIn and ForOf are guarded by eslint version
ForStatement(node) {
// make the rule skip the piece by removing it entirely
if (node.init && util.isTypeAssertion(node.init)) {
Expand Down Expand Up @@ -256,6 +241,56 @@ export default util.createRule<Options, MessageIds>({
}
},
};
if (semver.satisfies(TSESLint.ESLint.version, '>=7.19.0')) {
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 using a semver check here, as these can be pretty hairy to get right.

Just guard appropriately.

if (rules.ForInStatement && rules.ForOfStatement) {
  // new version
} else {
  // old version
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, thanks!

overrides.ForInStatement = function (node): void {
if (util.isTypeAssertion(node.right)) {
// makes the rule skip checking of the right
return rules.ForInStatement({
...node,
type: AST_NODE_TYPES.ForInStatement,
right: {
...node.right,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}

return rules.ForInStatement(node);
};
overrides.ForOfStatement = function (node): void {
if (util.isTypeAssertion(node.right)) {
// makes the rule skip checking of the right
return rules.ForOfStatement({
...node,
type: AST_NODE_TYPES.ForOfStatement,
right: {
...node.right,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}

return rules.ForOfStatement(node);
};
} else {
overrides['ForInStatement, ForOfStatement'] = function (
node: TSESTree.ForInStatement | TSESTree.ForOfStatement,
): void {
if (util.isTypeAssertion(node.right)) {
// makes the rule skip checking of the right
return rules['ForInStatement, ForOfStatement']({
...node,
type: AST_NODE_TYPES.ForOfStatement as any,
right: {
...node.right,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}

return rules['ForInStatement, ForOfStatement'](node);
};
}
return Object.assign({}, rules, overrides);
},
});
4 changes: 4 additions & 0 deletions packages/eslint-plugin/typings/eslint-rules.d.ts
Expand Up @@ -520,9 +520,13 @@ declare module 'eslint/lib/rules/no-extra-parens' {
ClassExpression(node: TSESTree.ClassExpression): void;
ConditionalExpression(node: TSESTree.ConditionalExpression): void;
DoWhileStatement(node: TSESTree.DoWhileStatement): void;
// eslint < 7.19.0
'ForInStatement, ForOfStatement'(
node: TSESTree.ForInStatement | TSESTree.ForOfStatement,
): void;
// eslint >= 7.19.0
ForInStatement(node: TSESTree.ForInStatement): void;
ForOfStatement(node: TSESTree.ForOfStatement): void;
ForStatement(node: TSESTree.ForStatement): void;
'ForStatement > *.init:exit'(node: TSESTree.Node): void;
IfStatement(node: TSESTree.IfStatement): void;
Expand Down