4 * Copyright 2013 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var tsutils_1 = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var OPTION_ALLOW_NULL_CHECK = "allow-null-check";
24 var OPTION_ALLOW_UNDEFINED_CHECK = "allow-undefined-check";
25 var Rule = /** @class */ (function (_super) {
26 tslib_1.__extends(Rule, _super);
28 return _super !== null && _super.apply(this, arguments) || this;
30 Rule.prototype.apply = function (sourceFile) {
31 return this.applyWithFunction(sourceFile, walk, {
32 allowNull: this.ruleArguments.indexOf(OPTION_ALLOW_NULL_CHECK) !== -1,
33 allowUndefined: this.ruleArguments.indexOf(OPTION_ALLOW_UNDEFINED_CHECK) !== -1,
36 /* tslint:disable:object-literal-sort-keys */
38 ruleName: "triple-equals",
39 description: "Requires `===` and `!==` in place of `==` and `!=`.",
40 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Two arguments may be optionally provided:\n\n * `\"allow-null-check\"` allows `==` and `!=` when comparing to `null`.\n * `\"allow-undefined-check\"` allows `==` and `!=` when comparing to `undefined`."], ["\n Two arguments may be optionally provided:\n\n * \\`\"allow-null-check\"\\` allows \\`==\\` and \\`!=\\` when comparing to \\`null\\`.\n * \\`\"allow-undefined-check\"\\` allows \\`==\\` and \\`!=\\` when comparing to \\`undefined\\`."]))),
45 enum: [OPTION_ALLOW_NULL_CHECK, OPTION_ALLOW_UNDEFINED_CHECK],
50 optionExamples: [true, [true, "allow-null-check"], [true, "allow-undefined-check"]],
51 type: "functionality",
52 typescriptOnly: false,
54 /* tslint:enable:object-literal-sort-keys */
55 Rule.EQ_FAILURE_STRING = "== should be ===";
56 Rule.NEQ_FAILURE_STRING = "!= should be !==";
58 }(Lint.Rules.AbstractRule));
61 return ts.forEachChild(ctx.sourceFile, function cb(node) {
62 if (tsutils_1.isBinaryExpression(node)) {
63 if ((node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken ||
64 node.operatorToken.kind === ts.SyntaxKind.ExclamationEqualsToken) &&
65 !(isExpressionAllowed(node.right, ctx.options) ||
66 isExpressionAllowed(node.left, ctx.options))) {
67 ctx.addFailureAtNode(node.operatorToken, node.operatorToken.kind === ts.SyntaxKind.EqualsEqualsToken
68 ? Rule.EQ_FAILURE_STRING
69 : Rule.NEQ_FAILURE_STRING);
72 return ts.forEachChild(node, cb);
75 function isExpressionAllowed(node, options) {
76 if (node.kind === ts.SyntaxKind.NullKeyword) {
77 return options.allowNull;
79 return (options.allowUndefined &&
80 node.kind === ts.SyntaxKind.Identifier &&
81 node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword);