1 | 'use strict';
|
2 |
|
3 | const configurationError = require('./utils/configurationError');
|
4 | const getModulePath = require('./utils/getModulePath');
|
5 | const globjoin = require('globjoin');
|
6 | const micromatch = require('micromatch');
|
7 | const normalizeAllRuleSettings = require('./normalizeAllRuleSettings');
|
8 | const normalizePath = require('normalize-path');
|
9 | const path = require('path');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | async function augmentConfigBasic(
|
35 | stylelint,
|
36 | config,
|
37 | configDir,
|
38 | allowOverrides,
|
39 | rootConfigDir,
|
40 | filePath,
|
41 | ) {
|
42 | let augmentedConfig = config;
|
43 |
|
44 | if (allowOverrides) {
|
45 | augmentedConfig = addOptions(stylelint, augmentedConfig);
|
46 | }
|
47 |
|
48 | if (filePath) {
|
49 | augmentedConfig = applyOverrides(augmentedConfig, rootConfigDir, filePath);
|
50 | }
|
51 |
|
52 | augmentedConfig = await extendConfig(
|
53 | stylelint,
|
54 | augmentedConfig,
|
55 | configDir,
|
56 | rootConfigDir,
|
57 | filePath,
|
58 | );
|
59 |
|
60 | const cwd = stylelint._options.cwd;
|
61 |
|
62 | return absolutizePaths(augmentedConfig, configDir, cwd);
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function augmentConfigExtended(cwd) {
|
73 | return async (cosmiconfigResult) => {
|
74 | if (!cosmiconfigResult) {
|
75 | return null;
|
76 | }
|
77 |
|
78 | const configDir = path.dirname(cosmiconfigResult.filepath || '');
|
79 | const { config } = cosmiconfigResult;
|
80 |
|
81 | const augmentedConfig = absolutizePaths(config, configDir, cwd);
|
82 |
|
83 | return {
|
84 | config: augmentedConfig,
|
85 | filepath: cosmiconfigResult.filepath,
|
86 | };
|
87 | };
|
88 | }
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | async function augmentConfigFull(stylelint, filePath, cosmiconfigResult) {
|
97 | if (!cosmiconfigResult) {
|
98 | return null;
|
99 | }
|
100 |
|
101 | const config = cosmiconfigResult.config;
|
102 | const filepath = cosmiconfigResult.filepath;
|
103 |
|
104 | const configDir = stylelint._options.configBasedir || path.dirname(filepath || '');
|
105 |
|
106 | let augmentedConfig = await augmentConfigBasic(
|
107 | stylelint,
|
108 | config,
|
109 | configDir,
|
110 | true,
|
111 | configDir,
|
112 | filePath,
|
113 | );
|
114 |
|
115 | augmentedConfig = addPluginFunctions(augmentedConfig);
|
116 | augmentedConfig = addProcessorFunctions(augmentedConfig);
|
117 |
|
118 | if (!augmentedConfig.rules) {
|
119 | throw configurationError(
|
120 | 'No rules found within configuration. Have you provided a "rules" property?',
|
121 | );
|
122 | }
|
123 |
|
124 | augmentedConfig = normalizeAllRuleSettings(augmentedConfig);
|
125 |
|
126 | return {
|
127 | config: augmentedConfig,
|
128 | filepath: cosmiconfigResult.filepath,
|
129 | };
|
130 | }
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | function absolutizePaths(config, configDir, cwd) {
|
144 | if (config.ignoreFiles) {
|
145 | config.ignoreFiles = [config.ignoreFiles].flat().map((glob) => {
|
146 | if (path.isAbsolute(glob.replace(/^!/, ''))) return glob;
|
147 |
|
148 | return globjoin(configDir, glob);
|
149 | });
|
150 | }
|
151 |
|
152 | if (config.plugins) {
|
153 | config.plugins = [config.plugins].flat().map((lookup) => getModulePath(configDir, lookup, cwd));
|
154 | }
|
155 |
|
156 | if (config.processors) {
|
157 | config.processors = absolutizeProcessors(config.processors, configDir);
|
158 | }
|
159 |
|
160 | return config;
|
161 | }
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | function absolutizeProcessors(processors, configDir) {
|
171 | const normalizedProcessors = Array.isArray(processors) ? processors : [processors];
|
172 |
|
173 | return normalizedProcessors.map((item) => {
|
174 | if (typeof item === 'string') {
|
175 | return getModulePath(configDir, item);
|
176 | }
|
177 |
|
178 | return [getModulePath(configDir, item[0]), item[1]];
|
179 | });
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | async function extendConfig(stylelint, config, configDir, rootConfigDir, filePath) {
|
191 | if (config.extends === undefined) {
|
192 | return config;
|
193 | }
|
194 |
|
195 | const { extends: configExtends, ...originalWithoutExtends } = config;
|
196 | const normalizedExtends = [configExtends].flat();
|
197 |
|
198 | let resultConfig = originalWithoutExtends;
|
199 |
|
200 | for (const extendLookup of normalizedExtends) {
|
201 | const extendResult = await loadExtendedConfig(stylelint, configDir, extendLookup);
|
202 |
|
203 | if (extendResult) {
|
204 | let extendResultConfig = extendResult.config;
|
205 | const extendConfigDir = path.dirname(extendResult.filepath || '');
|
206 |
|
207 | extendResultConfig = await augmentConfigBasic(
|
208 | stylelint,
|
209 | extendResultConfig,
|
210 | extendConfigDir,
|
211 | false,
|
212 | rootConfigDir,
|
213 | filePath,
|
214 | );
|
215 |
|
216 | resultConfig = mergeConfigs(resultConfig, extendResultConfig);
|
217 | }
|
218 | }
|
219 |
|
220 | return mergeConfigs(resultConfig, originalWithoutExtends);
|
221 | }
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | function loadExtendedConfig(stylelint, configDir, extendLookup) {
|
230 | const extendPath = getModulePath(configDir, extendLookup, stylelint._options.cwd);
|
231 |
|
232 | return stylelint._extendExplorer.load(extendPath);
|
233 | }
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 | function mergeConfigs(a, b) {
|
247 |
|
248 | const pluginMerger = {};
|
249 |
|
250 | if (a.plugins || b.plugins) {
|
251 | pluginMerger.plugins = [];
|
252 |
|
253 | if (a.plugins) {
|
254 | pluginMerger.plugins = pluginMerger.plugins.concat(a.plugins);
|
255 | }
|
256 |
|
257 | if (b.plugins) {
|
258 | pluginMerger.plugins = [...new Set(pluginMerger.plugins.concat(b.plugins))];
|
259 | }
|
260 | }
|
261 |
|
262 |
|
263 | const processorMerger = {};
|
264 |
|
265 | if (a.processors || b.processors) {
|
266 | processorMerger.processors = [];
|
267 |
|
268 | if (a.processors) {
|
269 | processorMerger.processors = processorMerger.processors.concat(a.processors);
|
270 | }
|
271 |
|
272 | if (b.processors) {
|
273 | processorMerger.processors = [...new Set(processorMerger.processors.concat(b.processors))];
|
274 | }
|
275 | }
|
276 |
|
277 |
|
278 | const overridesMerger = {};
|
279 |
|
280 | if (a.overrides || b.overrides) {
|
281 | overridesMerger.overrides = [];
|
282 |
|
283 | if (a.overrides) {
|
284 | overridesMerger.overrides = overridesMerger.overrides.concat(a.overrides);
|
285 | }
|
286 |
|
287 | if (b.overrides) {
|
288 | overridesMerger.overrides = [...new Set(overridesMerger.overrides.concat(b.overrides))];
|
289 | }
|
290 | }
|
291 |
|
292 | const rulesMerger = {};
|
293 |
|
294 | if (a.rules || b.rules) {
|
295 | rulesMerger.rules = { ...a.rules, ...b.rules };
|
296 | }
|
297 |
|
298 | const result = {
|
299 | ...a,
|
300 | ...b,
|
301 | ...processorMerger,
|
302 | ...pluginMerger,
|
303 | ...overridesMerger,
|
304 | ...rulesMerger,
|
305 | };
|
306 |
|
307 | return result;
|
308 | }
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | function addPluginFunctions(config) {
|
315 | if (!config.plugins) {
|
316 | return config;
|
317 | }
|
318 |
|
319 | const normalizedPlugins = [config.plugins].flat();
|
320 |
|
321 |
|
322 | const pluginFunctions = {};
|
323 |
|
324 | for (const pluginLookup of normalizedPlugins) {
|
325 | let pluginImport = require(pluginLookup);
|
326 |
|
327 |
|
328 | pluginImport = pluginImport.default || pluginImport;
|
329 |
|
330 |
|
331 |
|
332 | const normalizedPluginImport = [pluginImport].flat();
|
333 |
|
334 | for (const pluginRuleDefinition of normalizedPluginImport) {
|
335 | if (!pluginRuleDefinition.ruleName) {
|
336 | throw configurationError(
|
337 | `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.`,
|
338 | );
|
339 | }
|
340 |
|
341 | if (!pluginRuleDefinition.ruleName.includes('/')) {
|
342 | throw configurationError(
|
343 | `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.`,
|
344 | );
|
345 | }
|
346 |
|
347 | pluginFunctions[pluginRuleDefinition.ruleName] = pluginRuleDefinition.rule;
|
348 | }
|
349 | }
|
350 |
|
351 | config.pluginFunctions = pluginFunctions;
|
352 |
|
353 | return config;
|
354 | }
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | const processorCache = new Map();
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | function addProcessorFunctions(config) {
|
376 | if (!config.processors) return config;
|
377 |
|
378 |
|
379 | const codeProcessors = [];
|
380 |
|
381 | const resultProcessors = [];
|
382 |
|
383 | for (const processorConfig of [config.processors].flat()) {
|
384 | const processorKey = JSON.stringify(processorConfig);
|
385 |
|
386 | let initializedProcessor;
|
387 |
|
388 | if (processorCache.has(processorKey)) {
|
389 | initializedProcessor = processorCache.get(processorKey);
|
390 | } else {
|
391 | const processorLookup =
|
392 | typeof processorConfig === 'string' ? processorConfig : processorConfig[0];
|
393 | const processorOptions = typeof processorConfig === 'string' ? undefined : processorConfig[1];
|
394 | let processor = require(processorLookup);
|
395 |
|
396 | processor = processor.default || processor;
|
397 | initializedProcessor = processor(processorOptions);
|
398 | processorCache.set(processorKey, initializedProcessor);
|
399 | }
|
400 |
|
401 | if (initializedProcessor && initializedProcessor.code) {
|
402 | codeProcessors.push(initializedProcessor.code);
|
403 | }
|
404 |
|
405 | if (initializedProcessor && initializedProcessor.result) {
|
406 | resultProcessors.push(initializedProcessor.result);
|
407 | }
|
408 | }
|
409 |
|
410 | config.codeProcessors = codeProcessors;
|
411 | config.resultProcessors = resultProcessors;
|
412 |
|
413 | return config;
|
414 | }
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | function applyOverrides(fullConfig, rootConfigDir, filePath) {
|
423 | let { overrides, ...config } = fullConfig;
|
424 |
|
425 | if (!overrides) {
|
426 | return config;
|
427 | }
|
428 |
|
429 | if (!Array.isArray(overrides)) {
|
430 | throw new TypeError(
|
431 | 'The `overrides` configuration property should be an array, e.g. { "overrides": [{ "files": "*.css", "rules": {} }] }.',
|
432 | );
|
433 | }
|
434 |
|
435 | for (const override of overrides) {
|
436 | const { files, ...configOverrides } = override;
|
437 |
|
438 | if (!files) {
|
439 | throw new Error(
|
440 | 'Every object in the `overrides` configuration property should have a `files` property with globs, e.g. { "overrides": [{ "files": "*.css", "rules": {} }] }.',
|
441 | );
|
442 | }
|
443 |
|
444 | const filesGlobs = [files]
|
445 | .flat()
|
446 | .map((glob) => {
|
447 | if (path.isAbsolute(glob.replace(/^!/, ''))) {
|
448 | return glob;
|
449 | }
|
450 |
|
451 | return globjoin(rootConfigDir, glob);
|
452 | })
|
453 |
|
454 | .map((s) => normalizePath(s));
|
455 |
|
456 | if (micromatch.isMatch(filePath, filesGlobs, { dot: true })) {
|
457 | config = mergeConfigs(config, configOverrides);
|
458 | }
|
459 | }
|
460 |
|
461 | return config;
|
462 | }
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 | function addOptions(stylelint, config) {
|
473 | const augmentedConfig = {
|
474 | ...config,
|
475 | };
|
476 |
|
477 | if (stylelint._options.ignoreDisables) {
|
478 | augmentedConfig.ignoreDisables = stylelint._options.ignoreDisables;
|
479 | }
|
480 |
|
481 | if (stylelint._options.quiet) {
|
482 | augmentedConfig.quiet = stylelint._options.quiet;
|
483 | }
|
484 |
|
485 | if (stylelint._options.reportNeedlessDisables) {
|
486 | augmentedConfig.reportNeedlessDisables = stylelint._options.reportNeedlessDisables;
|
487 | }
|
488 |
|
489 | if (stylelint._options.reportInvalidScopeDisables) {
|
490 | augmentedConfig.reportInvalidScopeDisables = stylelint._options.reportInvalidScopeDisables;
|
491 | }
|
492 |
|
493 | if (stylelint._options.reportDescriptionlessDisables) {
|
494 | augmentedConfig.reportDescriptionlessDisables =
|
495 | stylelint._options.reportDescriptionlessDisables;
|
496 | }
|
497 |
|
498 | if (stylelint._options.customSyntax) {
|
499 | augmentedConfig.customSyntax = stylelint._options.customSyntax;
|
500 | }
|
501 |
|
502 | return augmentedConfig;
|
503 | }
|
504 |
|
505 | module.exports = { augmentConfigExtended, augmentConfigFull, applyOverrides };
|