4 * Copyright 2018 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 oneVariablePerDeclaration_examples_1 = require("./code-examples/oneVariablePerDeclaration.examples");
24 var OPTION_IGNORE_FOR_LOOP = "ignore-for-loop";
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 ignoreForLoop: this.ruleArguments.indexOf(OPTION_IGNORE_FOR_LOOP) !== -1,
35 /* tslint:disable:object-literal-sort-keys */
37 ruleName: "one-variable-per-declaration",
38 description: "Disallows multiple variable definitions in the same declaration statement.",
39 optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One argument may be optionally provided:\n\n * `", "` allows multiple variable definitions in a for loop declaration."], ["\n One argument may be optionally provided:\n\n * \\`", "\\` allows multiple variable definitions in a for loop declaration."])), OPTION_IGNORE_FOR_LOOP),
44 enum: [OPTION_IGNORE_FOR_LOOP],
49 optionExamples: [true, [true, OPTION_IGNORE_FOR_LOOP]],
51 typescriptOnly: false,
52 codeExamples: oneVariablePerDeclaration_examples_1.codeExamples,
54 /* tslint:enable:object-literal-sort-keys */
55 Rule.FAILURE_STRING = "Multiple variable declarations in the same statement are forbidden";
57 }(Lint.Rules.AbstractRule));
60 ts.forEachChild(ctx.sourceFile, function cb(node) {
61 if (tsutils_1.isVariableStatement(node) && node.declarationList.declarations.length > 1) {
62 ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
64 else if (tsutils_1.isForStatement(node) && !ctx.options.ignoreForLoop) {
65 var initializer = node.initializer;
66 if (initializer !== undefined &&
67 initializer.kind === ts.SyntaxKind.VariableDeclarationList &&
68 initializer.declarations.length > 1) {
69 ctx.addFailureAtNode(initializer, Rule.FAILURE_STRING);
72 ts.forEachChild(node, cb);