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

Custom resolver path #314

Merged
merged 10 commits into from
Jun 1, 2016
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
## [Unreleased]
### Added
- Added support for multiple webpack configs ([#181], thanks [@GreenGremlin])
- Added support for custom resolvers ([#314] thanks [@le0nik])

## [Unreleased]
### Fixed
Expand Down Expand Up @@ -251,6 +252,7 @@ for info on changes for earlier releases.
[#211]: https://github.com/benmosher/eslint-plugin-import/pull/211
[#164]: https://github.com/benmosher/eslint-plugin-import/pull/164
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#314]: https://github.com/benmosher/eslint-plugin-import/pull/314

[#342]: https://github.com/benmosher/eslint-plugin-import/issues/342
[#328]: https://github.com/benmosher/eslint-plugin-import/issues/328
Expand Down Expand Up @@ -310,4 +312,5 @@ for info on changes for earlier releases.
[@borisyankov]: https://github.com/borisyankov
[@gavriguy]: https://github.com/gavriguy
[@jkimbo]: https://github.com/jkimbo
[@le0nik]: https://github.com/le0nik
[@scottnonnenberg]: https://github.com/scottnonnenberg
68 changes: 63 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,77 @@ In the interest of supporting both of these, v0.11 introduces resolvers.
Currently [Node] and [Webpack] resolution have been implemented, but the
resolvers are just npm packages, so [third party packages are supported](https://github.com/benmosher/eslint-plugin-import/wiki/Resolvers) (and encouraged!).

Just install a resolver as `eslint-import-resolver-foo` and reference it as such:
You can reference resolvers in several ways(in order of precedence):

1. With an absolute path to resolver, used as a `computed property` name, which is supported since Node v4:

`.eslintrc.js`:
```js
{
settings: {
'import/resolver': {
[path.resolve('../../../my-resolver')]: { someConfig: value }
}
}
}
```

2. With a path relative to the closest `package.json` file:


`.eslintrc.js`:
```js
{
settings: {
'import/resolver': {
'./my-resolver': { someConfig: value }
}
}
}
```

`.eslintrc.yml`:
```yaml
settings:
import/resolver: foo
import/resolver: './my-resolver'
```

3. With an npm module name, like `my-awesome-npm-module`:

`.eslintrc.js`:
```js
{
settings: {
'import/resolver': {
'my-awesome-npm-module': { someConfig: value }
}
}
}
```

or with a config object:
`.eslintrc.yml`:
```yaml
settings:
import/resolver: 'my-awesome-npm-module'
```

4. As a conventional `eslint-import-resolver` name, like `eslint-import-resolver-foo`:

`.eslintrc.js`:
```js
{
settings: {
'import/resolver': {
foo: { someConfig: value }
}
}
}
```

`.eslintrc.yml`:
```yaml
settings:
import/resolver:
foo: { someConfigKey: value }
import/resolver: foo
```

If you are interesting in writing a resolver, see the [spec](./resolvers/README.md) for more details.
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"gulp-mocha": "^2.2.0",
"istanbul": "^0.4.0",
"mocha": "^2.2.1",
"pkg-dir": "^1.0.0",
"redux": "^3.0.4",
"rimraf": "2.5.2"
},
Expand All @@ -71,6 +72,7 @@
"es6-set": "^0.1.4",
"es6-symbol": "*",
"eslint-import-resolver-node": "^0.2.0",
"is-absolute": "^0.2.5",
"lodash.cond": "^4.3.0",
"lodash.endswith": "^4.0.1",
"lodash.find": "^4.3.0",
Expand Down
38 changes: 30 additions & 8 deletions src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import 'es6-symbol/implement'
import Map from 'es6-map'
import Set from 'es6-set'
import assign from 'object-assign'
import pkgDir from 'pkg-dir'
import isAbsoluteFallback from 'is-absolute'

import fs from 'fs'
import { dirname, basename, join } from 'path'
import { dirname, basename, join, isAbsolute as isAbsoluteNode } from 'path'

const isAbsolute = isAbsoluteNode || isAbsoluteFallback

export const CASE_SENSITIVE_FS = !fs.existsSync(join(__dirname, 'reSOLVE.js'))

Expand Down Expand Up @@ -53,7 +57,6 @@ function fileExistsWithCaseSync(filepath, cacheSettings) {
}

export function relative(modulePath, sourceFile, settings) {

const sourceDir = dirname(sourceFile)
, cacheKey = sourceDir + hashObject(settings) + modulePath

Expand All @@ -79,10 +82,10 @@ export function relative(modulePath, sourceFile, settings) {
function v1() {
try {
const path = resolver.resolveImport(modulePath, sourceFile, config)
if (path === undefined) return { found: false }
return { found: true, path }
if (path === undefined) return { found: false, path: null }
return { found: true, path: null }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@le0nik what motivated this change?

Copy link
Contributor Author

@le0nik le0nik Jun 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As i remember, it was due to this failing test after merge of master.

Ah, i see what you mean. My mistake. The test affected just the line above, not this one. I shouldn't have changed this.

} catch (err) {
return { found: false }
return { found: false, path: null }
}
}

Expand All @@ -106,7 +109,7 @@ export function relative(modulePath, sourceFile, settings) {
const resolvers = resolverReducer(configResolvers, new Map())

for (let [name, config] of resolvers) {
const resolver = requireResolver(name)
const resolver = requireResolver(name, modulePath)

let { path: fullPath, found } = withResolver(resolver, config)

Expand Down Expand Up @@ -143,9 +146,28 @@ function resolverReducer(resolvers, map) {
throw new Error('invalid resolver config')
}

function requireResolver(name) {
function requireResolver(name, modulePath) {
try {
return require(`eslint-import-resolver-${name}`)
// Try to resolve package with absolute path (/Volumes/....)
if (isAbsolute(name)) {
return require(name)
}

try {
// Try to resolve package with path, relative to closest package.json
const packageDir = pkgDir.sync(resolve(modulePath))

return require(join(packageDir, name))
} catch (err) {
try {
// Try to resolve package with custom name (@myorg/resolver-name)
return require(name)
} catch (err) { // eslint-disable-line no-shadow

// Try to resolve package with conventional name
return require(`eslint-import-resolver-${name}`)
}
}
} catch (err) {
throw new Error(`unable to load resolver "${name}".`)
}
Expand Down