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