Skip to content

Commit

Permalink
fix(linter): do not allow relative import of non-project files (#20563)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Dec 5, 2023
1 parent 5e174fc commit 7e5c459
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/eslint-plugin/src/rules/enforce-module-boundaries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,42 @@ Violation detected in:
expect(failures[0].message).toEqual(message);
expect(failures[1].message).toEqual(message);
});

it('should error when relatively importing the external resources', () => {
const failures = runRule(
{},
`${process.cwd()}/proj/libs/mylib/src/main.ts`,
`
import '../../../non-project';
import('/tmp/some/path/to/file');
`,
{
nodes: {
mylibName: {
name: 'mylibName',
type: 'lib',
data: {
root: 'libs/mylib',
tags: [],
implicitDependencies: [],
targets: {},
},
},
},
dependencies: {},
},
{
mylibName: [createFile(`libs/mylib/src/main.ts`)],
}
);
expect(failures.length).toEqual(2);
expect(failures[0].message).toEqual(
'External resources cannot be imported using a relative or absolute path'
);
expect(failures[1].message).toEqual(
'External resources cannot be imported using a relative or absolute path'
);
});
});

it('should error on absolute imports into libraries without using the npm scope', () => {
Expand Down
11 changes: 10 additions & 1 deletion packages/eslint-plugin/src/rules/enforce-module-boundaries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Options = [
];
export type MessageIds =
| 'noRelativeOrAbsoluteImportsAcrossLibraries'
| 'noRelativeOrAbsoluteExternals'
| 'noSelfCircularDependencies'
| 'noCircularDependencies'
| 'noImportsOfApps'
Expand Down Expand Up @@ -159,6 +160,7 @@ export default ESLintUtils.RuleCreator(() => ``)<Options, MessageIds>({
],
messages: {
noRelativeOrAbsoluteImportsAcrossLibraries: `Projects cannot be imported by a relative or absolute path, and must begin with a npm scope`,
noRelativeOrAbsoluteExternals: `External resources cannot be imported using a relative or absolute path`,
noCircularDependencies: `Circular dependency between "{{sourceProjectName}}" and "{{targetProjectName}}" detected: {{path}}\n\nCircular file chain:\n{{filePaths}}`,
noSelfCircularDependencies: `Projects should use relative imports to import from other files within the same project. Use "./path/to/file" instead of import from "{{imp}}"`,
noImportsOfApps: 'Imports of apps are forbidden',
Expand Down Expand Up @@ -345,8 +347,15 @@ export default ESLintUtils.RuleCreator(() => ``)<Options, MessageIds>({
imp
);

// If target is not part of an nx workspace, return.
if (!targetProject) {
// non-project imports cannot use relative or absolute paths
if (isRelativePath(imp) || imp.startsWith('/')) {
context.report({
node,
messageId: 'noRelativeOrAbsoluteExternals',
});
}
// If target is not found (including node internals) we bail early
return;
}

Expand Down

0 comments on commit 7e5c459

Please sign in to comment.