UNPKG

6.22 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const ts = __importStar(require("typescript"));
23const util = __importStar(require("../util"));
24exports.default = util.createRule({
25 name: 'restrict-plus-operands',
26 meta: {
27 type: 'problem',
28 docs: {
29 description: 'When adding two variables, operands must both be of type number or of type string',
30 recommended: 'error',
31 requiresTypeChecking: true,
32 },
33 messages: {
34 notNumbers: "Operands of '+' operation must either be both strings or both numbers.",
35 notStrings: "Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.",
36 notBigInts: "Operands of '+' operation must be both bigints.",
37 notValidAnys: "Operands of '+' operation with any is possible only with string, number, bigint or any",
38 notValidTypes: "Operands of '+' operation must either be one of string, number, bigint or any (if allowed by option)",
39 },
40 schema: [
41 {
42 type: 'object',
43 additionalProperties: false,
44 properties: {
45 checkCompoundAssignments: {
46 type: 'boolean',
47 },
48 allowAny: {
49 type: 'boolean',
50 },
51 },
52 },
53 ],
54 },
55 defaultOptions: [
56 {
57 checkCompoundAssignments: false,
58 allowAny: false,
59 },
60 ],
61 create(context, [{ checkCompoundAssignments, allowAny }]) {
62 const service = util.getParserServices(context);
63 const typeChecker = service.program.getTypeChecker();
64 /**
65 * Helper function to get base type of node
66 */
67 function getBaseTypeOfLiteralType(type) {
68 if (type.isNumberLiteral()) {
69 return 'number';
70 }
71 if (type.isStringLiteral() ||
72 util.isTypeFlagSet(type, ts.TypeFlags.TemplateLiteral)) {
73 return 'string';
74 }
75 // is BigIntLiteral
76 if (type.flags & ts.TypeFlags.BigIntLiteral) {
77 return 'bigint';
78 }
79 if (type.isUnion()) {
80 const types = type.types.map(getBaseTypeOfLiteralType);
81 return types.every(value => value === types[0]) ? types[0] : 'invalid';
82 }
83 if (type.isIntersection()) {
84 const types = type.types.map(getBaseTypeOfLiteralType);
85 return types.some(value => value === 'string') ? 'string' : 'invalid';
86 }
87 const stringType = typeChecker.typeToString(type);
88 if (stringType === 'number' ||
89 stringType === 'string' ||
90 stringType === 'bigint' ||
91 stringType === 'any') {
92 return stringType;
93 }
94 return 'invalid';
95 }
96 /**
97 * Helper function to get base type of node
98 * @param node the node to be evaluated.
99 */
100 function getNodeType(node) {
101 const tsNode = service.esTreeNodeToTSNodeMap.get(node);
102 const type = util.getConstrainedTypeAtLocation(typeChecker, tsNode);
103 return getBaseTypeOfLiteralType(type);
104 }
105 function checkPlusOperands(node) {
106 const leftType = getNodeType(node.left);
107 const rightType = getNodeType(node.right);
108 if (leftType === rightType) {
109 if (leftType === 'invalid') {
110 context.report({
111 node,
112 messageId: 'notValidTypes',
113 });
114 }
115 if (!allowAny && leftType === 'any') {
116 context.report({
117 node,
118 messageId: 'notValidAnys',
119 });
120 }
121 return;
122 }
123 if (leftType === 'any' || rightType === 'any') {
124 if (!allowAny || leftType === 'invalid' || rightType === 'invalid') {
125 context.report({
126 node,
127 messageId: 'notValidAnys',
128 });
129 }
130 return;
131 }
132 if (leftType === 'string' || rightType === 'string') {
133 return context.report({
134 node,
135 messageId: 'notStrings',
136 });
137 }
138 if (leftType === 'bigint' || rightType === 'bigint') {
139 return context.report({
140 node,
141 messageId: 'notBigInts',
142 });
143 }
144 if (leftType === 'number' || rightType === 'number') {
145 return context.report({
146 node,
147 messageId: 'notNumbers',
148 });
149 }
150 }
151 return Object.assign({ "BinaryExpression[operator='+']": checkPlusOperands }, (checkCompoundAssignments && {
152 "AssignmentExpression[operator='+=']"(node) {
153 checkPlusOperands(node);
154 },
155 }));
156 },
157});
158//# sourceMappingURL=restrict-plus-operands.js.map
\No newline at end of file