Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

[FEAT] [restrict-plus-operands] Add rule (Requires Type Info) #230

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ This guarantees 100% compatibility between the plugin and the parser.
| [`typescript/no-var-requires`](./docs/rules/no-var-requires.md) | Disallows the use of require statements except in import statements (`no-var-requires` from TSLint) | | |
| [`typescript/prefer-interface`](./docs/rules/prefer-interface.md) | Prefer an interface declaration over a type literal (type T = { ... }) (`interface-over-type-literal` from TSLint) | | :wrench: |
| [`typescript/prefer-namespace-keyword`](./docs/rules/prefer-namespace-keyword.md) | Require the use of the `namespace` keyword instead of the `module` keyword to declare custom TypeScript modules. (`no-internal-module` from TSLint) | | :wrench: |
| [`typescript/restrict-plus-operands`](./docs/rules/restrict-plus-operands.md) | When adding two variables, operands must both be of type number or of type string. (`restrict-plus-operands` from TSLint) | | |
| [`typescript/type-annotation-spacing`](./docs/rules/type-annotation-spacing.md) | Require consistent spacing around type annotations (`typedef-whitespace` from TSLint) | | :wrench: |

<!-- end rule list -->
1 change: 1 addition & 0 deletions docs/rules/restrict-plus-operands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# When adding two variables, operands must both be of type number or of type string. (restrict-plus-operands)
113 changes: 113 additions & 0 deletions lib/rules/restrict-plus-operands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* @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 template literals.",
},
schema: [],
},

create(context) {
const typeChecker =
context.parserServices &&
context.parserServices.program &&
context.parserServices.program.getTypeChecker();

const TSNodeMap =
context.parserServices &&
context.parserServices.esTreeNodeToTSNodeMap;

/**
* 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 = TSNodeMap.get(node);
const type = typeChecker.getTypeAtLocation(tsNode);

return getBaseTypeOfLiteralType(type);
}

//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
"BinaryExpression[operator='+']"(node) {
if (typeChecker && TSNodeMap) {
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",
});
}
}
}
},
};
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"dependencies": {
"requireindex": "^1.2.0",
"typescript-eslint-parser": "21.0.2"
"typescript-eslint-parser": "git+https://github.com/uniqueiniquity/typescript-eslint-parser.git#0625f29a9721c6f10a7522de6c9d236a671bd1ac"
},
"devDependencies": {
"eslint": "^5.9.0",
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions tests/lib/fixtures/empty/tsconfig.json
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"]
}
}
Loading