Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add new rule restrict-plus-operands#70
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
JamesHenry merged 9 commits intotypescript-eslint:masterfromarmano2:restrict-plus-operandsJan 22, 2019
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
9 commits Select commitHold shift + click to select a range
f00d78d
feat(eslint-plugin): add new rule restrict-plus-operands
armano21340865
docs(eslint-plugin): update wrong parser package name
armano2b891985
docs(eslint-plugin): update format
armano286affe6
Merge branch 'master' into restrict-plus-operands
armano201e04f2
Merge branch 'master' into restrict-plus-operands
armano2464f07e
Merge branch 'master' into restrict-plus-operands
armano24886250
Merge branch 'master' into restrict-plus-operands
armano272fd4cc
Merge branch 'master' into restrict-plus-operands
JamesHenry3a22072
Merge branch 'master' into restrict-plus-operands
JamesHenryFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.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
25 changes: 25 additions & 0 deletionspackages/eslint-plugin/docs/rules/restrict-plus-operands.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 @@ | ||
# When adding two variables, operands must both be of type number or of type string. (restrict-plus-operands) | ||
Examples of **correct** code: | ||
```ts | ||
var foo = parseInt('5.5', 10) + 10; | ||
``` | ||
Examples of **incorrect** code: | ||
```ts | ||
var foo = '5.5' + 5; | ||
``` | ||
## Options | ||
```json | ||
{ | ||
"@typescript-eslint/restrict-plus-operands": "error" | ||
} | ||
``` | ||
## Compatibility | ||
- TSLint: [restrict-plus-operands](https://palantir.github.io/tslint/rules/restrict-plus-operands/) |
104 changes: 104 additions & 0 deletionspackages/eslint-plugin/lib/rules/restrict-plus-operands.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,104 @@ | ||
/** | ||
* @fileoverview When adding two variables, operands must both be of type number or of type string. | ||
* @author James Henry | ||
* @author Armano <https://github.com/armano2> | ||
*/ | ||
'use strict'; | ||
const util = require('../util'); | ||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'When adding two variables, operands must both be of type number or of type string.', | ||
extraDescription: [util.tslintRule('restrict-plus-operands')], | ||
category: 'TypeScript', | ||
url: util.metaDocsUrl('restrict-plus-operands') | ||
}, | ||
messages: { | ||
notNumbers: | ||
"Operands of '+' operation must either be both strings or both numbers.", | ||
notStrings: | ||
"Operands of '+' operation must either be both strings or both numbers. Consider using a template literal." | ||
}, | ||
schema: [] | ||
}, | ||
create(context) { | ||
const service = util.getParserServices(context); | ||
const typeChecker = service.program.getTypeChecker(); | ||
/** | ||
* Helper function to get base type of node | ||
* @param {ts.Type} type type to be evaluated | ||
* @returns {*} string, number or invalid | ||
*/ | ||
function getBaseTypeOfLiteralType(type) { | ||
if (type.isNumberLiteral()) { | ||
return 'number'; | ||
} | ||
if (type.isStringLiteral()) { | ||
return 'string'; | ||
} | ||
if (type.isUnion()) { | ||
const types = type.types.map(getBaseTypeOfLiteralType); | ||
return types.every(value => value === types[0]) ? types[0] : 'invalid'; | ||
} | ||
const stringType = typeChecker.typeToString(type); | ||
if (stringType === 'number' || stringType === 'string') { | ||
return stringType; | ||
} | ||
return 'invalid'; | ||
} | ||
/** | ||
* Helper function to get base type of node | ||
* @param {ASTNode} node the node to be evaluated. | ||
* @returns {*} string, number or invalid | ||
*/ | ||
function getNodeType(node) { | ||
const tsNode = service.esTreeNodeToTSNodeMap.get(node); | ||
const type = typeChecker.getTypeAtLocation(tsNode); | ||
return getBaseTypeOfLiteralType(type); | ||
} | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
"BinaryExpression[operator='+']"(node) { | ||
const leftType = getNodeType(node.left); | ||
const rightType = getNodeType(node.right); | ||
if ( | ||
leftType === 'invalid' || | ||
rightType === 'invalid' || | ||
leftType !== rightType | ||
) { | ||
if (leftType === 'string' || rightType === 'string') { | ||
context.report({ | ||
node, | ||
messageId: 'notStrings' | ||
}); | ||
} else { | ||
context.report({ | ||
node, | ||
messageId: 'notNumbers' | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
9 changes: 9 additions & 0 deletionspackages/eslint-plugin/tests/fixtures/tsconfig.json
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"lib": ["es2015", "es2017"] | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.