Skip to content

Commit 3628cc7

Browse files
committed
Fix import detection for local files with the name of a workspace (#32)
1 parent d661070 commit 3628cc7

9 files changed

+63
-20
lines changed

lib/rules/no-absolute-imports.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const { dirname, relative } = require("path");
44
const {
55
getWorkspaces,
66
isSubPath,
7+
isWorkspacePath,
78
getImport,
89
pathToImport,
910
} = require("../utils");
@@ -26,21 +27,25 @@ module.exports.meta = {
2627
module.exports.create = (context) => {
2728
const workspaces = getWorkspaces(context);
2829
const filename = context.getFilename();
29-
return getImport(workspaces, filename, ({ node, path, start, end }) => {
30-
workspaces.forEach(({ package: { name }, location }) => {
31-
if (isSubPath(location, filename) && isSubPath(name, path)) {
32-
context.report({
33-
node,
34-
messageId: "noAbsoluteImports",
35-
fix: (fixer) =>
36-
fixer.replaceTextRange(
37-
[start + 1, end - 1],
38-
pathToImport(
39-
relative(dirname(filename), path.replace(name, location)),
30+
return getImport(
31+
workspaces,
32+
filename,
33+
({ node, path, value, start, end }) => {
34+
workspaces.forEach(({ package: { name }, location }) => {
35+
if (isSubPath(location, filename) && isWorkspacePath(name, value)) {
36+
context.report({
37+
node,
38+
messageId: "noAbsoluteImports",
39+
fix: (fixer) =>
40+
fixer.replaceTextRange(
41+
[start + 1, end - 1],
42+
pathToImport(
43+
relative(dirname(filename), path.replace(name, location)),
44+
),
4045
),
41-
),
42-
});
43-
}
44-
});
45-
});
46+
});
47+
}
48+
});
49+
},
50+
);
4651
};

lib/rules/no-cross-imports.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"use strict";
22

3-
const { getWorkspaces, isSubPath, getImport } = require("../utils");
3+
const {
4+
getWorkspaces,
5+
isSubPath,
6+
isWorkspacePath,
7+
getImport,
8+
} = require("../utils");
49

510
module.exports.meta = {
611
type: "problem",
@@ -109,7 +114,7 @@ module.exports.create = (context) => {
109114
.forEach(({ package: { name }, location }) => {
110115
if (
111116
name !== currentWorkspace.package.name &&
112-
(isSubPath(name, value) || isSubPath(location, path))
117+
(isWorkspacePath(name, value) || isSubPath(location, path))
113118
) {
114119
context.report({
115120
node,

lib/rules/require-dependency.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
"use strict";
22

3-
const { getWorkspaces, isSubPath, getImport } = require("../utils");
3+
const {
4+
getWorkspaces,
5+
isSubPath,
6+
isWorkspacePath,
7+
getImport,
8+
} = require("../utils");
49

510
module.exports.meta = {
611
type: "problem",
@@ -32,7 +37,7 @@ module.exports.create = (context) => {
3237

3338
if (
3439
name !== currentWorkspace.package.name &&
35-
(isSubPath(name, value) || isSubPath(location, path)) &&
40+
(isWorkspacePath(name, value) || isSubPath(location, path)) &&
3641
!Object.keys(dependencies).includes(name) &&
3742
!Object.keys(peerDependencies).includes(name) &&
3843
!Object.keys(optionalDependencies).includes(name) &&

lib/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ const isSubPath = (parent, path) => {
1616
return relativePath.split(sep)[0] !== ".." && !isAbsolute(relativePath);
1717
};
1818

19+
const isWorkspacePath = (name, path) =>
20+
path === name ||
21+
path.startsWith(name + sep) ||
22+
path.startsWith(name + posix.sep);
23+
1924
const resolvePath = (parent, path) => {
2025
if (path[0] !== ".") return path;
2126

@@ -90,6 +95,7 @@ const getWorkspaces = (context) => {
9095

9196
module.exports = {
9297
isSubPath,
98+
isWorkspacePath,
9399
getImport,
94100
pathToImport,
95101
getWorkspaces,

tests/mocks.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ module.exports.findWorkspacesMock = () => [
2020
name: "@test/third-workspace",
2121
},
2222
},
23+
{
24+
location: "/test/no-npm-scope-workspace",
25+
package: {
26+
name: "no-npm-scope-workspace",
27+
},
28+
},
2329
{
2430
location: "/test/peer-dependencies",
2531
package: {

tests/rules/no-absolute-imports.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ describe("no-absolute-imports", () => {
4040
code: "import workspace from '@test/workspace';",
4141
filename: "/some/path.js",
4242
},
43+
{
44+
filename: "/test/no-npm-scope-workspace/index.js",
45+
code: "import './no-npm-scope-workspace';",
46+
},
4347
],
4448

4549
invalid: [

tests/rules/no-cross-imports.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ describe("no-cross-imports", () => {
5858
filename: "/test/scope/workspace/file.js",
5959
code: "import '@test/shared-in-scope';",
6060
},
61+
{
62+
filename: "/test/workspace/index.js",
63+
code: "import './no-npm-scope-workspace';",
64+
},
6165
],
6266

6367
invalid: [

tests/rules/no-relative-imports.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ describe("no-relative-imports", () => {
4040
filename: "/some/file.js",
4141
code: "import '../test/workspace';",
4242
},
43+
{
44+
filename: "/test/workspace/index.js",
45+
code: "import './no-npm-scope-workspace';",
46+
},
4347
],
4448
invalid: [
4549
{

tests/rules/require-dependency.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ describe("require-dependency", () => {
3030
filename: "/test/optional-dependencies/test.js",
3131
code: "import '@test/optional-workspace'",
3232
},
33+
{
34+
filename: "/test/workspace/index.js",
35+
code: "import './no-npm-scope-workspace';",
36+
},
3337
],
3438
invalid: [
3539
{

0 commit comments

Comments
 (0)