Skip to content

Commit bf60e7b

Browse files
committed
Only use dir name from file path
1 parent 94b4006 commit bf60e7b

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

lib/utils.js

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

33
const getPackages = require('get-monorepo-packages');
4-
const { relative, sep, resolve, isAbsolute } = require('path');
4+
const { dirname, relative, sep, resolve, isAbsolute } = require('path');
55

66
const isSubPath = (parent, path) => {
77
const relativePath = relative(parent, path);
@@ -16,7 +16,7 @@ const getImport = (context, callback) => ({
1616
ImportDeclaration: node => {
1717
if (node.source.type === 'Literal') {
1818
const { value, range } = node.source;
19-
const path = resolve(context.getFilename(), value);
19+
const path = resolve(dirname(context.getFilename()), value);
2020
const [start, end] = range;
2121
callback({ node, value, path, start, end });
2222
}

tests/rules/no-cross-imports.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ ruleTester.run('no-cross-imports', rule, {
1313
'someFunction();',
1414
{
1515
options: [{ allow: '@test/workspace' }],
16-
filename: '/some/path',
16+
filename: '/some/path.js',
1717
code: "import '@test/workspace';",
1818
},
1919
{
2020
options: [{ allow: ['@test/workspace', '@test/another-workspace'] }],
21-
filename: '/some/path',
21+
filename: '/some/path.js',
2222
code: "import '@test/workspace';import '@test/another-workspace';",
2323
},
2424
{
25-
filename: '/test/workspace',
25+
filename: '/test/workspace/test.js',
2626
code: "import '@test/workspace';",
2727
},
2828
{
29-
filename: '/test/workspace',
29+
filename: '/test/workspace/test.js',
3030
code: "import './some/thing'",
3131
},
3232
],
3333

3434
invalid: [
3535
{
3636
code: "import workspace from '@test/workspace';",
37-
filename: '/some/path',
37+
filename: '/some/path.js',
3838
errors: [
3939
{
4040
message: 'Import from package "@test/workspace" is not allowed',
@@ -43,7 +43,7 @@ ruleTester.run('no-cross-imports', rule, {
4343
},
4444
{
4545
code: "import('@test/workspace');",
46-
filename: '/some/path',
46+
filename: '/some/path.js',
4747
errors: [
4848
{
4949
message: 'Import from package "@test/workspace" is not allowed',
@@ -52,7 +52,7 @@ ruleTester.run('no-cross-imports', rule, {
5252
},
5353
{
5454
code: "require('@test/workspace');",
55-
filename: '/some/path',
55+
filename: '/some/path.js',
5656
errors: [
5757
{
5858
message: 'Import from package "@test/workspace" is not allowed',
@@ -61,7 +61,7 @@ ruleTester.run('no-cross-imports', rule, {
6161
},
6262
{
6363
code: "import workspace from '@test/workspace';",
64-
filename: '/some/path',
64+
filename: '/some/path.js',
6565
errors: [
6666
{
6767
message: 'Import from package "@test/workspace" is not allowed',
@@ -70,7 +70,7 @@ ruleTester.run('no-cross-imports', rule, {
7070
},
7171
{
7272
code: "import '@test/workspace';",
73-
filename: '/some/path',
73+
filename: '/some/path.js',
7474
errors: [
7575
{
7676
message: 'Import from package "@test/workspace" is not allowed',
@@ -79,7 +79,7 @@ ruleTester.run('no-cross-imports', rule, {
7979
},
8080
{
8181
code: "import '@test/workspace/some/path';",
82-
filename: '/some/path',
82+
filename: '/some/path.js',
8383
errors: [
8484
{
8585
message: 'Import from package "@test/workspace" is not allowed',
@@ -88,7 +88,7 @@ ruleTester.run('no-cross-imports', rule, {
8888
},
8989
{
9090
code: "import '../../test/workspace';",
91-
filename: '/some/path',
91+
filename: '/some/path.js',
9292
errors: [
9393
{
9494
message: 'Import from package "@test/workspace" is not allowed',
@@ -97,7 +97,7 @@ ruleTester.run('no-cross-imports', rule, {
9797
},
9898
{
9999
code: "import '../../test/workspace/some/path';",
100-
filename: '/some/path',
100+
filename: '/some/path.js',
101101
errors: [
102102
{
103103
message: 'Import from package "@test/workspace" is not allowed',
@@ -106,7 +106,7 @@ ruleTester.run('no-cross-imports', rule, {
106106
},
107107
{
108108
code: "import '@test/workspace';import '@test/another-workspace';",
109-
filename: '/some/path',
109+
filename: '/some/path.js',
110110
errors: [
111111
{
112112
message: 'Import from package "@test/workspace" is not allowed',
@@ -120,7 +120,7 @@ ruleTester.run('no-cross-imports', rule, {
120120
{
121121
code: "import '@test/workspace';import '@test/another-workspace';",
122122
options: [{ allow: '@test/workspace' }],
123-
filename: '/some/path',
123+
filename: '/some/path.js',
124124
errors: [
125125
{
126126
message:
@@ -130,13 +130,23 @@ ruleTester.run('no-cross-imports', rule, {
130130
},
131131
{
132132
options: [{ allow: '@test/workspacetest' }],
133-
filename: '/some/path',
133+
filename: '/some/path.js',
134134
code: "import '@test/workspace';",
135135
errors: [
136136
{
137137
message: 'Import from package "@test/workspace" is not allowed',
138138
},
139139
],
140140
},
141+
{
142+
filename: '/test/workspace/test.js',
143+
code: "import '../another-workspace/test'",
144+
errors: [
145+
{
146+
message:
147+
'Import from package "@test/another-workspace" is not allowed',
148+
},
149+
],
150+
},
141151
],
142152
});

tests/rules/no-relative-imports.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ ruleTester.run('no-relative-imports', rule, {
1212
"import '../some/relative/path';",
1313
'someFunction();',
1414
{
15-
filename: '/test/workspace',
15+
filename: '/test/workspace/test.js',
1616
code: "import '../workspace';",
1717
},
1818
{
19-
filename: '/test/workspace',
19+
filename: '/test/workspace/test.js',
2020
code: "import './some/path';",
2121
},
2222
{
23-
filename: '/test/workspace/some/path',
23+
filename: '/test/workspace/some/path.js',
2424
code: "import '../another/path';",
2525
},
2626
],
2727

2828
invalid: [
2929
{
3030
code: "import workspace from '../../test/workspace';",
31-
filename: '/some/path',
31+
filename: '/some/path.js',
3232
output: "import workspace from '@test/workspace';",
3333
errors: [
3434
{
@@ -38,7 +38,7 @@ ruleTester.run('no-relative-imports', rule, {
3838
},
3939
{
4040
code: "require('../../test/workspace');",
41-
filename: '/some/path',
41+
filename: '/some/path.js',
4242
output: "require('@test/workspace');",
4343
errors: [
4444
{
@@ -48,7 +48,7 @@ ruleTester.run('no-relative-imports', rule, {
4848
},
4949
{
5050
code: "import('../../test/workspace');",
51-
filename: '/some/path',
51+
filename: '/some/path.js',
5252
output: "import('@test/workspace');",
5353
errors: [
5454
{
@@ -58,7 +58,7 @@ ruleTester.run('no-relative-imports', rule, {
5858
},
5959
{
6060
code: "import '../../test/workspace';",
61-
filename: '/some/path',
61+
filename: '/some/path.js',
6262
output: "import '@test/workspace';",
6363
errors: [
6464
{
@@ -68,7 +68,7 @@ ruleTester.run('no-relative-imports', rule, {
6868
},
6969
{
7070
code: "import workspace from '../another-workspace';",
71-
filename: '/test/workspace',
71+
filename: '/test/workspace/test.js',
7272
output: "import workspace from '@test/another-workspace';",
7373
errors: [
7474
{
@@ -78,7 +78,7 @@ ruleTester.run('no-relative-imports', rule, {
7878
},
7979
{
8080
code: "import workspace from '../another-workspace/testing';",
81-
filename: '/test/workspace',
81+
filename: '/test/workspace/test.js',
8282
output: "import workspace from '@test/another-workspace/testing';",
8383
errors: [
8484
{

0 commit comments

Comments
 (0)