noSubmoduleImportsRule.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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, this.ruleArguments);
  30. };
  31. /* tslint:disable:object-literal-sort-keys */
  32. Rule.metadata = {
  33. ruleName: "no-submodule-imports",
  34. description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows importing any submodule."], ["\n Disallows importing any submodule."]))),
  35. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Submodules of some packages are treated as private APIs and the import\n paths may change without deprecation periods. It's best to stick with\n top-level package exports."], ["\n Submodules of some packages are treated as private APIs and the import\n paths may change without deprecation periods. It's best to stick with\n top-level package exports."]))),
  36. optionsDescription: "A list of whitelisted package or submodule names.",
  37. options: {
  38. type: "array",
  39. items: {
  40. type: "string",
  41. },
  42. },
  43. optionExamples: [true, [true, "rxjs", "@angular/platform-browser", "@angular/core/testing"]],
  44. type: "functionality",
  45. typescriptOnly: false,
  46. };
  47. Rule.FAILURE_STRING = "Submodule import paths from this package are disallowed; import from the root instead";
  48. return Rule;
  49. }(Lint.Rules.AbstractRule));
  50. exports.Rule = Rule;
  51. function walk(ctx) {
  52. for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 31 /* All */); _i < _a.length; _i++) {
  53. var name = _a[_i];
  54. if (!ts.isExternalModuleNameRelative(name.text) &&
  55. isSubmodulePath(name.text) &&
  56. !isWhitelisted(name.text, ctx.options)) {
  57. ctx.addFailureAtNode(name, Rule.FAILURE_STRING);
  58. }
  59. }
  60. }
  61. function isWhitelisted(path, whitelist) {
  62. for (var _i = 0, whitelist_1 = whitelist; _i < whitelist_1.length; _i++) {
  63. var option = whitelist_1[_i];
  64. if (path === option || path.startsWith(option + "/")) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. function isSubmodulePath(path) {
  71. return path.split("/").length > (path[0] === "@" ? 2 : 1);
  72. }
  73. var templateObject_1, templateObject_2;