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

Commit 8e2b22f

Browse files
committed
WIP
1 parent cec0d0e commit 8e2b22f

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed

lib/rules/restrict-plus-operands.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @fileoverview When adding two variables, operands must both be of type number or of type string.
3+
* @author James Henry
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Rule Definition
9+
//------------------------------------------------------------------------------
10+
11+
module.exports = {
12+
meta: {
13+
docs: {
14+
description: "When adding two variables, operands must both be of type number or of type string.",
15+
category: "TypeScript"
16+
},
17+
schema: []
18+
},
19+
20+
create: function(context) {
21+
22+
//----------------------------------------------------------------------
23+
// Public
24+
//----------------------------------------------------------------------
25+
return {
26+
BinaryExpression(node) {
27+
28+
var ts = context.parserServices.ts;
29+
var tsNode = context.parserServices.getTSNode(node);
30+
31+
if (tsNode.operatorToken.kind === ts.SyntaxKind.PlusToken) {
32+
33+
var program = context.parserServices.getCurrentProgram();
34+
var tc = program.getTypeChecker();
35+
var leftType = tc.typeToString(tc.getTypeAtLocation(tsNode.left));
36+
var rightType = tc.typeToString(tc.getTypeAtLocation(tsNode.right));
37+
38+
if (leftType !== rightType) {
39+
context.report({
40+
node: node,
41+
message: "Cannot add a '" + leftType + "' to a '" + rightType + "'."
42+
});
43+
return;
44+
}
45+
46+
if (leftType !== "number" && leftType !== "string") {
47+
context.report({
48+
node: node,
49+
message: "'" + leftType + "' is not a supported type for addition."
50+
});
51+
return;
52+
}
53+
54+
}
55+
}
56+
};
57+
}
58+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
"requireindex": "~1.1.0"
1919
},
2020
"devDependencies": {
21-
"eslint": "~3.0.0",
21+
"eslint": "git+https://github.com/eslint/eslint.git#7695828dc0472e61c58298bfa3f0c07418c2edac",
2222
"eslint-config-eslint": "^3.0.0",
2323
"mocha": "^2.4.5",
24-
"typescript-eslint-parser": "^0.4.0"
24+
"typescript-eslint-parser": "git+https://github.com/eslint/typescript-eslint-parser.git#79983ec86a54898dd8cdaa6d037f3f30e9822b6d"
2525
},
2626
"engines": {
2727
"node": ">=4"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @fileoverview When adding two variables, operands must both be of type number or of type string.
3+
* @author James Henry
4+
*/
5+
"use strict";
6+
7+
//------------------------------------------------------------------------------
8+
// Requirements
9+
//------------------------------------------------------------------------------
10+
11+
var rule = require("../../../lib/rules/restrict-plus-operands"),
12+
RuleTester = require("eslint").RuleTester;
13+
14+
15+
//------------------------------------------------------------------------------
16+
// Tests
17+
//------------------------------------------------------------------------------
18+
19+
var ruleTester = new RuleTester();
20+
ruleTester.run("restrict-plus-operands", rule, {
21+
22+
valid: [
23+
{
24+
code: "var foo = 1 + 1;",
25+
parser: "typescript-eslint-parser"
26+
},
27+
{
28+
code: "var foo = '1' + '1';",
29+
parser: "typescript-eslint-parser"
30+
},
31+
],
32+
33+
invalid: [
34+
{
35+
code: "var foo = '1' + 1;",
36+
parser: "typescript-eslint-parser",
37+
errors: [{
38+
message: "Cannot add a 'string' to a 'number'.",
39+
line: 1,
40+
column: 11
41+
}]
42+
},
43+
{
44+
code: "var foo = [] + {};",
45+
parser: "typescript-eslint-parser",
46+
errors: [{
47+
message: "'{}' is not a supported type for addition.",
48+
line: 1,
49+
column: 11
50+
}]
51+
}
52+
]
53+
});

0 commit comments

Comments
 (0)