noUnnecessaryInitializerRule.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. type: "style",
  40. typescriptOnly: false,
  41. };
  42. /* tslint:enable:object-literal-sort-keys */
  43. Rule.FAILURE_STRING = "Unnecessary initialization to 'undefined'.";
  44. Rule.FAILURE_STRING_PARAMETER = "Use an optional parameter instead of initializing to 'undefined'. " +
  45. "Also, the type declaration does not need to include '| undefined'.";
  46. return Rule;
  47. }(Lint.Rules.AbstractRule));
  48. exports.Rule = Rule;
  49. function walk(ctx) {
  50. ts.forEachChild(ctx.sourceFile, function cb(node) {
  51. switch (node.kind) {
  52. case ts.SyntaxKind.BindingElement:
  53. checkInitializer(node);
  54. break;
  55. case ts.SyntaxKind.VariableDeclaration:
  56. if (!tsutils_1.isBindingPattern(node.name) && !tsutils_1.isNodeFlagSet(node.parent, ts.NodeFlags.Const)) {
  57. checkInitializer(node);
  58. }
  59. break;
  60. case ts.SyntaxKind.MethodDeclaration:
  61. case ts.SyntaxKind.FunctionDeclaration:
  62. case ts.SyntaxKind.Constructor: {
  63. var parameters_1 = node.parameters;
  64. parameters_1.forEach(function (parameter, i) {
  65. if (isUndefined(parameter.initializer)) {
  66. if (parametersAllOptionalAfter(parameters_1, i)) {
  67. // No fix since they may want to remove '| undefined' from the type.
  68. ctx.addFailureAtNode(parameter, Rule.FAILURE_STRING_PARAMETER);
  69. }
  70. else {
  71. failWithFix(parameter);
  72. }
  73. }
  74. });
  75. }
  76. }
  77. ts.forEachChild(node, cb);
  78. });
  79. function checkInitializer(node) {
  80. if (isUndefined(node.initializer)) {
  81. failWithFix(node);
  82. }
  83. }
  84. function failWithFix(node) {
  85. var fix = Lint.Replacement.deleteFromTo(tsutils_1.getChildOfKind(node, ts.SyntaxKind.EqualsToken).pos, node.end);
  86. ctx.addFailureAtNode(node, Rule.FAILURE_STRING, fix);
  87. }
  88. }
  89. function parametersAllOptionalAfter(parameters, idx) {
  90. for (var i = idx + 1; i < parameters.length; i++) {
  91. if (parameters[i].questionToken !== undefined) {
  92. return true;
  93. }
  94. if (parameters[i].initializer === undefined) {
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. function isUndefined(node) {
  101. return node !== undefined &&
  102. node.kind === ts.SyntaxKind.Identifier &&
  103. node.originalKeywordKind === ts.SyntaxKind.UndefinedKeyword;
  104. }