-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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): add new rule restrict-plus-operands (#70)
- Loading branch information
1 parent
4e781f1
commit c541ede
Showing
5 changed files
with
421 additions
and
0 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/restrict-plus-operands.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 @@ | ||
# 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 deletions
104
packages/eslint-plugin/lib/rules/restrict-plus-operands.js
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,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' | ||
}); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es5", | ||
"module": "commonjs", | ||
"strict": true, | ||
"esModuleInterop": true, | ||
"lib": ["es2015", "es2017"] | ||
} | ||
} |
Oops, something went wrong.