-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-unused-expressions] extend for optional chaining #1175
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
Merged
bradzacher
merged 11 commits into
typescript-eslint:master
from
Nizarius:no-unused-expressions
Nov 11, 2019
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ecdb016
fix(parser): optional member expression
6209a43
test: added test for no-use-before-define
67dcf3c
Merge branch 'master' into master
Nizarius 27e84d9
style: added OptionalMemberExpression description
f51b448
feat: added tests and optional chaining expression to referencer
f5eedb4
fix: no-unused-expressions-rule for optional call expression
d740175
fix: make no-unused-expressions rule based on eslint rule
daa4668
docs: update tests and docs
c876e7e
fix: removed dublicating code
adf89cb
Merge branch 'master' into no-unused-expressions
bradzacher b05c365
docs: add to readme
bradzacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.