This repository was archived by the owner on Jan 19, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +113
-2
lines changed Expand file tree Collapse file tree 3 files changed +113
-2
lines changed Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 18
18
"requireindex" : " ~1.1.0"
19
19
},
20
20
"devDependencies" : {
21
- "eslint" : " ~3.0.0 " ,
21
+ "eslint" : " git+https://github.com/eslint/eslint.git#7695828dc0472e61c58298bfa3f0c07418c2edac " ,
22
22
"eslint-config-eslint" : " ^3.0.0" ,
23
23
"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 "
25
25
},
26
26
"engines" : {
27
27
"node" : " >=4"
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments