eslint-module-utils
Advanced tools
Comparing version 2.5.0 to 2.5.1
@@ -8,2 +8,6 @@ # Change Log | ||
### Fixed | ||
- Uses createRequireFromPath to resolve loaders ([#1591], thanks [@arcanis]) | ||
- report the error stack on a resolution error ([#599], thanks [@sompylasar]) | ||
## v2.5.0 - 2019-12-07 | ||
@@ -63,3 +67,3 @@ | ||
[#1591]: https://github.com/benmosher/eslint-plugin-import/pull/1591 | ||
[#1551]: https://github.com/benmosher/eslint-plugin-import/pull/1551 | ||
@@ -74,2 +78,3 @@ [#1435]: https://github.com/benmosher/eslint-plugin-import/pull/1435 | ||
[#1035]: https://github.com/benmosher/eslint-plugin-import/issues/1035 | ||
[#599]: https://github.com/benmosher/eslint-plugin-import/pull/599 | ||
@@ -83,1 +88,3 @@ [@hulkish]: https://github.com/hulkish | ||
[@JounQin]: https://github.com/JounQin | ||
[@arcanis]: https://github.com/arcanis | ||
[@sompylasar]: https://github.com/sompylasar |
{ | ||
"name": "eslint-module-utils", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"description": "Core utilities to support eslint-plugin-import and other module-related plugins.", | ||
@@ -9,2 +9,3 @@ "engines": { | ||
"scripts": { | ||
"prepublishOnly": "cp ../{LICENSE,.npmrc} ./", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -11,0 +12,0 @@ }, |
@@ -7,2 +7,3 @@ 'use strict' | ||
const fs = require('fs') | ||
const Module = require('module') | ||
const path = require('path') | ||
@@ -16,9 +17,26 @@ | ||
const ERROR_NAME = 'EslintPluginImportResolveError' | ||
const fileExistsCache = new ModuleCache() | ||
function tryRequire(target) { | ||
// Polyfill Node's `Module.createRequireFromPath` if not present (added in Node v10.12.0) | ||
const createRequireFromPath = Module.createRequireFromPath || function (filename) { | ||
const mod = new Module(filename, null) | ||
mod.filename = filename | ||
mod.paths = Module._nodeModulePaths(path.dirname(filename)) | ||
mod._compile(`module.exports = require;`, filename) | ||
return mod.exports | ||
} | ||
function tryRequire(target, sourceFile) { | ||
let resolved | ||
try { | ||
// Check if the target exists | ||
resolved = require.resolve(target) | ||
if (sourceFile != null) { | ||
resolved = createRequireFromPath(sourceFile).resolve(target) | ||
} else { | ||
resolved = require.resolve(target) | ||
} | ||
} catch(e) { | ||
@@ -151,3 +169,5 @@ // If the target does not exist then just return undefined | ||
throw new Error('invalid resolver config') | ||
const err = new Error('invalid resolver config') | ||
err.name = ERROR_NAME | ||
throw err | ||
} | ||
@@ -160,11 +180,15 @@ | ||
// Try to resolve package with conventional name | ||
let resolver = tryRequire(`eslint-import-resolver-${name}`) || | ||
tryRequire(name) || | ||
let resolver = tryRequire(`eslint-import-resolver-${name}`, sourceFile) || | ||
tryRequire(name, sourceFile) || | ||
tryRequire(path.resolve(getBaseDir(sourceFile), name)) | ||
if (!resolver) { | ||
throw new Error(`unable to load resolver "${name}".`) | ||
const err = new Error(`unable to load resolver "${name}".`) | ||
err.name = ERROR_NAME | ||
throw err | ||
} | ||
if (!isResolverValid(resolver)) { | ||
throw new Error(`${name} with invalid interface loaded as resolver`) | ||
const err = new Error(`${name} with invalid interface loaded as resolver`) | ||
err.name = ERROR_NAME | ||
throw err | ||
} | ||
@@ -201,4 +225,10 @@ | ||
if (!erroredContexts.has(context)) { | ||
// The `err.stack` string starts with `err.name` followed by colon and `err.message`. | ||
// We're filtering out the default `err.name` because it adds little value to the message. | ||
let errMessage = err.message | ||
if (err.name !== ERROR_NAME && err.stack) { | ||
errMessage = err.stack.replace(/^Error: /, '') | ||
} | ||
context.report({ | ||
message: `Resolve error: ${err.message}`, | ||
message: `Resolve error: ${errMessage}`, | ||
loc: { line: 1, column: 0 }, | ||
@@ -205,0 +235,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24017
13
572
7