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