Skip to content

Commit

Permalink
fix: allow relative imports within a package
Browse files Browse the repository at this point in the history
  • Loading branch information
azz committed Dec 14, 2017
1 parent 592a7e1 commit 8669e77
Show file tree
Hide file tree
Showing 3 changed files with 933 additions and 27 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"eslint-module-utils": "^2.1.1",
"globby": "^7.1.1",
"load-json-file": "^4.0.0",
"minimatch": "^3.0.4",
"parse-package-name": "^0.1.0",
"path-is-inside": "^1.0.2"
}
Expand Down
13 changes: 12 additions & 1 deletion src/rules/no-relative-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import moduleVisitor, {
makeOptionsSchema,
} from 'eslint-module-utils/moduleVisitor';
import isInside from 'path-is-inside';
import minimatch from 'minimatch';
import path from 'path';
import getPackages from '../get-packages';

Expand All @@ -18,8 +19,9 @@ export const create = context => {

return moduleVisitor(node => {
const resolvedPath = resolve(node.value, context);
const packageDir = getPackageDir(sourceFsPath, packages);

if (!resolvedPath || isInside(resolvedPath, path.dirname(sourceFsPath))) {
if (!packageDir || !resolvedPath || isInside(resolvedPath, packageDir)) {
return;
}

Expand All @@ -41,3 +43,12 @@ export const create = context => {
}
}, moduleUtilOptions);
};

const getPackageDir = (filePath, packages) => {
const match = packages.find(pkg =>
minimatch(filePath, path.join(pkg.fsPath, '**'))
);
if (match) {
return match.fsPath;
}
};
Loading

0 comments on commit 8669e77

Please sign in to comment.