mirror of
https://github.com/DavidAnson/markdownlint.git
synced 2025-12-17 06:20:12 +01:00
Use require.resolve as a fallback of path.resolve (#342)
This commit is contained in:
parent
bc0b7373ff
commit
4bff44e33f
9 changed files with 5672 additions and 8 deletions
|
|
@ -894,6 +894,34 @@ function parseConfiguration(name, content, parsers) {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve referenced "extends" path in a configuration file
|
||||
* using path.resolve() and require.resolve() as a fallback.
|
||||
*
|
||||
* @param {string} configFile Configuration file name.
|
||||
* @param {string} referenceId Referenced identifier to resolve.
|
||||
* @returns {string} Resolved path to file.
|
||||
*/
|
||||
function resolveConfigExtends(configFile, referenceId) {
|
||||
const configFileDirname = path.dirname(configFile);
|
||||
const resolvedExtendsFile = path.resolve(configFileDirname, referenceId);
|
||||
try {
|
||||
if (fs.statSync(resolvedExtendsFile).isFile()) {
|
||||
return resolvedExtendsFile;
|
||||
}
|
||||
// eslint-disable-next-line unicorn/prefer-optional-catch-binding
|
||||
} catch (error) {
|
||||
// If fs.statSync has thrown, trying require.resolve
|
||||
}
|
||||
try {
|
||||
return require.resolve(referenceId, { "paths": [ configFileDirname ] });
|
||||
// eslint-disable-next-line unicorn/prefer-optional-catch-binding
|
||||
} catch (error) {
|
||||
// If require.resolve throws, returning resolvedExtendsFile for BC
|
||||
}
|
||||
return resolvedExtendsFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read specified configuration file.
|
||||
*
|
||||
|
|
@ -924,8 +952,8 @@ function readConfig(file, parsers, callback) {
|
|||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
delete config.extends;
|
||||
const extendsFile = path.resolve(path.dirname(file), configExtends);
|
||||
return readConfig(extendsFile, parsers, (errr, extendsConfig) => {
|
||||
const resolvedExtends = resolveConfigExtends(file, configExtends);
|
||||
return readConfig(resolvedExtends, parsers, (errr, extendsConfig) => {
|
||||
if (errr) {
|
||||
return callback(errr);
|
||||
}
|
||||
|
|
@ -973,11 +1001,9 @@ function readConfigSync(file, parsers) {
|
|||
const configExtends = config.extends;
|
||||
if (configExtends) {
|
||||
delete config.extends;
|
||||
const resolvedExtends = resolveConfigExtends(file, configExtends);
|
||||
return {
|
||||
...readConfigSync(
|
||||
path.resolve(path.dirname(file), configExtends),
|
||||
parsers
|
||||
),
|
||||
...readConfigSync(resolvedExtends, parsers),
|
||||
...config
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue