Skip to content

Commit f40639e

Browse files
armano2bradzacher
authored andcommitted
fix(eslint-plugin): type assertion in rule no-extra-parens (typescript-eslint#1376)
1 parent 3a15413 commit f40639e

File tree

2 files changed

+72
-5
lines changed

2 files changed

+72
-5
lines changed

packages/eslint-plugin/src/rules/no-extra-parens.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,26 @@ export default util.createRule<Options, MessageIds>({
3535
const rule = rules.BinaryExpression as (n: typeof node) => void;
3636

3737
// makes the rule think it should skip the left or right
38-
if (util.isTypeAssertion(node.left)) {
38+
const isLeftTypeAssertion = util.isTypeAssertion(node.left);
39+
const isRightTypeAssertion = util.isTypeAssertion(node.right);
40+
if (isLeftTypeAssertion && isRightTypeAssertion) {
41+
return; // ignore
42+
}
43+
if (isLeftTypeAssertion) {
3944
return rule({
4045
...node,
4146
left: {
4247
...node.left,
43-
type: AST_NODE_TYPES.BinaryExpression as any,
48+
type: AST_NODE_TYPES.SequenceExpression as any,
4449
},
4550
});
4651
}
47-
if (util.isTypeAssertion(node.right)) {
52+
if (isRightTypeAssertion) {
4853
return rule({
4954
...node,
5055
right: {
5156
...node.right,
52-
type: AST_NODE_TYPES.BinaryExpression as any,
57+
type: AST_NODE_TYPES.SequenceExpression as any,
5358
},
5459
});
5560
}

packages/eslint-plugin/tests/rules/no-extra-parens.test.ts

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ for (a in b, c);
2323
for (a in b);
2424
`,
2525
}),
26-
`t.true((me.get as SinonStub).calledWithExactly('/foo', other));`,
2726
...batchedSingleLineTests({
2827
code: `
2928
while ((foo = bar())) {}
@@ -120,6 +119,27 @@ typeof (a);
120119
}),
121120
...batchedSingleLineTests({
122121
code: `
122+
const x = (1 as 1) | (1 as 1);
123+
const x = (<1>1) | (<1>1);
124+
const x = (1 as 1) | 2;
125+
const x = (1 as 1) + 2 + 2;
126+
const x = 1 + 1 + (2 as 2);
127+
const x = 1 | (2 as 2);
128+
const x = (<1>1) | 2;
129+
const x = 1 | (<2>2);
130+
t.true((me.get as SinonStub).calledWithExactly('/foo', other));
131+
t.true((<SinonStub>me.get).calledWithExactly('/foo', other));
132+
(requestInit.headers as Headers).get('Cookie');
133+
(<Headers> requestInit.headers).get('Cookie');
134+
`,
135+
parserOptions: {
136+
ecmaFeatures: {
137+
jsx: false,
138+
},
139+
},
140+
}),
141+
...batchedSingleLineTests({
142+
code: `
123143
[a as b];
124144
() => (1 as 1);
125145
x = a as b;
@@ -155,6 +175,48 @@ switch (foo) { case 1: case (2 as 2): break; default: break; }
155175
},
156176
],
157177
}),
178+
...batchedSingleLineTests({
179+
code: `
180+
[<b>a];
181+
() => (<1>1);
182+
x = <b>a;
183+
const x = (<1>1) | 2;
184+
const x = 1 | (<2>2);
185+
const x = await (<Promise<void>>foo);
186+
const res2 = (<foo>fn)();
187+
(<boolean>x) ? 1 : 0;
188+
x ? (<1>1) : 2;
189+
x ? 1 : (<2>2);
190+
while (<boolean>foo) {};
191+
do {} while (<boolean>foo);
192+
for (let i of (<Foo>[])) {}
193+
for (let i in (<Foo>{})) {}
194+
for ((<1>1);;) {}
195+
for (;(<1>1);) {}
196+
for (;;(<1>1)) {}
197+
if (<1>1) {}
198+
const x = (<1>1).toString();
199+
new (<1>1)();
200+
const x = { ...(<1>1), ...{} };
201+
throw (<1>1);
202+
throw 1;
203+
const x = !(<1>1);
204+
const x = (<1>1)++;
205+
function *x() { yield (<1>1); yield 1; }
206+
switch (foo) { case 1: case (<2>2): break; default: break; }
207+
`,
208+
parserOptions: {
209+
ecmaFeatures: {
210+
jsx: false,
211+
},
212+
},
213+
options: [
214+
'all',
215+
{
216+
nestedBinaryExpressions: false,
217+
},
218+
],
219+
}),
158220
],
159221

160222
invalid: [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy