Skip to content

Commit

Permalink
feat: allow whitelisted modules to start with @ symbol (palantir#4192)
Browse files Browse the repository at this point in the history
- tests added for no-implicit-dependencies @ symbols
  • Loading branch information
ifiokjr authored and ericanderson committed Dec 12, 2018
1 parent cd5d7d0 commit 6fe8723
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
11 changes: 8 additions & 3 deletions src/rules/noImplicitDependenciesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
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);
const packageName = getPackageName(name.text, whitelist);
if (
!whitelist.has(packageName) &&
builtins.indexOf(packageName) === -1 &&
Expand All @@ -110,11 +110,16 @@ function walk(ctx: Lint.WalkContext<Options>) {
}
}

function getPackageName(name: string): string {
function getPackageName(name: string, whitelist: Set<string>): string {
const parts = name.split(/\//g);
if (name[0] !== "@") {
if (name[0] !== "@" || whitelist.has(parts[0])) {
return parts[0];
}

if (whitelist.has(name)) {
return name;
}

return `${parts[0]}/${parts[1]}`;
}

Expand Down
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
Expand Up @@ -2,9 +2,19 @@ import * as ts from 'typescript';

import * from 'src/a';

import * from '@components/a';

import * from '@com/fixer';

import * from 'app/b';

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

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

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

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

0 comments on commit 6fe8723

Please sign in to comment.