1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 | const assignDisabledRanges = require('./assignDisabledRanges');
|
5 | const getOsEol = require('./utils/getOsEol');
|
6 | const path = require('path');
|
7 | const reportUnknownRuleNames = require('./reportUnknownRuleNames');
|
8 | const rulesOrder = require('./rules');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | module.exports = function lintSource(stylelint, options = {}) {
|
23 | if (!options.filePath && options.code === undefined && !options.existingPostcssResult) {
|
24 | return Promise.reject(new Error('You must provide filePath, code, or existingPostcssResult'));
|
25 | }
|
26 |
|
27 | const isCodeNotFile = options.code !== undefined;
|
28 |
|
29 | const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath;
|
30 |
|
31 | if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) {
|
32 | if (isCodeNotFile) {
|
33 | return Promise.reject(new Error('codeFilename must be an absolute path'));
|
34 | }
|
35 |
|
36 | return Promise.reject(new Error('filePath must be an absolute path'));
|
37 | }
|
38 |
|
39 | const getIsIgnored = stylelint.isPathIgnored(inputFilePath).catch((err) => {
|
40 | if (isCodeNotFile && err.code === 'ENOENT') return false;
|
41 |
|
42 | throw err;
|
43 | });
|
44 |
|
45 | return getIsIgnored.then((isIgnored) => {
|
46 | if (isIgnored) {
|
47 |
|
48 | let postcssResult;
|
49 |
|
50 | if (options.existingPostcssResult) {
|
51 | postcssResult = Object.assign(options.existingPostcssResult, {
|
52 | stylelint: createEmptyStylelintPostcssResult(),
|
53 | });
|
54 | } else {
|
55 | postcssResult = createEmptyPostcssResult(inputFilePath);
|
56 | }
|
57 |
|
58 | return postcssResult;
|
59 | }
|
60 |
|
61 | const configSearchPath = stylelint._options.configFile || inputFilePath;
|
62 |
|
63 | const getConfig = stylelint.getConfigForFile(configSearchPath).catch((err) => {
|
64 | if (isCodeNotFile && err.code === 'ENOENT') return stylelint.getConfigForFile(process.cwd());
|
65 |
|
66 | throw err;
|
67 | });
|
68 |
|
69 | return getConfig.then((result) => {
|
70 | if (!result) {
|
71 | throw new Error('Config file not found');
|
72 | }
|
73 |
|
74 | const config = result.config;
|
75 | const existingPostcssResult = options.existingPostcssResult;
|
76 | const stylelintResult = {
|
77 | ruleSeverities: {},
|
78 | customMessages: {},
|
79 | disabledRanges: {},
|
80 | };
|
81 |
|
82 | if (existingPostcssResult) {
|
83 | const stylelintPostcssResult = Object.assign(existingPostcssResult, {
|
84 | stylelint: stylelintResult,
|
85 | });
|
86 |
|
87 | return lintPostcssResult(stylelint, stylelintPostcssResult, config).then(
|
88 | () => stylelintPostcssResult,
|
89 | );
|
90 | }
|
91 |
|
92 | return stylelint
|
93 | ._getPostcssResult({
|
94 | code: options.code,
|
95 | codeFilename: options.codeFilename,
|
96 | filePath: inputFilePath,
|
97 | codeProcessors: config.codeProcessors,
|
98 | })
|
99 | .then((postcssResult) => {
|
100 | const stylelintPostcssResult = Object.assign(postcssResult, {
|
101 | stylelint: stylelintResult,
|
102 | });
|
103 |
|
104 | return lintPostcssResult(stylelint, stylelintPostcssResult, config).then(
|
105 | () => stylelintPostcssResult,
|
106 | );
|
107 | });
|
108 | });
|
109 | });
|
110 | };
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | function lintPostcssResult(stylelint, postcssResult, config) {
|
119 | postcssResult.stylelint.ruleSeverities = {};
|
120 | postcssResult.stylelint.customMessages = {};
|
121 | postcssResult.stylelint.stylelintError = false;
|
122 | postcssResult.stylelint.quiet = config.quiet;
|
123 | postcssResult.stylelint.config = config;
|
124 |
|
125 |
|
126 | let newline;
|
127 | const postcssDoc = postcssResult.root;
|
128 |
|
129 | if (postcssDoc) {
|
130 | if (!('type' in postcssDoc)) {
|
131 | throw new Error('Unexpected Postcss root object!');
|
132 | }
|
133 |
|
134 |
|
135 | const newlineMatch = postcssDoc.source && postcssDoc.source.input.css.match(/\r?\n/);
|
136 |
|
137 | newline = newlineMatch ? newlineMatch[0] : getOsEol();
|
138 |
|
139 | assignDisabledRanges(postcssDoc, postcssResult);
|
140 | }
|
141 |
|
142 | if (stylelint._options.ignoreDisables) {
|
143 | postcssResult.stylelint.ignoreDisables = true;
|
144 | }
|
145 |
|
146 | if (stylelint._options.reportNeedlessDisables) {
|
147 | postcssResult.stylelint.reportNeedlessDisables = true;
|
148 | }
|
149 |
|
150 | const isFileFixCompatible = isFixCompatible(postcssResult);
|
151 |
|
152 | if (!isFileFixCompatible) {
|
153 | postcssResult.stylelint.disableWritingFix = true;
|
154 | }
|
155 |
|
156 | const postcssRoots = (postcssDoc &&
|
157 | postcssDoc.constructor.name === 'Document'
|
158 | ? postcssDoc.nodes
|
159 | : [postcssDoc]);
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | const performRules = [];
|
166 |
|
167 | const rules = config.rules
|
168 | ? Object.keys(config.rules).sort(
|
169 | (a, b) => Object.keys(rulesOrder).indexOf(a) - Object.keys(rulesOrder).indexOf(b),
|
170 | )
|
171 | : [];
|
172 |
|
173 | rules.forEach((ruleName) => {
|
174 | const ruleFunction = rulesOrder[ruleName] || _.get(config, ['pluginFunctions', ruleName]);
|
175 |
|
176 | if (ruleFunction === undefined) {
|
177 | performRules.push(
|
178 | Promise.all(
|
179 | postcssRoots.map((postcssRoot) =>
|
180 | reportUnknownRuleNames(ruleName, postcssRoot, postcssResult),
|
181 | ),
|
182 | ),
|
183 | );
|
184 |
|
185 | return;
|
186 | }
|
187 |
|
188 | const ruleSettings = _.get(config, ['rules', ruleName]);
|
189 |
|
190 | if (ruleSettings === null || ruleSettings[0] === null) {
|
191 | return;
|
192 | }
|
193 |
|
194 | const primaryOption = ruleSettings[0];
|
195 | const secondaryOptions = ruleSettings[1];
|
196 |
|
197 |
|
198 | const defaultSeverity = config.defaultSeverity || 'error';
|
199 |
|
200 | postcssResult.stylelint.ruleSeverities[ruleName] = _.get(
|
201 | secondaryOptions,
|
202 | 'severity',
|
203 | defaultSeverity,
|
204 | );
|
205 | postcssResult.stylelint.customMessages[ruleName] = _.get(secondaryOptions, 'message');
|
206 |
|
207 | performRules.push(
|
208 | Promise.all(
|
209 | postcssRoots.map((postcssRoot) =>
|
210 | ruleFunction(primaryOption, secondaryOptions, {
|
211 | fix:
|
212 | stylelint._options.fix &&
|
213 |
|
214 | isFileFixCompatible &&
|
215 | !postcssResult.stylelint.disabledRanges[ruleName],
|
216 | newline,
|
217 | })(postcssRoot, postcssResult),
|
218 | ),
|
219 | ),
|
220 | );
|
221 | });
|
222 |
|
223 | return Promise.all(performRules);
|
224 | }
|
225 |
|
226 |
|
227 |
|
228 |
|
229 | function createEmptyStylelintPostcssResult() {
|
230 | return {
|
231 | ruleSeverities: {},
|
232 | customMessages: {},
|
233 | disabledRanges: {},
|
234 | ignored: true,
|
235 | stylelintError: false,
|
236 | };
|
237 | }
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | function createEmptyPostcssResult(filePath) {
|
244 | return {
|
245 | root: {
|
246 | source: {
|
247 | input: { file: filePath },
|
248 | },
|
249 | },
|
250 | messages: [],
|
251 | opts: undefined,
|
252 | stylelint: createEmptyStylelintPostcssResult(),
|
253 | warn: () => {},
|
254 | };
|
255 | }
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | function isFixCompatible({ stylelint }) {
|
266 |
|
267 | if (stylelint.disabledRanges.all.length) return false;
|
268 |
|
269 | return true;
|
270 | }
|