Skip to content

Commit d3e0df6

Browse files
committed
Make 'no-relative-imports' fixable
1 parent b6c761c commit d3e0df6

File tree

4 files changed

+32
-21
lines changed

4 files changed

+32
-21
lines changed

lib/rules/no-cross-imports.js

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

3-
const { isSubPath, packages, getImportValue } = require('../utils');
4-
const { resolve } = require('path');
3+
const { isSubPath, packages, getImport } = require('../utils');
54

65
module.exports.meta = {
76
type: 'problem',
@@ -40,12 +39,9 @@ module.exports.create = context => {
4039
const allowed = typeof allow === 'string' ? [allow] : allow;
4140
const forbidden = packages.filter(({ name }) => !allowed.includes(name));
4241

43-
return getImportValue(context, (value, node) => {
42+
return getImport(context, ({ node, value, path }) => {
4443
forbidden.forEach(({ name, location }) => {
45-
if (
46-
isSubPath(name, value) ||
47-
isSubPath(location, resolve(context.getFilename(), value))
48-
) {
44+
if (isSubPath(name, value) || isSubPath(location, path)) {
4945
context.report({
5046
node,
5147
message: 'Import from package "{{name}}" is not allowed',

lib/rules/no-relative-imports.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
'use strict';
22

3-
const { isSubPath, packages, getImportValue } = require('../utils');
4-
const { resolve } = require('path');
3+
const { isSubPath, packages, getImport } = require('../utils');
54

65
module.exports.meta = {
76
type: 'problem',
87
docs: {
98
url:
109
'https://github.com/joshuajaco/eslint-plugin-workspaces/docs/rules/no-relative-imports.md',
1110
},
11+
fixable: 'code',
1212
};
1313

14-
module.exports.schema = [];
15-
1614
module.exports.create = context =>
17-
getImportValue(context, (value, node) => {
18-
packages.forEach(({ location }) => {
15+
getImport(context, ({ node, path, start, end }) => {
16+
packages.forEach(({ name, location }) => {
1917
if (
2018
!isSubPath(location, context.getFilename()) &&
21-
isSubPath(location, resolve(context.getFilename(), value))
22-
)
19+
isSubPath(location, path)
20+
) {
2321
context.report({
2422
node,
2523
message: 'Relative imports of other packages are not allowed',
24+
fix: fixer =>
25+
fixer.replaceTextRange(
26+
[start + 1, end - 1],
27+
path.replace(location, name),
28+
),
2629
});
30+
}
2731
});
2832
});

lib/utils.js

Lines changed: 11 additions & 6 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, isAbsolute } = require('path');
4+
const { relative, sep, resolve, isAbsolute } = require('path');
55

66
const isSubPath = (parent, path) => {
77
const relativePath = relative(parent, path);
@@ -12,10 +12,13 @@ const packages = getPackages(process.cwd()).map(
1212
({ package: { name }, location }) => ({ name, location }),
1313
);
1414

15-
const getImportValue = (context, callback) => ({
15+
const getImport = (context, callback) => ({
1616
ImportDeclaration: node => {
1717
if (node.source.type === 'Literal') {
18-
callback(node.source.value, node);
18+
const { value, range } = node.source;
19+
const path = resolve(context.getFilename(), value);
20+
const [start, end] = range;
21+
callback({ node, value, path, start, end });
1922
}
2023
},
2124
ExpressionStatement: node => {
@@ -26,10 +29,12 @@ const getImportValue = (context, callback) => ({
2629
(node.expression.callee.type === 'Identifier' &&
2730
node.expression.callee.name === 'require'))
2831
) {
29-
const { value } = node.expression.arguments[0];
30-
callback(value, node);
32+
const { value, range } = node.expression.arguments[0];
33+
const path = resolve(context.getFilename(), value);
34+
const [start, end] = range;
35+
callback({ node, value, path, start, end });
3136
}
3237
},
3338
});
3439

35-
module.exports = { isSubPath, packages, getImportValue };
40+
module.exports = { isSubPath, packages, getImport };

tests/rules/no-relative-imports.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ ruleTester.run('no-relative-imports', rule, {
2828
{
2929
code: "import workspace from '../../test/workspace';",
3030
filename: '/some/path',
31+
output: "import workspace from '@test/workspace';",
3132
errors: [
3233
{
3334
message: 'Relative imports of other packages are not allowed',
@@ -37,6 +38,7 @@ ruleTester.run('no-relative-imports', rule, {
3738
{
3839
code: "require('../../test/workspace');",
3940
filename: '/some/path',
41+
output: "require('@test/workspace');",
4042
errors: [
4143
{
4244
message: 'Relative imports of other packages are not allowed',
@@ -46,6 +48,7 @@ ruleTester.run('no-relative-imports', rule, {
4648
{
4749
code: "import('../../test/workspace');",
4850
filename: '/some/path',
51+
output: "import('@test/workspace');",
4952
errors: [
5053
{
5154
message: 'Relative imports of other packages are not allowed',
@@ -55,6 +58,7 @@ ruleTester.run('no-relative-imports', rule, {
5558
{
5659
code: "import '../../test/workspace';",
5760
filename: '/some/path',
61+
output: "import '@test/workspace';",
5862
errors: [
5963
{
6064
message: 'Relative imports of other packages are not allowed',
@@ -64,6 +68,7 @@ ruleTester.run('no-relative-imports', rule, {
6468
{
6569
code: "import workspace from '../another-workspace';",
6670
filename: '/test/workspace',
71+
output: "import workspace from '@test/another-workspace';",
6772
errors: [
6873
{
6974
message: 'Relative imports of other packages are not allowed',
@@ -73,6 +78,7 @@ ruleTester.run('no-relative-imports', rule, {
7378
{
7479
code: "import workspace from '../another-workspace/testing';",
7580
filename: '/test/workspace',
81+
output: "import workspace from '@test/another-workspace/testing';",
7682
errors: [
7783
{
7884
message: 'Relative imports of other packages are not allowed',

0 commit comments

Comments
 (0)