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): [no-unused-expressions] extend for optional chai…
…ning (#1175)
- Loading branch information
1 parent
026ceb9
commit 57d63b7
Showing
7 changed files
with
264 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
25 changes: 25 additions & 0 deletions
25
packages/eslint-plugin/docs/rules/no-unused-expressions.md
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,25 @@ | ||
# require or disallow semicolons instead of ASI (semi) | ||
|
||
This rule aims to eliminate unused expressions which have no effect on the state of the program. | ||
|
||
## Rule Details | ||
|
||
This rule extends the base [eslint/no-unused-expressions](https://eslint.org/docs/rules/no-unused-expressions) rule. | ||
It supports all options and features of the base rule. | ||
This version adds support for numerous typescript features. | ||
|
||
## How to use | ||
|
||
```cjson | ||
{ | ||
// note you must disable the base rule as it can report incorrect errors | ||
"no-unused-expressions": "off", | ||
"@typescript-eslint/no-unused-expressions": ["error"] | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
See [eslint/no-unused-expressions options](https://eslint.org/docs/rules/no-unused-expressions#options). | ||
|
||
<sup>Taken with ❤️ [from ESLint core](https://github.com/eslint/eslint/blob/master/docs/rules/no-unused-expressions.md)</sup> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; | ||
import baseRule from 'eslint/lib/rules/no-unused-expressions'; | ||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'no-unused-expressions', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow unused expressions', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
schema: baseRule.meta.schema, | ||
messages: { | ||
expected: | ||
'Expected an assignment or function call and instead saw an expression.', | ||
}, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const rules = baseRule.create(context); | ||
|
||
return { | ||
ExpressionStatement(node): void { | ||
if (node.expression.type === AST_NODE_TYPES.OptionalCallExpression) { | ||
return; | ||
} | ||
rules.ExpressionStatement(node); | ||
}, | ||
}; | ||
}, | ||
}); |
180 changes: 180 additions & 0 deletions
180
packages/eslint-plugin/tests/rules/no-unused-expressions.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,180 @@ | ||
import rule from '../../src/rules/no-unused-expressions'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: 'module', | ||
ecmaFeatures: {}, | ||
}, | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
// the base rule doesn't have messageIds | ||
function error( | ||
messages: { line: number; column: number }[], | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
): any[] { | ||
return messages.map(message => ({ | ||
...message, | ||
message: | ||
'Expected an assignment or function call and instead saw an expression.', | ||
})); | ||
} | ||
|
||
ruleTester.run('no-unused-expressions', rule, { | ||
valid: [ | ||
` | ||
test.age?.toLocaleString(); | ||
`, | ||
` | ||
let a = (a?.b).c; | ||
`, | ||
` | ||
let b = a?.['b']; | ||
`, | ||
` | ||
let c = one[2]?.[3][4]; | ||
`, | ||
` | ||
one[2]?.[3][4]?.(); | ||
`, | ||
` | ||
a?.['b']?.c(); | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
if(0) 0 | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 7, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
f(0), {} | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
a, b() | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
a() && function namedFunctionInExpressionContext () {f();} | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
a?.b | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
(a?.b).c | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
a?.['b'] | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
(a?.['b']).c | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
a?.b()?.c | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
(a?.b()).c | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
one[2]?.[3][4]; | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
{ | ||
code: ` | ||
one.two?.three.four; | ||
`, | ||
errors: error([ | ||
{ | ||
line: 2, | ||
column: 1, | ||
}, | ||
]), | ||
}, | ||
], | ||
}); |
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