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

Patch 1 #1

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# eslint-import-resolver-babel-root-import

A [babel-root-import](https://github.com/michaelzoidl/babel-root-import)
A [babel-plugin-root-import](https://github.com/michaelzoidl/babel-plugin-root-import)
resolver for [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import).

## Installation

```sh
npm install --save-dev eslint-plugin-import eslint-import-resolver-babel-root-import
npm install --save-dev eslint-plugin-import eslint-import-resolver-babel-plugin-root-import
```

## Usage

Inside your `.eslintrc` file, pass this resolver to `eslint-plugin-import`:
```
"settings": {
"import/resolver": "babel-root-import"
"import/resolver": "babel-plugin-root-import"
}
```

And see [babel-root-import][babel-root-import] to know how to configure
And see [babel-plugin-root-import][babel-plugin-root-import] to know how to configure
your prefix/suffix.

### Example
Expand All @@ -29,9 +29,11 @@ your prefix/suffix.
"rules": {},
"settings": {
"import/resolver": {
"babel-root-import": {}
"babel-plugin-root-import": {
"rootPathSuffix": "src",
"rootPathPrefix": "~"
}
}
}
}
```

Expand All @@ -40,4 +42,4 @@ your prefix/suffix.
MIT, see [LICENSE.md](/LICENSE.md) for details.


[babel-root-import]: https://github.com/michaelzoidl/babel-root-import
[babel-plugin-root-import]: https://github.com/michaelzoidl/babel-plugin-root-import
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "eslint-import-resolver-babel-root-import",
"name": "eslint-import-resolver-babel-plugin-root-import",
"version": "0.0.3",
"main": "src/index.js",
"description": "babel-plugin-root-import resolver for eslint-plugin-import",
Expand Down Expand Up @@ -35,9 +35,9 @@
"import"
],
"dependencies": {
"babel-root-import": "^3.2.2",
"eslint-import-resolver-node": "^0.2.1",
"json5": "^0.5.0"
"babel-plugin-root-import": "^5.1.0",
"eslint-import-resolver-node": "^0.3.0",
"json5": "^0.5.1"
},
"devDependencies": {
"chai": "^3.5.0",
Expand Down
43 changes: 5 additions & 38 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const JSON5 = require('json5');
const nodeResolve = require('eslint-import-resolver-node').resolve;

/* eslint-disable no-console */
const babelRootImport = require('babel-root-import/build/helper.js');
const babelRootImport = require('babel-plugin-root-import/build/helper.js');

// newer version of babel root import exports the 2 functions
// but older versions exported a class
Expand All @@ -24,38 +24,6 @@ if (babelRootImport.default) {
transformRelativeToRootPath = transformRelativeToRootPath.bind(babelRootImportObj);
}

// returns the root import config as an object
function getConfigFromBabel(start, babelrc = '.babelrc') {
if (start === '/') return [];

const packageJSONPath = path.join(start, 'package.json');
const packageJSON = require(packageJSONPath);
const babelConfig = packageJSON.babel;
if (babelConfig) {
const pluginConfig = babelConfig.plugins.find(p => (
p[0] === 'babel-root-import'
));
process.chdir(path.dirname(packageJSONPath));
return pluginConfig[1];
}

const babelrcPath = path.join(start, babelrc);
if (fs.existsSync(babelrcPath)) {
const babelrcJson = JSON5.parse(fs.readFileSync(babelrcPath, 'utf8'));
if (babelrcJson && Array.isArray(babelrcJson.plugins)) {
const pluginConfig = babelrcJson.plugins.find(p => (
p[0] === 'babel-root-import'
));
// The src path inside babelrc are from the root so we have
// to change the working directory for the same directory
// to make the mapping to work properly
process.chdir(path.dirname(babelrcPath));
return pluginConfig[1];
}
}
return getConfigFromBabel(path.dirname(start));
}

exports.interfaceVersion = 2;

/**
Expand All @@ -65,11 +33,10 @@ exports.interfaceVersion = 2;
* @param {string} source - the module to resolve; i.e './some-module'
* @param {string} file - the importing file's full path; i.e. '/usr/local/bin/file.js'
* @param {object} config - the resolver options
* @param {string} babelrc - the name of the babelrc file
* @return {object}
*/
exports.resolve = (source, file, config, babelrc) => {
const opts = getConfigFromBabel(process.cwd(), babelrc);
exports.resolve = (source, file, config) => {
const opts = config;

// [{rootPathPrefix: rootPathSuffix}]
const rootPathConfig = [];
Expand Down Expand Up @@ -99,7 +66,7 @@ exports.resolve = (source, file, config, babelrc) => {

let rootPathSuffix = '';
if (opts.rootPathSuffix && typeof opts.rootPathSuffix === 'string') {
rootPathSuffix = `/${opts.rootPathSuffix.replace(/^(\/)|(\/)$/g, '')}`;
rootPathSuffix = opts.rootPathSuffix;
}

rootPathConfig.push({
Expand All @@ -114,7 +81,7 @@ exports.resolve = (source, file, config, babelrc) => {
const prefix = option.rootPathPrefix;
const suffix = option.rootPathSuffix;
if (hasRootPathPrefixInString(source, option.rootPathPrefix)) {
transformedSource = transformRelativeToRootPath(source, suffix, prefix);
transformedSource = transformRelativeToRootPath(source, suffix, prefix, file);
break;
}
}
Expand Down