Skip to content

Commit

Permalink
fix(eslint-plugin): type assertion in rule no-extra-parens (#1376)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed Dec 24, 2019
1 parent 3a15413 commit f40639e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
13 changes: 9 additions & 4 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,26 @@ export default util.createRule<Options, MessageIds>({
const rule = rules.BinaryExpression as (n: typeof node) => void;

// makes the rule think it should skip the left or right
if (util.isTypeAssertion(node.left)) {
const isLeftTypeAssertion = util.isTypeAssertion(node.left);
const isRightTypeAssertion = util.isTypeAssertion(node.right);
if (isLeftTypeAssertion && isRightTypeAssertion) {
return; // ignore
}
if (isLeftTypeAssertion) {
return rule({
...node,
left: {
...node.left,
type: AST_NODE_TYPES.BinaryExpression as any,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}
if (util.isTypeAssertion(node.right)) {
if (isRightTypeAssertion) {
return rule({
...node,
right: {
...node.right,
type: AST_NODE_TYPES.BinaryExpression as any,
type: AST_NODE_TYPES.SequenceExpression as any,
},
});
}
Expand Down
64 changes: 63 additions & 1 deletion packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ for (a in b, c);
for (a in b);
`,
}),
`t.true((me.get as SinonStub).calledWithExactly('/foo', other));`,
...batchedSingleLineTests({
code: `
while ((foo = bar())) {}
Expand Down Expand Up @@ -120,6 +119,27 @@ typeof (a);
}),
...batchedSingleLineTests({
code: `
const x = (1 as 1) | (1 as 1);
const x = (<1>1) | (<1>1);
const x = (1 as 1) | 2;
const x = (1 as 1) + 2 + 2;
const x = 1 + 1 + (2 as 2);
const x = 1 | (2 as 2);
const x = (<1>1) | 2;
const x = 1 | (<2>2);
t.true((me.get as SinonStub).calledWithExactly('/foo', other));
t.true((<SinonStub>me.get).calledWithExactly('/foo', other));
(requestInit.headers as Headers).get('Cookie');
(<Headers> requestInit.headers).get('Cookie');
`,
parserOptions: {
ecmaFeatures: {
jsx: false,
},
},
}),
...batchedSingleLineTests({
code: `
[a as b];
() => (1 as 1);
x = a as b;
Expand Down Expand Up @@ -155,6 +175,48 @@ switch (foo) { case 1: case (2 as 2): break; default: break; }
},
],
}),
...batchedSingleLineTests({
code: `
[<b>a];
() => (<1>1);
x = <b>a;
const x = (<1>1) | 2;
const x = 1 | (<2>2);
const x = await (<Promise<void>>foo);
const res2 = (<foo>fn)();
(<boolean>x) ? 1 : 0;
x ? (<1>1) : 2;
x ? 1 : (<2>2);
while (<boolean>foo) {};
do {} while (<boolean>foo);
for (let i of (<Foo>[])) {}
for (let i in (<Foo>{})) {}
for ((<1>1);;) {}
for (;(<1>1);) {}
for (;;(<1>1)) {}
if (<1>1) {}
const x = (<1>1).toString();
new (<1>1)();
const x = { ...(<1>1), ...{} };
throw (<1>1);
throw 1;
const x = !(<1>1);
const x = (<1>1)++;
function *x() { yield (<1>1); yield 1; }
switch (foo) { case 1: case (<2>2): break; default: break; }
`,
parserOptions: {
ecmaFeatures: {
jsx: false,
},
},
options: [
'all',
{
nestedBinaryExpressions: false,
},
],
}),
],

invalid: [
Expand Down

0 comments on commit f40639e

Please sign in to comment.