oneVariablePerDeclarationRule.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2016 Palantir Technologies, Inc.
  5. *
  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
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  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.
  17. */
  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_IGNORE_FOR_LOOP = "ignore-for-loop";
  24. var Rule = /** @class */ (function (_super) {
  25. tslib_1.__extends(Rule, _super);
  26. function Rule() {
  27. return _super !== null && _super.apply(this, arguments) || this;
  28. }
  29. Rule.prototype.apply = function (sourceFile) {
  30. return this.applyWithFunction(sourceFile, walk, { ignoreForLoop: this.ruleArguments.indexOf(OPTION_IGNORE_FOR_LOOP) !== -1 });
  31. };
  32. /* tslint:disable:object-literal-sort-keys */
  33. Rule.metadata = {
  34. ruleName: "one-variable-per-declaration",
  35. description: "Disallows multiple variable definitions in the same declaration statement.",
  36. 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),
  37. options: {
  38. type: "array",
  39. items: {
  40. type: "string",
  41. enum: [OPTION_IGNORE_FOR_LOOP],
  42. },
  43. minLength: 0,
  44. maxLength: 1,
  45. },
  46. optionExamples: [true, [true, OPTION_IGNORE_FOR_LOOP]],
  47. type: "style",
  48. typescriptOnly: false,
  49. };
  50. /* tslint:enable:object-literal-sort-keys */
  51. Rule.FAILURE_STRING = "Multiple variable declarations in the same statement are forbidden";
  52. return Rule;
  53. }(Lint.Rules.AbstractRule));
  54. exports.Rule = Rule;
  55. function walk(ctx) {
  56. ts.forEachChild(ctx.sourceFile, function cb(node) {
  57. if (tsutils_1.isVariableStatement(node) && node.declarationList.declarations.length > 1) {
  58. ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
  59. }
  60. else if (tsutils_1.isForStatement(node) && !ctx.options.ignoreForLoop) {
  61. var initializer = node.initializer;
  62. if (initializer !== undefined
  63. && initializer.kind === ts.SyntaxKind.VariableDeclarationList
  64. && initializer.declarations.length > 1) {
  65. ctx.addFailureAtNode(initializer, Rule.FAILURE_STRING);
  66. }
  67. }
  68. ts.forEachChild(node, cb);
  69. });
  70. }
  71. var templateObject_1;