Skip to content

Commit

Permalink
no-submodule-imports: consider exact matches as whitelisted (palantir…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and HyphnKnight committed Apr 9, 2018
1 parent 99fe698 commit 7a0b0d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
21 changes: 7 additions & 14 deletions src/rules/noSubmoduleImportsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ export class Rule extends Lint.Rules.AbstractRule {
Submodules of some packages are treated as private APIs and the import
paths may change without deprecation periods. It's best to stick with
top-level package exports.`,
optionsDescription: "A list of packages whose submodules are whitelisted.",
optionsDescription: "A list of whitelisted package or submodule names.",
options: {
type: "array",
items: {
type: "string",
},
minLength: 0,
},
optionExamples: [true, [true, "rxjs", "@angular/core"]],
optionExamples: [true, [true, "rxjs", "@angular/platform-browser", "@angular/core/testing"]],
type: "functionality",
typescriptOnly: false,
};
Expand Down Expand Up @@ -81,18 +80,16 @@ class NoSubmoduleImportsWalker extends Lint.AbstractWalker<string[]> {
}

private checkForBannedImport(expression: ts.Expression) {
if (isTextualLiteral(expression)) {
if (isAbsoluteOrRelativePath(expression.text) || !isSubmodulePath(expression.text)) {
return;
}

/**
if (isTextualLiteral(expression) &&
ts.moduleHasNonRelativeName(expression.text) &&
isSubmodulePath(expression.text)) {
/*
* A submodule is being imported.
* Check if its path contains any
* of the whitelist packages.
*/
for (const option of this.options) {
if (expression.text.startsWith(`${option}/`)) {
if (expression.text === option || expression.text.startsWith(`${option}/`)) {
return;
}
}
Expand All @@ -102,10 +99,6 @@ class NoSubmoduleImportsWalker extends Lint.AbstractWalker<string[]> {
}
}

function isAbsoluteOrRelativePath(path: string): boolean {
return /^(..?(\/|$)|\/)/.test(path);
}

function isScopedPath(path: string): boolean {
return path[0] === "@";
}
Expand Down
7 changes: 6 additions & 1 deletion test/rules/no-submodule-imports/static-imports/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import { submodule } from "@angular/core/a/b/c/sub-module";
import submodule = require("@angular/core");
import submodule = require("@angular/core/a/b/c/sub-module");

import { submodule } from "@angular/platform-browser";
import { submodule } from "@angular/platform-browser/animations";
import { submodule } from "@angular/platform-browser/animations/foo";
import { submodule } from "@angular/platform-browser/bar";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [0]

import { submodule } from "lodash";

Expand Down Expand Up @@ -62,4 +67,4 @@ import { submodule } from "../myModule/a/package";
import submodule = require("./../node_modules/package");
import submodule = require("../myModule/a/package");

[0]: Submodule import paths from this package are disallowed; import from the root instead
[0]: Submodule import paths from this package are disallowed; import from the root instead
4 changes: 2 additions & 2 deletions test/rules/no-submodule-imports/static-imports/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"no-submodule-imports": [true, "@angular/core", "rxjs"]
"no-submodule-imports": [true, "@angular/core", "rxjs", "@angular/platform-browser/animations"]
}
}
}

0 comments on commit 7a0b0d4

Please sign in to comment.