Skip to content

Commit

Permalink
fix: imports should always use posix / http paths, even on win32
Browse files Browse the repository at this point in the history
  • Loading branch information
forivall committed Feb 8, 2022
1 parent e09468f commit 2ffc5bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/rules/no-relative-packages.js
Expand Up @@ -6,6 +6,12 @@ import moduleVisitor, { makeOptionsSchema } from 'eslint-module-utils/moduleVisi
import importType from '../core/importType';
import docsUrl from '../docsUrl';

/** @type {(filePath: string) => string} */
const toPosixPath =
path.sep === '\\'
? (filePath) => filePath.replace(/\\/g, '/')
: (filePath) => filePath;

function findNamedPackage(filePath) {
const found = readPkgUp({ cwd: filePath });
if (found.pkg && !found.pkg.name) {
Expand Down Expand Up @@ -42,7 +48,7 @@ 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(properImport))
fix: fixer => fixer.replaceText(node, JSON.stringify(toPosixPath(properImport)))
,
});
}
Expand Down
4 changes: 2 additions & 2 deletions tests/src/rules/no-relative-packages.js
Expand Up @@ -67,7 +67,7 @@ ruleTester.run('no-relative-packages', rule, {
line: 1,
column: 17,
} ],
output: `import foo from "${normalize('@scope/package-named')}"`,
output: `import foo from "@scope/package-named"`,
}),
test({
code: 'import bar from "../bar"',
Expand All @@ -77,7 +77,7 @@ ruleTester.run('no-relative-packages', rule, {
line: 1,
column: 17,
} ],
output: `import bar from "${normalize('eslint-plugin-import/tests/files/bar')}"`,
output: `import bar from "eslint-plugin-import/tests/files/bar"`,
}),
],
});

0 comments on commit 2ffc5bd

Please sign in to comment.