augmentConfig.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. 'use strict';
  2. const configurationError = require('./utils/configurationError');
  3. const getModulePath = require('./utils/getModulePath');
  4. const globjoin = require('globjoin');
  5. const micromatch = require('micromatch');
  6. const normalizeAllRuleSettings = require('./normalizeAllRuleSettings');
  7. const normalizePath = require('normalize-path');
  8. const path = require('path');
  9. /** @typedef {import('stylelint').ConfigPlugins} StylelintConfigPlugins */
  10. /** @typedef {import('stylelint').ConfigProcessor} StylelintConfigProcessor */
  11. /** @typedef {import('stylelint').ConfigProcessors} StylelintConfigProcessors */
  12. /** @typedef {import('stylelint').ConfigRules} StylelintConfigRules */
  13. /** @typedef {import('stylelint').ConfigOverride} StylelintConfigOverride */
  14. /** @typedef {import('stylelint').InternalApi} StylelintInternalApi */
  15. /** @typedef {import('stylelint').Config} StylelintConfig */
  16. /** @typedef {import('stylelint').CosmiconfigResult} StylelintCosmiconfigResult */
  17. /** @typedef {import('stylelint').CodeProcessor} StylelintCodeProcessor */
  18. /** @typedef {import('stylelint').ResultProcessor} StylelintResultProcessor */
  19. /**
  20. * - Merges config and stylelint options
  21. * - Makes all paths absolute
  22. * - Merges extends
  23. * @param {StylelintInternalApi} stylelint
  24. * @param {StylelintConfig} config
  25. * @param {string} configDir
  26. * @param {boolean} allowOverrides
  27. * @param {string} rootConfigDir
  28. * @param {string} [filePath]
  29. * @returns {Promise<StylelintConfig>}
  30. */
  31. async function augmentConfigBasic(
  32. stylelint,
  33. config,
  34. configDir,
  35. allowOverrides,
  36. rootConfigDir,
  37. filePath,
  38. ) {
  39. let augmentedConfig = config;
  40. if (allowOverrides) {
  41. augmentedConfig = addOptions(stylelint, augmentedConfig);
  42. }
  43. if (filePath) {
  44. augmentedConfig = applyOverrides(augmentedConfig, rootConfigDir, filePath);
  45. }
  46. augmentedConfig = await extendConfig(
  47. stylelint,
  48. augmentedConfig,
  49. configDir,
  50. rootConfigDir,
  51. filePath,
  52. );
  53. const cwd = stylelint._options.cwd;
  54. return absolutizePaths(augmentedConfig, configDir, cwd);
  55. }
  56. /**
  57. * Extended configs need to be run through augmentConfigBasic
  58. * but do not need the full treatment. Things like pluginFunctions
  59. * will be resolved and added by the parent config.
  60. * @param {string} cwd
  61. * @returns {(cosmiconfigResult?: StylelintCosmiconfigResult) => Promise<StylelintCosmiconfigResult>}
  62. */
  63. function augmentConfigExtended(cwd) {
  64. return async (cosmiconfigResult) => {
  65. if (!cosmiconfigResult) {
  66. return null;
  67. }
  68. const configDir = path.dirname(cosmiconfigResult.filepath || '');
  69. const { config } = cosmiconfigResult;
  70. const augmentedConfig = absolutizePaths(config, configDir, cwd);
  71. return {
  72. config: augmentedConfig,
  73. filepath: cosmiconfigResult.filepath,
  74. };
  75. };
  76. }
  77. /**
  78. * @param {StylelintInternalApi} stylelint
  79. * @param {string} [filePath]
  80. * @param {StylelintCosmiconfigResult} [cosmiconfigResult]
  81. * @returns {Promise<StylelintCosmiconfigResult>}
  82. */
  83. async function augmentConfigFull(stylelint, filePath, cosmiconfigResult) {
  84. if (!cosmiconfigResult) {
  85. return null;
  86. }
  87. const config = cosmiconfigResult.config;
  88. const filepath = cosmiconfigResult.filepath;
  89. const configDir = stylelint._options.configBasedir || path.dirname(filepath || '');
  90. let augmentedConfig = await augmentConfigBasic(
  91. stylelint,
  92. config,
  93. configDir,
  94. true,
  95. configDir,
  96. filePath,
  97. );
  98. augmentedConfig = addPluginFunctions(augmentedConfig);
  99. augmentedConfig = addProcessorFunctions(augmentedConfig);
  100. if (!augmentedConfig.rules) {
  101. throw configurationError(
  102. 'No rules found within configuration. Have you provided a "rules" property?',
  103. );
  104. }
  105. augmentedConfig = normalizeAllRuleSettings(augmentedConfig);
  106. return {
  107. config: augmentedConfig,
  108. filepath: cosmiconfigResult.filepath,
  109. };
  110. }
  111. /**
  112. * Make all paths in the config absolute:
  113. * - ignoreFiles
  114. * - plugins
  115. * - processors
  116. * (extends handled elsewhere)
  117. * @param {StylelintConfig} config
  118. * @param {string} configDir
  119. * @param {string} cwd
  120. * @returns {StylelintConfig}
  121. */
  122. function absolutizePaths(config, configDir, cwd) {
  123. if (config.ignoreFiles) {
  124. config.ignoreFiles = [config.ignoreFiles].flat().map((glob) => {
  125. if (path.isAbsolute(glob.replace(/^!/, ''))) return glob;
  126. return globjoin(configDir, glob);
  127. });
  128. }
  129. if (config.plugins) {
  130. config.plugins = [config.plugins].flat().map((lookup) => getModulePath(configDir, lookup, cwd));
  131. }
  132. if (config.processors) {
  133. config.processors = absolutizeProcessors(config.processors, configDir);
  134. }
  135. return config;
  136. }
  137. /**
  138. * Processors are absolutized in their own way because
  139. * they can be and return a string or an array
  140. * @param {StylelintConfigProcessors} processors
  141. * @param {string} configDir
  142. * @return {StylelintConfigProcessors}
  143. */
  144. function absolutizeProcessors(processors, configDir) {
  145. const normalizedProcessors = Array.isArray(processors) ? processors : [processors];
  146. return normalizedProcessors.map((item) => {
  147. if (typeof item === 'string') {
  148. return getModulePath(configDir, item);
  149. }
  150. return [getModulePath(configDir, item[0]), item[1]];
  151. });
  152. }
  153. /**
  154. * @param {StylelintInternalApi} stylelint
  155. * @param {StylelintConfig} config
  156. * @param {string} configDir
  157. * @param {string} rootConfigDir
  158. * @param {string} [filePath]
  159. * @return {Promise<StylelintConfig>}
  160. */
  161. async function extendConfig(stylelint, config, configDir, rootConfigDir, filePath) {
  162. if (config.extends === undefined) {
  163. return config;
  164. }
  165. const { extends: configExtends, ...originalWithoutExtends } = config;
  166. const normalizedExtends = [configExtends].flat();
  167. let resultConfig = originalWithoutExtends;
  168. for (const extendLookup of normalizedExtends) {
  169. const extendResult = await loadExtendedConfig(stylelint, configDir, extendLookup);
  170. if (extendResult) {
  171. let extendResultConfig = extendResult.config;
  172. const extendConfigDir = path.dirname(extendResult.filepath || '');
  173. extendResultConfig = await augmentConfigBasic(
  174. stylelint,
  175. extendResultConfig,
  176. extendConfigDir,
  177. false,
  178. rootConfigDir,
  179. filePath,
  180. );
  181. resultConfig = mergeConfigs(resultConfig, extendResultConfig);
  182. }
  183. }
  184. return mergeConfigs(resultConfig, originalWithoutExtends);
  185. }
  186. /**
  187. * @param {StylelintInternalApi} stylelint
  188. * @param {string} configDir
  189. * @param {string} extendLookup
  190. * @return {Promise<StylelintCosmiconfigResult>}
  191. */
  192. function loadExtendedConfig(stylelint, configDir, extendLookup) {
  193. const extendPath = getModulePath(configDir, extendLookup, stylelint._options.cwd);
  194. return stylelint._extendExplorer.load(extendPath);
  195. }
  196. /**
  197. * When merging configs (via extends)
  198. * - plugin and processor arrays are joined
  199. * - rules are merged via Object.assign, so there is no attempt made to
  200. * merge any given rule's settings. If b contains the same rule as a,
  201. * b's rule settings will override a's rule settings entirely.
  202. * - Everything else is merged via Object.assign
  203. * @param {StylelintConfig} a
  204. * @param {StylelintConfig} b
  205. * @returns {StylelintConfig}
  206. */
  207. function mergeConfigs(a, b) {
  208. /** @type {{plugins: StylelintConfigPlugins}} */
  209. const pluginMerger = {};
  210. if (a.plugins || b.plugins) {
  211. pluginMerger.plugins = [];
  212. if (a.plugins) {
  213. pluginMerger.plugins = pluginMerger.plugins.concat(a.plugins);
  214. }
  215. if (b.plugins) {
  216. pluginMerger.plugins = [...new Set(pluginMerger.plugins.concat(b.plugins))];
  217. }
  218. }
  219. /** @type {{processors: StylelintConfigProcessors}} */
  220. const processorMerger = {};
  221. if (a.processors || b.processors) {
  222. processorMerger.processors = [];
  223. if (a.processors) {
  224. processorMerger.processors = processorMerger.processors.concat(a.processors);
  225. }
  226. if (b.processors) {
  227. processorMerger.processors = [...new Set(processorMerger.processors.concat(b.processors))];
  228. }
  229. }
  230. /** @type {{overrides: StylelintConfigOverride[]}} */
  231. const overridesMerger = {};
  232. if (a.overrides || b.overrides) {
  233. overridesMerger.overrides = [];
  234. if (a.overrides) {
  235. overridesMerger.overrides = overridesMerger.overrides.concat(a.overrides);
  236. }
  237. if (b.overrides) {
  238. overridesMerger.overrides = [...new Set(overridesMerger.overrides.concat(b.overrides))];
  239. }
  240. }
  241. const rulesMerger = {};
  242. if (a.rules || b.rules) {
  243. rulesMerger.rules = { ...a.rules, ...b.rules };
  244. }
  245. const result = {
  246. ...a,
  247. ...b,
  248. ...processorMerger,
  249. ...pluginMerger,
  250. ...overridesMerger,
  251. ...rulesMerger,
  252. };
  253. return result;
  254. }
  255. /**
  256. * @param {StylelintConfig} config
  257. * @returns {StylelintConfig}
  258. */
  259. function addPluginFunctions(config) {
  260. if (!config.plugins) {
  261. return config;
  262. }
  263. const normalizedPlugins = [config.plugins].flat();
  264. /** @type {{[k: string]: Function}} */
  265. const pluginFunctions = {};
  266. for (const pluginLookup of normalizedPlugins) {
  267. let pluginImport = require(pluginLookup);
  268. // Handle either ES6 or CommonJS modules
  269. pluginImport = pluginImport.default || pluginImport;
  270. // A plugin can export either a single rule definition
  271. // or an array of them
  272. const normalizedPluginImport = [pluginImport].flat();
  273. for (const pluginRuleDefinition of normalizedPluginImport) {
  274. if (!pluginRuleDefinition.ruleName) {
  275. throw configurationError(
  276. `stylelint requires plugins to expose a ruleName. The plugin "${pluginLookup}" is not doing this, so will not work with stylelint. Please file an issue with the plugin.`,
  277. );
  278. }
  279. if (!pluginRuleDefinition.ruleName.includes('/')) {
  280. throw configurationError(
  281. `stylelint requires plugin rules to be namespaced, i.e. only \`plugin-namespace/plugin-rule-name\` plugin rule names are supported. The plugin rule "${pluginRuleDefinition.ruleName}" does not do this, so will not work. Please file an issue with the plugin.`,
  282. );
  283. }
  284. pluginFunctions[pluginRuleDefinition.ruleName] = pluginRuleDefinition.rule;
  285. }
  286. }
  287. config.pluginFunctions = pluginFunctions;
  288. return config;
  289. }
  290. /**
  291. * Given an array of processors strings, we want to add two
  292. * properties to the augmented config:
  293. * - codeProcessors: functions that will run on code as it comes in
  294. * - resultProcessors: functions that will run on results as they go out
  295. *
  296. * To create these properties, we need to:
  297. * - Find the processor module
  298. * - Initialize the processor module by calling its functions with any
  299. * provided options
  300. * - Push the processor's code and result processors to their respective arrays
  301. * @type {Map<string, string | Object>}
  302. */
  303. const processorCache = new Map();
  304. /**
  305. * @param {StylelintConfig} config
  306. * @return {StylelintConfig}
  307. */
  308. function addProcessorFunctions(config) {
  309. if (!config.processors) return config;
  310. /** @type {StylelintCodeProcessor[]} */
  311. const codeProcessors = [];
  312. /** @type {StylelintResultProcessor[]} */
  313. const resultProcessors = [];
  314. for (const processorConfig of [config.processors].flat()) {
  315. const processorKey = JSON.stringify(processorConfig);
  316. let initializedProcessor;
  317. if (processorCache.has(processorKey)) {
  318. initializedProcessor = processorCache.get(processorKey);
  319. } else {
  320. const processorLookup =
  321. typeof processorConfig === 'string' ? processorConfig : processorConfig[0];
  322. const processorOptions = typeof processorConfig === 'string' ? undefined : processorConfig[1];
  323. let processor = require(processorLookup);
  324. processor = processor.default || processor;
  325. initializedProcessor = processor(processorOptions);
  326. processorCache.set(processorKey, initializedProcessor);
  327. }
  328. if (initializedProcessor && initializedProcessor.code) {
  329. codeProcessors.push(initializedProcessor.code);
  330. }
  331. if (initializedProcessor && initializedProcessor.result) {
  332. resultProcessors.push(initializedProcessor.result);
  333. }
  334. }
  335. config.codeProcessors = codeProcessors;
  336. config.resultProcessors = resultProcessors;
  337. return config;
  338. }
  339. /**
  340. * @param {StylelintConfig} fullConfig
  341. * @param {string} rootConfigDir
  342. * @param {string} filePath
  343. * @return {StylelintConfig}
  344. */
  345. function applyOverrides(fullConfig, rootConfigDir, filePath) {
  346. let { overrides, ...config } = fullConfig;
  347. if (!overrides) {
  348. return config;
  349. }
  350. if (!Array.isArray(overrides)) {
  351. throw new TypeError(
  352. 'The `overrides` configuration property should be an array, e.g. { "overrides": [{ "files": "*.css", "rules": {} }] }.',
  353. );
  354. }
  355. for (const override of overrides) {
  356. const { files, ...configOverrides } = override;
  357. if (!files) {
  358. throw new Error(
  359. 'Every object in the `overrides` configuration property should have a `files` property with globs, e.g. { "overrides": [{ "files": "*.css", "rules": {} }] }.',
  360. );
  361. }
  362. const filesGlobs = [files]
  363. .flat()
  364. .map((glob) => {
  365. if (path.isAbsolute(glob.replace(/^!/, ''))) {
  366. return glob;
  367. }
  368. return globjoin(rootConfigDir, glob);
  369. })
  370. // Glob patterns for micromatch should be in POSIX-style
  371. .map((s) => normalizePath(s));
  372. if (micromatch.isMatch(filePath, filesGlobs, { dot: true })) {
  373. config = mergeConfigs(config, configOverrides);
  374. }
  375. }
  376. return config;
  377. }
  378. /**
  379. * Add options to the config
  380. *
  381. * @param {StylelintInternalApi} stylelint
  382. * @param {StylelintConfig} config
  383. *
  384. * @returns {StylelintConfig}
  385. */
  386. function addOptions(stylelint, config) {
  387. const augmentedConfig = {
  388. ...config,
  389. };
  390. if (stylelint._options.ignoreDisables) {
  391. augmentedConfig.ignoreDisables = stylelint._options.ignoreDisables;
  392. }
  393. if (stylelint._options.quiet) {
  394. augmentedConfig.quiet = stylelint._options.quiet;
  395. }
  396. if (stylelint._options.reportNeedlessDisables) {
  397. augmentedConfig.reportNeedlessDisables = stylelint._options.reportNeedlessDisables;
  398. }
  399. if (stylelint._options.reportInvalidScopeDisables) {
  400. augmentedConfig.reportInvalidScopeDisables = stylelint._options.reportInvalidScopeDisables;
  401. }
  402. if (stylelint._options.reportDescriptionlessDisables) {
  403. augmentedConfig.reportDescriptionlessDisables =
  404. stylelint._options.reportDescriptionlessDisables;
  405. }
  406. if (stylelint._options.customSyntax) {
  407. augmentedConfig.customSyntax = stylelint._options.customSyntax;
  408. }
  409. return augmentedConfig;
  410. }
  411. module.exports = { augmentConfigExtended, augmentConfigFull, applyOverrides };