forked from prettier/prettier
/
clean.js
215 lines (187 loc) · 5.19 KB
/
clean.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
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
"use strict";
const { isFrontMatterNode } = require("../common/util.js");
const getLast = require("../utils/get-last.js");
const ignoredProperties = new Set([
"raw", // front-matter
"raws",
"sourceIndex",
"source",
"before",
"after",
"trailingComma",
]);
function clean(ast, newObj, parent) {
if (isFrontMatterNode(ast) && ast.lang === "yaml") {
delete newObj.value;
}
if (
ast.type === "css-comment" &&
parent.type === "css-root" &&
parent.nodes.length > 0
) {
// --insert-pragma
// first non-front-matter comment
if (
parent.nodes[0] === ast ||
(isFrontMatterNode(parent.nodes[0]) && parent.nodes[1] === ast)
) {
/**
* something
*
* @format
*/
delete newObj.text;
// standalone pragma
if (/^\*\s*@(?:format|prettier)\s*$/.test(ast.text)) {
return null;
}
}
// Last comment is not parsed, when omitting semicolon, #8675
if (parent.type === "css-root" && getLast(parent.nodes) === ast) {
return null;
}
}
if (ast.type === "value-root") {
delete newObj.text;
}
if (
ast.type === "media-query" ||
ast.type === "media-query-list" ||
ast.type === "media-feature-expression"
) {
delete newObj.value;
}
if (ast.type === "css-rule") {
delete newObj.params;
}
if (ast.type === "selector-combinator") {
newObj.value = newObj.value.replace(/\s+/g, " ");
}
if (ast.type === "media-feature") {
newObj.value = newObj.value.replace(/ /g, "");
}
if (
(ast.type === "value-word" &&
((ast.isColor && ast.isHex) ||
["initial", "inherit", "unset", "revert"].includes(
newObj.value.replace().toLowerCase()
))) ||
ast.type === "media-feature" ||
ast.type === "selector-root-invalid" ||
ast.type === "selector-pseudo"
) {
newObj.value = newObj.value.toLowerCase();
}
if (ast.type === "css-decl") {
newObj.prop = newObj.prop.toLowerCase();
}
if (ast.type === "css-atrule" || ast.type === "css-import") {
newObj.name = newObj.name.toLowerCase();
}
if (ast.type === "value-number") {
newObj.unit = newObj.unit.toLowerCase();
}
if (
(ast.type === "media-feature" ||
ast.type === "media-keyword" ||
ast.type === "media-type" ||
ast.type === "media-unknown" ||
ast.type === "media-url" ||
ast.type === "media-value" ||
ast.type === "selector-attribute" ||
ast.type === "selector-string" ||
ast.type === "selector-class" ||
ast.type === "selector-combinator" ||
ast.type === "value-string") &&
newObj.value
) {
newObj.value = cleanCSSStrings(newObj.value);
}
if (ast.type === "selector-attribute") {
newObj.attribute = newObj.attribute.trim();
if (newObj.namespace) {
if (typeof newObj.namespace === "string") {
newObj.namespace = newObj.namespace.trim();
if (newObj.namespace.length === 0) {
newObj.namespace = true;
}
}
}
if (newObj.value) {
newObj.value = newObj.value.trim().replace(/^["']|["']$/g, "");
delete newObj.quoted;
}
}
if (
(ast.type === "media-value" ||
ast.type === "media-type" ||
ast.type === "value-number" ||
ast.type === "selector-root-invalid" ||
ast.type === "selector-class" ||
ast.type === "selector-combinator" ||
ast.type === "selector-tag") &&
newObj.value
) {
newObj.value = newObj.value.replace(
/([\d+.Ee-]+)([A-Za-z]*)/g,
(match, numStr, unit) => {
const num = Number(numStr);
return Number.isNaN(num) ? match : num + unit.toLowerCase();
}
);
}
if (ast.type === "selector-tag") {
const lowercasedValue = ast.value.toLowerCase();
if (["from", "to"].includes(lowercasedValue)) {
newObj.value = lowercasedValue;
}
}
// Workaround when `postcss-values-parser` parse `not`, `and` or `or` keywords as `value-func`
if (ast.type === "css-atrule" && ast.name.toLowerCase() === "supports") {
delete newObj.value;
}
// Workaround for SCSS nested properties
if (ast.type === "selector-unknown") {
delete newObj.value;
}
// Workaround for SCSS arbitrary arguments
if (ast.type === "value-comma_group") {
const index = ast.groups.findIndex(
(node) => node.type === "value-number" && node.unit === "..."
);
if (index !== -1) {
newObj.groups[index].unit = "";
newObj.groups.splice(index + 1, 0, {
type: "value-word",
value: "...",
isColor: false,
isHex: false,
});
}
}
// We parse `@var[ foo ]` and `@var[foo]` differently
if (
ast.type === "value-comma_group" &&
ast.groups.some(
(node) =>
typeof node.value === "string" &&
(node.value.endsWith("[") || node.value.startsWith("]"))
)
) {
return {
type: "value-atword",
value: ast.groups.map((node) => node.value).join(""),
group: {
open: null,
close: null,
groups: [],
type: "value-paren_group",
},
};
}
}
clean.ignoredProperties = ignoredProperties;
function cleanCSSStrings(value) {
return value.replace(/'/g, '"').replace(/\\([^\dA-Fa-f])/g, "$1");
}
module.exports = clean;