UNPKG

3.15 kBJavaScriptView Raw
1'use strict';
2
3/** @typedef {import('stylelint').PostcssResult} PostcssResult */
4/** @typedef {import('stylelint').LintResult} StylelintResult */
5
6/**
7 * @param {PostcssResult} [postcssResult]
8 * @param {import('stylelint').CssSyntaxError} [cssSyntaxError]
9 * @return {StylelintResult}
10 */
11module.exports = function (postcssResult, cssSyntaxError) {
12 /** @type {StylelintResult} */
13 let stylelintResult;
14 /** @type {string | undefined} */
15 let source;
16
17 if (postcssResult && postcssResult.root) {
18 if (postcssResult.root.source) {
19 source = postcssResult.root.source.input.file;
20
21 if (!source && 'id' in postcssResult.root.source.input) {
22 source = postcssResult.root.source.input.id;
23 }
24 }
25
26 const deprecationMessages = postcssResult.messages.filter(
27 (message) => message.stylelintType === 'deprecation',
28 );
29 const deprecations = deprecationMessages.map((deprecationMessage) => {
30 return {
31 text: deprecationMessage.text,
32 reference: deprecationMessage.stylelintReference,
33 };
34 });
35
36 const invalidOptionMessages = postcssResult.messages.filter(
37 (message) => message.stylelintType === 'invalidOption',
38 );
39 const invalidOptionWarnings = invalidOptionMessages.map((invalidOptionMessage) => {
40 return {
41 text: invalidOptionMessage.text,
42 };
43 });
44
45 const parseErrors = postcssResult.messages.filter(
46 (message) => message.stylelintType === 'parseError',
47 );
48
49 // Remove deprecation warnings, invalid options, and parse errors from the messages
50 postcssResult.messages = postcssResult.messages.filter(
51 (message) =>
52 message.stylelintType !== 'deprecation' &&
53 message.stylelintType !== 'invalidOption' &&
54 message.stylelintType !== 'parseError',
55 );
56
57 // This defines the stylelint result object that formatters receive
58 stylelintResult = {
59 source,
60 deprecations,
61 invalidOptionWarnings,
62 // @ts-expect-error -- TS2322: Type 'Message[]' is not assignable to type '(Warning & { stylelintType: string; })[]'.
63 parseErrors,
64 errored: postcssResult.stylelint.stylelintError,
65 warnings: postcssResult.messages.map((message) => {
66 return {
67 line: message.line,
68 column: message.column,
69 endLine: message.endLine,
70 endColumn: message.endColumn,
71 rule: message.rule,
72 severity: message.severity,
73 text: message.text,
74 };
75 }),
76 ignored: postcssResult.stylelint.ignored,
77 _postcssResult: postcssResult,
78 };
79 } else if (cssSyntaxError) {
80 if (cssSyntaxError.name !== 'CssSyntaxError') {
81 throw cssSyntaxError;
82 }
83
84 stylelintResult = {
85 source: cssSyntaxError.file || '<input css 1>',
86 deprecations: [],
87 invalidOptionWarnings: [],
88 parseErrors: [],
89 errored: true,
90 warnings: [
91 {
92 line: cssSyntaxError.line,
93 column: cssSyntaxError.column,
94 endLine: cssSyntaxError.endLine,
95 endColumn: cssSyntaxError.endColumn,
96 rule: cssSyntaxError.name,
97 severity: 'error',
98 text: `${cssSyntaxError.reason} (${cssSyntaxError.name})`,
99 },
100 ],
101 };
102 } else {
103 throw new Error(
104 'createPartialStylelintResult must be called with either postcssResult or CssSyntaxError',
105 );
106 }
107
108 return stylelintResult;
109};