Skip to content

Commit d68b03e

Browse files
feat: only Report ESLint errors for unresolvable Relative Imports #237
1 parent 6fc716d commit d68b03e

5 files changed

Lines changed: 68 additions & 2 deletions

File tree

packages/core/src/lib/eslint/tests/violates-dependency-rule.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ describe('violates dependency rules', () => {
119119
).toBe('import ./app.component cannot be resolved');
120120
});
121121

122+
it('should not report unresolvable external library import', () => {
123+
createProject({
124+
'tsconfig.json': tsConfig(),
125+
'sheriff.config.ts': sheriffConfig({ modules: {}, depRules: {} }),
126+
src: {
127+
'main.ts': ['node:fs'],
128+
},
129+
});
130+
131+
expect(
132+
violatesDependencyRule(
133+
'/project/src/main.ts',
134+
'node:fs',
135+
true,
136+
getFs().readFile(toFsPath('/project/src/main.ts')),
137+
),
138+
).toBe('');
139+
});
140+
122141
it('should ignore json imports ', () => {
123142
const fs = createProject({
124143
'tsconfig.json': tsConfig(),

packages/core/src/lib/eslint/tests/violates-encapsulation-rule.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,25 @@ describe('encapsulation', () => {
7878
).toBe('import ./app/app.component cannot be resolved');
7979
});
8080

81+
it('should not report unresolvable external library import', () => {
82+
testInit('src/main.ts', {
83+
'tsconfig.json': tsConfig(),
84+
src: {
85+
'main.ts': ['keycloak-js'],
86+
},
87+
});
88+
89+
expect(
90+
violatesEncapsulationRule(
91+
'/project/src/main.ts',
92+
'keycloak-js',
93+
true,
94+
getFs().readFile(toFsPath('/project/src/main.ts')),
95+
true,
96+
),
97+
).toBe('');
98+
});
99+
81100
it('should return a violation to a barrel-less modules', () => {
82101
testInit('src/app/main.ts', {
83102
'tsconfig.json': tsConfig(),

packages/core/src/lib/eslint/violates-dependency-rule.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
DependencyRuleViolation,
88
} from '../checks/check-for-dependency-rule-violation';
99
import { FileInfo } from '../modules/file.info';
10+
import { isRelativeImport } from '../util/is-relative-import';
1011

1112
let cache: Record<string, string> = {};
1213
let cacheActive = false;
@@ -55,7 +56,10 @@ export const violatesDependencyRule = (
5556
}
5657
}
5758

58-
if (throwIfNull(fileInfo).isUnresolvableImport(importCommand)) {
59+
if (
60+
throwIfNull(fileInfo).isUnresolvableImport(importCommand) &&
61+
isRelativeImport(importCommand)
62+
) {
5963
return `import ${importCommand} cannot be resolved`;
6064
}
6165

packages/core/src/lib/eslint/violates-encapsulation-rule.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { toFsPath } from '../file-info/fs-path';
22
import { init } from '../main/init';
33
import { hasEncapsulationViolations } from '../checks/has-encapsulation-violations';
44
import { FileInfo } from '../modules/file.info';
5+
import { isRelativeImport } from '../util/is-relative-import';
56

67
let cache: Record<string, FileInfo> = {};
78
let cachedFileInfo: FileInfo | undefined;
@@ -48,7 +49,10 @@ export const violatesEncapsulationRule = (
4849
cache = hasEncapsulationViolations(fsPath, projectInfo);
4950
}
5051

51-
if (cachedFileInfo.isUnresolvableImport(importCommand)) {
52+
if (
53+
cachedFileInfo.isUnresolvableImport(importCommand) &&
54+
isRelativeImport(importCommand)
55+
) {
5256
return `import ${importCommand} cannot be resolved`;
5357
}
5458

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Determines whether an import command is a relative import.
3+
*
4+
* Relative imports are those that start with a dot (e.g. './x', '../y').
5+
* Non-relative imports include:
6+
* - External packages (e.g. 'keycloak-js')
7+
* - Node.js built-in modules (e.g. 'node:url', 'fs', 'path')
8+
*
9+
* Sheriff ESLint rules only report unresolvable imports when they are relative.
10+
* This avoids false positives for external packages and Node built-ins while
11+
* still allowing the core to track all unresolvable imports for data/export.
12+
*
13+
* @param importCommand The raw import string as it appears in source code
14+
* @returns true if the import is relative; otherwise false
15+
*/
16+
export function isRelativeImport(importCommand: string): boolean {
17+
return importCommand.startsWith('.');
18+
}
19+
20+

0 commit comments

Comments
 (0)