4 * Copyright 2017 Palantir Technologies, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
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);
26 return _super !== null && _super.apply(this, arguments) || this;
28 Rule.prototype.apply = function (sourceFile) {
29 return this.applyWithFunction(sourceFile, walk, this.ruleArguments);
31 /* tslint:disable:object-literal-sort-keys */
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.",
45 [true, "rxjs", "@angular/platform-browser", "@angular/core/testing"],
47 type: "functionality",
48 typescriptOnly: false,
50 Rule.FAILURE_STRING = "Submodule import paths from this package are disallowed; import from the root instead";
52 }(Lint.Rules.AbstractRule));
55 for (var _i = 0, _a = tsutils_1.findImports(ctx.sourceFile, 63 /* All */); _i < _a.length; _i++) {
57 if (!ts.isExternalModuleNameRelative(name.text) &&
58 isSubmodulePath(name.text) &&
59 !isWhitelisted(name.text, ctx.options)) {
60 ctx.addFailureAtNode(name, Rule.FAILURE_STRING);
64 function isWhitelisted(path, whitelist) {
65 for (var _i = 0, whitelist_1 = whitelist; _i < whitelist_1.length; _i++) {
66 var option = whitelist_1[_i];
67 if (path === option || path.startsWith(option + "/")) {
73 function isSubmodulePath(path) {
74 return path.split("/").length > (path[0] === "@" ? 2 : 1);
76 var templateObject_1, templateObject_2;