1 | 'use strict';
|
2 |
|
3 | const assignDisabledRanges = require('./assignDisabledRanges');
|
4 | const getOsEol = require('./utils/getOsEol');
|
5 | const reportUnknownRuleNames = require('./reportUnknownRuleNames');
|
6 | const rulesOrder = require('./rules');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function lintPostcssResult(stylelintOptions, postcssResult, config) {
|
19 | postcssResult.stylelint.ruleSeverities = {};
|
20 | postcssResult.stylelint.customMessages = {};
|
21 | postcssResult.stylelint.stylelintError = false;
|
22 | postcssResult.stylelint.quiet = config.quiet;
|
23 | postcssResult.stylelint.config = config;
|
24 |
|
25 |
|
26 | let newline;
|
27 | const postcssDoc = postcssResult.root;
|
28 |
|
29 | if (postcssDoc) {
|
30 | if (!('type' in postcssDoc)) {
|
31 | throw new Error('Unexpected Postcss root object!');
|
32 | }
|
33 |
|
34 | const newlineMatch = postcssDoc.source && postcssDoc.source.input.css.match(/\r?\n/);
|
35 |
|
36 | newline = newlineMatch ? newlineMatch[0] : getOsEol();
|
37 |
|
38 | assignDisabledRanges(postcssDoc, postcssResult);
|
39 | }
|
40 |
|
41 | const isFileFixCompatible = isFixCompatible(postcssResult);
|
42 |
|
43 | if (!isFileFixCompatible) {
|
44 | postcssResult.stylelint.disableWritingFix = true;
|
45 | }
|
46 |
|
47 | const postcssRoots = (
|
48 | postcssDoc && postcssDoc.constructor.name === 'Document' ? postcssDoc.nodes : [postcssDoc]
|
49 | );
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | const performRules = [];
|
56 |
|
57 | const rules = config.rules
|
58 | ? Object.keys(config.rules).sort(
|
59 | (a, b) => Object.keys(rulesOrder).indexOf(a) - Object.keys(rulesOrder).indexOf(b),
|
60 | )
|
61 | : [];
|
62 |
|
63 | rules.forEach((ruleName) => {
|
64 | const ruleFunction =
|
65 | rulesOrder[ruleName] || (config.pluginFunctions && config.pluginFunctions[ruleName]);
|
66 |
|
67 | if (ruleFunction === undefined) {
|
68 | performRules.push(
|
69 | Promise.all(
|
70 | postcssRoots.map((postcssRoot) =>
|
71 | reportUnknownRuleNames(ruleName, postcssRoot, postcssResult),
|
72 | ),
|
73 | ),
|
74 | );
|
75 |
|
76 | return;
|
77 | }
|
78 |
|
79 | const ruleSettings = config.rules && config.rules[ruleName];
|
80 |
|
81 | if (ruleSettings === null || ruleSettings[0] === null) {
|
82 | return;
|
83 | }
|
84 |
|
85 | const primaryOption = ruleSettings[0];
|
86 | const secondaryOptions = ruleSettings[1];
|
87 |
|
88 |
|
89 | const defaultSeverity = config.defaultSeverity || 'error';
|
90 |
|
91 | const disableFix = (secondaryOptions && secondaryOptions.disableFix === true) || false;
|
92 |
|
93 | if (disableFix) {
|
94 | postcssResult.stylelint.ruleDisableFix = true;
|
95 | }
|
96 |
|
97 | postcssResult.stylelint.ruleSeverities[ruleName] =
|
98 | (secondaryOptions && secondaryOptions.severity) || defaultSeverity;
|
99 | postcssResult.stylelint.customMessages[ruleName] = secondaryOptions && secondaryOptions.message;
|
100 |
|
101 | performRules.push(
|
102 | Promise.all(
|
103 | postcssRoots.map((postcssRoot) =>
|
104 | ruleFunction(primaryOption, secondaryOptions, {
|
105 | fix:
|
106 | !disableFix &&
|
107 | stylelintOptions.fix &&
|
108 |
|
109 | isFileFixCompatible &&
|
110 | !postcssResult.stylelint.disabledRanges[ruleName],
|
111 | newline,
|
112 | })(postcssRoot, postcssResult),
|
113 | ),
|
114 | ),
|
115 | );
|
116 | });
|
117 |
|
118 | return Promise.all(performRules);
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | function isFixCompatible({ stylelint }) {
|
130 |
|
131 | if (stylelint.disabledRanges.all.length) return false;
|
132 |
|
133 | return true;
|
134 | }
|
135 |
|
136 | module.exports = lintPostcssResult;
|