Skip to content

Commit

Permalink
fix(lint): only allow importing libs using the configured npm scope
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed Dec 28, 2017
1 parent 2625b5f commit c836668
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion e2e/schematics/tslint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Lint', () => {
);

const out = runCLI('lint --type-check', { silenceError: true });
expect(out).toContain('relative imports of libraries are forbidden');
expect(out).toContain('library imports must start with @nrwl/');
expect(out).toContain('import of lazy-loaded libraries are forbidden');
expect(out).toContain('deep imports into libraries are forbidden');
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ describe('Enforce Module Boundaries', () => {
const failures = runRule({}, `import '../../../libs/mylib';`);

expect(failures.length).toEqual(1);
expect(failures[0].getFailure()).toEqual('relative imports of libraries are forbidden');
expect(failures[0].getFailure()).toEqual('library imports must start with @mycompany/');
});

it('should error on absolute imports into libraries without using the npm scope', () => {
const failures = runRule({}, `import 'libs/mylib';`);

expect(failures.length).toEqual(1);
expect(failures[0].getFailure()).toEqual('library imports must start with @mycompany/');
});

it('should error about deep imports into libraries', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class EnforceModuleBoundariesWalker extends Lint.RuleWalker {
return;
}

if (this.isRelative(imp) && this.isRelativeImportIntoAnotherProject(imp)) {
this.addFailureAt(node.getStart(), node.getWidth(), 'relative imports of libraries are forbidden');
if (this.isRelativeImportIntoAnotherProject(imp) || this.isAbsoluteImportIntoAnotherProject(imp)) {
this.addFailureAt(node.getStart(), node.getWidth(), `library imports must start with @${this.npmScope}/`);
return;
}

Expand All @@ -73,6 +73,7 @@ class EnforceModuleBoundariesWalker extends Lint.RuleWalker {
}

private isRelativeImportIntoAnotherProject(imp: string): boolean {
if (!this.isRelative(imp)) return false;
const sourceFile = this.getSourceFile().fileName.substring(this.projectPath.length);
const targetFile = path.resolve(path.dirname(sourceFile), imp);
if (this.workspacePath(sourceFile) && this.workspacePath(targetFile)) {
Expand All @@ -83,6 +84,10 @@ class EnforceModuleBoundariesWalker extends Lint.RuleWalker {
return false;
}

private isAbsoluteImportIntoAnotherProject(imp: string): boolean {
return imp.startsWith('libs/') || (imp.startsWith('/libs/') && imp.startsWith('apps/')) || imp.startsWith('/apps/');
}

private workspacePath(s: string): boolean {
return s.startsWith('/apps/') || s.startsWith('/libs/');
}
Expand Down

0 comments on commit c836668

Please sign in to comment.