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

Fixes #1023 - Load exceptions in user resolvers are not reported #1024

Merged
merged 1 commit into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/files/load-error-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('TEST ERROR')
16 changes: 16 additions & 0 deletions tests/src/core/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,22 @@ describe('resolve', function () {
)).to.equal(utils.testFilePath('./jsx/MyCoolComponent.jsx'))
})

it('reports load exception in a user resolver', function () {

const testContext = utils.testContext({ 'import/resolver': './load-error-resolver' })
const testContextReports = []
testContext.report = function (reportInfo) {
testContextReports.push(reportInfo)
}

expect(resolve( '../files/exception'
, Object.assign({}, testContext, { getFilename: function () { return utils.getFilename('exception.js') } })
)).to.equal(undefined)
expect(testContextReports[0]).to.be.an('object')
expect(testContextReports[0].message).to.equal('Resolve error: TEST ERROR')
expect(testContextReports[0].loc).to.eql({ line: 1, column: 0 })
})

const caseDescribe = (!CASE_SENSITIVE_FS ? describe : describe.skip)
caseDescribe('case sensitivity', function () {
let file
Expand Down
43 changes: 25 additions & 18 deletions utils/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ exports.CASE_SENSITIVE_FS = CASE_SENSITIVE_FS

const fileExistsCache = new ModuleCache()

function tryRequire(target) {
let resolved;
try {
// Check if the target exists
resolved = require.resolve(target);
} catch(e) {
// If the target does not exist then just return undefined
return undefined;
}

// If the target exists then return the loaded module
return require(resolved);
}

// http://stackoverflow.com/a/27382838
exports.fileExistsWithCaseSync = function fileExistsWithCaseSync(filepath, cacheSettings) {
// don't care if the FS is case-sensitive
Expand Down Expand Up @@ -135,27 +149,20 @@ function resolverReducer(resolvers, map) {
throw new Error('invalid resolver config')
}

function getBaseDir(sourceFile) {
return pkgDir.sync(sourceFile) || process.cwd()
}
function requireResolver(name, sourceFile) {
// Try to resolve package with conventional name
try {
return require(`eslint-import-resolver-${name}`)
} catch (err) { /* continue */ }
let resolver = tryRequire(`eslint-import-resolver-${name}`) ||
tryRequire(name) ||
tryRequire(path.resolve(getBaseDir(sourceFile), name))

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

// Try to resolve package with path, relative to closest package.json
// or current working directory
try {
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
throw new Error(`unable to load resolver "${name}".`)
if (!resolver) {
throw new Error(`unable to load resolver "${name}".`)
} else {
return resolver;
}
}

const erroredContexts = new Set()
Expand Down