|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
const { ConfigArray, ConfigArraySymbol } = require("@humanwhocodes/config-array"); |
|
const { flatConfigSchema } = require("./flat-config-schema"); |
|
const { RuleValidator } = require("./rule-validator"); |
|
const { defaultConfig } = require("./default-config"); |
|
const jsPlugin = require("@eslint/js"); |
|
|
|
|
|
|
|
|
|
|
|
const ruleValidator = new RuleValidator(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function splitPluginIdentifier(identifier) { |
|
const parts = identifier.split("/"); |
|
|
|
return { |
|
objectName: parts.pop(), |
|
pluginName: parts.join("/") |
|
}; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getObjectId(object) { |
|
|
|
|
|
let name = object.name; |
|
|
|
if (!name) { |
|
|
|
if (!object.meta) { |
|
return null; |
|
} |
|
|
|
name = object.meta.name; |
|
|
|
if (!name) { |
|
return null; |
|
} |
|
} |
|
|
|
|
|
let version = object.version; |
|
|
|
if (!version) { |
|
version = object.meta && object.meta.version; |
|
} |
|
|
|
|
|
if (version) { |
|
return `${name}@${version}`; |
|
} |
|
|
|
return name; |
|
} |
|
|
|
const originalBaseConfig = Symbol("originalBaseConfig"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FlatConfigArray extends ConfigArray { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(configs, { |
|
basePath, |
|
shouldIgnore = true, |
|
baseConfig = defaultConfig |
|
} = {}) { |
|
super(configs, { |
|
basePath, |
|
schema: flatConfigSchema |
|
}); |
|
|
|
if (baseConfig[Symbol.iterator]) { |
|
this.unshift(...baseConfig); |
|
} else { |
|
this.unshift(baseConfig); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
this[originalBaseConfig] = baseConfig; |
|
Object.defineProperty(this, originalBaseConfig, { writable: false }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.shouldIgnore = shouldIgnore; |
|
Object.defineProperty(this, "shouldIgnore", { writable: false }); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ConfigArraySymbol.preprocessConfig](config) { |
|
if (config === "eslint:recommended") { |
|
|
|
|
|
if (typeof process !== "undefined" && process.emitWarning) { |
|
process.emitWarning("The 'eslint:recommended' string configuration is deprecated and will be replaced by the @eslint/js package's 'recommended' config."); |
|
} |
|
|
|
return jsPlugin.configs.recommended; |
|
} |
|
|
|
if (config === "eslint:all") { |
|
|
|
|
|
if (typeof process !== "undefined" && process.emitWarning) { |
|
process.emitWarning("The 'eslint:all' string configuration is deprecated and will be replaced by the @eslint/js package's 'all' config."); |
|
} |
|
|
|
return jsPlugin.configs.all; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
!this.shouldIgnore && |
|
!this[originalBaseConfig].includes(config) && |
|
config.ignores && |
|
!config.files |
|
) { |
|
|
|
const { ignores, ...otherKeys } = config; |
|
|
|
return otherKeys; |
|
} |
|
|
|
return config; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[ConfigArraySymbol.finalizeConfig](config) { |
|
|
|
const { plugins, languageOptions, processor } = config; |
|
let parserName, processorName; |
|
let invalidParser = false, |
|
invalidProcessor = false; |
|
|
|
|
|
if (languageOptions && languageOptions.parser) { |
|
const { parser } = languageOptions; |
|
|
|
if (typeof parser === "object") { |
|
parserName = getObjectId(parser); |
|
|
|
if (!parserName) { |
|
invalidParser = true; |
|
} |
|
|
|
} else { |
|
invalidParser = true; |
|
} |
|
} |
|
|
|
|
|
if (processor) { |
|
if (typeof processor === "string") { |
|
const { pluginName, objectName: localProcessorName } = splitPluginIdentifier(processor); |
|
|
|
processorName = processor; |
|
|
|
if (!plugins || !plugins[pluginName] || !plugins[pluginName].processors || !plugins[pluginName].processors[localProcessorName]) { |
|
throw new TypeError(`Key "processor": Could not find "${localProcessorName}" in plugin "${pluginName}".`); |
|
} |
|
|
|
config.processor = plugins[pluginName].processors[localProcessorName]; |
|
} else if (typeof processor === "object") { |
|
processorName = getObjectId(processor); |
|
|
|
if (!processorName) { |
|
invalidProcessor = true; |
|
} |
|
|
|
} else { |
|
invalidProcessor = true; |
|
} |
|
} |
|
|
|
ruleValidator.validate(config); |
|
|
|
|
|
|
|
Object.defineProperty(config, "toJSON", { |
|
value: function() { |
|
|
|
if (invalidParser) { |
|
throw new Error("Could not serialize parser object (missing 'meta' object)."); |
|
} |
|
|
|
if (invalidProcessor) { |
|
throw new Error("Could not serialize processor object (missing 'meta' object)."); |
|
} |
|
|
|
return { |
|
...this, |
|
plugins: Object.entries(plugins).map(([namespace, plugin]) => { |
|
|
|
const pluginId = getObjectId(plugin); |
|
|
|
if (!pluginId) { |
|
return namespace; |
|
} |
|
|
|
return `${namespace}:${pluginId}`; |
|
}), |
|
languageOptions: { |
|
...languageOptions, |
|
parser: parserName |
|
}, |
|
processor: processorName |
|
}; |
|
} |
|
}); |
|
|
|
|
|
return config; |
|
} |
|
|
|
|
|
} |
|
|
|
exports.FlatConfigArray = FlatConfigArray; |
|
|