importBlacklistRule.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Lint = require("../index");
  22. var Rule = /** @class */ (function (_super) {
  23. tslib_1.__extends(Rule, _super);
  24. function Rule() {
  25. return _super !== null && _super.apply(this, arguments) || this;
  26. }
  27. Rule.prototype.isEnabled = function () {
  28. return _super.prototype.isEnabled.call(this) && this.ruleArguments.length > 0;
  29. };
  30. Rule.prototype.apply = function (sourceFile) {
  31. return this.applyWithFunction(sourceFile, walk, this.ruleArguments);
  32. };
  33. /* tslint:disable:object-literal-sort-keys */
  34. Rule.metadata = {
  35. ruleName: "import-blacklist",
  36. description: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Disallows importing the specified modules directly via `import` and `require`.\n Instead only sub modules may be imported from that module."], ["\n Disallows importing the specified modules directly via \\`import\\` and \\`require\\`.\n Instead only sub modules may be imported from that module."]))),
  37. rationale: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Some libraries allow importing their submodules instead of the entire module.\n This is good practise as it avoids loading unused modules."], ["\n Some libraries allow importing their submodules instead of the entire module.\n This is good practise as it avoids loading unused modules."]))),
  38. optionsDescription: "A list of blacklisted modules.",
  39. options: {
  40. type: "array",
  41. items: {
  42. type: "string",
  43. },
  44. minLength: 1,
  45. },
  46. optionExamples: [true, [true, "rxjs", "lodash"]],
  47. type: "functionality",
  48. typescriptOnly: false,
  49. };
  50. Rule.FAILURE_STRING = "This import is blacklisted, import a submodule instead";
  51. return Rule;
  52. }(Lint.Rules.AbstractRule));
  53. exports.Rule = Rule;
  54. function walk(ctx) {
  55. for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 31 /* All */); _i < _a.length; _i++) {
  56. var name = _a[_i];
  57. if (ctx.options.indexOf(name.text) !== -1) {
  58. ctx.addFailure(name.getStart(ctx.sourceFile) + 1, name.end - 1, Rule.FAILURE_STRING);
  59. }
  60. }
  61. }
  62. var templateObject_1, templateObject_2;