Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] no-relative-packages: add fixer #2381

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`,
}),
],
});