Skip to content

Commit 02ccbc1

Browse files
forivallljharb
authored andcommitted
[New] no-relative-packages: add fixer
1 parent 0595a2f commit 02ccbc1

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
99
### Added
1010
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`, `namespace`, `no-unused-modules`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
1111
- [`no-dynamic-require`]: support dynamic import with espree ([#2371], thanks [@sosukesuzuki])
12+
- [`no-relative-packages`]: add fixer ([#2381], thanks [@forivall])
1213

1314
### Fixed
1415
- [`default`]: `typescript-eslint-parser`: avoid a crash on exporting as namespace (thanks [@ljharb])
@@ -969,6 +970,7 @@ for info on changes for earlier releases.
969970

970971
[`memo-parser`]: ./memo-parser/README.md
971972

973+
[#2381]: https://github.com/import-js/eslint-plugin-import/pull/2381
972974
[#2378]: https://github.com/import-js/eslint-plugin-import/pull/2378
973975
[#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371
974976
[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367

docs/rules/no-relative-packages.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Use this rule to prevent importing packages through relative paths.
55
It's useful in Yarn/Lerna workspaces, were it's possible to import a sibling
66
package using `../package` relative path, while direct `package` is the correct one.
77

8+
+(fixable) The `--fix` option on the [command line] automatically fixes problems reported by this rule.
89

910
### Examples
1011

src/rules/no-relative-packages.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisi
66
import importType from '../core/importType';
77
import docsUrl from '../docsUrl';
88

9+
/** @param {string} filePath */
10+
function toPosixPath(filePath) {
11+
return filePath.replace(/\\/g, '/');
12+
}
13+
914
function findNamedPackage(filePath) {
1015
const found = readPkgUp({ cwd: filePath });
1116
if (found.pkg && !found.pkg.name) {
@@ -42,6 +47,8 @@ function checkImportForRelativePackage(context, importPath, node) {
4247
context.report({
4348
node,
4449
message: `Relative import from another package is not allowed. Use \`${properImport}\` instead of \`${importPath}\``,
50+
fix: fixer => fixer.replaceText(node, JSON.stringify(toPosixPath(properImport)))
51+
,
4552
});
4653
}
4754
}
@@ -52,6 +59,7 @@ module.exports = {
5259
docs: {
5360
url: docsUrl('no-relative-packages'),
5461
},
62+
fixable: 'code',
5563
schema: [makeOptionsSchema()],
5664
},
5765

tests/src/rules/no-relative-packages.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ ruleTester.run('no-relative-packages', rule, {
4747
line: 1,
4848
column: 17,
4949
} ],
50+
output: 'import foo from "package-named"',
5051
}),
5152
test({
5253
code: 'import foo from "../package-named"',
@@ -56,6 +57,7 @@ ruleTester.run('no-relative-packages', rule, {
5657
line: 1,
5758
column: 17,
5859
} ],
60+
output: 'import foo from "package-named"',
5961
}),
6062
test({
6163
code: 'import foo from "../package-scoped"',
@@ -65,6 +67,7 @@ ruleTester.run('no-relative-packages', rule, {
6567
line: 1,
6668
column: 17,
6769
} ],
70+
output: `import foo from "@scope/package-named"`,
6871
}),
6972
test({
7073
code: 'import bar from "../bar"',
@@ -74,6 +77,7 @@ ruleTester.run('no-relative-packages', rule, {
7477
line: 1,
7578
column: 17,
7679
} ],
80+
output: `import bar from "eslint-plugin-import/tests/files/bar"`,
7781
}),
7882
],
7983
});

0 commit comments

Comments
 (0)