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): type assertion in rule no-extra-parens #1376

Merged
merged 2 commits into from Dec 24, 2019
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
13 changes: 9 additions & 4 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
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) {
armano2 marked this conversation as resolved.
Show resolved Hide resolved
return; // ignore
}
if (isLeftTypeAssertion) {
return rule({
...node,
left: {
...node.left,
type: AST_NODE_TYPES.BinaryExpression as any,
type: AST_NODE_TYPES.SequenceExpression as any,
armano2 marked this conversation as resolved.
Show resolved Hide resolved
},
});
}
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
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