Skip to content

Commit fb0cbeb

Browse files
arcanisljharb
authored andcommitted
utils: Uses createRequireFromPath to resolve loaders
1 parent 16ae652 commit fb0cbeb

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

tests/files/node_modules/eslint-import-resolver-foo/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/src/core/resolve.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ describe('resolve', function () {
9494
)).to.equal(utils.testFilePath('./bar.jsx'))
9595
})
9696

97+
it('finds resolvers from the source files rather than eslint-module-utils', function () {
98+
const testContext = utils.testContext({ 'import/resolver': { 'foo': {} } })
99+
100+
expect(resolve( '../files/foo'
101+
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('foo.js') } }),
102+
)).to.equal(utils.testFilePath('./bar.jsx'))
103+
})
104+
97105
it('reports invalid import/resolver config', function () {
98106
const testContext = utils.testContext({ 'import/resolver': 123.456 })
99107
const testContextReports = []

tests/src/rules/no-unresolved.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,19 +343,19 @@ ruleTester.run('no-unresolved unknown resolver', rule, {
343343
// logs resolver load error
344344
test({
345345
code: 'import "./malformed.js"',
346-
settings: { 'import/resolver': 'foo' },
346+
settings: { 'import/resolver': 'doesnt-exist' },
347347
errors: [
348-
`Resolve error: unable to load resolver "foo".`,
348+
`Resolve error: unable to load resolver "doesnt-exist".`,
349349
`Unable to resolve path to module './malformed.js'.`,
350350
],
351351
}),
352352

353353
// only logs resolver message once
354354
test({
355355
code: 'import "./malformed.js"; import "./fake.js"',
356-
settings: { 'import/resolver': 'foo' },
356+
settings: { 'import/resolver': 'doesnt-exist' },
357357
errors: [
358-
`Resolve error: unable to load resolver "foo".`,
358+
`Resolve error: unable to load resolver "doesnt-exist".`,
359359
`Unable to resolve path to module './malformed.js'.`,
360360
`Unable to resolve path to module './fake.js'.`,
361361
],

utils/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
55

66
## Unreleased
77

8+
### Fixed
9+
- Uses createRequireFromPath to resolve loaders ([#1591], thanks [@arcanis])
10+
811
## v2.5.0 - 2019-12-07
912

1013
### Added
@@ -59,7 +62,7 @@ Yanked due to critical issue with cache key resulting from #839.
5962
- `unambiguous.test()` regex is now properly in multiline mode
6063

6164

62-
65+
[#1591]: https://github.com/benmosher/eslint-plugin-import/pull/1591
6366
[#1551]: https://github.com/benmosher/eslint-plugin-import/pull/1551
6467
[#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435
6568
[#1409]: https://github.com/benmosher/eslint-plugin-import/pull/1409
@@ -77,3 +80,4 @@ Yanked due to critical issue with cache key resulting from #839.
7780
[@christophercurrie]: https://github.com/christophercurrie
7881
[@brettz9]: https://github.com/brettz9
7982
[@JounQin]: https://github.com/JounQin
83+
[@arcanis]: https://github.com/arcanis

utils/resolve.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ exports.__esModule = true
44
const pkgDir = require('pkg-dir')
55

66
const fs = require('fs')
7+
const Module = require('module')
78
const path = require('path')
89

910
const hashObject = require('./hash').hashObject
@@ -14,11 +15,26 @@ exports.CASE_SENSITIVE_FS = CASE_SENSITIVE_FS
1415

1516
const fileExistsCache = new ModuleCache()
1617

17-
function tryRequire(target) {
18+
// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0)
19+
const createRequireFromPath = Module.createRequireFromPath || function (filename) {
20+
const mod = new Module(filename, null)
21+
mod.filename = filename
22+
mod.paths = Module._nodeModulePaths(path.dirname(filename))
23+
24+
mod._compile(`module.exports = require;`, filename)
25+
26+
return mod.exports
27+
}
28+
29+
function tryRequire(target, sourceFile) {
1830
let resolved
1931
try {
2032
// Check if the target exists
21-
resolved = require.resolve(target)
33+
if (sourceFile != null) {
34+
resolved = createRequireFromPath(sourceFile).resolve(target)
35+
} else {
36+
resolved = require.resolve(target)
37+
}
2238
} catch(e) {
2339
// If the target does not exist then just return undefined
2440
return undefined
@@ -154,8 +170,8 @@ function getBaseDir(sourceFile) {
154170
}
155171
function requireResolver(name, sourceFile) {
156172
// Try to resolve package with conventional name
157-
let resolver = tryRequire(`eslint-import-resolver-${name}`) ||
158-
tryRequire(name) ||
173+
let resolver = tryRequire(`eslint-import-resolver-${name}`, sourceFile) ||
174+
tryRequire(name, sourceFile) ||
159175
tryRequire(path.resolve(getBaseDir(sourceFile), name))
160176

161177
if (!resolver) {

0 commit comments

Comments
 (0)