-
Notifications
You must be signed in to change notification settings - Fork 21
/
sass.js
311 lines (259 loc) · 8.45 KB
/
sass.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
//[sass]
function fetchWithTimeout(url) {
var controller = new AbortController();
setTimeout(() => {controller.abort()}, 5000);
return fetch(url, {signal: controller.signal});
}
const SassFunctions = [
// Color
"color.adjust", "adjust-color",
"color.scale", "scale-color",
"color.change",
"adjust-hue",
"opacity",
"darken", "lighten",
"desaturate", "saturate",
"opacify", "transparentize",
"fade-in", "fade-out",
...[
"red", "green", "blue",
"hue", "saturation", "lightness",
"alpha", "complement",
"grayscale",
"ie-hex-str",
"invert",
"mix"
].flatMap(n => ["color." + n, n]),
// List
"list.separator", "list-separator",
...[
"append", "index", "is-bracketed", "join",
"length", "set-nth", "nth", "zip"
].flatMap(n => ["list." + n, n]),
// Map
...[
"get", "has-key", "keys", "merge", "remove", "values"
].flatMap(n => ["map." + n, "map-" + n]),
// Math
...[
"ceil", "floor", "round",
"clamp", "min", "max",
"abs", "comparable", "unit", "percentage", "random"
].flatMap(n => ["math." + n, n]),
"math.hypot", "math.log", "math.pow", "math.sqrt",
"math.sin", "math.cos", "math.tan",
"math.asin", "math.acos", "math.atan", "math.atan2",
"math.is-unitless", "unitless",
// Meta
"meta.load-css",
"meta.module-functions", "meta.module-variables",
...[
"call", "get-function", "keywords",
"content-exists", "feature-exists", "function-exists", "global-variable-exists", "variable-exists", "mixin-exists",
"inspect", "type-of"
].flatMap(n => ["meta." + n, n]),
"if"
];
const sortObject = (obj, f = x => x) => Object.fromEntries(Object.entries(obj).sort((a, b) => f(b[1]) - f(a[1])));
function analyzeSCSS(scss, ret) {
if (!scss) {
return null;
}
// Drop (most) comments to minimize false positives since we have to parse with regexes
// We want to be conservative here and err on the side of NOT removing comments
scss = scss.replace(/^\s*\/\*[\s\S]*?\*\//mg, "");
scss = scss.replace(/^\s*\/\/.+/mg, "");
// Most popular variable names
ret.variables = {};
scss.replace(/\$[\w-]+\b/g, variable => {
ret.variables[variable] = (ret.variables[variable] || 0) + 1;
});
ret.variables = sortObject(ret.variables);
// Mixins
// Note: We are not taking the Indented Mixin Syntax into account
ret.mixins = {};
scss.replace(/@mixin\s+([\w-]+)(?:\((.+?)\))?/g, ($0, name, args) => {
// Note: - and _ are equivalent in mixin names and refer to the same mixin
// we are not taking that into account here. This applies to functions too (below)
ret.mixins[name] = {
// Default values can't have commas, so split should work
args: args?.split(/\s*,\s*/), // TODO separate name from default value?
calls: scss.match(RegExp("@include\\s+" + name + "\\b", "gi"))?.length
};
});
ret.mixins = sortObject(ret.mixins, o => o.calls);
// Custom functions
ret.functions = {};
let lastName; // used to track down the nearest @return after a @function
scss.replace(/@function\s+([\w-]+)(?:\((.+?)\))?|@return (.+)/g, ($0, name, args, returnValue) => {
if ($0.indexOf("@return") === 0 && lastName) {
ret.functions[lastName].returns = ret.functions[lastName].returns || [];
ret.functions[lastName].returns.push(returnValue);
return $0;
}
lastName = name;
ret.functions[name] = {
// Default values can't have commas, so split should work
args: args?.split(/\s*,\s*/), // TODO separate name from default value?
calls: scss.match(RegExp("\\b" + name + "\\(", "gi"))?.length
};
});
ret.functions = sortObject(ret.functions, o => o.calls);
// TODO Measure usage of Sass functions
ret.functionCalls = {};
let functionCallRegex = new RegExp(`(?<![-.\\w])(${SassFunctions.join("|").replace(".", "\\.")})\\(`, "gi");
scss.replace(functionCallRegex, ($0, name) => {
ret.functionCalls[name] = (ret.functionCalls[name] || 0) + 1;
});
ret.functionCalls = sortObject(ret.functionCalls);
// Conditionals
ret.ifs = [];
scss.replace(/@(?:(else )?if\s+?(.+?)|@else)(?=\s*\{)/g, ($0, elseIf, test) => {
if ($0 === "@else") {
// Else for previous condition
let obj = ret.ifs.pop();
console.log(ret.ifs, obj);
obj.hasElse = true;
ret.ifs.push(obj);
}
else if (elseIf) {
// Else if for previous condition
let obj = ret.ifs.pop();
obj.elseIfs = obj.elseIfs || [];
obj.elseIfs.push(test);
ret.ifs.push(obj);
}
else {
// New conditional
ret.ifs.push({test});
}
});
// @each, @for, @while Loops
ret.eaches = {};
scss.replace(/@each\s+(.+ in .+)(?=\s*\{)/g, ($0, args) => {
ret.eaches[args] = (ret.eaches[args] || 0) + 1;
});
ret.fors = {};
// console.log("@for", scss);
scss.replace(/@for\s+(.+)(?=\s*\{)/g, ($0, args) => {
ret.fors[args] = (ret.fors[args] || 0) + 1;
});
ret.whiles = {};
scss.replace(/@while\s+(.+)(?=\s*\{)/g, ($0, args) => {
ret.whiles[args] = (ret.whiles[args] || 0) + 1;
});
// @extend
ret.extends = {};
scss.replace(/@extend\s*(.+)(?=\s*(?:[;}]|$))/g, ($0, selector) => {
ret.extends[selector] = (ret.extends[selector] || 0) + 1;
});
// @error
ret.errors = scss.match(/@error (.+)/g)?.map(e => e.slice(7));
// CSS variables that are set with Sass variables
// Note that this will fail on multiline values (it will return the first line only)
ret.variablesCombined = {value: {}, name: {}};
scss.replace(/(?<=^|\s)--([\w-]+):\s*(.*#\{.+\}.*)\s*$/gm, ($0, name, value) => {
ret.variablesCombined.value["--" + name] = value;
});
scss.replace(/(?<=^|\s)--([\w-]*#\{.+?\}[\w-]*):\s*(.+?)(?=;|$)/gm, ($0, name, value) => {
ret.variablesCombined.name["--" + name] = value;
});
// Heuristic for nesting &
ret.nested = {
"descendant": scss.match(/&\s+(?=[\w[.:#])/g)?.length,
">": scss.match(/&\s*(?=>)/g)?.length,
"+": scss.match(/&\s*(?=\+)/g)?.length,
"~": scss.match(/&\s*(?=~)/g)?.length,
"lone": scss.match(/&\s*(?=[,{]|$})/g)?.length,
"pseudo-element": scss.match(/&::/g)?.length,
"pseudo-class": scss.match(/&:(?!:)/g)?.length,
"id": scss.match(/&#\w+/g)?.length,
"class": scss.match(/&\.\w+/g)?.length,
"attr": scss.match(/&\[\w+/g)?.length,
};
}
let results = (async () => {
const results = {};
const $$ = s => [...document.querySelectorAll(s)];
const sourcemapRegex = /\/\*[#@] sourceMappingURL=(.+?) \*\//;
let stylesheets = $WPT_BODIES.filter(request => request.type == "Stylesheet")
.map(file => {
return {url: file.url, body: file.response_body};
});
results.stylesheets = {
remote: stylesheets.length
};
stylesheets.push(...$$("style").map(s => {
return {
url: location,
body: s.textContent
}
}));
results.stylesheets.inline = stylesheets.length - results.stylesheets.remote;
let sourcemapURLs = stylesheets.map(o => {
if (o) {
let url = o.body.match(sourcemapRegex)?.[1];
if (url) {
// Source map URL is relative to stylesheet URL
return new URL(url, o.url);
}
}
}).filter(url => !!url);
results.sourcemaps = {
count: sourcemapURLs.length,
ext: {}
};
if (sourcemapURLs.length === 0) {
return results;
}
// Assumption: Either all sources are SCSS or none.
let scss = await Promise.all(sourcemapURLs.map(async url => {
try {
var response = await fetchWithTimeout(url);
var json = await response.json();
}
catch (e) {
return;
}
let sources = json.sources;
let base = json.sourceRoot? new URL(json.sourceRoot, url) : url;
let scss = 0;
sources = sources.map(s => {
let url = new URL(s, base);
let ext = url.pathname.match(/\.(\w+)$/)?.[1];
if (ext) {
results.sourcemaps.ext[ext] = (results.sourcemaps.ext[ext] || 0) + 1;
if (ext === "scss") {
scss++;
}
}
return url;
});
if (scss > 0) {
if (json.sourcesContent) {
// Source is already here, no more requests needed, yay!
return json.sourcesContent;
}
let code = await Promise.all(sources.map(async s => {
try {
let response = await fetchWithTimeout(s);
let text = response.ok? await response.text() : "";
return text;
}
catch (e) {
return "";
}
}));
return code.join("\n");
}
}));
scss = scss.filter(s => !!s).join("\n");
results.scss = {
size: scss.length,
stats: {}
};
analyzeSCSS(scss, results.scss.stats)
return results;
})();
// results.then(r => console.log(JSON.stringify(r, null, "\t")));
return results.then(r => JSON.stringify(r));