-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): add no-unnecessary-type-assertion rule #157
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
uniqueiniquity
merged 8 commits into
typescript-eslint:master
from
uniqueiniquity:noUnnecAssert
Jan 29, 2019
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c32a72b
feat(eslint-plugin): add no-unnecessary-type-assertion rule
5d540b2
test: test couldBeTupleType
1fd19b6
test: convert to messageId
f4e471b
fix: use object for options instead of array
f65330a
chore: fix trimmed spaces
d3bdc23
fix: respond to CR
c3b8f34
test: improve code coverage
eebc187
Merge branch 'master' into noUnnecAssert
JamesHenry 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
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
57 changes: 57 additions & 0 deletions
57
packages/eslint-plugin/docs/rules/no-unnecessary-type-assertion.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,57 @@ | ||
# Warns if a type assertion does not change the type of an expression (no-unnecessary-type-assertion) | ||
|
||
This rule prohibits using a type assertion that does not change the type of an expression. | ||
|
||
## Rule Details | ||
|
||
This rule aims to prevent unnecessary type assertions. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
const foo = 3; | ||
const bar = foo!; | ||
``` | ||
|
||
```ts | ||
const foo = <3>3; | ||
``` | ||
|
||
```ts | ||
type Foo = 3; | ||
const foo = <Foo>3; | ||
``` | ||
|
||
```ts | ||
type Foo = 3; | ||
const foo = 3 as Foo; | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
const foo = <number>3; | ||
``` | ||
|
||
```ts | ||
const foo = 3 as number; | ||
``` | ||
|
||
### Options | ||
|
||
This rule optionally takes an object with a single property `typesToIgnore`, which can be set to a list of type names to ignore. | ||
|
||
For example, with `@typescript-eslint/no-unnecessary-type-assertion: ["error", { typesToIgnore: ['Foo'] }]`, the following is **correct** code": | ||
|
||
```ts | ||
type Foo = 3; | ||
const foo: Foo = 3; | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about having no-op type assertions in your code, then you can turn off this rule. | ||
|
||
## Related to | ||
|
||
- TSLint: ['no-unnecessary-type-assertion`](https://palantir.github.io/tslint/rules/no-unnecessary-type-assertion/) |
179 changes: 179 additions & 0 deletions
179
packages/eslint-plugin/lib/rules/no-unnecessary-type-assertion.js
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,179 @@ | ||
/** | ||
* @fileoverview Rule to warn if a type assertion does not change the type of an expression | ||
* @author Benjamin Lichtman | ||
*/ | ||
|
||
'use strict'; | ||
const tsutils = require('tsutils'); | ||
const ts = require('typescript'); | ||
const util = require('../util'); | ||
|
||
/** @typedef {import("estree").Node} Node */ | ||
/** @typedef {import("eslint").Rule.RuleContext} Context */ | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
/** | ||
* Sometimes tuple types don't have ObjectFlags.Tuple set, like when they're being matched against an inferred type. | ||
* So, in addition, check if there are integer properties 0..n and no other numeric keys | ||
* @param {ts.ObjectType} type type | ||
* @returns {boolean} true if type could be a tuple type | ||
*/ | ||
function couldBeTupleType(type) { | ||
const properties = type.getProperties(); | ||
|
||
if (properties.length === 0) { | ||
return false; | ||
} | ||
let i = 0; | ||
|
||
for (; i < properties.length; ++i) { | ||
const name = properties[i].name; | ||
|
||
if (String(i) !== name) { | ||
if (i === 0) { | ||
// if there are no integer properties, this is not a tuple | ||
return false; | ||
} | ||
break; | ||
} | ||
} | ||
for (; i < properties.length; ++i) { | ||
if (String(+properties[i].name) === properties[i].name) { | ||
return false; // if there are any other numeric properties, this is not a tuple | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* | ||
* @param {Node} node node being linted | ||
* @param {Context} context linting context | ||
* @param {ts.TypeChecker} checker TypeScript typechecker | ||
* @returns {void} | ||
*/ | ||
function checkNonNullAssertion(node, context, checker) { | ||
/** | ||
* Corresponding TSNode is guaranteed to be in map | ||
* @type {ts.NonNullExpression} | ||
*/ | ||
const originalNode = context.parserServices.esTreeNodeToTSNodeMap.get(node); | ||
const type = checker.getTypeAtLocation(originalNode.expression); | ||
|
||
if (type === checker.getNonNullableType(type)) { | ||
context.report({ | ||
node, | ||
messageId: 'unnecessaryAssertion', | ||
fix(fixer) { | ||
return fixer.removeRange([ | ||
originalNode.expression.end, | ||
originalNode.end | ||
]); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param {Node} node node being linted | ||
* @param {Context} context linting context | ||
* @param {ts.TypeChecker} checker TypeScript typechecker | ||
* @returns {void} | ||
*/ | ||
function verifyCast(node, context, checker) { | ||
/** | ||
* * Corresponding TSNode is guaranteed to be in map | ||
* @type {ts.AssertionExpression} | ||
*/ | ||
const originalNode = context.parserServices.esTreeNodeToTSNodeMap.get(node); | ||
const options = context.options[0]; | ||
|
||
if ( | ||
options && | ||
options.typesToIgnore && | ||
options.typesToIgnore.indexOf(originalNode.type.getText()) !== -1 | ||
) { | ||
return; | ||
} | ||
const castType = checker.getTypeAtLocation(originalNode); | ||
|
||
if ( | ||
tsutils.isTypeFlagSet(castType, ts.TypeFlags.Literal) || | ||
(tsutils.isObjectType(castType) && | ||
(tsutils.isObjectFlagSet(castType, ts.ObjectFlags.Tuple) || | ||
couldBeTupleType(castType))) | ||
) { | ||
// It's not always safe to remove a cast to a literal type or tuple | ||
// type, as those types are sometimes widened without the cast. | ||
return; | ||
} | ||
|
||
const uncastType = checker.getTypeAtLocation(originalNode.expression); | ||
|
||
if (uncastType === castType) { | ||
context.report({ | ||
node, | ||
messageId: 'unnecessaryAssertion', | ||
fix(fixer) { | ||
return originalNode.kind === ts.SyntaxKind.TypeAssertionExpression | ||
? fixer.removeRange([ | ||
originalNode.getStart(), | ||
originalNode.expression.getStart() | ||
]) | ||
: fixer.removeRange([originalNode.expression.end, originalNode.end]); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** @type {import("eslint").Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: | ||
'Warns if a type assertion does not change the type of an expression', | ||
category: 'TypeScript-specific', | ||
recommended: false, | ||
extraDescription: [util.tslintRule('no-unnecessary-type-assertion')], | ||
url: util.metaDocsUrl('no-unnecessary-type-assertion') | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
unnecessaryAssertion: | ||
'This assertion is unnecessary since it does not change the type of the expression.' | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
typesToIgnore: { | ||
type: 'array', | ||
items: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
], | ||
armano2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type: 'suggestion' | ||
}, | ||
|
||
create(context) { | ||
const checker = util.getParserServices(context).program.getTypeChecker(); | ||
|
||
return { | ||
TSNonNullExpression(node) { | ||
checkNonNullAssertion(node, context, checker); | ||
}, | ||
TSTypeAssertion(node) { | ||
verifyCast(node, context, checker); | ||
}, | ||
TSAsExpression(node) { | ||
verifyCast(node, context, checker); | ||
} | ||
}; | ||
} | ||
}; |
122 changes: 122 additions & 0 deletions
122
packages/eslint-plugin/tests/lib/rules/no-unnecessary-type-assertion.js
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,122 @@ | ||
/** | ||
* @fileoverview Warns if a type assertion does not change the type of an expression. | ||
* @author Benjamin Lichtman | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-unnecessary-type-assertion'), | ||
RuleTester = require('eslint').RuleTester, | ||
path = require('path'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const rootDir = path.join(process.cwd(), 'tests/fixtures'); | ||
const parserOptions = { | ||
ecmaVersion: 2015, | ||
tsconfigRootDir: rootDir, | ||
project: './tsconfig.json' | ||
}; | ||
const ruleTester = new RuleTester({ | ||
parserOptions, | ||
parser: '@typescript-eslint/parser' | ||
}); | ||
|
||
ruleTester.run('no-unnecessary-type-assertion', rule, { | ||
valid: [ | ||
armano2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'const foo = 3 as number;', | ||
'const foo = <number> 3;', | ||
'const foo = <3>3;', | ||
'const foo = 3 as 3;', | ||
` | ||
type Tuple = [3, "hi", "bye"]; | ||
const foo = ([3, "hi", "bye"]) as Tuple;`, | ||
` | ||
type PossibleTuple = {}; | ||
const foo = ({}) as PossibleTuple;`, | ||
` | ||
type PossibleTuple = { hello: "hello" }; | ||
const foo = ({ hello: "hello" }) as PossibleTuple;`, | ||
` | ||
type PossibleTuple = { 0: "hello", 5: "hello" }; | ||
const foo = ({ 0: "hello", 5: "hello" }) as PossibleTuple;`, | ||
{ | ||
code: ` | ||
type Foo = number; | ||
const foo = (3 + 5) as Foo;`, | ||
options: [{ typesToIgnore: ['Foo'] }] | ||
}, | ||
{ | ||
code: ` | ||
type Foo = number; | ||
const foo = <Foo>(3 + 5);`, | ||
options: [{ typesToIgnore: ['Foo'] }] | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
const foo = 3; | ||
const bar = foo!;`, | ||
errors: [ | ||
{ | ||
messageId: 'unnecessaryAssertion', | ||
line: 3, | ||
column: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
const foo = (3 + 5) as number;`, | ||
errors: [ | ||
{ | ||
messageId: 'unnecessaryAssertion', | ||
line: 2, | ||
column: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
const foo = <number>(3 + 5);`, | ||
errors: [ | ||
{ | ||
messageId: 'unnecessaryAssertion', | ||
line: 2, | ||
column: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
type Foo = number; | ||
const foo = (3 + 5) as Foo;`, | ||
errors: [ | ||
{ | ||
messageId: 'unnecessaryAssertion', | ||
line: 3, | ||
column: 13 | ||
} | ||
] | ||
}, | ||
{ | ||
code: ` | ||
type Foo = number; | ||
const foo = <Foo>(3 + 5);`, | ||
errors: [ | ||
{ | ||
messageId: 'unnecessaryAssertion', | ||
line: 3, | ||
column: 13 | ||
} | ||
] | ||
} | ||
] | ||
}); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@uniqueiniquity @JamesHenry Upgraded to latest canary (
1.1.2-alpha.4
) to work around #161.Which then triggered
Had to add
tsutils
topackage.json
. Is that expected ?Maybe a slight addition to the README would be helpful regarding this. Or even better, add
tsutils
as a dependency of that plugin.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nulltoken My mistake. I'm not sure how I missed adding it to the package.json and it still managed to pass the tests...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I get it;
tslint
is being installed as a dev dependency, and that happens to pull intsutils
so it works in a dev scenario. I'll get a fix up right now.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍