Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /**
  2. * @fileoverview Responsible for loading config files
  3. * @author Seth McLaughlin
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const path = require("path"),
  10. os = require("os"),
  11. ConfigOps = require("./config/config-ops"),
  12. ConfigFile = require("./config/config-file"),
  13. ConfigCache = require("./config/config-cache"),
  14. Plugins = require("./config/plugins"),
  15. FileFinder = require("./util/file-finder"),
  16. isResolvable = require("is-resolvable");
  17. const debug = require("debug")("eslint:config");
  18. //------------------------------------------------------------------------------
  19. // Constants
  20. //------------------------------------------------------------------------------
  21. const PERSONAL_CONFIG_DIR = os.homedir();
  22. const SUBCONFIG_SEP = ":";
  23. //------------------------------------------------------------------------------
  24. // Helpers
  25. //------------------------------------------------------------------------------
  26. /**
  27. * Determines if any rules were explicitly passed in as options.
  28. * @param {Object} options The options used to create our configuration.
  29. * @returns {boolean} True if rules were passed in as options, false otherwise.
  30. * @private
  31. */
  32. function hasRules(options) {
  33. return options.rules && Object.keys(options.rules).length > 0;
  34. }
  35. //------------------------------------------------------------------------------
  36. // API
  37. //------------------------------------------------------------------------------
  38. /**
  39. * Configuration class
  40. */
  41. class Config {
  42. /**
  43. * @param {Object} providedOptions Options to be passed in
  44. * @param {Linter} linterContext Linter instance object
  45. */
  46. constructor(providedOptions, linterContext) {
  47. const options = providedOptions || {};
  48. this.linterContext = linterContext;
  49. this.plugins = new Plugins(linterContext.environments, linterContext.rules);
  50. this.options = options;
  51. this.ignore = options.ignore;
  52. this.ignorePath = options.ignorePath;
  53. this.parser = options.parser;
  54. this.parserOptions = options.parserOptions || {};
  55. this.configCache = new ConfigCache();
  56. this.baseConfig = options.baseConfig
  57. ? ConfigOps.merge({}, ConfigFile.loadObject(options.baseConfig, this))
  58. : { rules: {} };
  59. this.baseConfig.filePath = "";
  60. this.baseConfig.baseDirectory = this.options.cwd;
  61. this.configCache.setConfig(this.baseConfig.filePath, this.baseConfig);
  62. this.configCache.setMergedVectorConfig(this.baseConfig.filePath, this.baseConfig);
  63. this.useEslintrc = (options.useEslintrc !== false);
  64. this.env = (options.envs || []).reduce((envs, name) => {
  65. envs[name] = true;
  66. return envs;
  67. }, {});
  68. /*
  69. * Handle declared globals.
  70. * For global variable foo, handle "foo:false" and "foo:true" to set
  71. * whether global is writable.
  72. * If user declares "foo", convert to "foo:false".
  73. */
  74. this.globals = (options.globals || []).reduce((globals, def) => {
  75. const parts = def.split(SUBCONFIG_SEP);
  76. globals[parts[0]] = (parts.length > 1 && parts[1] === "true");
  77. return globals;
  78. }, {});
  79. this.loadSpecificConfig(options.configFile);
  80. // Empty values in configs don't merge properly
  81. const cliConfigOptions = {
  82. env: this.env,
  83. rules: this.options.rules,
  84. globals: this.globals,
  85. parserOptions: this.parserOptions,
  86. plugins: this.options.plugins
  87. };
  88. this.cliConfig = {};
  89. Object.keys(cliConfigOptions).forEach(configKey => {
  90. const value = cliConfigOptions[configKey];
  91. if (value) {
  92. this.cliConfig[configKey] = value;
  93. }
  94. });
  95. }
  96. /**
  97. * Loads the config options from a config specified on the command line.
  98. * @param {string} [config] A shareable named config or path to a config file.
  99. * @returns {void}
  100. */
  101. loadSpecificConfig(config) {
  102. if (config) {
  103. debug(`Using command line config ${config}`);
  104. const isNamedConfig =
  105. isResolvable(config) ||
  106. isResolvable(`eslint-config-${config}`) ||
  107. config.charAt(0) === "@";
  108. this.specificConfig = ConfigFile.load(
  109. isNamedConfig ? config : path.resolve(this.options.cwd, config),
  110. this
  111. );
  112. }
  113. }
  114. /**
  115. * Gets the personal config object from user's home directory.
  116. * @returns {Object} the personal config object (null if there is no personal config)
  117. * @private
  118. */
  119. getPersonalConfig() {
  120. if (typeof this.personalConfig === "undefined") {
  121. let config;
  122. const filename = ConfigFile.getFilenameForDirectory(PERSONAL_CONFIG_DIR);
  123. if (filename) {
  124. debug("Using personal config");
  125. config = ConfigFile.load(filename, this);
  126. }
  127. this.personalConfig = config || null;
  128. }
  129. return this.personalConfig;
  130. }
  131. /**
  132. * Builds a hierarchy of config objects, including the base config, all local configs from the directory tree,
  133. * and a config file specified on the command line, if applicable.
  134. * @param {string} directory a file in whose directory we start looking for a local config
  135. * @returns {Object[]} The config objects, in ascending order of precedence
  136. * @private
  137. */
  138. getConfigHierarchy(directory) {
  139. debug(`Constructing config file hierarchy for ${directory}`);
  140. // Step 1: Always include baseConfig
  141. let configs = [this.baseConfig];
  142. // Step 2: Add user-specified config from .eslintrc.* and package.json files
  143. if (this.useEslintrc) {
  144. debug("Using .eslintrc and package.json files");
  145. configs = configs.concat(this.getLocalConfigHierarchy(directory));
  146. } else {
  147. debug("Not using .eslintrc or package.json files");
  148. }
  149. // Step 3: Merge in command line config file
  150. if (this.specificConfig) {
  151. debug("Using command line config file");
  152. configs.push(this.specificConfig);
  153. }
  154. return configs;
  155. }
  156. /**
  157. * Gets a list of config objects extracted from local config files that apply to the current directory, in
  158. * descending order, beginning with the config that is highest in the directory tree.
  159. * @param {string} directory The directory to start looking in for local config files.
  160. * @returns {Object[]} The shallow local config objects, in ascending order of precedence (closest to the current
  161. * directory at the end), or an empty array if there are no local configs.
  162. * @private
  163. */
  164. getLocalConfigHierarchy(directory) {
  165. const localConfigFiles = this.findLocalConfigFiles(directory),
  166. projectConfigPath = ConfigFile.getFilenameForDirectory(this.options.cwd),
  167. searched = [],
  168. configs = [];
  169. for (const localConfigFile of localConfigFiles) {
  170. const localConfigDirectory = path.dirname(localConfigFile);
  171. const localConfigHierarchyCache = this.configCache.getHierarchyLocalConfigs(localConfigDirectory);
  172. if (localConfigHierarchyCache) {
  173. const localConfigHierarchy = localConfigHierarchyCache.concat(configs);
  174. this.configCache.setHierarchyLocalConfigs(searched, localConfigHierarchy);
  175. return localConfigHierarchy;
  176. }
  177. /*
  178. * Don't consider the personal config file in the home directory,
  179. * except if the home directory is the same as the current working directory
  180. */
  181. if (localConfigDirectory === PERSONAL_CONFIG_DIR && localConfigFile !== projectConfigPath) {
  182. continue;
  183. }
  184. debug(`Loading ${localConfigFile}`);
  185. const localConfig = ConfigFile.load(localConfigFile, this);
  186. // Ignore empty config files
  187. if (!localConfig) {
  188. continue;
  189. }
  190. debug(`Using ${localConfigFile}`);
  191. configs.unshift(localConfig);
  192. searched.push(localConfigDirectory);
  193. // Stop traversing if a config is found with the root flag set
  194. if (localConfig.root) {
  195. break;
  196. }
  197. }
  198. if (!configs.length && !this.specificConfig) {
  199. // Fall back on the personal config from ~/.eslintrc
  200. debug("Using personal config file");
  201. const personalConfig = this.getPersonalConfig();
  202. if (personalConfig) {
  203. configs.unshift(personalConfig);
  204. } else if (!hasRules(this.options) && !this.options.baseConfig) {
  205. // No config file, no manual configuration, and no rules, so error.
  206. const noConfigError = new Error("No ESLint configuration found.");
  207. noConfigError.messageTemplate = "no-config-found";
  208. noConfigError.messageData = {
  209. directory,
  210. filesExamined: localConfigFiles
  211. };
  212. throw noConfigError;
  213. }
  214. }
  215. // Set the caches for the parent directories
  216. this.configCache.setHierarchyLocalConfigs(searched, configs);
  217. return configs;
  218. }
  219. /**
  220. * Gets the vector of applicable configs and subconfigs from the hierarchy for a given file. A vector is an array of
  221. * entries, each of which in an object specifying a config file path and an array of override indices corresponding
  222. * to entries in the config file's overrides section whose glob patterns match the specified file path; e.g., the
  223. * vector entry { configFile: '/home/john/app/.eslintrc', matchingOverrides: [0, 2] } would indicate that the main
  224. * project .eslintrc file and its first and third override blocks apply to the current file.
  225. * @param {string} filePath The file path for which to build the hierarchy and config vector.
  226. * @returns {Array<Object>} config vector applicable to the specified path
  227. * @private
  228. */
  229. getConfigVector(filePath) {
  230. const directory = filePath ? path.dirname(filePath) : this.options.cwd;
  231. return this.getConfigHierarchy(directory).map(config => {
  232. const vectorEntry = {
  233. filePath: config.filePath,
  234. matchingOverrides: []
  235. };
  236. if (config.overrides) {
  237. const relativePath = path.relative(config.baseDirectory, filePath || directory);
  238. config.overrides.forEach((override, i) => {
  239. if (ConfigOps.pathMatchesGlobs(relativePath, override.files, override.excludedFiles)) {
  240. vectorEntry.matchingOverrides.push(i);
  241. }
  242. });
  243. }
  244. return vectorEntry;
  245. });
  246. }
  247. /**
  248. * Finds local config files from the specified directory and its parent directories.
  249. * @param {string} directory The directory to start searching from.
  250. * @returns {GeneratorFunction} The paths of local config files found.
  251. */
  252. findLocalConfigFiles(directory) {
  253. if (!this.localConfigFinder) {
  254. this.localConfigFinder = new FileFinder(ConfigFile.CONFIG_FILES, this.options.cwd);
  255. }
  256. return this.localConfigFinder.findAllInDirectoryAndParents(directory);
  257. }
  258. /**
  259. * Builds the authoritative config object for the specified file path by merging the hierarchy of config objects
  260. * that apply to the current file, including the base config (conf/eslint-recommended), the user's personal config
  261. * from their homedir, all local configs from the directory tree, any specific config file passed on the command
  262. * line, any configuration overrides set directly on the command line, and finally the environment configs
  263. * (conf/environments).
  264. * @param {string} filePath a file in whose directory we start looking for a local config
  265. * @returns {Object} config object
  266. */
  267. getConfig(filePath) {
  268. const vector = this.getConfigVector(filePath);
  269. let config = this.configCache.getMergedConfig(vector);
  270. if (config) {
  271. debug("Using config from cache");
  272. return config;
  273. }
  274. // Step 1: Merge in the filesystem configurations (base, local, and personal)
  275. config = ConfigOps.getConfigFromVector(vector, this.configCache);
  276. // Step 2: Merge in command line configurations
  277. config = ConfigOps.merge(config, this.cliConfig);
  278. if (this.cliConfig.plugins) {
  279. this.plugins.loadAll(this.cliConfig.plugins);
  280. }
  281. /*
  282. * Step 3: Override parser only if it is passed explicitly through the command line
  283. * or if it's not defined yet (because the final object will at least have the parser key)
  284. */
  285. if (this.parser || !config.parser) {
  286. config = ConfigOps.merge(config, { parser: this.parser });
  287. }
  288. // Step 4: Apply environments to the config
  289. config = ConfigOps.applyEnvironments(config, this.linterContext.environments);
  290. this.configCache.setMergedConfig(vector, config);
  291. return config;
  292. }
  293. }
  294. module.exports = Config;