1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const configurationError = require('./utils/configurationError');
|
5 | const getModulePath = require('./utils/getModulePath');
|
6 | const globjoin = require('globjoin');
|
7 | const normalizeAllRuleSettings = require('./normalizeAllRuleSettings');
|
8 | const path = require('path');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function augmentConfigBasic(stylelint, config, configDir, allowOverrides) {
|
29 | return Promise.resolve()
|
30 | .then(() => {
|
31 | if (!allowOverrides) return config;
|
32 |
|
33 | return _.merge(config, stylelint._options.configOverrides);
|
34 | })
|
35 | .then((augmentedConfig) => {
|
36 | return extendConfig(stylelint, augmentedConfig, configDir);
|
37 | })
|
38 | .then((augmentedConfig) => {
|
39 | return absolutizePaths(augmentedConfig, configDir);
|
40 | });
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function augmentConfigExtended(stylelint, cosmiconfigResult) {
|
52 | if (!cosmiconfigResult) return Promise.resolve(null);
|
53 |
|
54 | const configDir = path.dirname(cosmiconfigResult.filepath || '');
|
55 | const { ignoreFiles, ...cleanedConfig } = cosmiconfigResult.config;
|
56 |
|
57 | return augmentConfigBasic(stylelint, cleanedConfig, configDir).then((augmentedConfig) => {
|
58 | return {
|
59 | config: augmentedConfig,
|
60 | filepath: cosmiconfigResult.filepath,
|
61 | };
|
62 | });
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function augmentConfigFull(stylelint, cosmiconfigResult) {
|
71 | if (!cosmiconfigResult) return Promise.resolve(null);
|
72 |
|
73 | const config = cosmiconfigResult.config;
|
74 | const filepath = cosmiconfigResult.filepath;
|
75 |
|
76 | const configDir = stylelint._options.configBasedir || path.dirname(filepath || '');
|
77 |
|
78 | return augmentConfigBasic(stylelint, config, configDir, true)
|
79 | .then((augmentedConfig) => {
|
80 | return addPluginFunctions(augmentedConfig);
|
81 | })
|
82 | .then((augmentedConfig) => {
|
83 | return addProcessorFunctions(augmentedConfig);
|
84 | })
|
85 | .then((augmentedConfig) => {
|
86 | if (!augmentedConfig.rules) {
|
87 | throw configurationError(
|
88 | 'No rules found within configuration. Have you provided a "rules" property?',
|
89 | );
|
90 | }
|
91 |
|
92 | return normalizeAllRuleSettings(augmentedConfig);
|
93 | })
|
94 | .then((augmentedConfig) => {
|
95 | return {
|
96 | config: augmentedConfig,
|
97 | filepath: cosmiconfigResult.filepath,
|
98 | };
|
99 | });
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function absolutizePaths(config, configDir) {
|
113 | if (config.ignoreFiles) {
|
114 | config.ignoreFiles = ([]).concat(config.ignoreFiles).map((glob) => {
|
115 | if (path.isAbsolute(glob.replace(/^!/, ''))) return glob;
|
116 |
|
117 | return globjoin(configDir, glob);
|
118 | });
|
119 | }
|
120 |
|
121 | if (config.plugins) {
|
122 | config.plugins = ([]).concat(config.plugins).map((lookup) => {
|
123 | return getModulePath(configDir, lookup);
|
124 | });
|
125 | }
|
126 |
|
127 | if (config.processors) {
|
128 | config.processors = absolutizeProcessors(config.processors, configDir);
|
129 | }
|
130 |
|
131 | return config;
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | function absolutizeProcessors(processors, configDir) {
|
142 | const normalizedProcessors = Array.isArray(processors) ? processors : [processors];
|
143 |
|
144 | return normalizedProcessors.map((item) => {
|
145 | if (typeof item === 'string') {
|
146 | return getModulePath(configDir, item);
|
147 | }
|
148 |
|
149 | return [getModulePath(configDir, item[0]), item[1]];
|
150 | });
|
151 | }
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | function extendConfig(stylelint, config, configDir) {
|
160 | if (config.extends === undefined) return Promise.resolve(config);
|
161 |
|
162 | const normalizedExtends = Array.isArray(config.extends) ? config.extends : [config.extends];
|
163 | const { extends: configExtends, ...originalWithoutExtends } = config;
|
164 |
|
165 | const loadExtends = normalizedExtends.reduce((resultPromise, extendLookup) => {
|
166 | return resultPromise.then((resultConfig) => {
|
167 | return loadExtendedConfig(stylelint, resultConfig, configDir, extendLookup).then(
|
168 | (extendResult) => {
|
169 | if (!extendResult) return resultConfig;
|
170 |
|
171 | return mergeConfigs(resultConfig, extendResult.config);
|
172 | },
|
173 | );
|
174 | });
|
175 | }, Promise.resolve(originalWithoutExtends));
|
176 |
|
177 | return loadExtends.then((resultConfig) => {
|
178 | return mergeConfigs(resultConfig, originalWithoutExtends);
|
179 | });
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | function loadExtendedConfig(stylelint, config, configDir, extendLookup) {
|
190 | const extendPath = getModulePath(configDir, extendLookup);
|
191 |
|
192 | return stylelint._extendExplorer.load(extendPath);
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | function mergeConfigs(a, b) {
|
207 |
|
208 | const pluginMerger = {};
|
209 |
|
210 | if (a.plugins || b.plugins) {
|
211 | pluginMerger.plugins = [];
|
212 |
|
213 | if (a.plugins) {
|
214 | pluginMerger.plugins = pluginMerger.plugins.concat(a.plugins);
|
215 | }
|
216 |
|
217 | if (b.plugins) {
|
218 | pluginMerger.plugins = [...new Set(pluginMerger.plugins.concat(b.plugins))];
|
219 | }
|
220 | }
|
221 |
|
222 |
|
223 | const processorMerger = {};
|
224 |
|
225 | if (a.processors || b.processors) {
|
226 | processorMerger.processors = [];
|
227 |
|
228 | if (a.processors) {
|
229 | processorMerger.processors = processorMerger.processors.concat(a.processors);
|
230 | }
|
231 |
|
232 | if (b.processors) {
|
233 | processorMerger.processors = [...new Set(processorMerger.processors.concat(b.processors))];
|
234 | }
|
235 | }
|
236 |
|
237 | const rulesMerger = {};
|
238 |
|
239 | if (a.rules || b.rules) {
|
240 | rulesMerger.rules = { ...a.rules, ...b.rules };
|
241 | }
|
242 |
|
243 | const result = { ...a, ...b, ...processorMerger, ...pluginMerger, ...rulesMerger };
|
244 |
|
245 | return result;
|
246 | }
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | function addPluginFunctions(config) {
|
253 | if (!config.plugins) return config;
|
254 |
|
255 | const normalizedPlugins = Array.isArray(config.plugins) ? config.plugins : [config.plugins];
|
256 |
|
257 | const pluginFunctions = normalizedPlugins.reduce((result, pluginLookup) => {
|
258 | let pluginImport = require(pluginLookup);
|
259 |
|
260 |
|
261 | pluginImport = pluginImport.default || pluginImport;
|
262 |
|
263 |
|
264 |
|
265 | const normalizedPluginImport = Array.isArray(pluginImport) ? pluginImport : [pluginImport];
|
266 |
|
267 | normalizedPluginImport.forEach((pluginRuleDefinition) => {
|
268 | if (!pluginRuleDefinition.ruleName) {
|
269 | throw configurationError(
|
270 | 'stylelint v3+ requires plugins to expose a ruleName. ' +
|
271 | `The plugin "${pluginLookup}" is not doing this, so will not work ` +
|
272 | 'with stylelint v3+. Please file an issue with the plugin.',
|
273 | );
|
274 | }
|
275 |
|
276 | if (!pluginRuleDefinition.ruleName.includes('/')) {
|
277 | throw configurationError(
|
278 | 'stylelint v7+ requires plugin rules to be namespaced, ' +
|
279 | 'i.e. only `plugin-namespace/plugin-rule-name` plugin rule names are supported. ' +
|
280 | `The plugin rule "${pluginRuleDefinition.ruleName}" does not do this, so will not work. ` +
|
281 | 'Please file an issue with the plugin.',
|
282 | );
|
283 | }
|
284 |
|
285 | result[pluginRuleDefinition.ruleName] = pluginRuleDefinition.rule;
|
286 | });
|
287 |
|
288 | return result;
|
289 | }, ({}));
|
290 |
|
291 | config.pluginFunctions = pluginFunctions;
|
292 |
|
293 | return config;
|
294 | }
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | const processorCache = new Map();
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | function addProcessorFunctions(config) {
|
316 | if (!config.processors) return config;
|
317 |
|
318 |
|
319 | const codeProcessors = [];
|
320 |
|
321 | const resultProcessors = [];
|
322 |
|
323 | ([])
|
324 | .concat(config.processors)
|
325 | .forEach((processorConfig) => {
|
326 | const processorKey = JSON.stringify(processorConfig);
|
327 |
|
328 | let initializedProcessor;
|
329 |
|
330 | if (processorCache.has(processorKey)) {
|
331 | initializedProcessor = processorCache.get(processorKey);
|
332 | } else {
|
333 | const processorLookup =
|
334 | typeof processorConfig === 'string' ? processorConfig : processorConfig[0];
|
335 | const processorOptions =
|
336 | typeof processorConfig === 'string' ? undefined : processorConfig[1];
|
337 | let processor = require(processorLookup);
|
338 |
|
339 | processor = processor.default || processor;
|
340 | initializedProcessor = processor(processorOptions);
|
341 | processorCache.set(processorKey, initializedProcessor);
|
342 | }
|
343 |
|
344 | if (initializedProcessor && initializedProcessor.code) {
|
345 | codeProcessors.push(initializedProcessor.code);
|
346 | }
|
347 |
|
348 | if (initializedProcessor && initializedProcessor.result) {
|
349 | resultProcessors.push(initializedProcessor.result);
|
350 | }
|
351 | });
|
352 |
|
353 | config.codeProcessors = codeProcessors;
|
354 | config.resultProcessors = resultProcessors;
|
355 |
|
356 | return config;
|
357 | }
|
358 |
|
359 | module.exports = { augmentConfigExtended, augmentConfigFull };
|