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

Commit

Permalink
[new-rule-option] [no-implicit-dependencies] - add whitelist (#3839) (#…
Browse files Browse the repository at this point in the history
…3979)

* feature - no-implicit-dependencies - add whitelist (#3839)

This allows you to specify a whitelist of modules that will not be part of package.json such as project-relative aliases

* feature - no-implicit-dependencies - add whitelist (#3839)

Use Set for whitelist package name comparison instead of Array.
  • Loading branch information
ajcrites authored and suchanlee committed Jun 28, 2018
1 parent 4a11f95 commit 9d7db47
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/rules/noImplicitDependenciesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as Lint from "../index";
interface Options {
dev: boolean;
optional: boolean;
whitelist: string[];
}

const OPTION_DEV = "dev";
Expand All @@ -43,17 +44,28 @@ export class Rule extends Lint.Rules.AbstractRule {
By default the rule looks at \`"dependencies"\` and \`"peerDependencies"\`.
By adding the \`"${OPTION_DEV}"\` option the rule also looks at \`"devDependencies"\`.
By adding the \`"${OPTION_OPTIONAL}"\` option the rule also looks at \`"optionalDependencies"\`.
An array of whitelisted modules can be added to skip checking their existence in package.json.
`,
options: {
type: "array",
items: {
type: "string",
enum: [OPTION_DEV, OPTION_OPTIONAL],
},
items: [
{
type: "string",
enum: [OPTION_DEV, OPTION_OPTIONAL],
},
{
type: "array",
},
],
minItems: 0,
maxItems: 2,
maxItems: 3,
},
optionExamples: [true, [true, OPTION_DEV], [true, OPTION_OPTIONAL]],
optionExamples: [
true,
[true, OPTION_DEV],
[true, OPTION_OPTIONAL],
[true, ["src", "app"]],
],
type: "functionality",
typescriptOnly: false,
};
Expand All @@ -64,20 +76,27 @@ export class Rule extends Lint.Rules.AbstractRule {
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
let whitelist = this.ruleArguments.find((arg) => Array.isArray(arg)) as string[];
if (whitelist === null || whitelist === undefined) {
whitelist = [];
}

return this.applyWithFunction(sourceFile, walk, {
dev: this.ruleArguments.indexOf(OPTION_DEV) !== - 1,
optional: this.ruleArguments.indexOf(OPTION_OPTIONAL) !== -1,
whitelist,
});
}
}

function walk(ctx: Lint.WalkContext<Options>) {
const {options} = ctx;
let dependencies: Set<string> | undefined;
const whitelist = new Set(options.whitelist);
for (const name of findImports(ctx.sourceFile, ImportKind.All)) {
if (!ts.isExternalModuleNameRelative(name.text)) {
const packageName = getPackageName(name.text);
if (builtins.indexOf(packageName) === -1 && !hasDependency(packageName)) {
if (!whitelist.has(packageName) && builtins.indexOf(packageName) === -1 && !hasDependency(packageName)) {
ctx.addFailureAtNode(name, Rule.FAILURE_STRING_FACTORY(packageName));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as ts from 'typescript';
~~~~~~~~~~~~ [err % ('typescript')]

import * from 'src/a';

import * from 'app/b';

import * from 'notapp/c';
~~~~~~~~~~ [err % ('notapp')]

[err]: Module '%s' is not listed as dependency in package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-implicit-dependencies": [true, "dev", ["src", "app"]]
}
}
10 changes: 10 additions & 0 deletions test/rules/no-implicit-dependencies/whitelist/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as ts from 'typescript';

import * from 'src/a';

import * from 'app/b';

import * from 'notapp/c';
~~~~~~~~~~ [err % ('notapp')]

[err]: Module '%s' is not listed as dependency in package.json
5 changes: 5 additions & 0 deletions test/rules/no-implicit-dependencies/whitelist/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-implicit-dependencies": [true, ["src", "app"]]
}
}

0 comments on commit 9d7db47

Please sign in to comment.