Skip to content

Commit 686cfa8

Browse files
committed
Fix for nested packages
1 parent 7553f83 commit 686cfa8

File tree

10 files changed

+86
-18
lines changed

10 files changed

+86
-18
lines changed

lib/rules/no-absolute-imports.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,26 @@ module.exports.create = (context) => {
3131
return getImport(
3232
workspaces,
3333
filename,
34-
({ node, path, value, start, end }) => {
35-
workspaces.forEach(({ package: { name }, location }) => {
36-
if (isSubPath(location, filename) && isWorkspacePath(name, value)) {
37-
context.report({
38-
node,
39-
messageId: "noAbsoluteImports",
40-
fix: (fixer) =>
41-
fixer.replaceTextRange(
42-
[start + 1, end - 1],
43-
pathToImport(
44-
relative(dirname(filename), path.replace(name, location)),
34+
({ node, path, value, start, end, currentWorkspace }) => {
35+
if (isWorkspacePath(currentWorkspace.package.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(
44+
dirname(filename),
45+
path.replace(
46+
currentWorkspace.package.name,
47+
currentWorkspace.location,
48+
),
4549
),
4650
),
47-
});
48-
}
49-
});
51+
),
52+
});
53+
}
5054
},
5155
);
5256
};

lib/rules/no-cross-imports.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ module.exports.create = (context) => {
117117
.forEach(({ package: { name }, location }) => {
118118
if (
119119
name !== currentWorkspace.package.name &&
120+
!isSubPath(currentWorkspace.location, path) &&
120121
(isWorkspacePath(name, value) || isSubPath(location, path))
121122
) {
122123
context.report({

lib/rules/no-relative-imports.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports.create = (context) => {
2727
if (
2828
name !== path &&
2929
name !== currentWorkspace.package.name &&
30-
isSubPath(location, path)
30+
isSubPath(location, path) &&
31+
!isSubPath(currentWorkspace.location, path)
3132
) {
3233
context.report({
3334
node,

lib/rules/require-dependency.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module.exports.create = (context) => {
3838

3939
if (
4040
name !== currentWorkspace.package.name &&
41+
!isSubPath(currentWorkspace.location, path) &&
4142
(isWorkspacePath(name, value) || isSubPath(location, path)) &&
4243
!Object.keys(dependencies).includes(name) &&
4344
!Object.keys(peerDependencies).includes(name) &&

lib/utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ const pathToImport = (path) => {
4646
const getImport = (workspaces, filename, callback) => {
4747
if (!workspaces) return {};
4848

49-
const currentWorkspace = workspaces.find(({ location }) =>
50-
isSubPath(location, filename),
51-
);
49+
const currentWorkspace = workspaces
50+
.filter(({ location }) => isSubPath(location, filename))
51+
.sort((a, b) => b.location.length - a.location.length)
52+
.at(0);
5253

5354
if (!currentWorkspace) return {};
5455

tests/mocks.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ module.exports.findWorkspacesMock = () => [
2020
name: "@test/third-workspace",
2121
},
2222
},
23+
{
24+
location: "/test/outer",
25+
package: {
26+
name: "@test/outer",
27+
},
28+
},
29+
{
30+
location: "/test/outer/inner",
31+
package: {
32+
name: "@test/inner",
33+
},
34+
},
2335
{
2436
location: "/test/no-npm-scope-workspace",
2537
package: {

tests/rules/no-absolute-imports.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ describe("no-absolute-imports", () => {
4444
filename: "/test/no-npm-scope-workspace/index.js",
4545
code: "import './no-npm-scope-workspace';",
4646
},
47+
{
48+
filename: "/test/outer/index.js",
49+
code: "import '@test/inner';",
50+
},
51+
{
52+
filename: "/test/outer/inner/index.js",
53+
code: "import '@test/outer';",
54+
},
4755
],
4856

4957
invalid: [

tests/rules/no-cross-imports.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ describe("no-cross-imports", () => {
6262
filename: "/test/workspace/index.js",
6363
code: "import './no-npm-scope-workspace';",
6464
},
65+
{
66+
filename: "/test/outer/index.js",
67+
code: "import '@test/outer';",
68+
},
69+
{
70+
filename: "/test/outer/inner/index.js",
71+
code: "import '@test/inner';",
72+
},
73+
{
74+
filename: "/test/outer/index.js",
75+
code: "import './utils.js';",
76+
},
77+
{
78+
filename: "/test/outer/inner/index.js",
79+
code: "import './utils.js';",
80+
},
6581
],
6682

6783
invalid: [

tests/rules/no-relative-imports.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ describe("no-relative-imports", () => {
4444
filename: "/test/workspace/index.js",
4545
code: "import './no-npm-scope-workspace';",
4646
},
47+
{
48+
filename: "/test/outer/index.js",
49+
code: "import './utils.js';",
50+
},
51+
{
52+
filename: "/test/outer/inner/index.js",
53+
code: "import './utils.js';",
54+
},
4755
],
4856
invalid: [
4957
{

tests/rules/require-dependency.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ describe("require-dependency", () => {
3434
filename: "/test/workspace/index.js",
3535
code: "import './no-npm-scope-workspace';",
3636
},
37+
{
38+
filename: "/test/outer/index.js",
39+
code: "import '@test/outer';",
40+
},
41+
{
42+
filename: "/test/outer/inner/index.js",
43+
code: "import '@test/inner';",
44+
},
45+
{
46+
filename: "/test/outer/index.js",
47+
code: "import './utils.js';",
48+
},
49+
{
50+
filename: "/test/outer/inner/index.js",
51+
code: "import './utils.js';",
52+
},
3753
],
3854
invalid: [
3955
{

0 commit comments

Comments
 (0)