Skip to content

Commit

Permalink
fix(linter): don't confuse buildable libs for secondary ng entry point (
Browse files Browse the repository at this point in the history
#16367)

(cherry picked from commit c8960b7)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Apr 24, 2023
1 parent c1cf1f9 commit f504030
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ export default createESLintRule<Options, MessageIds>({
if (
!allowCircularSelfDependency &&
!isRelativePath(imp) &&
!isAngularSecondaryEntrypoint(imp, sourceFilePath)
!isAngularSecondaryEntrypoint(
imp,
sourceFilePath,
sourceProject.data.root
)
) {
context.report({
node,
Expand Down
41 changes: 36 additions & 5 deletions packages/eslint-plugin-nx/src/utils/runtime-lint-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ describe('isAngularSecondaryEntrypoint', () => {
],
'@project/features': ['libs/features/src/index.ts'],
'@project/features/*': ['libs/features/*/random/folder/api.ts'],
'@project/buildable': ['libs/buildable/src/index.ts'],
},
},
};
Expand All @@ -421,25 +422,55 @@ describe('isAngularSecondaryEntrypoint', () => {
lib: { entryFile: 'random/folder/api.ts' },
}),
'libs/features/secondary/random/folder/api.ts': 'const bla = "foo"',
'libs/buildable/ng-package.json': JSON.stringify({
lib: { entryFile: 'src/index.ts' },
}),
};
vol.fromJSON(fsJson, '/root');
});

it('should return true for secondary entrypoints', () => {
expect(
isAngularSecondaryEntrypoint('@project/standard', 'apps/app.ts')
isAngularSecondaryEntrypoint(
'@project/standard',
'apps/app.ts',
'libs/standard'
)
).toBe(false);
expect(
isAngularSecondaryEntrypoint('@project/standard/secondary', 'apps/app.ts')
isAngularSecondaryEntrypoint(
'@project/standard/secondary',
'apps/app.ts',
'libs/standard'
)
).toBe(true);
expect(
isAngularSecondaryEntrypoint('@project/standard/tertiary', 'apps/app.ts')
isAngularSecondaryEntrypoint(
'@project/standard/tertiary',
'apps/app.ts',
'libs/standard'
)
).toBe(true);
expect(
isAngularSecondaryEntrypoint('@project/features', 'apps/app.ts')
isAngularSecondaryEntrypoint(
'@project/features',
'apps/app.ts',
'libs/features'
)
).toBe(false);
expect(
isAngularSecondaryEntrypoint('@project/features/secondary', 'apps/app.ts')
isAngularSecondaryEntrypoint(
'@project/features/secondary',
'apps/app.ts',
'libs/features'
)
).toBe(true);
expect(
isAngularSecondaryEntrypoint(
'@project/buildable',
'apps/app.ts',
'libs/buildable'
)
).toBe(false);
});
});
11 changes: 7 additions & 4 deletions packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,20 +447,23 @@ export function groupImports(
*/
export function isAngularSecondaryEntrypoint(
importExpr: string,
filePath: string
filePath: string,
projectRoot: string
): boolean {
const resolvedModule = resolveModuleByImport(
importExpr,
filePath,
join(workspaceRoot, getRootTsConfigFileName())
);

return !!resolvedModule && fileIsSecondaryEntryPoint(resolvedModule);
return (
!!resolvedModule && fileIsSecondaryEntryPoint(resolvedModule, projectRoot)
);
}

function fileIsSecondaryEntryPoint(file: string): boolean {
function fileIsSecondaryEntryPoint(file: string, projectRoot: string): boolean {
let parent = joinPathFragments(file, '../');
while (parent !== './') {
while (parent !== `${projectRoot}/`) {
// we need to find closest existing ng-package.json
// in order to determine if the file matches the secondary entry point
const ngPackageContent = readFileIfExisting(
Expand Down

0 comments on commit f504030

Please sign in to comment.