Skip to content

Commit

Permalink
[New] no-relative-packages: add fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall authored and ljharb committed Feb 4, 2022
1 parent 0595a2f commit 02ccbc1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
### Added
- [`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])
- [`no-dynamic-require`]: support dynamic import with espree ([#2371], thanks [@sosukesuzuki])
- [`no-relative-packages`]: add fixer ([#2381], thanks [@forivall])

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

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

[#2381]: https://github.com/import-js/eslint-plugin-import/pull/2381
[#2378]: https://github.com/import-js/eslint-plugin-import/pull/2378
[#2371]: https://github.com/import-js/eslint-plugin-import/pull/2371
[#2367]: https://github.com/import-js/eslint-plugin-import/pull/2367
Expand Down
1 change: 1 addition & 0 deletions docs/rules/no-relative-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Use this rule to prevent importing packages through relative paths.
It's useful in Yarn/Lerna workspaces, were it's possible to import a sibling
package using `../package` relative path, while direct `package` is the correct one.

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

### Examples

Expand Down
8 changes: 8 additions & 0 deletions src/rules/no-relative-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisi
import importType from '../core/importType';
import docsUrl from '../docsUrl';

/** @param {string} filePath */
function toPosixPath(filePath) {
return filePath.replace(/\\/g, '/');
}

function findNamedPackage(filePath) {
const found = readPkgUp({ cwd: filePath });
if (found.pkg && !found.pkg.name) {
Expand Down Expand Up @@ -42,6 +47,8 @@ function checkImportForRelativePackage(context, importPath, node) {
context.report({
node,
message: `Relative import from another package is not allowed. Use \`${properImport}\` instead of \`${importPath}\``,
fix: fixer => fixer.replaceText(node, JSON.stringify(toPosixPath(properImport)))
,
});
}
}
Expand All @@ -52,6 +59,7 @@ module.exports = {
docs: {
url: docsUrl('no-relative-packages'),
},
fixable: 'code',
schema: [makeOptionsSchema()],
},

Expand Down
4 changes: 4 additions & 0 deletions tests/src/rules/no-relative-packages.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ ruleTester.run('no-relative-packages', rule, {
line: 1,
column: 17,
} ],
output: 'import foo from "package-named"',
}),
test({
code: 'import foo from "../package-named"',
Expand All @@ -56,6 +57,7 @@ ruleTester.run('no-relative-packages', rule, {
line: 1,
column: 17,
} ],
output: 'import foo from "package-named"',
}),
test({
code: 'import foo from "../package-scoped"',
Expand All @@ -65,6 +67,7 @@ ruleTester.run('no-relative-packages', rule, {
line: 1,
column: 17,
} ],
output: `import foo from "@scope/package-named"`,
}),
test({
code: 'import bar from "../bar"',
Expand All @@ -74,6 +77,7 @@ ruleTester.run('no-relative-packages', rule, {
line: 1,
column: 17,
} ],
output: `import bar from "eslint-plugin-import/tests/files/bar"`,
}),
],
});

0 comments on commit 02ccbc1

Please sign in to comment.