forked from prettier/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-postcss.js
More file actions
235 lines (210 loc) · 5.82 KB
/
Copy pathparser-postcss.js
File metadata and controls
235 lines (210 loc) · 5.82 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"use strict";
const createError = require("./parser-create-error");
function parseSelector(selector) {
const selectorParser = require("postcss-selector-parser");
let result;
selectorParser(result_ => {
result = result_;
}).process(selector);
return addTypePrefix(result, "selector-");
}
function parseValueNodes(nodes) {
let parenGroup = {
open: null,
close: null,
groups: [],
type: "paren_group"
};
const parenGroupStack = [parenGroup];
const rootParenGroup = parenGroup;
let commaGroup = {
groups: [],
type: "comma_group"
};
const commaGroupStack = [commaGroup];
for (let i = 0; i < nodes.length; ++i) {
const node = nodes[i];
if (node.type === "paren" && node.value === "(") {
parenGroup = {
open: node,
close: null,
groups: [],
type: "paren_group"
};
parenGroupStack.push(parenGroup);
commaGroup = {
groups: [],
type: "comma_group"
};
commaGroupStack.push(commaGroup);
} else if (node.type === "paren" && node.value === ")") {
if (commaGroup.groups.length) {
parenGroup.groups.push(commaGroup);
}
parenGroup.close = node;
if (commaGroupStack.length === 1) {
throw new Error("Unbalanced parenthesis");
}
commaGroupStack.pop();
commaGroup = commaGroupStack[commaGroupStack.length - 1];
commaGroup.groups.push(parenGroup);
parenGroupStack.pop();
parenGroup = parenGroupStack[parenGroupStack.length - 1];
} else if (node.type === "comma") {
parenGroup.groups.push(commaGroup);
commaGroup = {
groups: [],
type: "comma_group"
};
commaGroupStack[commaGroupStack.length - 1] = commaGroup;
} else {
commaGroup.groups.push(node);
}
}
if (commaGroup.groups.length > 0) {
parenGroup.groups.push(commaGroup);
}
return rootParenGroup;
}
function flattenGroups(node) {
if (
node.type === "paren_group" &&
!node.open &&
!node.close &&
node.groups.length === 1
) {
return flattenGroups(node.groups[0]);
}
if (node.type === "comma_group" && node.groups.length === 1) {
return flattenGroups(node.groups[0]);
}
if (node.type === "paren_group" || node.type === "comma_group") {
return Object.assign({}, node, { groups: node.groups.map(flattenGroups) });
}
return node;
}
function addTypePrefix(node, prefix) {
if (node && typeof node === "object") {
delete node.parent;
for (const key in node) {
addTypePrefix(node[key], prefix);
if (key === "type" && typeof node[key] === "string") {
if (!node[key].startsWith(prefix)) {
node[key] = prefix + node[key];
}
}
}
}
return node;
}
function addMissingType(node) {
if (node && typeof node === "object") {
delete node.parent;
for (const key in node) {
addMissingType(node[key]);
}
if (!Array.isArray(node) && node.value && !node.type) {
node.type = "unknown";
}
}
return node;
}
function parseNestedValue(node) {
if (node && typeof node === "object") {
delete node.parent;
for (const key in node) {
parseNestedValue(node[key]);
if (key === "nodes") {
node.group = flattenGroups(parseValueNodes(node[key]));
delete node[key];
}
}
}
return node;
}
function parseValue(value) {
const valueParser = require("postcss-values-parser");
const result = valueParser(value, { loose: true }).parse();
const parsedResult = parseNestedValue(result);
return addTypePrefix(parsedResult, "value-");
}
function parseMediaQuery(value) {
const mediaParser = require("postcss-media-query-parser").default;
const result = addMissingType(mediaParser(value));
return addTypePrefix(result, "media-");
}
function parseNestedCSS(node) {
if (node && typeof node === "object") {
delete node.parent;
for (const key in node) {
parseNestedCSS(node[key]);
}
if (typeof node.selector === "string") {
const selector = node.raws.selector
? node.raws.selector.raw
: node.selector;
try {
node.selector = parseSelector(selector);
} catch (e) {
// Fail silently. It's better to print it as is than to try and parse it
node.selector = selector;
}
}
if (node.type && typeof node.value === "string") {
try {
node.value = parseValue(node.value);
} catch (e) {
throw createError(
"(postcss-values-parser) " + e.toString(),
node.source
);
}
}
if (node.type === "css-atrule" && typeof node.params === "string") {
node.params = parseMediaQuery(node.params);
}
}
return node;
}
function parseWithParser(parser, text) {
let result;
try {
result = parser.parse(text);
} catch (e) {
if (typeof e.line !== "number") {
throw e;
}
throw createError("(postcss) " + e.name + " " + e.reason, { start: e });
}
const prefixedResult = addTypePrefix(result, "css-");
const parsedResult = parseNestedCSS(prefixedResult);
return parsedResult;
}
function requireParser(isSCSS) {
if (isSCSS) {
return require("postcss-scss");
}
// TODO: Remove this hack when this issue is fixed:
// https://github.com/shellscape/postcss-less/issues/88
const LessParser = require("postcss-less/dist/less-parser");
LessParser.prototype.atrule = function() {
return Object.getPrototypeOf(LessParser.prototype).atrule.apply(
this,
arguments
);
};
return require("postcss-less");
}
function parse(text /*, parsers, opts*/) {
const isLikelySCSS = !!text.match(/(\w\s*: [^}:]+|#){|@import[^\n]+(url|,)/);
try {
return parseWithParser(requireParser(isLikelySCSS), text);
} catch (e) {
try {
return parseWithParser(requireParser(!isLikelySCSS), text);
} catch (e2) {
throw e;
}
}
}
module.exports = parse;