noUnnecessaryInitializerRule.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2017 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 Rule = /** @class */ (function (_super) {
  24. tslib_1.__extends(Rule, _super);
  25. function Rule() {
  26. return _super !== null && _super.apply(this, arguments) || this;
  27. }
  28. Rule.prototype.apply = function (sourceFile) {
  29. return this.applyWithFunction(sourceFile, walk);
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "no-unnecessary-initializer",
  34. description: "Forbids a 'var'/'let' statement or destructuring initializer to be initialized to 'undefined'.",
  35. hasFix: true,
  36. optionsDescription: "Not configurable.",
  37. options: null,
  38. optionExamples: [true],
  39. rationale: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Values in JavaScript default to `undefined`.\n There's no need to do so manually.\n "], ["\n Values in JavaScript default to \\`undefined\\`.\n There's no need to do so manually.\n "]))),
  40. type: "style",
  41. typescriptOnly: false,
  42. };
  43. /* tslint:enable:object-literal-sort-keys */
  44. Rule.FAILURE_STRING = "Unnecessary initialization to 'undefined'.";
  45. Rule.FAILURE_STRING_PARAMETER = "Use an optional parameter instead of initializing to 'undefined'. " +
  46. "Also, the type declaration does not need to include '| undefined'.";
  47. return Rule;
  48. }(Lint.Rules.AbstractRule));
  49. exports.Rule = Rule;
  50. function walk(ctx) {
  51. ts.forEachChild(ctx.sourceFile, function cb(node) {
  52. switch (node.kind) {
  53. case ts.SyntaxKind.BindingElement:
  54. checkInitializer(node);
  55. break;
  56. case ts.SyntaxKind.VariableDeclaration:
  57. if (!tsutils_1.isBindingPattern(node.name) && !tsutils_1.isNodeFlagSet(node.parent, ts.NodeFlags.Const)) {
  58. checkInitializer(node);
  59. }
  60. break;
  61. case ts.SyntaxKind.MethodDeclaration:
  62. case ts.SyntaxKind.FunctionDeclaration:
  63. case ts.SyntaxKind.Constructor: {
  64. var parameters_1 = node.parameters;
  65. parameters_1.forEach(function (parameter, i) {
  66. if (isUndefined(parameter.initializer)) {
  67. if (parametersAllOptionalAfter(parameters_1, i)) {
  68. // No fix since they may want to remove '| undefined' from the type.
  69. ctx.addFailureAtNode(parameter, Rule.FAILURE_STRING_PARAMETER);
  70. }
  71. else {
  72. failWithFix(parameter);
  73. }
  74. }
  75. });
  76. }
  77. }
  78. ts.forEachChild(node, cb);
  79. });
  80. function checkInitializer(node) {
  81. if (isUndefined(node.initializer)) {
  82. failWithFix(node);
  83. }
  84. }
  85. function failWithFix(node) {
  86. var fix = Lint.Replacement.deleteFromTo(tsutils_1.getChildOfKind(node, ts.SyntaxKind.EqualsToken).pos, node.end);
  87. ctx.addFailureAtNode(node, Rule.FAILURE_STRING, fix);
  88. }
  89. }
  90. function parametersAllOptionalAfter(parameters, idx) {
  91. for (var i = idx + 1; i < parameters.length; i++) {
  92. if (parameters[i].questionToken !== undefined) {
  93. return true;
  94. }
  95. if (parameters[i].initializer === undefined) {
  96. return false;
  97. }
  98. }
  99. return true;
  100. }
  101. function isUndefined(node) {
  102. return node !== undefined &&
  103. node.kind === ts.SyntaxKind.Identifier &&
  104. node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword;
  105. }
  106. var templateObject_1;