Skip to content

Commit

Permalink
reorder precedence to fix #373
Browse files Browse the repository at this point in the history
  • Loading branch information
benmosher committed Jun 9, 2016
1 parent a36c576 commit e044e37
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 60 deletions.
48 changes: 15 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,32 @@ resolvers are just npm packages, so [third party packages are supported](https:/

You can reference resolvers in several ways(in order of precedence):

- with an absolute path to resolver, used as a `computed property` name, which is supported since Node v4:
- as a conventional `eslint-import-resolver` name, like `eslint-import-resolver-foo`:

```js
// .eslintrc.js
module.exports = {
settings: {
'import/resolver': {
[path.resolve('../../../my-resolver')]: { someConfig: value }
}
}
}
```yaml
# .eslintrc.yml
settings:
# uses 'eslint-import-resolver-foo':
import/resolver: foo
```

- with a path relative to the closest `package.json` file:

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

- with a full npm module name, like `my-awesome-npm-module`:

```yaml
# .eslintrc.yml
settings:
import/resolver: './my-resolver'
import/resolver: 'my-awesome-npm-module'
```

- with an npm module name, like `my-awesome-npm-module`:

```js
// .eslintrc.js
module.exports = {
Expand All @@ -171,32 +162,23 @@ module.exports = {
}
```


```yaml
# .eslintrc.yml
settings:
import/resolver: 'my-awesome-npm-module'
```

- as a conventional `eslint-import-resolver` name, like `eslint-import-resolver-foo`:

- with a filesystem path to resolver, defined in this example as a `computed property` name:

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

`.eslintrc.yml`:
```yaml
settings:
import/resolver: foo
```
Relative paths will be resolved relative to the source's nearest `package.json` or
the process's current working directory if no `package.json` is found.



If you are interesting in writing a resolver, see the [spec](./resolvers/README.md) for more details.

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"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
44 changes: 18 additions & 26 deletions src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ 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, isAbsolute as isAbsoluteNode } from 'path'
import * as path from 'path'

const isAbsolute = isAbsoluteNode || isAbsoluteFallback

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

const fileExistsCache = new Map()

Expand All @@ -36,7 +33,7 @@ function fileExistsWithCaseSync(filepath, cacheSettings) {
// null means it resolved to a builtin
if (filepath === null) return true

const dir = dirname(filepath)
const dir = path.dirname(filepath)

let result = checkCache(filepath, cacheSettings)
if (result != null) return result
Expand All @@ -46,7 +43,7 @@ function fileExistsWithCaseSync(filepath, cacheSettings) {
result = true
} else {
const filenames = fs.readdirSync(dir)
if (filenames.indexOf(basename(filepath)) === -1) {
if (filenames.indexOf(path.basename(filepath)) === -1) {
result = false
} else {
result = fileExistsWithCaseSync(dir, cacheSettings)
Expand All @@ -58,7 +55,7 @@ function fileExistsWithCaseSync(filepath, cacheSettings) {

export function relative(modulePath, sourceFile, settings) {

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

const cacheSettings = assign({
Expand All @@ -73,18 +70,18 @@ export function relative(modulePath, sourceFile, settings) {
const cachedPath = checkCache(cacheKey, cacheSettings)
if (cachedPath !== undefined) return cachedPath

function cache(path) {
cachePath(cacheKey, path)
return path
function cache(resolvedPath) {
cachePath(cacheKey, resolvedPath)
return resolvedPath
}

function withResolver(resolver, config) {

function v1() {
try {
const path = resolver.resolveImport(modulePath, sourceFile, config)
if (path === undefined) return { found: false }
return { found: true, path }
const resolved = resolver.resolveImport(modulePath, sourceFile, config)
if (resolved === undefined) return { found: false }
return { found: true, path: resolved }
} catch (err) {
return { found: false }
}
Expand Down Expand Up @@ -148,27 +145,22 @@ function resolverReducer(resolvers, map) {
}

function requireResolver(name, sourceFile) {
// Try to resolve package with absolute path (/Volumes/....)
if (isAbsolute(name)) {
try {
return require(name)
} catch (err) { /* continue */ }
}

// Try to resolve package with path, relative to closest package.json
// Try to resolve package with conventional name
try {
const packageDir = pkgDir.sync(sourceFile)
return require(join(packageDir, name))
return require(`eslint-import-resolver-${name}`)
} catch (err) { /* continue */ }

// Try to resolve package with custom name (@myorg/resolver-name)
try {
return require(name)
} catch (err) { /* continue */ }

// Try to resolve package with conventional name
// Try to resolve package with path, relative to closest package.json
// or current working directory
try {
return require(`eslint-import-resolver-${name}`)
const baseDir = pkgDir.sync(sourceFile) || process.cwd()
// absolute paths ignore base, so this covers both
return require(path.resolve(baseDir, name))
} catch (err) { /* continue */ }

// all else failed
Expand Down

0 comments on commit e044e37

Please sign in to comment.