From 8e6a66cdb27e5256e8a568e7f74d20ece4768354 Mon Sep 17 00:00:00 2001 From: Juri Date: Fri, 22 Jul 2022 13:50:43 +0200 Subject: [PATCH] fix(linter): correctly fix absolute imports across package boundaries --- e2e/linter/src/linter.test.ts | 9 +++++++++ .../src/rules/enforce-module-boundaries.ts | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/e2e/linter/src/linter.test.ts b/e2e/linter/src/linter.test.ts index 91f3f43a728ef..8bcdc60f0af88 100644 --- a/e2e/linter/src/linter.test.ts +++ b/e2e/linter/src/linter.test.ts @@ -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'; } @@ -297,6 +301,7 @@ 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'; @@ -304,6 +309,7 @@ describe('Linter', () => { export function tslibB(): string { // someNonPublicLibFunction(); someSelectivelyExportedFn(); + libASayHi(); libASayHello(); return 'hi there'; } @@ -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}';` ); diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts index 5c68b05e646c8..f9e921a41329a 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts @@ -17,6 +17,7 @@ import { findProjectUsingImport, findSourceProject, findTransitiveExternalDependencies, + findTargetProject, getSourceFilePath, getTargetProjectBasedOnRelativeImport, groupImports, @@ -203,16 +204,24 @@ export default createESLintRule({ } // 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,