|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
-
-
- "use strict";
-
-
-
-
-
- const fs = require("fs"),
- path = require("path"),
- ConfigOps = require("./config-ops"),
- validator = require("./config-validator"),
- ModuleResolver = require("../util/module-resolver"),
- naming = require("../util/naming"),
- pathIsInside = require("path-is-inside"),
- stripComments = require("strip-json-comments"),
- stringify = require("json-stable-stringify-without-jsonify"),
- requireUncached = require("require-uncached");
-
- const debug = require("debug")("eslint:config-file");
-
-
-
-
-
-
- function sortByKey(a, b) {
- return a.key > b.key ? 1 : -1;
- }
-
-
-
-
-
- const CONFIG_FILES = [
- ".eslintrc.js",
- ".eslintrc.yaml",
- ".eslintrc.yml",
- ".eslintrc.json",
- ".eslintrc",
- "package.json"
- ];
-
- const resolver = new ModuleResolver();
-
-
- function readFile(filePath) {
- return fs.readFileSync(filePath, "utf8").replace(/^\ufeff/, "");
- }
-
-
- function isFilePath(filePath) {
- return path.isAbsolute(filePath) || !/\w|@/.test(filePath.charAt(0));
- }
-
-
- function loadYAMLConfigFile(filePath) {
- debug(`Loading YAML config file: ${filePath}`);
-
-
- const yaml = require("js-yaml");
-
- try {
-
-
- return yaml.safeLoad(readFile(filePath)) || {};
- } catch (e) {
- debug(`Error reading YAML file: ${filePath}`);
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
- throw e;
- }
- }
-
-
- function loadJSONConfigFile(filePath) {
- debug(`Loading JSON config file: ${filePath}`);
-
- try {
- return JSON.parse(stripComments(readFile(filePath)));
- } catch (e) {
- debug(`Error reading JSON file: ${filePath}`);
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
- e.messageTemplate = "failed-to-read-json";
- e.messageData = {
- path: filePath,
- message: e.message
- };
- throw e;
- }
- }
-
-
- function loadLegacyConfigFile(filePath) {
- debug(`Loading config file: ${filePath}`);
-
-
- const yaml = require("js-yaml");
-
- try {
- return yaml.safeLoad(stripComments(readFile(filePath))) || {};
- } catch (e) {
- debug(`Error reading YAML file: ${filePath}`);
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
- throw e;
- }
- }
-
-
- function loadJSConfigFile(filePath) {
- debug(`Loading JS config file: ${filePath}`);
- try {
- return requireUncached(filePath);
- } catch (e) {
- debug(`Error reading JavaScript file: ${filePath}`);
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
- throw e;
- }
- }
-
-
- function loadPackageJSONConfigFile(filePath) {
- debug(`Loading package.json config file: ${filePath}`);
- try {
- return loadJSONConfigFile(filePath).eslintConfig || null;
- } catch (e) {
- debug(`Error reading package.json file: ${filePath}`);
- e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
- throw e;
- }
- }
-
-
- function configMissingError(configName) {
- const error = new Error(`Failed to load config "${configName}" to extend from.`);
-
- error.messageTemplate = "extend-config-missing";
- error.messageData = {
- configName
- };
- return error;
- }
-
-
- function loadConfigFile(file) {
- const filePath = file.filePath;
- let config;
-
- switch (path.extname(filePath)) {
- case ".js":
- config = loadJSConfigFile(filePath);
- if (file.configName) {
- config = config.configs[file.configName];
- if (!config) {
- throw configMissingError(file.configFullName);
- }
- }
- break;
-
- case ".json":
- if (path.basename(filePath) === "package.json") {
- config = loadPackageJSONConfigFile(filePath);
- if (config === null) {
- return null;
- }
- } else {
- config = loadJSONConfigFile(filePath);
- }
- break;
-
- case ".yaml":
- case ".yml":
- config = loadYAMLConfigFile(filePath);
- break;
-
- default:
- config = loadLegacyConfigFile(filePath);
- }
-
- return ConfigOps.merge(ConfigOps.createEmptyConfig(), config);
- }
-
-
- function writeJSONConfigFile(config, filePath) {
- debug(`Writing JSON config file: ${filePath}`);
-
- const content = stringify(config, { cmp: sortByKey, space: 4 });
-
- fs.writeFileSync(filePath, content, "utf8");
- }
-
-
- function writeYAMLConfigFile(config, filePath) {
- debug(`Writing YAML config file: ${filePath}`);
-
-
- const yaml = require("js-yaml");
-
- const content = yaml.safeDump(config, { sortKeys: true });
-
- fs.writeFileSync(filePath, content, "utf8");
- }
-
-
- function writeJSConfigFile(config, filePath) {
- debug(`Writing JS config file: ${filePath}`);
-
- const content = `module.exports = ${stringify(config, { cmp: sortByKey, space: 4 })};`;
-
- fs.writeFileSync(filePath, content, "utf8");
- }
-
-
- function write(config, filePath) {
- switch (path.extname(filePath)) {
- case ".js":
- writeJSConfigFile(config, filePath);
- break;
-
- case ".json":
- writeJSONConfigFile(config, filePath);
- break;
-
- case ".yaml":
- case ".yml":
- writeYAMLConfigFile(config, filePath);
- break;
-
- default:
- throw new Error("Can't write to unknown file type.");
- }
- }
-
-
- function getBaseDir(configFilePath) {
-
-
- const projectPath = path.resolve(__dirname, "../../../");
-
- if (configFilePath && pathIsInside(configFilePath, projectPath)) {
-
-
- return path.join(path.resolve(configFilePath));
- }
-
-
-
- return path.join(projectPath);
- }
-
-
- function getLookupPath(configFilePath) {
- const basedir = getBaseDir(configFilePath);
-
- return path.join(basedir, "node_modules");
- }
-
-
- function getEslintCoreConfigPath(name) {
- if (name === "eslint:recommended") {
-
-
-
- return path.resolve(__dirname, "../../conf/eslint-recommended.js");
- }
-
- if (name === "eslint:all") {
-
-
-
- return path.resolve(__dirname, "../../conf/eslint-all.js");
- }
-
- throw configMissingError(name);
- }
-
-
- function applyExtends(config, configContext, filePath, relativeTo) {
- let configExtends = config.extends;
-
-
- if (!Array.isArray(config.extends)) {
- configExtends = [config.extends];
- }
-
-
- return configExtends.reduceRight((previousValue, parentPath) => {
- try {
- let extensionPath;
-
- if (parentPath.startsWith("eslint:")) {
- extensionPath = getEslintCoreConfigPath(parentPath);
- } else if (isFilePath(parentPath)) {
-
-
-
- extensionPath = (path.isAbsolute(parentPath)
- ? parentPath
- : path.join(relativeTo || path.dirname(filePath), parentPath)
- );
- } else {
- extensionPath = parentPath;
- }
- debug(`Loading ${extensionPath}`);
-
-
- return ConfigOps.merge(load(extensionPath, configContext, relativeTo), previousValue);
- } catch (e) {
-
-
-
- e.message += `\nReferenced from: ${filePath}`;
- throw e;
- }
-
- }, config);
- }
-
-
- function resolve(filePath, relativeTo) {
- if (isFilePath(filePath)) {
- const fullPath = path.resolve(relativeTo || "", filePath);
-
- return { filePath: fullPath, configFullName: fullPath };
- }
- let normalizedPackageName;
-
- if (filePath.startsWith("plugin:")) {
- const configFullName = filePath;
- const pluginName = filePath.slice(7, filePath.lastIndexOf("/"));
- const configName = filePath.slice(filePath.lastIndexOf("/") + 1);
-
- normalizedPackageName = naming.normalizePackageName(pluginName, "eslint-plugin");
- debug(`Attempting to resolve ${normalizedPackageName}`);
-
- return {
- filePath: require.resolve(normalizedPackageName),
- configName,
- configFullName
- };
- }
- normalizedPackageName = naming.normalizePackageName(filePath, "eslint-config");
- debug(`Attempting to resolve ${normalizedPackageName}`);
-
- return {
- filePath: resolver.resolve(normalizedPackageName, getLookupPath(relativeTo)),
- configFullName: filePath
- };
-
-
- }
-
-
- function loadFromDisk(resolvedPath, configContext) {
- const dirname = path.dirname(resolvedPath.filePath),
- lookupPath = getLookupPath(dirname);
- let config = loadConfigFile(resolvedPath);
-
- if (config) {
-
-
- if (config.plugins) {
- configContext.plugins.loadAll(config.plugins);
- }
-
-
- if (config.parser) {
- if (isFilePath(config.parser)) {
- config.parser = path.resolve(dirname || "", config.parser);
- } else {
- config.parser = resolver.resolve(config.parser, lookupPath);
- }
- }
-
- const ruleMap = configContext.linterContext.getRules();
-
-
- validator.validate(config, resolvedPath.configFullName, ruleMap.get.bind(ruleMap), configContext.linterContext.environments);
-
-
-
- if (config.extends) {
- config = applyExtends(config, configContext, resolvedPath.filePath, dirname);
- }
- }
-
- return config;
- }
-
-
- function loadObject(configObject, configContext) {
- return configObject.extends ? applyExtends(configObject, configContext, "") : configObject;
- }
-
-
- function load(filePath, configContext, relativeTo) {
- const resolvedPath = resolve(filePath, relativeTo);
-
- const cachedConfig = configContext.configCache.getConfig(resolvedPath.configFullName);
-
- if (cachedConfig) {
- return cachedConfig;
- }
-
- const config = loadFromDisk(resolvedPath, configContext);
-
- if (config) {
- config.filePath = resolvedPath.filePath;
- config.baseDirectory = path.dirname(resolvedPath.filePath);
- configContext.configCache.setConfig(resolvedPath.configFullName, config);
- }
-
- return config;
- }
-
-
- function isExistingFile(filename) {
- try {
- return fs.statSync(filename).isFile();
- } catch (err) {
- if (err.code === "ENOENT") {
- return false;
- }
- throw err;
- }
- }
-
-
-
-
-
-
- module.exports = {
-
- getBaseDir,
- getLookupPath,
- load,
- loadObject,
- resolve,
- write,
- applyExtends,
- CONFIG_FILES,
-
-
-
- getFilenameForDirectory(directory) {
- return CONFIG_FILES.map(filename => path.join(directory, filename)).find(isExistingFile) || null;
- }
- };
|