noDuplicateImportsRule.js 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.FAILURE_STRING = function (module) {
  29. return "Multiple imports from '" + module + "' can be combined into one.";
  30. };
  31. Rule.prototype.apply = function (sourceFile) {
  32. return this.applyWithFunction(sourceFile, walk);
  33. };
  34. /* tslint:disable:object-literal-sort-keys */
  35. Rule.metadata = {
  36. ruleName: "no-duplicate-imports",
  37. description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows multiple import statements from the same module."], ["\n Disallows multiple import statements from the same module."]))),
  38. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Using a single import statement per module will make the code clearer because you can see everything being imported\n from that module on one line."], ["\n Using a single import statement per module will make the code clearer because you can see everything being imported\n from that module on one line."]))),
  39. optionsDescription: "Not configurable",
  40. options: null,
  41. optionExamples: [true],
  42. type: "maintainability",
  43. typescriptOnly: false,
  44. };
  45. return Rule;
  46. }(Lint.Rules.AbstractRule));
  47. exports.Rule = Rule;
  48. function walk(ctx) {
  49. walkWorker(ctx, ctx.sourceFile.statements, new Set());
  50. }
  51. function walkWorker(ctx, statements, seen) {
  52. for (var _i = 0, statements_1 = statements; _i < statements_1.length; _i++) {
  53. var statement = statements_1[_i];
  54. if (tsutils_1.isImportDeclaration(statement) && tsutils_1.isLiteralExpression(statement.moduleSpecifier)) {
  55. var text = statement.moduleSpecifier.text;
  56. if (seen.has(text)) {
  57. ctx.addFailureAtNode(statement, Rule.FAILURE_STRING(text));
  58. }
  59. seen.add(text);
  60. }
  61. if (tsutils_1.isModuleDeclaration(statement) && statement.body !== undefined && statement.name.kind === ts.SyntaxKind.StringLiteral) {
  62. // If this is a module augmentation, re-use `seen` since those imports could be moved outside.
  63. // If this is an ambient module, create a fresh `seen`
  64. // because they should have separate imports to avoid becoming augmentations.
  65. walkWorker(ctx, statement.body.statements, ts.isExternalModule(ctx.sourceFile) ? seen : new Set());
  66. }
  67. }
  68. }
  69. var templateObject_1, templateObject_2;