Skip to content

Commit

Permalink
fix(linter): correctly fix absolute imports across package boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
juristr committed Jul 22, 2022
1 parent 2fa7578 commit 8e6a66c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions e2e/linter/src/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,10 @@ describe('Linter', () => {
createFile(
`libs/${libA}/src/lib/tslib-a.ts`,
`
export function libASayHi(): string {
return 'hi there';
}
export function libASayHello(): string {
return 'Hi from tslib-a';
}
Expand Down Expand Up @@ -297,13 +301,15 @@ describe('Linter', () => {
createFile(
`libs/${libB}/src/lib/tslib-b.ts`,
`
import { libASayHi } from 'libs/${libA}/src/lib/tslib-a';
import { libASayHello } from '../../../${libA}/src/lib/tslib-a';
// import { someNonPublicLibFunction } from '../../../${libA}/src/lib/some-non-exported-function';
import { someSelectivelyExportedFn } from '../../../${libA}/src/lib/some-non-exported-function';
export function tslibB(): string {
// someNonPublicLibFunction();
someSelectivelyExportedFn();
libASayHi();
libASayHello();
return 'hi there';
}
Expand Down Expand Up @@ -410,6 +416,9 @@ export function tslibC(): string {
expect(fileContent).toContain(
`import { libASayHello } from '@${projScope}/${libA}';`
);
expect(fileContent).toContain(
`import { libASayHi } from '@${projScope}/${libA}';`
);
expect(fileContent).toContain(
`import { someSelectivelyExportedFn } from '@${projScope}/${libA}';`
);
Expand Down
15 changes: 12 additions & 3 deletions packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
findProjectUsingImport,
findSourceProject,
findTransitiveExternalDependencies,
findTargetProject,
getSourceFilePath,
getTargetProjectBasedOnRelativeImport,
groupImports,
Expand Down Expand Up @@ -203,16 +204,24 @@ export default createESLintRule<Options, MessageIds>({
}

// check for relative and absolute imports
let targetProject: ProjectGraphProjectNode | ProjectGraphExternalNode =
getTargetProjectBasedOnRelativeImport(
const isAbsoluteImportIntoAnotherProj =
isAbsoluteImportIntoAnotherProject(imp, workspaceLayout);
let targetProject: ProjectGraphProjectNode | ProjectGraphExternalNode;

if (isAbsoluteImportIntoAnotherProj) {
targetProject = findTargetProject(projectGraph, imp);
} else {
targetProject = getTargetProjectBasedOnRelativeImport(
imp,
projectPath,
projectGraph,
sourceFilePath
);
}

if (
(targetProject && sourceProject !== targetProject) ||
isAbsoluteImportIntoAnotherProject(imp, workspaceLayout)
isAbsoluteImportIntoAnotherProj
) {
context.report({
node,
Expand Down

0 comments on commit 8e6a66c

Please sign in to comment.