Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): support type assertions in no-extra-parens rule (#…
…311)
- Loading branch information
1 parent
5592a2c
commit 116ca75
Showing
6 changed files
with
351 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# disallow unnecessary parentheses (no-extra-parens) | ||
|
||
This rule restricts the use of parentheses to only where they are necessary. | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [eslint/no-extra-parens](https://eslint.org/docs/rules/no-extra-parens) rule. | ||
It supports all options and features of the base rule plus TS type assertions. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"no-extra-parens": "off", | ||
"@typescript-eslint/no-extra-parens": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [eslint/no-extra-parens options](https://eslint.org/docs/rules/no-extra-parens#options). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/typescript-estree'; | ||
import baseRule from 'eslint/lib/rules/no-extra-parens'; | ||
import * as util from '../util'; | ||
|
||
type Options = util.InferOptionsTypeFromRule<typeof baseRule>; | ||
type MessageIds = util.InferMessageIdsTypeFromRule<typeof baseRule>; | ||
|
||
export default util.createRule<Options, MessageIds>({ | ||
name: 'no-extra-parens', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'disallow unnecessary parentheses', | ||
category: 'Possible Errors', | ||
recommended: false, | ||
}, | ||
fixable: 'code', | ||
schema: baseRule.meta.schema, | ||
messages: baseRule.meta.messages, | ||
}, | ||
defaultOptions: ['all'], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
|
||
return Object.assign({}, rules, { | ||
MemberExpression(node: TSESTree.MemberExpression) { | ||
if (node.object.type !== AST_NODE_TYPES.TSAsExpression) { | ||
return rules.MemberExpression(node); | ||
} | ||
}, | ||
}); | ||
}, | ||
}); |
266 changes: 266 additions & 0 deletions
266
packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,266 @@ | ||
import rule from '../../src/rules/no-extra-parens'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-extra-parens', rule, { | ||
valid: [ | ||
{ | ||
code: ` | ||
(0).toString(); | ||
(function(){}) ? a() : b(); | ||
(/^a$/).test(x); | ||
for (a of (b, c)); | ||
for (a of b); | ||
for (a in b, c); | ||
for (a in b); | ||
`, | ||
}, | ||
{ | ||
code: `t.true((me.get as SinonStub).calledWithExactly('/foo', other));`, | ||
}, | ||
{ | ||
code: `while ((foo = bar())) {}`, | ||
options: ['all', { conditionalAssign: false }], | ||
}, | ||
{ | ||
code: `if ((foo = bar())) {}`, | ||
options: ['all', { conditionalAssign: false }], | ||
}, | ||
{ | ||
code: `do; while ((foo = bar()))`, | ||
options: ['all', { conditionalAssign: false }], | ||
}, | ||
{ | ||
code: `for (;(a = b););`, | ||
options: ['all', { conditionalAssign: false }], | ||
}, | ||
{ | ||
code: ` | ||
function a(b) { | ||
return (b = 1); | ||
} | ||
`, | ||
options: ['all', { returnAssign: false }], | ||
}, | ||
{ | ||
code: ` | ||
function a(b) { | ||
return b ? (c = d) : (c = e); | ||
} | ||
`, | ||
options: ['all', { returnAssign: false }], | ||
}, | ||
{ | ||
code: `b => (b = 1);`, | ||
options: ['all', { returnAssign: false }], | ||
}, | ||
{ | ||
code: `b => b ? (c = d) : (c = e);`, | ||
options: ['all', { returnAssign: false }], | ||
}, | ||
{ | ||
code: `x = a || (b && c);`, | ||
options: ['all', { nestedBinaryExpressions: false }], | ||
}, | ||
{ | ||
code: `x = a + (b * c);`, | ||
options: ['all', { nestedBinaryExpressions: false }], | ||
}, | ||
{ | ||
code: `x = (a * b) / c;`, | ||
options: ['all', { nestedBinaryExpressions: false }], | ||
}, | ||
{ | ||
code: ` | ||
const Component = (<div />) | ||
const Component = ( | ||
<div | ||
prop={true} | ||
/> | ||
) | ||
`, | ||
options: ['all', { ignoreJSX: 'all' }], | ||
}, | ||
{ | ||
code: ` | ||
const Component = ( | ||
<div> | ||
<p /> | ||
</div> | ||
) | ||
const Component = ( | ||
<div | ||
prop={true} | ||
/> | ||
) | ||
`, | ||
options: ['all', { ignoreJSX: 'multi-line' }], | ||
}, | ||
{ | ||
code: ` | ||
const Component = (<div />) | ||
const Component = (<div><p /></div>) | ||
`, | ||
options: ['all', { ignoreJSX: 'single-line' }], | ||
}, | ||
{ | ||
code: ` | ||
const b = a => 1 ? 2 : 3; | ||
const d = c => (1 ? 2 : 3); | ||
`, | ||
options: ['all', { enforceForArrowConditionals: false }], | ||
}, | ||
{ | ||
code: ` | ||
(0).toString(); | ||
(Object.prototype.toString.call()); | ||
({}.toString.call()); | ||
(function(){} ? a() : b()); | ||
(/^a$/).test(x); | ||
a = (b * c); | ||
(a * b) + c; | ||
typeof (a); | ||
`, | ||
options: ['functions'], | ||
}, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: `a = (b * c);`, | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 5, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `(a * b) + c;`, | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 1, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `for (a in (b, c));`, | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 11, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `for (a in (b));`, | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 11, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `for (a of (b));`, | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 11, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `typeof (a);`, | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 8, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const Component = (<div />) | ||
const Component = (<div><p /></div>) | ||
`, | ||
options: ['all', { ignoreJSX: 'multi-line' }], | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 2, | ||
column: 27, | ||
}, | ||
{ | ||
messageId: 'unexpected', | ||
line: 3, | ||
column: 27, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const Component = ( | ||
<div> | ||
<p /> | ||
</div> | ||
) | ||
const Component = ( | ||
<div | ||
prop={true} | ||
/> | ||
) | ||
`, | ||
options: ['all', { ignoreJSX: 'single-line' }], | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 2, | ||
column: 27, | ||
}, | ||
{ | ||
messageId: 'unexpected', | ||
line: 7, | ||
column: 27, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `((function foo() {}))();`, | ||
options: ['functions'], | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 2, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `var y = (function () {return 1;});`, | ||
options: ['functions'], | ||
errors: [ | ||
{ | ||
messageId: 'unexpected', | ||
line: 1, | ||
column: 9, | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters