-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
core-entrypoint.js
139 lines (113 loc) · 4.27 KB
/
core-entrypoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const LazyResult = require('postcss/lib/lazy-result');
const postcss = require('postcss');
const postcssSafeParser = require('postcss-safe-parser');
const createPartialStylelintResult = require('./createPartialStylelintResult');
const jsonFormatter = require('./formatters/jsonFormatter');
const lintPostcssResult = require('./lintPostcssResult');
const normalizeAllRuleSettings = require('./normalizeAllRuleSettings');
const prepareReturnValue = require('./prepareReturnValue');
const postcssProcessor = postcss();
/** @typedef {import('stylelint').StylelintStandaloneOptions} StylelintStandaloneOptions */
/** @typedef {import('stylelint').StylelintStandaloneReturnValue} StylelintStandaloneReturnValue */
/** @typedef {import('stylelint').StylelintResult} StylelintResult */
/** @typedef {import('stylelint').StylelintInternalApi} StylelintInternalApi */
/** @typedef {import('postcss').Syntax} Syntax */
/** TODO: create new type that's a subset of StylelintStandaloneOptions */
/**
* @param {StylelintStandaloneOptions} options
* @returns {Promise<StylelintStandaloneReturnValue>}
*/
async function lint(options) {
if (!options.code) {
throw new Error(
`"options.code" is missing. You must provide a string value in "options.code".`,
);
}
if (!options.config) {
throw new Error(
`"options.config" is missing. You must provide a config object in "options.config".`,
);
}
if (options.formatter) {
throw new Error(
`"options.formatter" is not supported for stylelint's browser bundle. Remove the formatter option or try the standalone version of stylelint. TODO: link here?`,
);
}
if (options.syntax) {
throw new Error(
`"options.syntax" is not supported for stylelint's browser bundle. You must load a syntax and pass it in to "options.customSyntax". Refer to the stylelint browser bundle docs for more info. TODO: url here`,
);
}
if (options.config.plugins) {
throw new Error(
`"options.config.plugins" is not supported for stylelint's browser bundle. Remove the plugins from your config or try the standalone version of stylelint. TODO: url here`,
);
}
// TODO: more input validation? check for other options that aren't supported here
const syntax = syntaxFromOptions(options);
const postcssOptions = {
syntax,
from: undefined,
};
let cssResult;
let stylelintResult /** @type StylelintResult */;
try {
const lazyResult = await new LazyResult(postcssProcessor, options.code, postcssOptions);
cssResult = Object.assign(lazyResult, {
stylelint: {
ruleSeverities: {},
customMessages: {},
disabledRanges: {},
},
});
const normalizedConfig = normalizeAllRuleSettings(options.config);
await lintPostcssResult(options, cssResult, normalizedConfig);
stylelintResult = createPartialStylelintResult(cssResult);
} catch (error) {
// This is equivalent to the `handleError` function in standalone.js
if (error.name === 'CssSyntaxError') {
stylelintResult = createPartialStylelintResult(undefined, error);
} else {
throw error;
}
}
// TODO: this is mostly the same in standalone.js. Extract out?
const postcssResult = stylelintResult._postcssResult;
const returnValue = prepareReturnValue([stylelintResult], options, jsonFormatter);
if (options.fix && postcssResult && !postcssResult.stylelint.ignored) {
if (!postcssResult.stylelint.disableWritingFix) {
// If we're fixing, the output should be the fixed code
// @ts-ignore note: ts complaining that .root might not exist
returnValue.output = postcssResult.root.toString(syntax);
} else {
// If the writing of the fix is disabled, the input code is returned as-is
returnValue.output = options.code;
}
}
return returnValue;
}
/**
* @param {StylelintStandaloneOptions} options
* @returns {Syntax}
*/
function syntaxFromOptions(options) {
const { customSyntax, fix } = options;
if (!customSyntax)
return {
parse: fix ? postcssSafeParser : postcss.parse,
stringify: postcss.stringify,
};
if (
customSyntax &&
typeof customSyntax === 'object' &&
typeof customSyntax.parse == 'function' &&
typeof customSyntax.stringify == 'function'
) {
return customSyntax;
}
throw new Error(
`Provided customSyntax is invalid. You must provide an object containing 'parse' and 'stringify' methods. TODO docs link?`,
);
}
module.exports = { lint };