eslint-plugin-node
Advanced tools
Comparing version 5.0.0 to 5.1.0
@@ -16,2 +16,3 @@ /** | ||
const getImportTargets = require("../util/get-import-export-targets") | ||
const getResolvePaths = require("../util/get-resolve-paths") | ||
@@ -64,2 +65,3 @@ //------------------------------------------------------------------------------ | ||
convertPath: getConvertPath.schema, | ||
resolvePaths: getResolvePaths.schema, | ||
}, | ||
@@ -66,0 +68,0 @@ additionalProperties: false, |
@@ -16,2 +16,3 @@ /** | ||
const getRequireTargets = require("../util/get-require-targets") | ||
const getResolvePaths = require("../util/get-resolve-paths") | ||
@@ -64,2 +65,3 @@ //------------------------------------------------------------------------------ | ||
convertPath: getConvertPath.schema, | ||
resolvePaths: getResolvePaths.schema, | ||
}, | ||
@@ -66,0 +68,0 @@ additionalProperties: false, |
@@ -63,4 +63,3 @@ /** | ||
getImportExportTargets(context, node, true) | ||
) | ||
.filter(t => CORE_MODULES.has(t.moduleName)) | ||
).filter(t => CORE_MODULES.has(t.moduleName)) | ||
@@ -67,0 +66,0 @@ for (const target of targets) { |
@@ -15,2 +15,3 @@ /** | ||
const getImportExportTargets = require("../util/get-import-export-targets") | ||
const getResolvePaths = require("../util/get-resolve-paths") | ||
const getTryExtensions = require("../util/get-try-extensions") | ||
@@ -38,3 +39,2 @@ | ||
context, | ||
filePath, | ||
getImportExportTargets(context, node) | ||
@@ -65,2 +65,3 @@ ) | ||
tryExtensions: getTryExtensions.schema, | ||
resolvePaths: getResolvePaths.schema, | ||
}, | ||
@@ -67,0 +68,0 @@ additionalProperties: false, |
@@ -15,2 +15,3 @@ /** | ||
const getRequireTargets = require("../util/get-require-targets") | ||
const getResolvePaths = require("../util/get-resolve-paths") | ||
const getTryExtensions = require("../util/get-try-extensions") | ||
@@ -38,3 +39,2 @@ | ||
context, | ||
filePath, | ||
getRequireTargets(context) | ||
@@ -65,2 +65,3 @@ ) | ||
tryExtensions: getTryExtensions.schema, | ||
resolvePaths: getResolvePaths.schema, | ||
}, | ||
@@ -67,0 +68,0 @@ additionalProperties: false, |
@@ -16,2 +16,3 @@ /** | ||
const getImportExportTargets = require("../util/get-import-export-targets") | ||
const getResolvePaths = require("../util/get-resolve-paths") | ||
const getTryExtensions = require("../util/get-try-extensions") | ||
@@ -65,2 +66,3 @@ | ||
convertPath: getConvertPath.schema, | ||
resolvePaths: getResolvePaths.schema, | ||
tryExtensions: getTryExtensions.schema, | ||
@@ -67,0 +69,0 @@ }, |
@@ -16,2 +16,3 @@ /** | ||
const getRequireTargets = require("../util/get-require-targets") | ||
const getResolvePaths = require("../util/get-resolve-paths") | ||
const getTryExtensions = require("../util/get-try-extensions") | ||
@@ -65,2 +66,3 @@ | ||
convertPath: getConvertPath.schema, | ||
resolvePaths: getResolvePaths.schema, | ||
tryExtensions: getTryExtensions.schema, | ||
@@ -67,0 +69,0 @@ }, |
@@ -13,4 +13,2 @@ /** | ||
const path = require("path") | ||
const resolve = require("resolve") | ||
const exists = require("./exists") | ||
@@ -30,37 +28,28 @@ const getAllowModules = require("./get-allow-modules") | ||
* @param {RuleContext} context - A context to report. | ||
* @param {string} filePath - The current file path. | ||
* @param {ImportTarget[]} targets - A list of target information to check. | ||
* @returns {void} | ||
*/ | ||
module.exports = function checkForExistence(context, filePath, targets) { | ||
const allowed = getAllowModules(context) | ||
const opts = {basedir: path.dirname(path.resolve(filePath))} | ||
module.exports = function checkForExistence(context, targets) { | ||
const allowed = new Set(getAllowModules(context)) | ||
for (const target of targets) { | ||
// Workaround for https://github.com/substack/node-resolve/issues/78 | ||
if (target.filePath) { | ||
if (exists(target.filePath)) { | ||
continue | ||
} | ||
const missingModule = ( | ||
target.moduleName != null && | ||
!allowed.has(target.moduleName) && | ||
target.filePath == null | ||
) | ||
const missingFile = ( | ||
target.moduleName == null && | ||
!exists(target.filePath) | ||
) | ||
if (missingModule || missingFile) { | ||
context.report({ | ||
node: target.node, | ||
loc: target.node.loc, | ||
message: "\"{{name}}\" is not found.", | ||
data: target, | ||
}) | ||
} | ||
else if (allowed.indexOf(target.moduleName) !== -1) { | ||
continue | ||
} | ||
else { | ||
try { | ||
resolve.sync(target.name, opts) | ||
continue | ||
} | ||
catch (_err) { | ||
// ignore. | ||
} | ||
} | ||
context.report({ | ||
node: target.node, | ||
loc: target.node.loc, | ||
message: "\"{{name}}\" is not found.", | ||
data: target, | ||
}) | ||
} | ||
} |
@@ -12,29 +12,6 @@ /** | ||
const path = require("path") | ||
const resolve = require("resolve") | ||
const getAllowModules = require("./get-allow-modules") | ||
const getConvertPath = require("./get-convert-path") | ||
const getPackageJson = require("./get-package-json") | ||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
/** | ||
* Check whether the given package exists or not. | ||
* @param {string} name The package name to check. | ||
* @param {string} basedir The path to starting directory. | ||
* @returns {boolean} `true` if the package was found. | ||
*/ | ||
function exists(name, basedir) { | ||
try { | ||
resolve.sync(name, {basedir}) | ||
return true | ||
} | ||
catch (_err) { | ||
return false | ||
} | ||
} | ||
//------------------------------------------------------------------------------ | ||
// Public Interface | ||
@@ -53,3 +30,3 @@ //------------------------------------------------------------------------------ | ||
*/ | ||
module.exports = function checkForExtraeous(context, filePath, targets) { | ||
module.exports = function checkForExtraneous(context, filePath, targets) { | ||
const packageInfo = getPackageJson(filePath) | ||
@@ -61,13 +38,2 @@ if (!packageInfo) { | ||
const allowed = new Set(getAllowModules(context)) | ||
const convertPath = getConvertPath(context) | ||
const rootPath = path.dirname(packageInfo.filePath) | ||
const toRelative = function(fullPath) { // eslint-disable-line func-style | ||
const retv = path.relative(rootPath, fullPath).replace(/\\/g, "/") | ||
return convertPath(retv) | ||
} | ||
const convertedPath = path.resolve( | ||
rootPath, | ||
toRelative(path.resolve(filePath)) | ||
) | ||
const basedir = path.dirname(convertedPath) | ||
const dependencies = new Set( | ||
@@ -83,8 +49,7 @@ [].concat( | ||
for (const target of targets) { | ||
const name = target.moduleName | ||
const extraneous = ( | ||
name != null && | ||
!dependencies.has(name) && | ||
!allowed.has(name) && | ||
exists(name, basedir) | ||
target.moduleName != null && | ||
target.filePath != null && | ||
!dependencies.has(target.moduleName) && | ||
!allowed.has(target.moduleName) | ||
) | ||
@@ -91,0 +56,0 @@ |
@@ -50,2 +50,9 @@ /** | ||
) | ||
const dependencies = new Set( | ||
[].concat( | ||
Object.keys(packageInfo.dependencies || {}), | ||
Object.keys(packageInfo.peerDependencies || {}), | ||
Object.keys(packageInfo.optionalDependencies || {}) | ||
) | ||
) | ||
@@ -62,2 +69,3 @@ if (!npmignore.match(toRelative(filePath))) { | ||
devDependencies.has(target.moduleName) && | ||
!dependencies.has(target.moduleName) && | ||
!allowed.has(target.moduleName) | ||
@@ -64,0 +72,0 @@ ) |
@@ -14,2 +14,3 @@ /** | ||
const resolve = require("resolve") | ||
const getResolvePaths = require("./get-resolve-paths") | ||
const getTryExtensions = require("./get-try-extensions") | ||
@@ -42,3 +43,5 @@ const ImportTarget = require("./import-target") | ||
const basedir = path.dirname(path.resolve(context.getFilename())) | ||
const paths = getResolvePaths(context) | ||
const extensions = getTryExtensions(context) | ||
const options = {basedir, paths, extensions} | ||
@@ -55,3 +58,3 @@ for (const statement of programNode.body) { | ||
if (name && (includeCore || !resolve.isCore(name))) { | ||
retv.push(new ImportTarget(node, name, basedir, extensions)) | ||
retv.push(new ImportTarget(node, name, options)) | ||
} | ||
@@ -58,0 +61,0 @@ } |
@@ -14,2 +14,3 @@ /** | ||
const resolve = require("resolve") | ||
const getResolvePaths = require("./get-resolve-paths") | ||
const getTryExtensions = require("./get-try-extensions") | ||
@@ -65,4 +66,6 @@ const getValueIfString = require("./get-value-if-string") | ||
const basedir = path.dirname(path.resolve(context.getFilename())) | ||
const paths = getResolvePaths(context) | ||
const references = getReferencesOfRequire(context.getScope()) | ||
const extensions = getTryExtensions(context) | ||
const options = {basedir, paths, extensions} | ||
@@ -82,3 +85,3 @@ for (const reference of references) { | ||
if (name && (includeCore || !resolve.isCore(name))) { | ||
retv.push(new ImportTarget(targetNode, name, basedir, extensions)) | ||
retv.push(new ImportTarget(targetNode, name, options)) | ||
} | ||
@@ -85,0 +88,0 @@ } |
@@ -12,6 +12,4 @@ /** | ||
const fs = require("fs") | ||
const path = require("path") | ||
const exists = require("./exists") | ||
const getPackageJson = require("./get-package-json") | ||
const resolve = require("resolve") | ||
@@ -23,84 +21,22 @@ //------------------------------------------------------------------------------ | ||
/** | ||
* Checks whether or not a given path is a directory. | ||
* | ||
* @param {string} filePath - A file path to check. | ||
* @returns {boolean} `true` if the path is a directory. | ||
* Resolve the given id to file paths. | ||
* @param {boolean} isModule The flag which indicates this id is a module. | ||
* @param {string} id The id to resolve. | ||
* @param {object} options The options of node-resolve module. | ||
* It requires `options.basedir`. | ||
* @returns {string|null} The resolved path. | ||
*/ | ||
function isDirectory(filePath) { | ||
function getFilePath(isModule, id, options) { | ||
try { | ||
return fs.statSync(filePath).isDirectory() | ||
return resolve.sync(id, options) | ||
} | ||
catch (_err) { | ||
return false | ||
} | ||
} | ||
/** | ||
* Resolve a given path as a file with given extensions. | ||
* | ||
* @param {string} filePath - A path to resolve. | ||
* @param {string[]} exts - Extensions that it checks whether or not the file exists. | ||
* @returns {string|null} The resolved path. Or `null` if failed to resolve. | ||
*/ | ||
function tryExtentions(filePath, exts) { | ||
for (const ext of exts) { | ||
if (exists(filePath + ext)) { | ||
return filePath + ext | ||
if (isModule) { | ||
return null | ||
} | ||
return path.resolve(options.basedir, id) | ||
} | ||
return null | ||
} | ||
/** | ||
* Resolve a given path as a file. | ||
* | ||
* @param {string} filePath - A path to resolve. | ||
* @param {string[]} exts - Extensions that it checks whether or not the file exists. | ||
* @returns {string|null} The resolved path. Or `null` if failed to resolve. | ||
*/ | ||
function resolveAsFile(filePath, exts) { | ||
if (exists(filePath)) { | ||
return filePath | ||
} | ||
return tryExtentions(filePath, exts) | ||
} | ||
/** | ||
* Resolve a given path as a directory. | ||
* | ||
* @param {string} filePath - A path to resolve. | ||
* @param {string[]} exts - Extensions that it checks whether or not the file exists. | ||
* @returns {string|null} The resolved path. Or `null` if failed to resolve. | ||
*/ | ||
function resolveAsDirectory(filePath, exts) { | ||
if (!isDirectory(filePath)) { | ||
return null | ||
} | ||
const p = getPackageJson(path.join(filePath, "package.json")) | ||
if (p && path.dirname(p.filePath) === filePath && p.main) { | ||
return resolveAsFile(path.join(filePath, p.main), exts) | ||
} | ||
return tryExtentions(path.join(filePath, "index"), exts) | ||
} | ||
/** | ||
* Resolves the file. | ||
* | ||
* @param {string} basedir - The path of base directory to resolve relative path. | ||
* @param {string} name - The name of an import target. | ||
* @param {string[]} exts - Extensions that it checks whether or not the file exists. | ||
* @returns {string} The resolved path. | ||
*/ | ||
function resolve(basedir, name, exts) { | ||
const resolvedPath = path.resolve(basedir, name) | ||
return ( | ||
resolveAsFile(resolvedPath, exts) || | ||
resolveAsDirectory(resolvedPath, exts) || | ||
resolvedPath | ||
) | ||
} | ||
/** | ||
* Gets the module name of a given path. | ||
@@ -134,7 +70,6 @@ * | ||
* @param {string} name - The name of an import target. | ||
* @param {string} basedir - The path of base directory to resolve relative path. | ||
* @param {string[]} exts - Extensions that it checks whether or not the file exists. | ||
* @param {object} options - The options of `node-resolve` module. | ||
*/ | ||
constructor(node, name, basedir, exts) { | ||
const relative = /^\./.test(name) | ||
constructor(node, name, options) { | ||
const isModule = !/^(?:[./\\]|\w+:)/.test(name) | ||
@@ -155,6 +90,6 @@ /** | ||
* The full path of this import target. | ||
* If the target is a module then this is `null`. | ||
* If the target is a module and it does not exist then this is `null`. | ||
* @type {string|null} | ||
*/ | ||
this.filePath = relative ? resolve(basedir, name, exts) : null | ||
this.filePath = getFilePath(isModule, name, options) | ||
@@ -166,4 +101,4 @@ /** | ||
*/ | ||
this.moduleName = relative ? null : getModuleName(name) | ||
this.moduleName = isModule ? getModuleName(name) : null | ||
} | ||
} |
{ | ||
"name": "eslint-plugin-node", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Additional ESLint's rules for Node.js", | ||
@@ -36,4 +36,4 @@ "main": "lib/index.js", | ||
"codecov": "^2.2.0", | ||
"eslint": "^3.19.0", | ||
"eslint-config-mysticatea": "^10.0.0", | ||
"eslint": "^4.1.0", | ||
"eslint-config-mysticatea": "^11.0.0", | ||
"mocha": "^3.4.2", | ||
@@ -40,0 +40,0 @@ "nyc": "^11.0.1", |
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
37
4
159501
4575