Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
update importBlacklistRule to use regular expressions (#3504)
Browse files Browse the repository at this point in the history
* update importBlacklistRule to use regular expressions

* fix doc typo

* update to a backwards compatible solution

* adjusted previous importBlacklistRule updates against latest code

* fixed lint errors
  • Loading branch information
jbsingh authored and ericanderson committed Dec 18, 2018
1 parent 620ea71 commit 3725aac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
39 changes: 35 additions & 4 deletions src/rules/importBlacklistRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ export class Rule extends Lint.Rules.AbstractRule {
ruleName: "import-blacklist",
description: Lint.Utils.dedent`
Disallows importing the specified modules via \`import\` and \`require\`,
or importing specific named exports of the specified modules.`,
or importing specific named exports of the specified modules,
or using imports matching specified regular expression patterns.`,
rationale: Lint.Utils.dedent`
For some libraries, importing the library directly can cause unused
submodules to be loaded, so you may want to block these imports and
require that users directly import only the submodules they need.
In other cases, you may simply want to ban an import because using
it is undesirable or unsafe.`,
optionsDescription: "A list of blacklisted modules or named imports.",
optionsDescription:
"A list of blacklisted modules, named imports, or regular expression patterns.",
options: {
type: "array",
items: {
Expand All @@ -58,13 +60,21 @@ export class Rule extends Lint.Rules.AbstractRule {
},
},
},
{
type: "array",
items: {
type: "string",
},
minLength: 1,
},
],
},
},
optionExamples: [
true,
[true, "rxjs", "lodash"],
[true, "lodash", { lodash: ["pull", "pullAll"] }],
[true, "rxjs", { lodash: ["pull", "pullAll"] }, [".*\\.temp$", ".*\\.tmp$"]],
],
type: "functionality",
typescriptOnly: false,
Expand All @@ -78,6 +88,8 @@ export class Rule extends Lint.Rules.AbstractRule {
"(or re-exporting). Import/re-export only the specific values you want, " +
"instead of the whole module.";

public static FAILURE_STRING_REGEX = "This import is blacklisted by ";

public static MAKE_NAMED_IMPORT_FAILURE_STRING(importName: string) {
return importName === "default"
? "Importing (or re-exporting) the default export is blacklisted."
Expand All @@ -93,7 +105,7 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

type Options = Array<string | { [moduleName: string]: string[] }>;
type Options = Array<string | { [moduleName: string]: string[] } | string[]>;

function walk(ctx: Lint.WalkContext<Options>) {
interface BannedImports {
Expand All @@ -107,7 +119,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
(acc, it) => {
if (typeof it === "string") {
acc[it] = true;
} else {
} else if (!Array.isArray(it)) {
Object.keys(it).forEach(moduleName => {
if (acc[moduleName] instanceof Set) {
it[moduleName].forEach(bannedImport => {
Expand All @@ -123,6 +135,15 @@ function walk(ctx: Lint.WalkContext<Options>) {
Object.create(null) as BannedImports,
);

const regexOptions = [];
for (const option of ctx.options) {
if (Array.isArray(option)) {
for (const pattern of option) {
regexOptions.push(RegExp(pattern));
}
}
}

for (const name of findImports(ctx.sourceFile, ImportKind.All)) {
// TODO #3963: Resolve/normalize relative file imports to a canonical path?
const importedModule = name.text;
Expand Down Expand Up @@ -233,5 +254,15 @@ function walk(ctx: Lint.WalkContext<Options>) {
);
}
}

for (const regex of regexOptions) {
if (regex.test(name.text)) {
ctx.addFailure(
name.getStart(ctx.sourceFile) + 1,
name.end - 1,
Rule.FAILURE_STRING_REGEX + regex.toString(),
);
}
}
}
}
8 changes: 8 additions & 0 deletions test/rules/import-blacklist/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@ import { pull as notPull } from "lodash";
import test from "dummy";
~~~~ [3]

import {myFunc} from './my.temp';
~~~~~~~~~ [4]

import {myFunc} from './my.tmp';
~~~~~~~~ [5]


[0]: Importing this module is blacklisted. Try importing a submodule instead.
[1]: Some named exports from this module are blacklisted for importing (or re-exporting). Import/re-export only the specific values you want, instead of the whole module.
[2]: The export "pull" is blacklisted.
[3]: Importing (or re-exporting) the default export is blacklisted.
[4]: This import is blacklisted by /.*\.temp$/
[5]: This import is blacklisted by /.*\.tmp$/
2 changes: 1 addition & 1 deletion test/rules/import-blacklist/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"import-blacklist": [true, "rxjs", { "lodash": ["pullAll", "pull"] }, { "dummy": ["default"] }]
"import-blacklist": [true, "rxjs", { "lodash": ["pullAll", "pull"] }, { "dummy": ["default"] }, [".*\\.temp$", ".*\\.tmp$"]]
}
}

0 comments on commit 3725aac

Please sign in to comment.