noImportSideEffectRule.js 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 utils = require("tsutils");
  21. var Lint = require("../index");
  22. var OPTION_IGNORE_MODULE = "ignore-module";
  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. var patternConfig = this.ruleArguments[this.ruleArguments.length - 1];
  30. var ignorePattern = patternConfig === undefined ? undefined : new RegExp(patternConfig[OPTION_IGNORE_MODULE]);
  31. return this.applyWithFunction(sourceFile, walk, ignorePattern);
  32. };
  33. Rule.metadata = {
  34. description: "Avoid import statements with side-effect.",
  35. optionExamples: [true, [true, (_a = {}, _a[OPTION_IGNORE_MODULE] = "(\\.html|\\.css)$", _a)]],
  36. options: {
  37. items: {
  38. properties: {
  39. "ignore-module": {
  40. type: "string",
  41. },
  42. },
  43. type: "object",
  44. },
  45. maxLength: 1,
  46. minLength: 0,
  47. type: "array",
  48. },
  49. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n One argument may be optionally provided:\n\n * `", "` allows to specify a regex and ignore modules which it matches."], ["\n One argument may be optionally provided:\n\n * \\`", "\\` allows to specify a regex and ignore modules which it matches."])), OPTION_IGNORE_MODULE),
  50. rationale: "Imports with side effects may have behavior which is hard for static verification.",
  51. ruleName: "no-import-side-effect",
  52. type: "typescript",
  53. typescriptOnly: false,
  54. };
  55. Rule.FAILURE_STRING = "import with explicit side-effect";
  56. return Rule;
  57. }(Lint.Rules.AbstractRule));
  58. exports.Rule = Rule;
  59. function walk(ctx) {
  60. var ignorePattern = ctx.options, sourceFile = ctx.sourceFile;
  61. for (var _i = 0, _a = sourceFile.statements; _i < _a.length; _i++) {
  62. var statement = _a[_i];
  63. if (!utils.isImportDeclaration(statement)) {
  64. continue;
  65. }
  66. var importClause = statement.importClause, moduleSpecifier = statement.moduleSpecifier;
  67. if (importClause !== undefined || !utils.isStringLiteral(moduleSpecifier)) {
  68. continue;
  69. }
  70. if (ignorePattern === undefined || !ignorePattern.test(moduleSpecifier.text)) {
  71. ctx.addFailureAtNode(statement, Rule.FAILURE_STRING);
  72. }
  73. }
  74. }
  75. var templateObject_1;
  76. var _a;