@tbela99/css-parser 1.4.11 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +39 -0
- package/README.md +18 -11
- package/dist/index-umd-web.js +6558 -5310
- package/dist/index.cjs +6576 -5313
- package/dist/index.d.ts +805 -279
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +65 -15
- package/dist/lib/ast/features/calc.js +19 -56
- package/dist/lib/ast/features/if.js +28 -4
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +52 -73
- package/dist/lib/ast/features/shorthand.js +11 -6
- package/dist/lib/ast/features/transform.js +6 -3
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +117 -49
- package/dist/lib/ast/node.js +52 -0
- package/dist/lib/ast/transform/compute.js +63 -4
- package/dist/lib/ast/transform/minify.js +6 -6
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +46 -18
- package/dist/lib/ast/types.js +1 -1
- package/dist/lib/ast/walk.js +55 -11
- package/dist/lib/fs/resolve.js +68 -27
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +40 -13
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -12
- package/dist/lib/parser/parse.js +843 -912
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +1489 -725
- package/dist/lib/parser/utils/at-rule-container.js +29 -196
- package/dist/lib/parser/utils/at-rule-generic.js +6 -7
- package/dist/lib/parser/utils/at-rule-import.js +17 -13
- package/dist/lib/parser/utils/at-rule-media.js +36 -19
- package/dist/lib/parser/utils/at-rule-support.js +19 -9
- package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
- package/dist/lib/parser/utils/at-rule.js +0 -18
- package/dist/lib/parser/utils/declaration.js +30 -95
- package/dist/lib/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +164 -141
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +230 -125
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
- package/dist/lib/syntax/color/a98rgb.js +14 -4
- package/dist/lib/syntax/color/cmyk.js +10 -18
- package/dist/lib/syntax/color/color-mix.js +21 -34
- package/dist/lib/syntax/color/color.js +32 -49
- package/dist/lib/syntax/color/hsl.js +15 -15
- package/dist/lib/syntax/color/hwb.js +18 -17
- package/dist/lib/syntax/color/lab.js +17 -17
- package/dist/lib/syntax/color/lch.js +13 -13
- package/dist/lib/syntax/color/oklab.js +10 -11
- package/dist/lib/syntax/color/oklch.js +15 -18
- package/dist/lib/syntax/color/p3.js +18 -12
- package/dist/lib/syntax/color/prophotorgb.js +26 -28
- package/dist/lib/syntax/color/rec2020.js +8 -4
- package/dist/lib/syntax/color/relative-color.js +26 -10
- package/dist/lib/syntax/color/srgb.js +5 -4
- package/dist/lib/syntax/color/utils/distance.js +1 -1
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/color/xyzd50.js +2 -2
- package/dist/lib/syntax/constants.js +12 -2
- package/dist/lib/syntax/syntax.js +45 -258
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +110 -27
- package/dist/node.js +55 -33
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +73 -0
- package/dist/web.js +37 -30
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
- package/llms.txt +0 -73
package/dist/lib/parser/parse.js
CHANGED
|
@@ -4,9 +4,9 @@ import { renderValue } from '../renderer/render.js';
|
|
|
4
4
|
import { EnumToken, EnumAstNodeStatus, ModuleCaseTransformEnum, ModuleScopeEnumOptions } from '../ast/types.js';
|
|
5
5
|
import { minify } from '../ast/minify.js';
|
|
6
6
|
import { expand } from '../ast/expand.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
7
|
+
import { walk, walkValues, WalkerEvent } from '../ast/walk.js';
|
|
8
|
+
import { Tokenizer } from './tokenize.js';
|
|
9
|
+
import { LOCSRCID, LOCSTA, LOCEND, tokensfuncDefMap, STATE, PARENT, TOKENS, ROOT, ERRORS, pageMarginBoxType } from '../syntax/constants.js';
|
|
10
10
|
import { hashAlgorithms, hash, syncHash } from './utils/hash.js';
|
|
11
11
|
import { parseSelector } from './utils/selector.js';
|
|
12
12
|
import { parseDeclaration } from './utils/declaration.js';
|
|
@@ -24,6 +24,7 @@ import { parseAtRuleFontFeatureValues } from './utils/at-rule-font-feature-value
|
|
|
24
24
|
import { matchGenericSyntax } from './utils/at-rule-generic.js';
|
|
25
25
|
import { memoize } from './utils/cache.js';
|
|
26
26
|
import { SourceFile } from './source.js';
|
|
27
|
+
import { dirname } from '../fs/resolve.js';
|
|
27
28
|
|
|
28
29
|
function renderTokens(tokens, options) {
|
|
29
30
|
if (tokens == null || tokens.length === 0)
|
|
@@ -47,7 +48,6 @@ const BadTokensTypes = [
|
|
|
47
48
|
EnumToken.BadStringTokenType,
|
|
48
49
|
];
|
|
49
50
|
let keyNameCounter = 0;
|
|
50
|
-
const forbiddenStartCharacters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"].map((c) => c.charCodeAt(0));
|
|
51
51
|
/**
|
|
52
52
|
* Short-scoped name generator.
|
|
53
53
|
*
|
|
@@ -58,12 +58,15 @@ const forbiddenStartCharacters = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "
|
|
|
58
58
|
*
|
|
59
59
|
* @returns string
|
|
60
60
|
*/
|
|
61
|
-
const getShortNameGenerator = memoize((
|
|
61
|
+
const getShortNameGenerator = memoize(() => {
|
|
62
62
|
let value = keyNameCounter.toString(36);
|
|
63
|
+
let val = value.charAt(0).charCodeAt(0);
|
|
63
64
|
keyNameCounter++;
|
|
64
|
-
|
|
65
|
+
// starts with'0' - '9'
|
|
66
|
+
while (48 <= val && val <= 57) {
|
|
65
67
|
value = keyNameCounter.toString(36);
|
|
66
68
|
keyNameCounter++;
|
|
69
|
+
val = value.charAt(0).charCodeAt(0);
|
|
67
70
|
}
|
|
68
71
|
return value;
|
|
69
72
|
});
|
|
@@ -290,6 +293,145 @@ const generateSyncScopedName = memoize((localName, filePath, pattern, hashLength
|
|
|
290
293
|
// if leading char is digit, prefix underscore (very rare)
|
|
291
294
|
return (/^[0-9]/.test(result) ? "_" : "") + result;
|
|
292
295
|
});
|
|
296
|
+
/**
|
|
297
|
+
*
|
|
298
|
+
* @param visitorsDef
|
|
299
|
+
* @param errors
|
|
300
|
+
* @private
|
|
301
|
+
*/
|
|
302
|
+
function parseVisitors(visitorsDef, errors) {
|
|
303
|
+
const visitors = Object.entries(typeof visitorsDef === "function" ? [visitorsDef] : visitorsDef);
|
|
304
|
+
let key;
|
|
305
|
+
let value;
|
|
306
|
+
let i;
|
|
307
|
+
const valuesHandlers = new Map();
|
|
308
|
+
const preValuesHandlers = new Map();
|
|
309
|
+
const postValuesHandlers = new Map();
|
|
310
|
+
const visitorsHandlersMap = new Map();
|
|
311
|
+
const preVisitorsHandlersMap = new Map();
|
|
312
|
+
const postVisitorsHandlersMap = new Map();
|
|
313
|
+
for (i = 0; i < visitors.length; i++) {
|
|
314
|
+
key = visitors[i][0];
|
|
315
|
+
value = visitors[i][1];
|
|
316
|
+
if (Number.isInteger(+key)) {
|
|
317
|
+
// if (Array.isArray(value)) {
|
|
318
|
+
// visitors.splice(i + 1, 0, ...Object.entries(value));
|
|
319
|
+
// continue;
|
|
320
|
+
// }
|
|
321
|
+
if (typeof value == "function") {
|
|
322
|
+
key = value.name;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
// if (Array.isArray(value)) {
|
|
326
|
+
// // @ts-ignore
|
|
327
|
+
// visitors.splice(i + 1, 0, ...value.map((item) => [key, item]));
|
|
328
|
+
// continue;
|
|
329
|
+
// }
|
|
330
|
+
if (key in EnumToken) {
|
|
331
|
+
if (typeof value == "function") {
|
|
332
|
+
if (!valuesHandlers.has(EnumToken[key])) {
|
|
333
|
+
valuesHandlers.set(EnumToken[key], []);
|
|
334
|
+
}
|
|
335
|
+
valuesHandlers.get(EnumToken[key]).push(value);
|
|
336
|
+
}
|
|
337
|
+
else if (typeof value == "object") {
|
|
338
|
+
if ("type" in value && "handler" in value && value.type in WalkerEvent) {
|
|
339
|
+
if (value.type == WalkerEvent.Enter) {
|
|
340
|
+
if (!preValuesHandlers.has(EnumToken[key])) {
|
|
341
|
+
preValuesHandlers.set(EnumToken[key], []);
|
|
342
|
+
}
|
|
343
|
+
preValuesHandlers
|
|
344
|
+
.get(EnumToken[key])
|
|
345
|
+
.push(value.handler);
|
|
346
|
+
}
|
|
347
|
+
else if (value.type == WalkerEvent.Leave) {
|
|
348
|
+
if (!postValuesHandlers.has(EnumToken[key])) {
|
|
349
|
+
postValuesHandlers.set(EnumToken[key], []);
|
|
350
|
+
}
|
|
351
|
+
postValuesHandlers
|
|
352
|
+
.get(EnumToken[key])
|
|
353
|
+
.push(value.handler);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
else {
|
|
357
|
+
for (const val of Object.entries(value)) {
|
|
358
|
+
visitors.push(val);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
else if (["Declaration", "Rule", "AtRule", "KeyframesRule", "KeyframesAtRule"].includes(key)) {
|
|
367
|
+
if (typeof value == "function") {
|
|
368
|
+
if (!visitorsHandlersMap.has(key)) {
|
|
369
|
+
visitorsHandlersMap.set(key, []);
|
|
370
|
+
}
|
|
371
|
+
visitorsHandlersMap
|
|
372
|
+
.get(key)
|
|
373
|
+
.push(value);
|
|
374
|
+
}
|
|
375
|
+
else if (typeof value == "object") {
|
|
376
|
+
if ("type" in value && "handler" in value && value.type in WalkerEvent) {
|
|
377
|
+
if (value.type == WalkerEvent.Enter) {
|
|
378
|
+
if (!preVisitorsHandlersMap.has(key)) {
|
|
379
|
+
preVisitorsHandlersMap.set(key, []);
|
|
380
|
+
}
|
|
381
|
+
preVisitorsHandlersMap
|
|
382
|
+
.get(key)
|
|
383
|
+
.push(value.handler);
|
|
384
|
+
}
|
|
385
|
+
else if (value.type == WalkerEvent.Leave) {
|
|
386
|
+
if (!postVisitorsHandlersMap.has(key)) {
|
|
387
|
+
postVisitorsHandlersMap.set(key, []);
|
|
388
|
+
}
|
|
389
|
+
postVisitorsHandlersMap
|
|
390
|
+
.get(key)
|
|
391
|
+
.push(value.handler);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
if (!visitorsHandlersMap.has(key)) {
|
|
396
|
+
visitorsHandlersMap.set(key, []);
|
|
397
|
+
}
|
|
398
|
+
visitorsHandlersMap
|
|
399
|
+
.get(key)
|
|
400
|
+
.push(value);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
const allHandlers = [];
|
|
412
|
+
if (preVisitorsHandlersMap.size > 0) {
|
|
413
|
+
allHandlers.push(preVisitorsHandlersMap);
|
|
414
|
+
}
|
|
415
|
+
if (preValuesHandlers.size > 0) {
|
|
416
|
+
allHandlers.push(preValuesHandlers);
|
|
417
|
+
}
|
|
418
|
+
if (visitorsHandlersMap.size > 0) {
|
|
419
|
+
allHandlers.push(visitorsHandlersMap);
|
|
420
|
+
}
|
|
421
|
+
if (valuesHandlers.size > 0) {
|
|
422
|
+
allHandlers.push(valuesHandlers);
|
|
423
|
+
}
|
|
424
|
+
if (postVisitorsHandlersMap.size > 0) {
|
|
425
|
+
allHandlers.push(postVisitorsHandlersMap);
|
|
426
|
+
}
|
|
427
|
+
if (postValuesHandlers.size > 0) {
|
|
428
|
+
allHandlers.push(postValuesHandlers);
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
allHandlers,
|
|
432
|
+
includeTokens: preValuesHandlers.size > 0 || valuesHandlers.size > 0 || postValuesHandlers.size > 0,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
293
435
|
/**
|
|
294
436
|
* Parse css string
|
|
295
437
|
* @param iter
|
|
@@ -298,7 +440,7 @@ const generateSyncScopedName = memoize((localName, filePath, pattern, hashLength
|
|
|
298
440
|
* @throws Error
|
|
299
441
|
* @private
|
|
300
442
|
*/
|
|
301
|
-
function doParseSync(
|
|
443
|
+
function doParseSync(tokenizer, options = {}) {
|
|
302
444
|
if (options.signal != null) {
|
|
303
445
|
options.signal.addEventListener("abort", reject);
|
|
304
446
|
}
|
|
@@ -355,164 +497,83 @@ function doParseSync(iter, options = {}) {
|
|
|
355
497
|
};
|
|
356
498
|
let tokens = [];
|
|
357
499
|
let context = ast;
|
|
358
|
-
ast[ROOT] = ast;
|
|
359
|
-
ast[LOC] = {
|
|
360
|
-
sta: 0,
|
|
361
|
-
end: 0,
|
|
362
|
-
srcId: options.source.id,
|
|
363
|
-
};
|
|
364
|
-
let valuesHandlers;
|
|
365
|
-
let preValuesHandlers;
|
|
366
|
-
let postValuesHandlers;
|
|
367
|
-
let preVisitorsHandlersMap;
|
|
368
|
-
let visitorsHandlersMap;
|
|
369
|
-
let postVisitorsHandlersMap;
|
|
370
500
|
let item;
|
|
371
501
|
let node;
|
|
372
502
|
// @ts-ignore ignore error
|
|
373
503
|
let parensMatch = 0;
|
|
374
504
|
let curlyBracketMatch = 0;
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
value = visitors[i][1];
|
|
389
|
-
if (Number.isInteger(+key)) {
|
|
390
|
-
visitors.splice(i + 1, 0, ...Object.entries(value));
|
|
391
|
-
continue;
|
|
392
|
-
}
|
|
393
|
-
if (Array.isArray(value)) {
|
|
394
|
-
// @ts-ignore
|
|
395
|
-
visitors.splice(i + 1, 0, ...value.map((item) => [key, item]));
|
|
396
|
-
continue;
|
|
397
|
-
}
|
|
398
|
-
if (key in EnumToken) {
|
|
399
|
-
if (typeof value == "function") {
|
|
400
|
-
if (!valuesHandlers.has(EnumToken[key])) {
|
|
401
|
-
valuesHandlers.set(EnumToken[key], []);
|
|
402
|
-
}
|
|
403
|
-
valuesHandlers.get(EnumToken[key]).push(value);
|
|
404
|
-
}
|
|
405
|
-
else if (typeof value == "object" &&
|
|
406
|
-
"type" in value &&
|
|
407
|
-
"handler" in value &&
|
|
408
|
-
value.type in WalkerEvent) {
|
|
409
|
-
if (value.type == WalkerEvent.Enter) {
|
|
410
|
-
if (!preValuesHandlers.has(EnumToken[key])) {
|
|
411
|
-
preValuesHandlers.set(EnumToken[key], []);
|
|
412
|
-
}
|
|
413
|
-
preValuesHandlers
|
|
414
|
-
.get(EnumToken[key])
|
|
415
|
-
.push(value.handler);
|
|
416
|
-
}
|
|
417
|
-
else if (value.type == WalkerEvent.Leave) {
|
|
418
|
-
if (!postValuesHandlers.has(EnumToken[key])) {
|
|
419
|
-
postValuesHandlers.set(EnumToken[key], []);
|
|
420
|
-
}
|
|
421
|
-
postValuesHandlers
|
|
422
|
-
.get(EnumToken[key])
|
|
423
|
-
.push(value.handler);
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
else {
|
|
427
|
-
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
else if (["Declaration", "Rule", "AtRule", "KeyframesRule", "KeyframesAtRule"].includes(key)) {
|
|
431
|
-
if (typeof value == "function") {
|
|
432
|
-
if (!visitorsHandlersMap.has(key)) {
|
|
433
|
-
visitorsHandlersMap.set(key, []);
|
|
434
|
-
}
|
|
435
|
-
visitorsHandlersMap
|
|
436
|
-
.get(key)
|
|
437
|
-
.push(value);
|
|
438
|
-
}
|
|
439
|
-
else if (typeof value == "object") {
|
|
440
|
-
if ("type" in value && "handler" in value && value.type in WalkerEvent) {
|
|
441
|
-
if (value.type == WalkerEvent.Enter) {
|
|
442
|
-
if (!preVisitorsHandlersMap.has(key)) {
|
|
443
|
-
preVisitorsHandlersMap.set(key, []);
|
|
444
|
-
}
|
|
445
|
-
preVisitorsHandlersMap
|
|
446
|
-
.get(key)
|
|
447
|
-
.push(value.handler);
|
|
448
|
-
}
|
|
449
|
-
else if (value.type == WalkerEvent.Leave) {
|
|
450
|
-
if (!postVisitorsHandlersMap.has(key)) {
|
|
451
|
-
postVisitorsHandlersMap.set(key, []);
|
|
452
|
-
}
|
|
453
|
-
postVisitorsHandlersMap
|
|
454
|
-
.get(key)
|
|
455
|
-
.push(value.handler);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
else {
|
|
459
|
-
if (!visitorsHandlersMap.has(key)) {
|
|
460
|
-
visitorsHandlersMap.set(key, []);
|
|
461
|
-
}
|
|
462
|
-
visitorsHandlersMap
|
|
463
|
-
.get(key)
|
|
464
|
-
.push(value);
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
else {
|
|
468
|
-
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
else {
|
|
472
|
-
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
473
|
-
}
|
|
505
|
+
// let currentItemIndex: number;
|
|
506
|
+
ast[LOCSRCID] = options.source.id;
|
|
507
|
+
ast[LOCSTA] = 0;
|
|
508
|
+
// let tokenizer: Tokenizer;
|
|
509
|
+
while (!tokenizer.done()) {
|
|
510
|
+
tokenizer.next();
|
|
511
|
+
// item = (iter as Array<TokenizeResult>)[currentItemIndex];
|
|
512
|
+
if (tokenizer.unit != null) {
|
|
513
|
+
item = {
|
|
514
|
+
typ: tokenizer.typ,
|
|
515
|
+
val: tokenizer.val,
|
|
516
|
+
unit: tokenizer.unit,
|
|
517
|
+
};
|
|
474
518
|
}
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
519
|
+
else if (tokenizer.nam != null) {
|
|
520
|
+
item = {
|
|
521
|
+
typ: tokenizer.typ,
|
|
522
|
+
nam: tokenizer.nam,
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
else if (tokenizer.val === null) {
|
|
526
|
+
item = {
|
|
527
|
+
typ: tokenizer.typ,
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
else if (tokenizer.kin != null) {
|
|
531
|
+
item = {
|
|
532
|
+
typ: tokenizer.typ,
|
|
533
|
+
val: tokenizer.val,
|
|
534
|
+
kin: tokenizer.kin,
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
else {
|
|
538
|
+
item = {
|
|
539
|
+
typ: tokenizer.typ,
|
|
540
|
+
val: tokenizer.val,
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
item[LOCSRCID] = tokenizer.srcId;
|
|
544
|
+
item[LOCSTA] = tokenizer.sta;
|
|
545
|
+
item[LOCEND] = tokenizer.end;
|
|
546
|
+
stats.bytesIn = tokenizer.bytesIn;
|
|
484
547
|
stats.tokensCount++;
|
|
485
|
-
if (BadTokensTypes.includes(item.
|
|
486
|
-
tokens.push(item
|
|
548
|
+
if (BadTokensTypes.includes(item.typ)) {
|
|
549
|
+
tokens.push(item);
|
|
487
550
|
errors.push({
|
|
488
551
|
action: "drop",
|
|
489
552
|
message: "Bad token",
|
|
490
553
|
syntax: null,
|
|
491
|
-
node: item
|
|
492
|
-
location: options.source.getSourceLocation(item
|
|
554
|
+
node: item,
|
|
555
|
+
location: options.source.getSourceLocation(item[LOCSTA]),
|
|
493
556
|
});
|
|
494
557
|
// bad token
|
|
495
558
|
continue;
|
|
496
559
|
}
|
|
497
|
-
if (item.
|
|
560
|
+
if (item.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(item.typ)) {
|
|
498
561
|
parensMatch++;
|
|
499
562
|
}
|
|
500
|
-
else if (item.
|
|
563
|
+
else if (item.typ === EnumToken.EndParensTokenType && parensMatch > 0) {
|
|
501
564
|
parensMatch--;
|
|
502
565
|
}
|
|
503
|
-
if (item.
|
|
566
|
+
if (item.typ === EnumToken.BlockStartTokenType) {
|
|
504
567
|
curlyBracketMatch++;
|
|
505
568
|
}
|
|
506
|
-
else if (item.
|
|
569
|
+
else if (item.typ === EnumToken.BlockEndTokenType && curlyBracketMatch > 0) {
|
|
507
570
|
curlyBracketMatch--;
|
|
508
571
|
}
|
|
509
|
-
tokens.push(item
|
|
510
|
-
// console.debug([item.token, {parensMatch, curlyBracketMatch}]);
|
|
511
|
-
// if (parensMatch === 0) {
|
|
572
|
+
tokens.push(item);
|
|
512
573
|
if (parensMatch === 0 &&
|
|
513
|
-
(item.
|
|
514
|
-
item.
|
|
515
|
-
item.
|
|
574
|
+
(item.typ === EnumToken.SemiColonTokenType ||
|
|
575
|
+
item.typ === EnumToken.BlockStartTokenType ||
|
|
576
|
+
item.typ === EnumToken.EOFTokenType)) {
|
|
516
577
|
node = parseNode(tokens, context, options, errors, stats, invalidNodes);
|
|
517
578
|
if (node != null) {
|
|
518
579
|
if ("chi" in node) {
|
|
@@ -520,38 +581,67 @@ function doParseSync(iter, options = {}) {
|
|
|
520
581
|
context = node;
|
|
521
582
|
}
|
|
522
583
|
}
|
|
523
|
-
else if (item.
|
|
584
|
+
else if (item.typ == EnumToken.BlockStartTokenType) {
|
|
524
585
|
let inBlock = 1;
|
|
525
|
-
tokens =
|
|
586
|
+
tokens.length = 0;
|
|
587
|
+
tokens.push(item);
|
|
526
588
|
do {
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
589
|
+
tokenizer.next();
|
|
590
|
+
if (tokenizer.unit != null) {
|
|
591
|
+
item = {
|
|
592
|
+
typ: tokenizer.typ,
|
|
593
|
+
val: tokenizer.val,
|
|
594
|
+
unit: tokenizer.unit,
|
|
595
|
+
};
|
|
596
|
+
}
|
|
597
|
+
else if (tokenizer.nam != null) {
|
|
598
|
+
item = {
|
|
599
|
+
typ: tokenizer.typ,
|
|
600
|
+
nam: tokenizer.nam,
|
|
601
|
+
};
|
|
602
|
+
}
|
|
603
|
+
else if (tokenizer.val === null) {
|
|
604
|
+
item = {
|
|
605
|
+
typ: tokenizer.typ,
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
else if (tokenizer.kin != null) {
|
|
609
|
+
item = {
|
|
610
|
+
typ: tokenizer.typ,
|
|
611
|
+
val: tokenizer.val,
|
|
612
|
+
kin: tokenizer.kin,
|
|
613
|
+
};
|
|
531
614
|
}
|
|
532
|
-
|
|
533
|
-
|
|
615
|
+
else {
|
|
616
|
+
item = {
|
|
617
|
+
typ: tokenizer.typ,
|
|
618
|
+
val: tokenizer.val,
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
item[LOCSRCID] = tokenizer.srcId;
|
|
622
|
+
item[LOCSTA] = tokenizer.sta;
|
|
623
|
+
item[LOCEND] = tokenizer.end;
|
|
624
|
+
tokens.push(item);
|
|
625
|
+
if (item.typ === EnumToken.BlockStartTokenType) {
|
|
534
626
|
inBlock++;
|
|
535
627
|
}
|
|
536
|
-
else if (item.
|
|
628
|
+
else if (item.typ === EnumToken.BlockEndTokenType) {
|
|
537
629
|
inBlock--;
|
|
538
630
|
}
|
|
539
|
-
} while (inBlock != 0);
|
|
631
|
+
} while (inBlock != 0 && !tokenizer.done());
|
|
540
632
|
if (tokens.length > 0) {
|
|
541
633
|
errors.push({
|
|
542
634
|
action: "drop",
|
|
543
635
|
message: "invalid block",
|
|
544
|
-
location: options.source.getSourceLocation(tokens[0][
|
|
636
|
+
location: options.source.getSourceLocation(tokens[0][LOCSTA]),
|
|
545
637
|
});
|
|
546
638
|
}
|
|
547
639
|
}
|
|
548
|
-
tokens =
|
|
640
|
+
tokens.length = 0;
|
|
549
641
|
}
|
|
550
|
-
else if ((parensMatch === 0 || curlyBracketMatch === 0) && item.
|
|
642
|
+
else if ((parensMatch === 0 || curlyBracketMatch === 0) && item.typ === EnumToken.BlockEndTokenType) {
|
|
551
643
|
parseNode(tokens, context, options, errors, stats, invalidNodes);
|
|
552
|
-
|
|
553
|
-
context[LOC].end = item.token[LOC].end;
|
|
554
|
-
}
|
|
644
|
+
context[LOCEND] = item[LOCEND];
|
|
555
645
|
const previousNode = stack.pop();
|
|
556
646
|
context = (stack[stack.length - 1] ?? ast);
|
|
557
647
|
if (options.removeEmpty &&
|
|
@@ -560,7 +650,7 @@ function doParseSync(iter, options = {}) {
|
|
|
560
650
|
context.chi[context.chi.length - 1] == previousNode) {
|
|
561
651
|
context.chi.pop();
|
|
562
652
|
}
|
|
563
|
-
tokens =
|
|
653
|
+
tokens.length = 0;
|
|
564
654
|
parensMatch = 0;
|
|
565
655
|
curlyBracketMatch = 0;
|
|
566
656
|
}
|
|
@@ -580,198 +670,168 @@ function doParseSync(iter, options = {}) {
|
|
|
580
670
|
ast = expand(ast);
|
|
581
671
|
}
|
|
582
672
|
let replacement;
|
|
583
|
-
let callable;
|
|
584
673
|
if (options.visitor != null) {
|
|
674
|
+
const handlers = [];
|
|
675
|
+
const visitors = parseVisitors(options.visitor, errors);
|
|
676
|
+
const subNodes = [];
|
|
585
677
|
let parens;
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
? "Declaration"
|
|
607
|
-
: result.node.typ == EnumToken.AtRuleNodeType
|
|
608
|
-
? "AtRule"
|
|
609
|
-
: "KeyframesAtRule";
|
|
610
|
-
if (preVisitorsHandlersMap.has(key)) {
|
|
611
|
-
handlers.push(
|
|
612
|
-
// @ts-expect-error
|
|
613
|
-
...preVisitorsHandlersMap.get(key));
|
|
614
|
-
}
|
|
615
|
-
if (visitorsHandlersMap.has(key)) {
|
|
616
|
-
// @ts-ignore
|
|
617
|
-
handlers.push(...visitorsHandlersMap.get(key));
|
|
618
|
-
}
|
|
619
|
-
if (postVisitorsHandlersMap.has(key)) {
|
|
620
|
-
// @ts-ignore
|
|
621
|
-
handlers.push(...postVisitorsHandlersMap.get(key));
|
|
622
|
-
}
|
|
623
|
-
let node = result.node;
|
|
624
|
-
for (const handler of handlers) {
|
|
625
|
-
callable =
|
|
626
|
-
typeof handler == "function"
|
|
627
|
-
? handler
|
|
628
|
-
: handler[camelize(node.typ === EnumToken.DeclarationNodeType ||
|
|
629
|
-
node.typ === EnumToken.AtRuleNodeType
|
|
630
|
-
? node.nam
|
|
631
|
-
: node.val)];
|
|
632
|
-
if (callable == null) {
|
|
633
|
-
continue;
|
|
634
|
-
}
|
|
635
|
-
// @ts-expect-error
|
|
636
|
-
replacement = callable(node, result[PARENT], ast, function* () {
|
|
637
|
-
if (parens == null) {
|
|
638
|
-
// @ts-expect-error
|
|
639
|
-
parens = [...result.parents()];
|
|
640
|
-
}
|
|
641
|
-
yield* parens[Symbol.iterator]();
|
|
642
|
-
});
|
|
643
|
-
if (replacement == null) {
|
|
644
|
-
continue;
|
|
678
|
+
let genericKey;
|
|
679
|
+
let nodes = new Array(stats.tokensCount);
|
|
680
|
+
let i;
|
|
681
|
+
let k;
|
|
682
|
+
let j;
|
|
683
|
+
let freeBlock = 1;
|
|
684
|
+
nodes[0] = ast;
|
|
685
|
+
for (i = 0; i < nodes.length; i++) {
|
|
686
|
+
if (nodes[i] == null) {
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
subNodes.length = 0;
|
|
690
|
+
if (visitors.includeTokens) {
|
|
691
|
+
switch (nodes[i].typ) {
|
|
692
|
+
case EnumToken.RuleNodeType:
|
|
693
|
+
case EnumToken.AtRuleNodeType:
|
|
694
|
+
case EnumToken.KeyframesRuleNodeType:
|
|
695
|
+
case EnumToken.KeyframesAtRuleNodeType:
|
|
696
|
+
for (const token of nodes[i][TOKENS]) {
|
|
697
|
+
subNodes.push(token);
|
|
645
698
|
}
|
|
646
|
-
|
|
647
|
-
|
|
699
|
+
break;
|
|
700
|
+
case EnumToken.DeclarationNodeType:
|
|
701
|
+
for (const token of nodes[i].val) {
|
|
702
|
+
subNodes.push(token);
|
|
648
703
|
}
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
// @ts-ignore
|
|
708
|
+
if (nodes[i].chi != null) {
|
|
709
|
+
// @ts-ignore
|
|
710
|
+
for (const child of nodes[i].chi) {
|
|
711
|
+
subNodes.push(child);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
if (subNodes.length > 0) {
|
|
715
|
+
if (freeBlock <= i) {
|
|
716
|
+
freeBlock = i + 1;
|
|
717
|
+
}
|
|
718
|
+
for (k = 0; k < subNodes.length; k++) {
|
|
719
|
+
j = k + freeBlock;
|
|
720
|
+
nodes[j] = subNodes[k];
|
|
721
|
+
nodes[j][PARENT] = nodes[i];
|
|
722
|
+
}
|
|
723
|
+
freeBlock += subNodes.length;
|
|
724
|
+
}
|
|
725
|
+
parens = null;
|
|
726
|
+
handlers.length = 0;
|
|
727
|
+
genericKey =
|
|
728
|
+
nodes[i].typ == EnumToken.DeclarationNodeType
|
|
729
|
+
? "Declaration"
|
|
730
|
+
: nodes[i].typ == EnumToken.AtRuleNodeType
|
|
731
|
+
? "AtRule"
|
|
732
|
+
: nodes[i].typ == EnumToken.KeyframesAtRuleNodeType
|
|
733
|
+
? "KeyframesAtRule"
|
|
734
|
+
: nodes[i].typ === EnumToken.KeyframesRuleNodeType
|
|
735
|
+
? "KeyframesRule"
|
|
736
|
+
: nodes[i].typ == EnumToken.RuleNodeType
|
|
737
|
+
? "Rule"
|
|
738
|
+
: nodes[i].typ == EnumToken.KeyframesRuleNodeType
|
|
739
|
+
? "KeyframesRule"
|
|
740
|
+
: null;
|
|
741
|
+
let keyName = nodes[i].typ == EnumToken.DeclarationNodeType || nodes[i].typ == EnumToken.AtRuleNodeType
|
|
742
|
+
? camelize(nodes[i].nam)
|
|
743
|
+
: nodes[i].typ == EnumToken.KeyframesAtRuleNodeType
|
|
744
|
+
? camelize(nodes[i].val)
|
|
745
|
+
: null;
|
|
746
|
+
for (const map of visitors.allHandlers) {
|
|
747
|
+
// @ts-ignore
|
|
748
|
+
if (genericKey != null && map.has(genericKey)) {
|
|
749
|
+
// @ts-ignore
|
|
750
|
+
for (const handler of map.get(genericKey)) {
|
|
751
|
+
if (typeof handler == "function") {
|
|
752
|
+
handlers.push(handler);
|
|
753
|
+
}
|
|
754
|
+
// else if (Array.isArray(handler)) {
|
|
755
|
+
// for (const h of handler) {
|
|
756
|
+
// if (typeof h == "function") {
|
|
757
|
+
// handlers.push(h);
|
|
758
|
+
// }
|
|
759
|
+
// // @ts-ignore
|
|
760
|
+
// else if (h[keyName] != null) {
|
|
761
|
+
// // @ts-ignore
|
|
762
|
+
// handlers.push(h[keyName]);
|
|
763
|
+
// }
|
|
764
|
+
// }
|
|
765
|
+
// } else if (typeof handler.handler! == "function") {
|
|
766
|
+
// handlers.push(handler.handler);
|
|
767
|
+
// }
|
|
649
768
|
// @ts-ignore
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
769
|
+
else if (typeof handler[keyName] == "function") {
|
|
770
|
+
// @ts-ignore
|
|
771
|
+
handlers.push(handler[keyName]);
|
|
653
772
|
}
|
|
654
773
|
}
|
|
655
|
-
if (node != result.node) {
|
|
656
|
-
replaceNodeOrValue(result.parent, result.node, node);
|
|
657
|
-
}
|
|
658
774
|
}
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
}
|
|
672
|
-
if (visitorsHandlersMap.has(key)) {
|
|
673
|
-
handlers.push(...visitorsHandlersMap.get(key));
|
|
674
|
-
}
|
|
675
|
-
if (postVisitorsHandlersMap.has(key)) {
|
|
676
|
-
handlers.push(...postVisitorsHandlersMap.get(key));
|
|
677
|
-
}
|
|
678
|
-
let node = result.node;
|
|
679
|
-
for (const callable of handlers) {
|
|
680
|
-
replacement = callable(node, result.parent, result.root,
|
|
681
|
-
// @ts-expect-error
|
|
682
|
-
function* () {
|
|
683
|
-
if (parens == null) {
|
|
684
|
-
// @ts-expect-error
|
|
685
|
-
parens = [...result.parents()];
|
|
775
|
+
// @ts-ignore
|
|
776
|
+
if (map.has(nodes[i].typ)) {
|
|
777
|
+
// @ts-ignore
|
|
778
|
+
for (const handler of map.get(nodes[i].typ)) {
|
|
779
|
+
if (typeof handler == "function") {
|
|
780
|
+
handlers.push(handler);
|
|
781
|
+
}
|
|
782
|
+
else if (Array.isArray(handler)) {
|
|
783
|
+
for (const h of handler) {
|
|
784
|
+
if (typeof h == "function") {
|
|
785
|
+
handlers.push(h);
|
|
786
|
+
}
|
|
686
787
|
}
|
|
687
|
-
yield* parens[Symbol.iterator]();
|
|
688
|
-
});
|
|
689
|
-
if (replacement == null) {
|
|
690
|
-
continue;
|
|
691
788
|
}
|
|
692
|
-
if (
|
|
693
|
-
|
|
789
|
+
else if (typeof handler.handler == "function") {
|
|
790
|
+
handlers.push(handler.handler);
|
|
694
791
|
}
|
|
695
792
|
// @ts-ignore
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
break;
|
|
793
|
+
else if (typeof handler[keyName] == "function") {
|
|
794
|
+
// @ts-ignore
|
|
795
|
+
handlers.push(handler[keyName]);
|
|
700
796
|
}
|
|
701
797
|
}
|
|
702
|
-
// @ts-ignore
|
|
703
|
-
if (node != result.node) {
|
|
704
|
-
// @ts-ignore
|
|
705
|
-
replaceNodeOrValue(result.parent, result.node, node);
|
|
706
|
-
}
|
|
707
798
|
}
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
});
|
|
723
|
-
if (replacement == null) {
|
|
724
|
-
continue;
|
|
725
|
-
}
|
|
726
|
-
if (replacement != null && replacement != node) {
|
|
727
|
-
node = replacement;
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
if (node != result.node) {
|
|
732
|
-
// @ts-ignore
|
|
733
|
-
replaceNodeOrValue(result[PARENT], value, node);
|
|
734
|
-
}
|
|
735
|
-
const tokens = Array.isArray(result.node[TOKENS]) ? result.node[TOKENS] : [];
|
|
736
|
-
if (Array.isArray(result.node.val)) {
|
|
737
|
-
tokens.push(...result.node.val);
|
|
738
|
-
}
|
|
739
|
-
if (tokens.length == 0) {
|
|
740
|
-
continue;
|
|
741
|
-
}
|
|
742
|
-
for (const { value, parent, root, parents } of walkValues(tokens, result.node)) {
|
|
743
|
-
node = value;
|
|
744
|
-
if (valuesHandlers.has(node.typ)) {
|
|
745
|
-
let parens = null;
|
|
746
|
-
for (const valueHandler of valuesHandlers.get(node.typ)) {
|
|
747
|
-
callable = valueHandler;
|
|
748
|
-
// @ts-expect-error
|
|
749
|
-
let result = callable(node, parent, root, function* () {
|
|
750
|
-
if (parens == null) {
|
|
751
|
-
// @ts-expect-error
|
|
752
|
-
parens = [...parents()];
|
|
753
|
-
}
|
|
754
|
-
yield* parens[Symbol.iterator]();
|
|
755
|
-
});
|
|
756
|
-
if (result == null) {
|
|
757
|
-
continue;
|
|
758
|
-
}
|
|
759
|
-
if (result != null && result != node) {
|
|
760
|
-
node = result;
|
|
761
|
-
}
|
|
762
|
-
if (Array.isArray(node)) {
|
|
763
|
-
break;
|
|
764
|
-
}
|
|
765
|
-
}
|
|
766
|
-
}
|
|
767
|
-
if (node != value) {
|
|
768
|
-
// @ts-ignore
|
|
769
|
-
replaceNodeOrValue(parent, value, node);
|
|
799
|
+
}
|
|
800
|
+
if (handlers.length == 0) {
|
|
801
|
+
continue;
|
|
802
|
+
}
|
|
803
|
+
let node = nodes[i];
|
|
804
|
+
for (const callable of handlers) {
|
|
805
|
+
replacement = callable(node, nodes[i][PARENT], ast,
|
|
806
|
+
// @ts-expect-error
|
|
807
|
+
function* () {
|
|
808
|
+
if (parens == null) {
|
|
809
|
+
let node = nodes[i][PARENT];
|
|
810
|
+
while (node != null) {
|
|
811
|
+
yield node;
|
|
812
|
+
node = node[PARENT];
|
|
770
813
|
}
|
|
771
814
|
}
|
|
815
|
+
});
|
|
816
|
+
if (replacement == null) {
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
if (replacement == node) {
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
// @ts-ignore
|
|
823
|
+
node = replacement;
|
|
824
|
+
//
|
|
825
|
+
if (Array.isArray(node)) {
|
|
826
|
+
break;
|
|
772
827
|
}
|
|
773
828
|
}
|
|
829
|
+
if (node != nodes[i]) {
|
|
830
|
+
// @ts-ignore
|
|
831
|
+
replaceNodeOrValue(nodes[i][PARENT], nodes[i], node);
|
|
832
|
+
}
|
|
774
833
|
}
|
|
834
|
+
nodes = null;
|
|
775
835
|
}
|
|
776
836
|
if (invalidNodes.length > 0) {
|
|
777
837
|
let count = invalidNodes.length;
|
|
@@ -794,19 +854,6 @@ function doParseSync(iter, options = {}) {
|
|
|
794
854
|
}
|
|
795
855
|
}
|
|
796
856
|
}
|
|
797
|
-
while (stack.length > 0 && context != ast) {
|
|
798
|
-
const previousNode = stack.pop();
|
|
799
|
-
context = (stack[stack.length - 1] ?? ast);
|
|
800
|
-
// remove empty nodes
|
|
801
|
-
if (options.removeEmpty &&
|
|
802
|
-
previousNode != null &&
|
|
803
|
-
previousNode.chi.length == 0 &&
|
|
804
|
-
context.chi[context.chi.length - 1] == previousNode) {
|
|
805
|
-
context.chi.pop();
|
|
806
|
-
continue;
|
|
807
|
-
}
|
|
808
|
-
break;
|
|
809
|
-
}
|
|
810
857
|
if (options.minify) {
|
|
811
858
|
if (ast.chi.length > 0) {
|
|
812
859
|
let passes = options.pass ?? 1;
|
|
@@ -824,7 +871,7 @@ function doParseSync(iter, options = {}) {
|
|
|
824
871
|
...stats,
|
|
825
872
|
parse: `${(endParseTime - startTime).toFixed(2)}ms`,
|
|
826
873
|
minify: `${(endTime - endParseTime).toFixed(2)}ms`,
|
|
827
|
-
tokenize: `${(options?.parseInfo?.time ?? 0).toFixed(2)}ms`,
|
|
874
|
+
// tokenize: `${(options?.parseInfo?.time ?? 0).toFixed(2)}ms`,
|
|
828
875
|
total: `${(endTime - startTime).toFixed(2)}ms`,
|
|
829
876
|
},
|
|
830
877
|
};
|
|
@@ -835,7 +882,7 @@ function doParseSync(iter, options = {}) {
|
|
|
835
882
|
scoped: ModuleScopeEnumOptions.Local,
|
|
836
883
|
naming: ModuleCaseTransformEnum.IgnoreCase,
|
|
837
884
|
pattern: "",
|
|
838
|
-
generateScopedName,
|
|
885
|
+
generateScopedName: generateSyncScopedName,
|
|
839
886
|
...(typeof options.module != "object" ? {} : options.module),
|
|
840
887
|
};
|
|
841
888
|
const parseModuleTime = performance.now();
|
|
@@ -853,7 +900,7 @@ function doParseSync(iter, options = {}) {
|
|
|
853
900
|
: (moduleSettings.filePath ?? options.src);
|
|
854
901
|
filePath =
|
|
855
902
|
filePath === ""
|
|
856
|
-
? options.src
|
|
903
|
+
? options.resolve(options.src, options.cwd).relative
|
|
857
904
|
: options.resolve(filePath, options.dirname(options.src), options.cwd).relative;
|
|
858
905
|
if (typeof options.module == "number") {
|
|
859
906
|
if (options.module & ModuleCaseTransformEnum.CamelCase) {
|
|
@@ -898,8 +945,9 @@ function doParseSync(iter, options = {}) {
|
|
|
898
945
|
for (const { node, parent } of walk(ast)) {
|
|
899
946
|
if (node.typ == EnumToken.CssVariableImportTokenType) {
|
|
900
947
|
throw new Error("css variable import not supported by parseSync() or transformSync(). use parse() or transform() instead.\nat " +
|
|
901
|
-
options.source.getSourceLocation(node[
|
|
948
|
+
options.source.getSourceLocation(node[LOCSTA]).join(":"));
|
|
902
949
|
}
|
|
950
|
+
// @ts-ignore
|
|
903
951
|
if (node.typ == EnumToken.CssVariableDeclarationMapTokenType) {
|
|
904
952
|
const from = node.from.find((t) => t.typ == EnumToken.IdenTokenType || isIdentColor(t));
|
|
905
953
|
if (!(from.val in cssVariablesMap)) {
|
|
@@ -941,10 +989,9 @@ function doParseSync(iter, options = {}) {
|
|
|
941
989
|
if (node.typ == EnumToken.DeclarationNodeType) {
|
|
942
990
|
if (node.nam.startsWith("--")) {
|
|
943
991
|
if (!(node.nam in namesMapping)) {
|
|
944
|
-
let
|
|
992
|
+
let value = moduleSettings.scoped & ModuleScopeEnumOptions.Global
|
|
945
993
|
? node.nam
|
|
946
994
|
: moduleSettings.generateScopedName(node.nam, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
|
|
947
|
-
let value = result;
|
|
948
995
|
mapping[node.nam] =
|
|
949
996
|
"--" +
|
|
950
997
|
(moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
|
|
@@ -986,10 +1033,9 @@ function doParseSync(iter, options = {}) {
|
|
|
986
1033
|
continue;
|
|
987
1034
|
}
|
|
988
1035
|
if (!(rule.val in mapping)) {
|
|
989
|
-
let
|
|
1036
|
+
let value = moduleSettings.scoped & ModuleScopeEnumOptions.Global
|
|
990
1037
|
? rule.val
|
|
991
1038
|
: moduleSettings.generateScopedName(rule.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
|
|
992
|
-
let value = result;
|
|
993
1039
|
mapping[rule.val] =
|
|
994
1040
|
(rule.typ == EnumToken.DashedIdenTokenType ? "--" : "") +
|
|
995
1041
|
(moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
|
|
@@ -1018,7 +1064,7 @@ function doParseSync(iter, options = {}) {
|
|
|
1018
1064
|
}
|
|
1019
1065
|
// composes: a b c from 'file.css';
|
|
1020
1066
|
else if (token.r.typ == EnumToken.String) {
|
|
1021
|
-
throw new Error(`composes from file is not supported using parseSync() or transformSync(). Use parse() or transform() instead.\nat ${options.source.getSourceLocation(node[
|
|
1067
|
+
throw new Error(`composes from file is not supported using parseSync() or transformSync(). Use parse() or transform() instead.\nat ${options.source.getSourceLocation(node[LOCSTA]).join(":")}`);
|
|
1022
1068
|
}
|
|
1023
1069
|
// composes: a b c from global;
|
|
1024
1070
|
else if (token.r.typ == EnumToken.IdenTokenType) {
|
|
@@ -1160,10 +1206,10 @@ function doParseSync(iter, options = {}) {
|
|
|
1160
1206
|
"unset",
|
|
1161
1207
|
].includes(value.val)) {
|
|
1162
1208
|
if (!(value.val in mapping)) {
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1209
|
+
mapping[value.val] =
|
|
1210
|
+
moduleSettings.scoped & ModuleScopeEnumOptions.Global
|
|
1211
|
+
? value.val
|
|
1212
|
+
: moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
|
|
1167
1213
|
revMapping[mapping[value.val]] = value.val;
|
|
1168
1214
|
}
|
|
1169
1215
|
value.val = mapping[value.val];
|
|
@@ -1176,6 +1222,7 @@ function doParseSync(iter, options = {}) {
|
|
|
1176
1222
|
}
|
|
1177
1223
|
else if ((value.typ == EnumToken.IdenTokenType || isIdentColor(value)) &&
|
|
1178
1224
|
value.val in importedCssVariables) {
|
|
1225
|
+
// @ts-ignore
|
|
1179
1226
|
replaceNodeOrValue(parent, value, importedCssVariables[value.val].val);
|
|
1180
1227
|
}
|
|
1181
1228
|
}
|
|
@@ -1235,10 +1282,9 @@ function doParseSync(iter, options = {}) {
|
|
|
1235
1282
|
if (value.typ == EnumToken.ClassSelectorTokenType) {
|
|
1236
1283
|
const val = value.val.slice(1);
|
|
1237
1284
|
if (!(val in mapping)) {
|
|
1238
|
-
|
|
1285
|
+
let value = moduleSettings.scoped & ModuleScopeEnumOptions.Global
|
|
1239
1286
|
? val
|
|
1240
1287
|
: moduleSettings.generateScopedName(val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
|
|
1241
|
-
let value = result;
|
|
1242
1288
|
mapping[val] =
|
|
1243
1289
|
moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
|
|
1244
1290
|
moduleSettings.naming & ModuleCaseTransformEnum.CamelCaseOnly
|
|
@@ -1252,7 +1298,7 @@ function doParseSync(iter, options = {}) {
|
|
|
1252
1298
|
}
|
|
1253
1299
|
if (moduleSettings.scoped & ModuleScopeEnumOptions.Pure) {
|
|
1254
1300
|
if (!hasIdOrClass) {
|
|
1255
|
-
throw new Error(`pure module: No id or class found in selector '${node.sel}' at '${node[
|
|
1301
|
+
throw new Error(`pure module: No id or class found in selector '${node.sel}' at '${options.source.getOffsets(node[LOCSTA]).join(":")}'`);
|
|
1256
1302
|
}
|
|
1257
1303
|
}
|
|
1258
1304
|
node.sel = "";
|
|
@@ -1271,10 +1317,9 @@ function doParseSync(iter, options = {}) {
|
|
|
1271
1317
|
if ((prefix == "--" && value.typ == EnumToken.DashedIdenTokenType) ||
|
|
1272
1318
|
(prefix == "" && value.typ == EnumToken.IdenTokenType)) {
|
|
1273
1319
|
if (!(value.val in mapping)) {
|
|
1274
|
-
|
|
1320
|
+
let val = moduleSettings.scoped & ModuleScopeEnumOptions.Global
|
|
1275
1321
|
? value.val
|
|
1276
1322
|
: moduleSettings.generateScopedName(value.val, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
|
|
1277
|
-
let val = result;
|
|
1278
1323
|
mapping[value.val] =
|
|
1279
1324
|
prefix +
|
|
1280
1325
|
(moduleSettings.naming & ModuleCaseTransformEnum.DashCaseOnly ||
|
|
@@ -1292,12 +1337,13 @@ function doParseSync(iter, options = {}) {
|
|
|
1292
1337
|
}
|
|
1293
1338
|
if (moduleSettings.naming != ModuleCaseTransformEnum.IgnoreCase) {
|
|
1294
1339
|
revMapping = {};
|
|
1295
|
-
mapping =
|
|
1296
|
-
|
|
1297
|
-
|
|
1340
|
+
mapping = {};
|
|
1341
|
+
let keyName;
|
|
1342
|
+
for (const [key, value] of Object.entries(mapping)) {
|
|
1343
|
+
keyName = getKeyName(key, moduleSettings.naming);
|
|
1344
|
+
mapping[keyName] = value;
|
|
1298
1345
|
revMapping[value] = keyName;
|
|
1299
|
-
|
|
1300
|
-
}, {});
|
|
1346
|
+
}
|
|
1301
1347
|
}
|
|
1302
1348
|
result.mapping = mapping;
|
|
1303
1349
|
result.revMapping = revMapping;
|
|
@@ -1382,168 +1428,87 @@ async function doParse(iter, options = {}) {
|
|
|
1382
1428
|
};
|
|
1383
1429
|
let tokens = [];
|
|
1384
1430
|
let context = ast;
|
|
1385
|
-
ast[ROOT] = ast;
|
|
1386
|
-
ast[LOC] = {
|
|
1387
|
-
sta: 0,
|
|
1388
|
-
end: 0,
|
|
1389
|
-
srcId: options.source.id,
|
|
1390
|
-
};
|
|
1391
|
-
let valuesHandlers;
|
|
1392
|
-
let preValuesHandlers;
|
|
1393
|
-
let postValuesHandlers;
|
|
1394
|
-
let preVisitorsHandlersMap;
|
|
1395
|
-
let visitorsHandlersMap;
|
|
1396
|
-
let postVisitorsHandlersMap;
|
|
1397
1431
|
const imports = [];
|
|
1398
1432
|
let item;
|
|
1399
1433
|
let node;
|
|
1400
|
-
// @ts-ignore ignore error
|
|
1401
|
-
let isAsync = typeof iter[Symbol.asyncIterator] === "function";
|
|
1402
1434
|
let parensMatch = 0;
|
|
1403
1435
|
let curlyBracketMatch = 0;
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
}
|
|
1422
|
-
if (Array.isArray(value)) {
|
|
1423
|
-
// @ts-ignore
|
|
1424
|
-
visitors.splice(i + 1, 0, ...value.map((item) => [key, item]));
|
|
1425
|
-
continue;
|
|
1426
|
-
}
|
|
1427
|
-
if (key in EnumToken) {
|
|
1428
|
-
if (typeof value == "function") {
|
|
1429
|
-
if (!valuesHandlers.has(EnumToken[key])) {
|
|
1430
|
-
valuesHandlers.set(EnumToken[key], []);
|
|
1431
|
-
}
|
|
1432
|
-
valuesHandlers.get(EnumToken[key]).push(value);
|
|
1433
|
-
}
|
|
1434
|
-
else if (typeof value == "object" &&
|
|
1435
|
-
"type" in value &&
|
|
1436
|
-
"handler" in value &&
|
|
1437
|
-
value.type in WalkerEvent) {
|
|
1438
|
-
if (value.type == WalkerEvent.Enter) {
|
|
1439
|
-
if (!preValuesHandlers.has(EnumToken[key])) {
|
|
1440
|
-
preValuesHandlers.set(EnumToken[key], []);
|
|
1441
|
-
}
|
|
1442
|
-
preValuesHandlers
|
|
1443
|
-
.get(EnumToken[key])
|
|
1444
|
-
.push(value.handler);
|
|
1445
|
-
}
|
|
1446
|
-
else if (value.type == WalkerEvent.Leave) {
|
|
1447
|
-
if (!postValuesHandlers.has(EnumToken[key])) {
|
|
1448
|
-
postValuesHandlers.set(EnumToken[key], []);
|
|
1449
|
-
}
|
|
1450
|
-
postValuesHandlers
|
|
1451
|
-
.get(EnumToken[key])
|
|
1452
|
-
.push(value.handler);
|
|
1453
|
-
}
|
|
1454
|
-
}
|
|
1455
|
-
else {
|
|
1456
|
-
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
1457
|
-
}
|
|
1458
|
-
}
|
|
1459
|
-
else if (["Declaration", "Rule", "AtRule", "KeyframesRule", "KeyframesAtRule"].includes(key)) {
|
|
1460
|
-
if (typeof value == "function") {
|
|
1461
|
-
if (!visitorsHandlersMap.has(key)) {
|
|
1462
|
-
visitorsHandlersMap.set(key, []);
|
|
1463
|
-
}
|
|
1464
|
-
visitorsHandlersMap
|
|
1465
|
-
.get(key)
|
|
1466
|
-
.push(value);
|
|
1467
|
-
}
|
|
1468
|
-
else if (typeof value == "object") {
|
|
1469
|
-
if ("type" in value && "handler" in value && value.type in WalkerEvent) {
|
|
1470
|
-
if (value.type == WalkerEvent.Enter) {
|
|
1471
|
-
if (!preVisitorsHandlersMap.has(key)) {
|
|
1472
|
-
preVisitorsHandlersMap.set(key, []);
|
|
1473
|
-
}
|
|
1474
|
-
preVisitorsHandlersMap
|
|
1475
|
-
.get(key)
|
|
1476
|
-
.push(value.handler);
|
|
1477
|
-
}
|
|
1478
|
-
else if (value.type == WalkerEvent.Leave) {
|
|
1479
|
-
if (!postVisitorsHandlersMap.has(key)) {
|
|
1480
|
-
postVisitorsHandlersMap.set(key, []);
|
|
1481
|
-
}
|
|
1482
|
-
postVisitorsHandlersMap
|
|
1483
|
-
.get(key)
|
|
1484
|
-
.push(value.handler);
|
|
1485
|
-
}
|
|
1486
|
-
}
|
|
1487
|
-
else {
|
|
1488
|
-
if (!visitorsHandlersMap.has(key)) {
|
|
1489
|
-
visitorsHandlersMap.set(key, []);
|
|
1490
|
-
}
|
|
1491
|
-
visitorsHandlersMap
|
|
1492
|
-
.get(key)
|
|
1493
|
-
.push(value);
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
|
-
else {
|
|
1497
|
-
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
1498
|
-
}
|
|
1499
|
-
}
|
|
1500
|
-
else {
|
|
1501
|
-
errors.push({ action: "ignore", message: `doParse: visitor.${key} is not a valid key name` });
|
|
1502
|
-
}
|
|
1436
|
+
let tokenizer = iter instanceof Promise ? await iter : iter;
|
|
1437
|
+
// ast[ROOT] = ast;
|
|
1438
|
+
ast[LOCSRCID] = options.source.id;
|
|
1439
|
+
ast[LOCSTA] = 0;
|
|
1440
|
+
ast[LOCEND] = 0;
|
|
1441
|
+
// if (Array.isArray(iter)) {
|
|
1442
|
+
// // @ts-expect-error
|
|
1443
|
+
// iter = iter[Symbol.iterator]() as Iterator<TokenizeResult>;
|
|
1444
|
+
// }
|
|
1445
|
+
while (!tokenizer.done()) {
|
|
1446
|
+
tokenizer.next();
|
|
1447
|
+
if (tokenizer.unit != null) {
|
|
1448
|
+
item = {
|
|
1449
|
+
typ: tokenizer.typ,
|
|
1450
|
+
val: tokenizer.val,
|
|
1451
|
+
unit: tokenizer.unit,
|
|
1452
|
+
};
|
|
1503
1453
|
}
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1454
|
+
else if (tokenizer.nam != null) {
|
|
1455
|
+
item = {
|
|
1456
|
+
typ: tokenizer.typ,
|
|
1457
|
+
nam: tokenizer.nam,
|
|
1458
|
+
};
|
|
1459
|
+
}
|
|
1460
|
+
else if (tokenizer.val === null) {
|
|
1461
|
+
item = {
|
|
1462
|
+
typ: tokenizer.typ,
|
|
1463
|
+
};
|
|
1464
|
+
}
|
|
1465
|
+
else if (tokenizer.kin != null) {
|
|
1466
|
+
item = {
|
|
1467
|
+
typ: tokenizer.typ,
|
|
1468
|
+
val: tokenizer.val,
|
|
1469
|
+
kin: tokenizer.kin,
|
|
1470
|
+
};
|
|
1471
|
+
}
|
|
1472
|
+
else {
|
|
1473
|
+
item = {
|
|
1474
|
+
typ: tokenizer.typ,
|
|
1475
|
+
val: tokenizer.val,
|
|
1476
|
+
};
|
|
1477
|
+
}
|
|
1478
|
+
item[LOCSRCID] = tokenizer.srcId;
|
|
1479
|
+
item[LOCSTA] = tokenizer.sta;
|
|
1480
|
+
item[LOCEND] = tokenizer.end;
|
|
1481
|
+
stats.bytesIn = tokenizer.bytesIn;
|
|
1515
1482
|
stats.tokensCount++;
|
|
1516
|
-
if (BadTokensTypes.includes(item.
|
|
1517
|
-
tokens.push(item
|
|
1483
|
+
if (BadTokensTypes.includes(item.typ)) {
|
|
1484
|
+
tokens.push(item);
|
|
1518
1485
|
errors.push({
|
|
1519
1486
|
action: "drop",
|
|
1520
1487
|
message: "Bad token",
|
|
1521
1488
|
syntax: null,
|
|
1522
|
-
node: item
|
|
1523
|
-
location: options.source.getSourceLocation(item
|
|
1489
|
+
node: item,
|
|
1490
|
+
location: options.source.getSourceLocation(item[LOCSTA]),
|
|
1524
1491
|
});
|
|
1525
1492
|
// bad token
|
|
1526
1493
|
continue;
|
|
1527
1494
|
}
|
|
1528
|
-
if (item.
|
|
1495
|
+
if (item.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(item.typ)) {
|
|
1529
1496
|
parensMatch++;
|
|
1530
1497
|
}
|
|
1531
|
-
else if (item.
|
|
1498
|
+
else if (item.typ === EnumToken.EndParensTokenType && parensMatch > 0) {
|
|
1532
1499
|
parensMatch--;
|
|
1533
1500
|
}
|
|
1534
|
-
if (item.
|
|
1501
|
+
if (item.typ === EnumToken.BlockStartTokenType) {
|
|
1535
1502
|
curlyBracketMatch++;
|
|
1536
1503
|
}
|
|
1537
|
-
else if (item.
|
|
1504
|
+
else if (item.typ === EnumToken.BlockEndTokenType && curlyBracketMatch > 0) {
|
|
1538
1505
|
curlyBracketMatch--;
|
|
1539
1506
|
}
|
|
1540
|
-
tokens.push(item
|
|
1541
|
-
// console.debug([item.token, {parensMatch, curlyBracketMatch}]);
|
|
1542
|
-
// if (parensMatch === 0) {
|
|
1507
|
+
tokens.push(item);
|
|
1543
1508
|
if (parensMatch === 0 &&
|
|
1544
|
-
(item.
|
|
1545
|
-
item.
|
|
1546
|
-
item.
|
|
1509
|
+
(item.typ === EnumToken.SemiColonTokenType ||
|
|
1510
|
+
item.typ === EnumToken.BlockStartTokenType ||
|
|
1511
|
+
item.typ === EnumToken.EOFTokenType)) {
|
|
1547
1512
|
node = parseNode(tokens, context, options, errors, stats, invalidNodes);
|
|
1548
1513
|
if (node != null) {
|
|
1549
1514
|
if ("chi" in node) {
|
|
@@ -1554,41 +1519,67 @@ async function doParse(iter, options = {}) {
|
|
|
1554
1519
|
imports.push(node);
|
|
1555
1520
|
}
|
|
1556
1521
|
}
|
|
1557
|
-
else if (item.
|
|
1522
|
+
else if (item.typ == EnumToken.BlockStartTokenType) {
|
|
1558
1523
|
let inBlock = 1;
|
|
1559
|
-
tokens =
|
|
1524
|
+
tokens.length = 0;
|
|
1525
|
+
tokens.push(item);
|
|
1560
1526
|
do {
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1527
|
+
tokenizer.next();
|
|
1528
|
+
if (tokenizer.unit != null) {
|
|
1529
|
+
item = {
|
|
1530
|
+
typ: tokenizer.typ,
|
|
1531
|
+
val: tokenizer.val,
|
|
1532
|
+
unit: tokenizer.unit,
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
else if (tokenizer.nam != null) {
|
|
1536
|
+
item = {
|
|
1537
|
+
typ: tokenizer.typ,
|
|
1538
|
+
nam: tokenizer.nam,
|
|
1539
|
+
};
|
|
1540
|
+
}
|
|
1541
|
+
else if (tokenizer.val === null) {
|
|
1542
|
+
item = {
|
|
1543
|
+
typ: tokenizer.typ,
|
|
1544
|
+
};
|
|
1545
|
+
}
|
|
1546
|
+
else if (tokenizer.kin != null) {
|
|
1547
|
+
item = {
|
|
1548
|
+
typ: tokenizer.typ,
|
|
1549
|
+
val: tokenizer.val,
|
|
1550
|
+
kin: tokenizer.kin,
|
|
1551
|
+
};
|
|
1568
1552
|
}
|
|
1569
|
-
|
|
1570
|
-
|
|
1553
|
+
else {
|
|
1554
|
+
item = {
|
|
1555
|
+
typ: tokenizer.typ,
|
|
1556
|
+
val: tokenizer.val,
|
|
1557
|
+
};
|
|
1558
|
+
}
|
|
1559
|
+
item[LOCSRCID] = tokenizer.srcId;
|
|
1560
|
+
item[LOCSTA] = tokenizer.sta;
|
|
1561
|
+
item[LOCEND] = tokenizer.end;
|
|
1562
|
+
tokens.push(item);
|
|
1563
|
+
if (item.typ === EnumToken.BlockStartTokenType) {
|
|
1571
1564
|
inBlock++;
|
|
1572
1565
|
}
|
|
1573
|
-
else if (item.
|
|
1566
|
+
else if (item.typ === EnumToken.BlockEndTokenType) {
|
|
1574
1567
|
inBlock--;
|
|
1575
1568
|
}
|
|
1576
|
-
} while (inBlock != 0);
|
|
1569
|
+
} while (inBlock != 0 && !tokenizer.done());
|
|
1577
1570
|
if (tokens.length > 0) {
|
|
1578
1571
|
errors.push({
|
|
1579
1572
|
action: "drop",
|
|
1580
1573
|
message: "invalid block",
|
|
1581
|
-
location: options.source.getSourceLocation(tokens[0][
|
|
1574
|
+
location: options.source.getSourceLocation(tokens[0][LOCSTA]),
|
|
1582
1575
|
});
|
|
1583
1576
|
}
|
|
1584
1577
|
}
|
|
1585
|
-
tokens =
|
|
1578
|
+
tokens.length = 0;
|
|
1586
1579
|
}
|
|
1587
|
-
else if ((parensMatch === 0 || curlyBracketMatch === 0) && item.
|
|
1580
|
+
else if ((parensMatch === 0 || curlyBracketMatch === 0) && item.typ === EnumToken.BlockEndTokenType) {
|
|
1588
1581
|
parseNode(tokens, context, options, errors, stats, invalidNodes);
|
|
1589
|
-
|
|
1590
|
-
context[LOC].end = item.token[LOC].end;
|
|
1591
|
-
}
|
|
1582
|
+
context[LOCEND] = item[LOCEND];
|
|
1592
1583
|
const previousNode = stack.pop();
|
|
1593
1584
|
context = (stack[stack.length - 1] ?? ast);
|
|
1594
1585
|
if (options.removeEmpty &&
|
|
@@ -1597,7 +1588,7 @@ async function doParse(iter, options = {}) {
|
|
|
1597
1588
|
context.chi[context.chi.length - 1] == previousNode) {
|
|
1598
1589
|
context.chi.pop();
|
|
1599
1590
|
}
|
|
1600
|
-
tokens =
|
|
1591
|
+
tokens.length = 0;
|
|
1601
1592
|
parensMatch = 0;
|
|
1602
1593
|
curlyBracketMatch = 0;
|
|
1603
1594
|
}
|
|
@@ -1623,7 +1614,7 @@ async function doParse(iter, options = {}) {
|
|
|
1623
1614
|
const token = node[TOKENS][0];
|
|
1624
1615
|
const url = token.typ == EnumToken.StringTokenType ? token.val.slice(1, -1) : token.val;
|
|
1625
1616
|
try {
|
|
1626
|
-
const src = options.resolve(url, options.src
|
|
1617
|
+
const src = options.resolve(url, options.src ? dirname(options.src) : options.cwd);
|
|
1627
1618
|
const result = options.load(src);
|
|
1628
1619
|
const stream = result instanceof Promise || Object.getPrototypeOf(result).constructor.name == "AsyncFunction"
|
|
1629
1620
|
? await result
|
|
@@ -1632,13 +1623,15 @@ async function doParse(iter, options = {}) {
|
|
|
1632
1623
|
options.sourcesMap.set(source.id, source);
|
|
1633
1624
|
const parseInfo = {
|
|
1634
1625
|
stream,
|
|
1635
|
-
buffer: "",
|
|
1636
1626
|
offset: 0,
|
|
1637
1627
|
source,
|
|
1638
1628
|
position: 0,
|
|
1639
|
-
currentPosition:
|
|
1629
|
+
currentPosition: 0,
|
|
1630
|
+
time: 0,
|
|
1640
1631
|
};
|
|
1641
|
-
const root = await doParse(stream instanceof ReadableStream
|
|
1632
|
+
const root = await doParse(stream instanceof ReadableStream
|
|
1633
|
+
? new Tokenizer(parseInfo, stream).tokenizeStream()
|
|
1634
|
+
: new Tokenizer(parseInfo), Object.assign({}, options, {
|
|
1642
1635
|
minify: false,
|
|
1643
1636
|
setParent: false,
|
|
1644
1637
|
src: options.resolve(url, options.src || options.cwd).relative,
|
|
@@ -1647,9 +1640,12 @@ async function doParse(iter, options = {}) {
|
|
|
1647
1640
|
stats.nodesCount += root.stats.nodesCount;
|
|
1648
1641
|
stats.tokensCount += root.stats.tokensCount;
|
|
1649
1642
|
stats.imports.push(root.stats);
|
|
1643
|
+
// @ts-ignore
|
|
1650
1644
|
node[PARENT].chi.splice(node[PARENT].chi.indexOf(node), 1, ...root.ast.chi);
|
|
1651
1645
|
if (root.errors.length > 0) {
|
|
1652
|
-
|
|
1646
|
+
for (const error of root.errors) {
|
|
1647
|
+
errors.push(error);
|
|
1648
|
+
}
|
|
1653
1649
|
}
|
|
1654
1650
|
}
|
|
1655
1651
|
catch (error) {
|
|
@@ -1663,210 +1659,172 @@ async function doParse(iter, options = {}) {
|
|
|
1663
1659
|
ast = expand(ast);
|
|
1664
1660
|
}
|
|
1665
1661
|
let replacement;
|
|
1666
|
-
let callable;
|
|
1667
1662
|
if (options.visitor != null) {
|
|
1668
1663
|
let parens;
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
: "KeyframesAtRule";
|
|
1693
|
-
if (preVisitorsHandlersMap.has(key)) {
|
|
1694
|
-
handlers.push(
|
|
1695
|
-
// @ts-expect-error
|
|
1696
|
-
...preVisitorsHandlersMap.get(key));
|
|
1697
|
-
}
|
|
1698
|
-
if (visitorsHandlersMap.has(key)) {
|
|
1699
|
-
// @ts-ignore
|
|
1700
|
-
handlers.push(...visitorsHandlersMap.get(key));
|
|
1701
|
-
}
|
|
1702
|
-
if (postVisitorsHandlersMap.has(key)) {
|
|
1703
|
-
// @ts-ignore
|
|
1704
|
-
handlers.push(...postVisitorsHandlersMap.get(key));
|
|
1705
|
-
}
|
|
1706
|
-
let node = result.node;
|
|
1707
|
-
for (const handler of handlers) {
|
|
1708
|
-
callable =
|
|
1709
|
-
typeof handler == "function"
|
|
1710
|
-
? handler
|
|
1711
|
-
: handler[camelize(node.typ === EnumToken.DeclarationNodeType ||
|
|
1712
|
-
node.typ === EnumToken.AtRuleNodeType
|
|
1713
|
-
? node.nam
|
|
1714
|
-
: node.val)];
|
|
1715
|
-
if (callable == null) {
|
|
1716
|
-
continue;
|
|
1717
|
-
}
|
|
1718
|
-
// @ts-expect-error
|
|
1719
|
-
replacement = callable(node, result[PARENT], ast, function* () {
|
|
1720
|
-
if (parens == null) {
|
|
1721
|
-
// @ts-expect-error
|
|
1722
|
-
parens = [...result.parents()];
|
|
1723
|
-
}
|
|
1724
|
-
yield* parens[Symbol.iterator]();
|
|
1725
|
-
});
|
|
1726
|
-
if (replacement == null) {
|
|
1727
|
-
continue;
|
|
1728
|
-
}
|
|
1729
|
-
if (replacement instanceof Promise) {
|
|
1730
|
-
replacement = await replacement;
|
|
1664
|
+
let genericKey;
|
|
1665
|
+
const handlers = [];
|
|
1666
|
+
const visitors = parseVisitors(options.visitor, errors);
|
|
1667
|
+
let nodes = new Array(stats.tokensCount);
|
|
1668
|
+
const subNodes = [];
|
|
1669
|
+
let i;
|
|
1670
|
+
let k;
|
|
1671
|
+
let j;
|
|
1672
|
+
let freeblock = 1;
|
|
1673
|
+
nodes[0] = ast;
|
|
1674
|
+
for (i = 0; i < nodes.length; i++) {
|
|
1675
|
+
if (nodes[i] == null) {
|
|
1676
|
+
break;
|
|
1677
|
+
}
|
|
1678
|
+
subNodes.length = 0;
|
|
1679
|
+
if (visitors.includeTokens) {
|
|
1680
|
+
switch (nodes[i].typ) {
|
|
1681
|
+
case EnumToken.RuleNodeType:
|
|
1682
|
+
case EnumToken.AtRuleNodeType:
|
|
1683
|
+
case EnumToken.KeyframesRuleNodeType:
|
|
1684
|
+
case EnumToken.KeyframesAtRuleNodeType:
|
|
1685
|
+
for (const token of nodes[i][TOKENS]) {
|
|
1686
|
+
subNodes.push(token);
|
|
1731
1687
|
}
|
|
1732
|
-
|
|
1733
|
-
|
|
1688
|
+
break;
|
|
1689
|
+
case EnumToken.DeclarationNodeType:
|
|
1690
|
+
for (const token of nodes[i].val) {
|
|
1691
|
+
subNodes.push(token);
|
|
1734
1692
|
}
|
|
1693
|
+
break;
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
// @ts-ignore
|
|
1697
|
+
if (nodes[i].chi != null) {
|
|
1698
|
+
// @ts-ignore
|
|
1699
|
+
for (k = 0; k < nodes[i].chi.length; k++) {
|
|
1700
|
+
// @ts-ignore
|
|
1701
|
+
subNodes.push(nodes[i].chi[k]);
|
|
1702
|
+
}
|
|
1703
|
+
}
|
|
1704
|
+
if (subNodes.length > 0) {
|
|
1705
|
+
if (freeblock <= i) {
|
|
1706
|
+
freeblock = i + 1;
|
|
1707
|
+
}
|
|
1708
|
+
for (k = 0; k < subNodes.length; k++) {
|
|
1709
|
+
j = k + freeblock;
|
|
1710
|
+
nodes[j] = subNodes[k];
|
|
1711
|
+
nodes[j][PARENT] = nodes[i];
|
|
1712
|
+
}
|
|
1713
|
+
freeblock += subNodes.length;
|
|
1714
|
+
}
|
|
1715
|
+
parens = null;
|
|
1716
|
+
handlers.length = 0;
|
|
1717
|
+
genericKey =
|
|
1718
|
+
nodes[i].typ == EnumToken.DeclarationNodeType
|
|
1719
|
+
? "Declaration"
|
|
1720
|
+
: nodes[i].typ == EnumToken.AtRuleNodeType
|
|
1721
|
+
? "AtRule"
|
|
1722
|
+
: nodes[i].typ == EnumToken.KeyframesAtRuleNodeType
|
|
1723
|
+
? "KeyframesAtRule"
|
|
1724
|
+
: nodes[i].typ === EnumToken.KeyframesRuleNodeType
|
|
1725
|
+
? "KeyframesRule"
|
|
1726
|
+
: nodes[i].typ == EnumToken.RuleNodeType
|
|
1727
|
+
? "Rule"
|
|
1728
|
+
: nodes[i].typ == EnumToken.KeyframesRuleNodeType
|
|
1729
|
+
? "KeyframesRule"
|
|
1730
|
+
: null;
|
|
1731
|
+
let keyName = nodes[i].typ == EnumToken.DeclarationNodeType || nodes[i].typ == EnumToken.AtRuleNodeType
|
|
1732
|
+
? camelize(nodes[i].nam)
|
|
1733
|
+
: nodes[i].typ == EnumToken.KeyframesAtRuleNodeType
|
|
1734
|
+
? camelize(nodes[i].val)
|
|
1735
|
+
: null;
|
|
1736
|
+
for (const map of visitors.allHandlers) {
|
|
1737
|
+
// @ts-ignore
|
|
1738
|
+
if (genericKey != null && map.has(genericKey)) {
|
|
1739
|
+
// @ts-ignore
|
|
1740
|
+
for (const handler of map.get(genericKey)) {
|
|
1741
|
+
if (typeof handler == "function") {
|
|
1742
|
+
handlers.push(handler);
|
|
1743
|
+
}
|
|
1744
|
+
// else if (Array.isArray(handler)) {
|
|
1745
|
+
// for (const h of handler) {
|
|
1746
|
+
// if (typeof h == "function") {
|
|
1747
|
+
// handlers.push(h);
|
|
1748
|
+
// }
|
|
1749
|
+
// // @ts-ignore
|
|
1750
|
+
// else if (h[keyName] != null) {
|
|
1751
|
+
// // @ts-ignore
|
|
1752
|
+
// handlers.push(h[keyName]);
|
|
1753
|
+
// }
|
|
1754
|
+
// }
|
|
1755
|
+
// } else if (typeof handler.handler! == "function") {
|
|
1756
|
+
// handlers.push(handler.handler);
|
|
1757
|
+
// }
|
|
1735
1758
|
// @ts-ignore
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1759
|
+
else if (typeof handler[keyName] == "function") {
|
|
1760
|
+
// @ts-ignore
|
|
1761
|
+
handlers.push(handler[keyName]);
|
|
1739
1762
|
}
|
|
1740
1763
|
}
|
|
1741
|
-
if (node != result.node) {
|
|
1742
|
-
replaceNodeOrValue(result.parent, result.node, node);
|
|
1743
|
-
}
|
|
1744
1764
|
}
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
}
|
|
1758
|
-
if (visitorsHandlersMap.has(key)) {
|
|
1759
|
-
handlers.push(...visitorsHandlersMap.get(key));
|
|
1760
|
-
}
|
|
1761
|
-
if (postVisitorsHandlersMap.has(key)) {
|
|
1762
|
-
handlers.push(...postVisitorsHandlersMap.get(key));
|
|
1763
|
-
}
|
|
1764
|
-
let node = result.node;
|
|
1765
|
-
for (const callable of handlers) {
|
|
1766
|
-
replacement = callable(node, result.parent, result.root,
|
|
1767
|
-
// @ts-expect-error
|
|
1768
|
-
function* () {
|
|
1769
|
-
if (parens == null) {
|
|
1770
|
-
// @ts-expect-error
|
|
1771
|
-
parens = [...result.parents()];
|
|
1765
|
+
// @ts-ignore
|
|
1766
|
+
if (map.has(nodes[i].typ)) {
|
|
1767
|
+
// @ts-ignore
|
|
1768
|
+
for (const handler of map.get(nodes[i].typ)) {
|
|
1769
|
+
if (typeof handler == "function") {
|
|
1770
|
+
handlers.push(handler);
|
|
1771
|
+
}
|
|
1772
|
+
else if (Array.isArray(handler)) {
|
|
1773
|
+
for (const h of handler) {
|
|
1774
|
+
if (typeof h == "function") {
|
|
1775
|
+
handlers.push(h);
|
|
1776
|
+
}
|
|
1772
1777
|
}
|
|
1773
|
-
yield* parens[Symbol.iterator]();
|
|
1774
|
-
});
|
|
1775
|
-
if (replacement == null) {
|
|
1776
|
-
continue;
|
|
1777
1778
|
}
|
|
1778
|
-
if (
|
|
1779
|
-
|
|
1780
|
-
}
|
|
1781
|
-
if (replacement == null || replacement == node) {
|
|
1782
|
-
continue;
|
|
1779
|
+
else if (typeof handler.handler == "function") {
|
|
1780
|
+
handlers.push(handler.handler);
|
|
1783
1781
|
}
|
|
1784
1782
|
// @ts-ignore
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
break;
|
|
1783
|
+
else if (typeof handler[keyName] == "function") {
|
|
1784
|
+
// @ts-ignore
|
|
1785
|
+
handlers.push(handler[keyName]);
|
|
1789
1786
|
}
|
|
1790
1787
|
}
|
|
1791
|
-
// @ts-ignore
|
|
1792
|
-
if (node != result.node) {
|
|
1793
|
-
// @ts-ignore
|
|
1794
|
-
replaceNodeOrValue(result.parent, result.node, node);
|
|
1795
|
-
}
|
|
1796
1788
|
}
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
});
|
|
1812
|
-
if (replacement == null) {
|
|
1813
|
-
continue;
|
|
1814
|
-
}
|
|
1815
|
-
if (replacement instanceof Promise) {
|
|
1816
|
-
replacement = await replacement;
|
|
1817
|
-
}
|
|
1818
|
-
if (replacement != null && replacement != node) {
|
|
1819
|
-
node = replacement;
|
|
1820
|
-
}
|
|
1821
|
-
}
|
|
1822
|
-
}
|
|
1823
|
-
if (node != result.node) {
|
|
1824
|
-
// @ts-ignore
|
|
1825
|
-
replaceNodeOrValue(result[PARENT], value, node);
|
|
1826
|
-
}
|
|
1827
|
-
const tokens = Array.isArray(result.node[TOKENS]) ? result.node[TOKENS] : [];
|
|
1828
|
-
if (Array.isArray(result.node.val)) {
|
|
1829
|
-
tokens.push(...result.node.val);
|
|
1830
|
-
}
|
|
1831
|
-
if (tokens.length == 0) {
|
|
1832
|
-
continue;
|
|
1833
|
-
}
|
|
1834
|
-
for (const { value, parent, root, parents } of walkValues(tokens, result.node)) {
|
|
1835
|
-
node = value;
|
|
1836
|
-
if (valuesHandlers.has(node.typ)) {
|
|
1837
|
-
let parens = null;
|
|
1838
|
-
for (const valueHandler of valuesHandlers.get(node.typ)) {
|
|
1839
|
-
callable = valueHandler;
|
|
1840
|
-
// @ts-expect-error
|
|
1841
|
-
let result = callable(node, parent, root, function* () {
|
|
1842
|
-
if (parens == null) {
|
|
1843
|
-
// @ts-expect-error
|
|
1844
|
-
parens = [...parents()];
|
|
1845
|
-
}
|
|
1846
|
-
yield* parens[Symbol.iterator]();
|
|
1847
|
-
});
|
|
1848
|
-
if (result == null) {
|
|
1849
|
-
continue;
|
|
1850
|
-
}
|
|
1851
|
-
if (result instanceof Promise) {
|
|
1852
|
-
result = await result;
|
|
1853
|
-
}
|
|
1854
|
-
if (result != null && result != node) {
|
|
1855
|
-
node = result;
|
|
1856
|
-
}
|
|
1857
|
-
if (Array.isArray(node)) {
|
|
1858
|
-
break;
|
|
1859
|
-
}
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
if (node != value) {
|
|
1863
|
-
// @ts-ignore
|
|
1864
|
-
replaceNodeOrValue(parent, value, node);
|
|
1789
|
+
}
|
|
1790
|
+
if (handlers.length == 0) {
|
|
1791
|
+
continue;
|
|
1792
|
+
}
|
|
1793
|
+
let node = nodes[i];
|
|
1794
|
+
for (const callable of handlers) {
|
|
1795
|
+
replacement = callable(node, nodes[i][PARENT], ast,
|
|
1796
|
+
// @ts-expect-error
|
|
1797
|
+
function* () {
|
|
1798
|
+
if (parens == null) {
|
|
1799
|
+
let node = nodes[i][PARENT];
|
|
1800
|
+
while (node != null) {
|
|
1801
|
+
yield node;
|
|
1802
|
+
node = node[PARENT];
|
|
1865
1803
|
}
|
|
1866
1804
|
}
|
|
1805
|
+
});
|
|
1806
|
+
if (replacement == null) {
|
|
1807
|
+
continue;
|
|
1808
|
+
}
|
|
1809
|
+
if (replacement instanceof Promise) {
|
|
1810
|
+
replacement = await replacement;
|
|
1811
|
+
}
|
|
1812
|
+
if (replacement == null || replacement == node) {
|
|
1813
|
+
continue;
|
|
1814
|
+
}
|
|
1815
|
+
// @ts-ignore
|
|
1816
|
+
node = replacement;
|
|
1817
|
+
//
|
|
1818
|
+
if (Array.isArray(node)) {
|
|
1819
|
+
break;
|
|
1867
1820
|
}
|
|
1868
1821
|
}
|
|
1822
|
+
if (node != nodes[i]) {
|
|
1823
|
+
// @ts-ignore
|
|
1824
|
+
replaceNodeOrValue(nodes[i][PARENT], nodes[i], node);
|
|
1825
|
+
}
|
|
1869
1826
|
}
|
|
1827
|
+
nodes = null;
|
|
1870
1828
|
}
|
|
1871
1829
|
if (invalidNodes.length > 0) {
|
|
1872
1830
|
let count = invalidNodes.length;
|
|
@@ -1889,19 +1847,6 @@ async function doParse(iter, options = {}) {
|
|
|
1889
1847
|
}
|
|
1890
1848
|
}
|
|
1891
1849
|
}
|
|
1892
|
-
while (stack.length > 0 && context != ast) {
|
|
1893
|
-
const previousNode = stack.pop();
|
|
1894
|
-
context = (stack[stack.length - 1] ?? ast);
|
|
1895
|
-
// remove empty nodes
|
|
1896
|
-
if (options.removeEmpty &&
|
|
1897
|
-
previousNode != null &&
|
|
1898
|
-
previousNode.chi.length == 0 &&
|
|
1899
|
-
context.chi[context.chi.length - 1] == previousNode) {
|
|
1900
|
-
context.chi.pop();
|
|
1901
|
-
continue;
|
|
1902
|
-
}
|
|
1903
|
-
break;
|
|
1904
|
-
}
|
|
1905
1850
|
if (options.minify) {
|
|
1906
1851
|
if (ast.chi.length > 0) {
|
|
1907
1852
|
let passes = options.pass ?? 1;
|
|
@@ -1919,7 +1864,7 @@ async function doParse(iter, options = {}) {
|
|
|
1919
1864
|
...stats,
|
|
1920
1865
|
parse: `${(endParseTime - startTime).toFixed(2)}ms`,
|
|
1921
1866
|
minify: `${(endTime - endParseTime).toFixed(2)}ms`,
|
|
1922
|
-
tokenize: `${(options?.parseInfo?.time ?? 0).toFixed(2)}ms`,
|
|
1867
|
+
// tokenize: `${(options?.parseInfo?.time ?? 0).toFixed(2)}ms`,
|
|
1923
1868
|
total: `${(endTime - startTime).toFixed(2)}ms`,
|
|
1924
1869
|
},
|
|
1925
1870
|
};
|
|
@@ -1948,7 +1893,7 @@ async function doParse(iter, options = {}) {
|
|
|
1948
1893
|
: (moduleSettings.filePath ?? options.src);
|
|
1949
1894
|
filePath =
|
|
1950
1895
|
filePath === ""
|
|
1951
|
-
? options.src
|
|
1896
|
+
? options.resolve(options.src, options.cwd).relative
|
|
1952
1897
|
: options.resolve(filePath, options.dirname(options.src), options.cwd).relative;
|
|
1953
1898
|
if (typeof options.module == "number") {
|
|
1954
1899
|
if (options.module & ModuleCaseTransformEnum.CamelCase) {
|
|
@@ -2007,19 +1952,22 @@ async function doParse(iter, options = {}) {
|
|
|
2007
1952
|
time: 0,
|
|
2008
1953
|
source,
|
|
2009
1954
|
position: 0,
|
|
2010
|
-
currentPosition:
|
|
1955
|
+
currentPosition: 0,
|
|
2011
1956
|
};
|
|
2012
|
-
const root = await doParse(stream instanceof ReadableStream
|
|
1957
|
+
const root = await doParse(stream instanceof ReadableStream
|
|
1958
|
+
? new Tokenizer(parseInfo, stream).tokenizeStream()
|
|
1959
|
+
: new Tokenizer(parseInfo), Object.assign({}, options, {
|
|
2013
1960
|
source,
|
|
2014
1961
|
minify: false,
|
|
2015
1962
|
setParent: false,
|
|
2016
1963
|
src: src.relative,
|
|
2017
1964
|
}));
|
|
2018
|
-
options.parseInfo
|
|
1965
|
+
// options.parseInfo!.time += parseInfo.time;
|
|
2019
1966
|
cssVariablesMap[node.nam] = root.cssModuleVariables;
|
|
2020
1967
|
parent.chi.splice(parent.chi.indexOf(node), 1);
|
|
2021
1968
|
continue;
|
|
2022
1969
|
}
|
|
1970
|
+
// @ts-ignore
|
|
2023
1971
|
if (node.typ == EnumToken.CssVariableDeclarationMapTokenType) {
|
|
2024
1972
|
const from = node.from.find((t) => t.typ == EnumToken.IdenTokenType || isIdentColor(t));
|
|
2025
1973
|
if (!(from.val in cssVariablesMap)) {
|
|
@@ -2064,6 +2012,7 @@ async function doParse(iter, options = {}) {
|
|
|
2064
2012
|
let result = moduleSettings.scoped & ModuleScopeEnumOptions.Global
|
|
2065
2013
|
? node.nam
|
|
2066
2014
|
: moduleSettings.generateScopedName(node.nam, moduleSettings.filePath, moduleSettings.pattern, moduleSettings.hashLength);
|
|
2015
|
+
// @ts-ignore
|
|
2067
2016
|
let value = result instanceof Promise ? await result : result;
|
|
2068
2017
|
mapping[node.nam] =
|
|
2069
2018
|
"--" +
|
|
@@ -2097,6 +2046,7 @@ async function doParse(iter, options = {}) {
|
|
|
2097
2046
|
parentRule.chi.splice(parentRule.chi.indexOf(node), 1);
|
|
2098
2047
|
continue;
|
|
2099
2048
|
}
|
|
2049
|
+
const resolvedSrc = options.resolve(options.src, options.cwd);
|
|
2100
2050
|
for (const token of composeSelectors) {
|
|
2101
2051
|
// composes: a b c;
|
|
2102
2052
|
if (token.r == null) {
|
|
@@ -2147,27 +2097,27 @@ async function doParse(iter, options = {}) {
|
|
|
2147
2097
|
? await result
|
|
2148
2098
|
: result;
|
|
2149
2099
|
const root = await doParse(stream instanceof ReadableStream
|
|
2150
|
-
?
|
|
2151
|
-
buffer: "",
|
|
2100
|
+
? new Tokenizer({
|
|
2152
2101
|
offset: 0,
|
|
2153
2102
|
source: new SourceFile("", [], src.relative),
|
|
2154
2103
|
position: 0,
|
|
2155
|
-
currentPosition:
|
|
2156
|
-
})
|
|
2157
|
-
:
|
|
2104
|
+
currentPosition: 0,
|
|
2105
|
+
}, stream).tokenizeStream()
|
|
2106
|
+
: new Tokenizer({
|
|
2158
2107
|
stream,
|
|
2159
|
-
buffer: "",
|
|
2160
2108
|
offset: 0,
|
|
2161
2109
|
position: 0,
|
|
2162
2110
|
source: new SourceFile(stream, [], src.relative),
|
|
2163
|
-
currentPosition:
|
|
2111
|
+
currentPosition: 0,
|
|
2164
2112
|
}), Object.assign({}, options, {
|
|
2165
2113
|
minify: false,
|
|
2166
2114
|
setParent: false,
|
|
2167
2115
|
src: src.relative,
|
|
2168
2116
|
}));
|
|
2169
|
-
|
|
2170
|
-
|
|
2117
|
+
let srcIndex = options.resolve(src.absolute, resolvedSrc.absolute).relative;
|
|
2118
|
+
if (!srcIndex.startsWith("/") && !srcIndex.startsWith("../")) {
|
|
2119
|
+
srcIndex = `./${srcIndex}`;
|
|
2120
|
+
}
|
|
2171
2121
|
if (Object.keys(root.mapping).length > 0) {
|
|
2172
2122
|
importMapping[srcIndex] = {};
|
|
2173
2123
|
}
|
|
@@ -2368,30 +2318,13 @@ async function doParse(iter, options = {}) {
|
|
|
2368
2318
|
}
|
|
2369
2319
|
for (const { value, parent } of walkValues(node.val, node)) {
|
|
2370
2320
|
if (value.typ == EnumToken.DashedIdenTokenType) {
|
|
2371
|
-
// if (!((value as DashedIdentToken).val in mapping)) {
|
|
2372
|
-
// const result =
|
|
2373
|
-
// moduleSettings.scoped! & ModuleScopeEnumOptions.Global
|
|
2374
|
-
// ? (value as DashedIdentToken).val
|
|
2375
|
-
// : moduleSettings.generateScopedName!(
|
|
2376
|
-
// (value as DashedIdentToken).val,
|
|
2377
|
-
// moduleSettings.filePath as string,
|
|
2378
|
-
// moduleSettings.pattern as string,
|
|
2379
|
-
// moduleSettings.hashLength,
|
|
2380
|
-
// );
|
|
2381
|
-
// let val: string = result instanceof Promise ? await result : result;
|
|
2382
|
-
// mapping[(value as DashedIdentToken).val] =
|
|
2383
|
-
// "--" +
|
|
2384
|
-
// (moduleSettings.naming! & ModuleCaseTransformEnum.DashCaseOnly ||
|
|
2385
|
-
// moduleSettings.naming! & ModuleCaseTransformEnum.CamelCaseOnly
|
|
2386
|
-
// ? getKeyName(val, moduleSettings.naming as ModuleCaseTransformEnum)
|
|
2387
|
-
// : val);
|
|
2388
|
-
// revMapping[mapping[(value as DashedIdentToken).val]] = (value as DashedIdentToken).val;
|
|
2389
|
-
// }
|
|
2390
2321
|
value.val = mapping[value.val];
|
|
2391
2322
|
}
|
|
2392
2323
|
else if ((value.typ == EnumToken.IdenTokenType || isIdentColor(value)) &&
|
|
2393
2324
|
value.val in importedCssVariables) {
|
|
2394
|
-
replaceNodeOrValue(
|
|
2325
|
+
replaceNodeOrValue(
|
|
2326
|
+
// @ts-ignore
|
|
2327
|
+
parent, value, importedCssVariables[value.val].val);
|
|
2395
2328
|
}
|
|
2396
2329
|
}
|
|
2397
2330
|
}
|
|
@@ -2419,26 +2352,6 @@ async function doParse(iter, options = {}) {
|
|
|
2419
2352
|
EnumToken.DescendantCombinatorTokenType) {
|
|
2420
2353
|
parent[TOKENS].splice(index, 1);
|
|
2421
2354
|
}
|
|
2422
|
-
// if (val == ":global") {
|
|
2423
|
-
// for (; index < (parent as AstRule)[TOKENS]!.length; index++) {
|
|
2424
|
-
// if (
|
|
2425
|
-
// (parent as AstRule)[TOKENS]![index].typ ==
|
|
2426
|
-
// EnumToken.CommaTokenType ||
|
|
2427
|
-
// ([
|
|
2428
|
-
// EnumToken.PseudoClassFuncTokenType,
|
|
2429
|
-
// EnumToken.PseudoClassTokenType,
|
|
2430
|
-
// ].includes((parent as AstRule)[TOKENS]![index].typ) &&
|
|
2431
|
-
// [":global", ":local"].includes(
|
|
2432
|
-
// (
|
|
2433
|
-
// (parent as AstRule)[TOKENS]![index] as PseudoClassToken
|
|
2434
|
-
// ).val.toLowerCase(),
|
|
2435
|
-
// ))
|
|
2436
|
-
// ) {
|
|
2437
|
-
// break;
|
|
2438
|
-
// }
|
|
2439
|
-
// global.add((parent as AstRule)[TOKENS]![index]);
|
|
2440
|
-
// }
|
|
2441
|
-
// }
|
|
2442
2355
|
}
|
|
2443
2356
|
break;
|
|
2444
2357
|
}
|
|
@@ -2452,12 +2365,6 @@ async function doParse(iter, options = {}) {
|
|
|
2452
2365
|
case ":local":
|
|
2453
2366
|
parent[TOKENS].splice(parent[TOKENS].indexOf(value), 1, ...value.chi);
|
|
2454
2367
|
break;
|
|
2455
|
-
// (parent as AstRule)[TOKENS]!.splice(
|
|
2456
|
-
// (parent as AstRule)[TOKENS]!.indexOf(value),
|
|
2457
|
-
// 1,
|
|
2458
|
-
// ...(value as FunctionToken).chi,
|
|
2459
|
-
// );
|
|
2460
|
-
// break;
|
|
2461
2368
|
}
|
|
2462
2369
|
}
|
|
2463
2370
|
})) {
|
|
@@ -2495,7 +2402,7 @@ async function doParse(iter, options = {}) {
|
|
|
2495
2402
|
}
|
|
2496
2403
|
if (moduleSettings.scoped & ModuleScopeEnumOptions.Pure) {
|
|
2497
2404
|
if (!hasIdOrClass) {
|
|
2498
|
-
throw new Error(`pure module: No id or class found in selector '${node.sel}' at '${
|
|
2405
|
+
throw new Error(`pure module: No id or class found in selector '${node.sel}' at '${(options.source?.getOffsets?.(node[LOCSTA]) ?? []).join(":")}'`);
|
|
2499
2406
|
}
|
|
2500
2407
|
}
|
|
2501
2408
|
node.sel = "";
|
|
@@ -2532,31 +2439,6 @@ async function doParse(iter, options = {}) {
|
|
|
2532
2439
|
}
|
|
2533
2440
|
node.val = renderTokens(node[TOKENS]);
|
|
2534
2441
|
}
|
|
2535
|
-
// else {
|
|
2536
|
-
// let isReplaced: boolean = false;
|
|
2537
|
-
// for (const { value, parent } of walkValues(node[TOKENS], node)) {
|
|
2538
|
-
// if (
|
|
2539
|
-
// EnumToken.MediaQueryConditionTokenType == parent.typ &&
|
|
2540
|
-
// // @ts-expect-error
|
|
2541
|
-
// value != (parent as MediaQueryConditionToken).l
|
|
2542
|
-
// ) {
|
|
2543
|
-
// if (
|
|
2544
|
-
// (value.typ == EnumToken.IdenTokenType || isIdentColor(value)) &&
|
|
2545
|
-
// (value as IdentToken).val in importedCssVariables
|
|
2546
|
-
// ) {
|
|
2547
|
-
// isReplaced = true;
|
|
2548
|
-
// (parent as MediaQueryConditionToken).r.splice(
|
|
2549
|
-
// (parent as MediaQueryConditionToken).r.indexOf(value),
|
|
2550
|
-
// 1,
|
|
2551
|
-
// ...importedCssVariables[(value as IdentToken).val].val,
|
|
2552
|
-
// );
|
|
2553
|
-
// }
|
|
2554
|
-
// }
|
|
2555
|
-
// }
|
|
2556
|
-
// if (isReplaced) {
|
|
2557
|
-
// node.val = renderTokens(node[TOKENS]!);
|
|
2558
|
-
// }
|
|
2559
|
-
// }
|
|
2560
2442
|
}
|
|
2561
2443
|
}
|
|
2562
2444
|
if (moduleSettings.naming != ModuleCaseTransformEnum.IgnoreCase) {
|
|
@@ -2588,7 +2470,6 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
|
|
|
2588
2470
|
tokens.pop();
|
|
2589
2471
|
// check parenthesis are balanced
|
|
2590
2472
|
let matchCount = 0;
|
|
2591
|
-
let position = tokens.at(-1)?.[LOC];
|
|
2592
2473
|
for (let i = 0; i < tokens.length; i++) {
|
|
2593
2474
|
const token = tokens[i];
|
|
2594
2475
|
if (token.typ === EnumToken.StartParensTokenType || tokensfuncDefMap.has(token.typ)) {
|
|
@@ -2609,7 +2490,9 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
|
|
|
2609
2490
|
while (matchCount > 0) {
|
|
2610
2491
|
tokens.push({
|
|
2611
2492
|
typ: EnumToken.EndParensTokenType,
|
|
2612
|
-
[
|
|
2493
|
+
[LOCSRCID]: tokens[k]?.[LOCSRCID],
|
|
2494
|
+
[LOCSTA]: tokens[k]?.[LOCSTA],
|
|
2495
|
+
[LOCEND]: tokens[k]?.[LOCEND],
|
|
2613
2496
|
});
|
|
2614
2497
|
matchCount--;
|
|
2615
2498
|
}
|
|
@@ -2621,7 +2504,7 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
|
|
|
2621
2504
|
action: "drop",
|
|
2622
2505
|
message: `CDOCOMM not allowed here ${JSON.stringify(tokens[i], null, 1)}`,
|
|
2623
2506
|
node: tokens[i],
|
|
2624
|
-
location: options.source.getSourceLocation(tokens[i][
|
|
2507
|
+
location: options.source.getSourceLocation(tokens[i][LOCSTA]),
|
|
2625
2508
|
});
|
|
2626
2509
|
tokens[i].typ = EnumToken.InvalidCommentTokenType;
|
|
2627
2510
|
continue;
|
|
@@ -2644,7 +2527,7 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
|
|
|
2644
2527
|
action: "drop",
|
|
2645
2528
|
message: `CDOCOMM not allowed here ${JSON.stringify(tokens[i], null, 1)}`,
|
|
2646
2529
|
node: tokens[i],
|
|
2647
|
-
location: options.source.getSourceLocation(tokens[i][
|
|
2530
|
+
location: options.source.getSourceLocation(tokens[i][LOCSTA]),
|
|
2648
2531
|
});
|
|
2649
2532
|
tokens[i].typ = EnumToken.InvalidCommentTokenType;
|
|
2650
2533
|
continue;
|
|
@@ -2725,7 +2608,7 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
|
|
|
2725
2608
|
message: "<declaration> not allowed in <stylesheet>",
|
|
2726
2609
|
action: "drop",
|
|
2727
2610
|
node,
|
|
2728
|
-
location: options.source.getSourceLocation(node[
|
|
2611
|
+
location: options.source.getSourceLocation(node[LOCSTA]),
|
|
2729
2612
|
});
|
|
2730
2613
|
}
|
|
2731
2614
|
else if (options.lenient || node.typ === EnumToken.DeclarationNodeType) {
|
|
@@ -2743,6 +2626,8 @@ function parseNode(tokens, context, options, errors, stats, invalidNodes) {
|
|
|
2743
2626
|
return null;
|
|
2744
2627
|
}
|
|
2745
2628
|
/**
|
|
2629
|
+
* @param stream
|
|
2630
|
+
* @param context
|
|
2746
2631
|
* @param options
|
|
2747
2632
|
* @param errors
|
|
2748
2633
|
* @param parseAsBlock
|
|
@@ -2762,7 +2647,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2762
2647
|
errors.push({
|
|
2763
2648
|
action: "drop",
|
|
2764
2649
|
node: atRule,
|
|
2765
|
-
location: options.source.getSourceLocation(atRule[
|
|
2650
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
2766
2651
|
message: "unknown at-rule",
|
|
2767
2652
|
});
|
|
2768
2653
|
const result = matchGenericSyntax(stream, options);
|
|
@@ -2783,7 +2668,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2783
2668
|
errors.push({
|
|
2784
2669
|
action: "drop",
|
|
2785
2670
|
node: atRule,
|
|
2786
|
-
location: options.source.getSourceLocation(atRule[
|
|
2671
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
2787
2672
|
message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
|
|
2788
2673
|
});
|
|
2789
2674
|
atRule[TOKENS] = parseTokens(stream);
|
|
@@ -2801,8 +2686,8 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2801
2686
|
errors.push({
|
|
2802
2687
|
action: "drop",
|
|
2803
2688
|
node: token,
|
|
2804
|
-
location: options.source.getSourceLocation(token[
|
|
2805
|
-
message: `unexpected token
|
|
2689
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
2690
|
+
message: `unexpected token`,
|
|
2806
2691
|
});
|
|
2807
2692
|
atRule[TOKENS] = parseTokens(stream);
|
|
2808
2693
|
atRule[STATE] = EnumAstNodeStatus.Invalid;
|
|
@@ -2819,11 +2704,10 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2819
2704
|
parseAsBlock = blockAllowed;
|
|
2820
2705
|
}
|
|
2821
2706
|
if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed) {
|
|
2822
|
-
success = false;
|
|
2823
2707
|
errors.push({
|
|
2824
2708
|
action: "drop",
|
|
2825
2709
|
node: atRule,
|
|
2826
|
-
location: options.source.getSourceLocation(atRule[
|
|
2710
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
2827
2711
|
message: parseAsBlock ? "at-rule block not supported" : "at-rule block is required",
|
|
2828
2712
|
});
|
|
2829
2713
|
atRule[TOKENS] = parseTokens(stream);
|
|
@@ -2846,7 +2730,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2846
2730
|
errors.push({
|
|
2847
2731
|
action: "drop",
|
|
2848
2732
|
node: stream[0] ?? atRule,
|
|
2849
|
-
location: options.source.getSourceLocation((stream[0] ?? atRule)[
|
|
2733
|
+
location: options.source.getSourceLocation((stream[0] ?? atRule)[LOCSTA]),
|
|
2850
2734
|
message: "expecting <space>",
|
|
2851
2735
|
});
|
|
2852
2736
|
}
|
|
@@ -2855,7 +2739,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2855
2739
|
errors.push({
|
|
2856
2740
|
action: "drop",
|
|
2857
2741
|
node: stream[1] ?? atRule,
|
|
2858
|
-
location: options.source.getSourceLocation((stream[1] ?? atRule)[
|
|
2742
|
+
location: options.source.getSourceLocation((stream[1] ?? atRule)[LOCSTA]),
|
|
2859
2743
|
message: "expecting <string>",
|
|
2860
2744
|
});
|
|
2861
2745
|
}
|
|
@@ -2864,7 +2748,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2864
2748
|
errors.push({
|
|
2865
2749
|
action: "drop",
|
|
2866
2750
|
node: stream[1] ?? atRule,
|
|
2867
|
-
location: options.source.getSourceLocation((stream[1] ?? atRule)[
|
|
2751
|
+
location: options.source.getSourceLocation((stream[1] ?? atRule)[LOCSTA]),
|
|
2868
2752
|
message: "expecting double-quoted string",
|
|
2869
2753
|
});
|
|
2870
2754
|
}
|
|
@@ -2872,7 +2756,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2872
2756
|
atRule[TOKENS] = stream;
|
|
2873
2757
|
atRule[STATE] = EnumAstNodeStatus.Invalid;
|
|
2874
2758
|
atRule[ERRORS] = [errors[errors.length - 1]];
|
|
2875
|
-
atRule[
|
|
2759
|
+
atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
|
|
2876
2760
|
// @ts-expect-error
|
|
2877
2761
|
return Object.assign(atRule, {
|
|
2878
2762
|
typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
|
|
@@ -2885,7 +2769,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2885
2769
|
atRule[TOKENS] = stream;
|
|
2886
2770
|
atRule[STATE] = EnumAstNodeStatus.Validated;
|
|
2887
2771
|
atRule[ERRORS] = [];
|
|
2888
|
-
atRule[
|
|
2772
|
+
atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
|
|
2889
2773
|
// @ts-expect-error
|
|
2890
2774
|
return Object.assign(atRule, {
|
|
2891
2775
|
typ: success ? EnumToken.AtRuleNodeType : EnumToken.InvalidRuleNodeType,
|
|
@@ -2895,12 +2779,14 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2895
2779
|
case "font-feature-values": {
|
|
2896
2780
|
const result = parseAtRuleFontFeatureValues(stream, atRule, options);
|
|
2897
2781
|
if (result.errors.length > 0) {
|
|
2898
|
-
|
|
2782
|
+
for (const error of result.errors) {
|
|
2783
|
+
errors.push(error);
|
|
2784
|
+
}
|
|
2899
2785
|
}
|
|
2900
2786
|
atRule[TOKENS] = stream;
|
|
2901
2787
|
atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
2902
2788
|
atRule[ERRORS] = result.success ? [] : result.errors;
|
|
2903
|
-
atRule[
|
|
2789
|
+
atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
|
|
2904
2790
|
// @ts-expect-error
|
|
2905
2791
|
return Object.assign(atRule, {
|
|
2906
2792
|
typ: EnumToken.AtRuleNodeType,
|
|
@@ -2919,7 +2805,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2919
2805
|
errors.push({
|
|
2920
2806
|
action: "drop",
|
|
2921
2807
|
node: atRule,
|
|
2922
|
-
location: options.source.getSourceLocation(atRule[
|
|
2808
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
2923
2809
|
message: `unexpected at-rule ${atRule.nam}`,
|
|
2924
2810
|
});
|
|
2925
2811
|
}
|
|
@@ -2930,13 +2816,13 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2930
2816
|
errors.push({
|
|
2931
2817
|
action: "drop",
|
|
2932
2818
|
node: token,
|
|
2933
|
-
location: options.source.getSourceLocation(token[
|
|
2934
|
-
message: `unexpected token
|
|
2819
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
2820
|
+
message: `unexpected token`,
|
|
2935
2821
|
});
|
|
2936
2822
|
}
|
|
2937
2823
|
}
|
|
2938
2824
|
}
|
|
2939
|
-
atRule[
|
|
2825
|
+
atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
|
|
2940
2826
|
atRule[TOKENS] = stream;
|
|
2941
2827
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
2942
2828
|
atRule[ERRORS] = [errors[errors.length - 1]];
|
|
@@ -2950,9 +2836,11 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2950
2836
|
case "container": {
|
|
2951
2837
|
const result = parseAtRuleContainerQueryList(stream, atRule, options);
|
|
2952
2838
|
if (result.errors.length > 0) {
|
|
2953
|
-
|
|
2839
|
+
for (const error of result.errors) {
|
|
2840
|
+
errors.push(error);
|
|
2841
|
+
}
|
|
2954
2842
|
}
|
|
2955
|
-
atRule[
|
|
2843
|
+
atRule[LOCEND] = (stream.at(-1) ?? atRule)[LOCEND];
|
|
2956
2844
|
atRule[TOKENS] = stream;
|
|
2957
2845
|
atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
2958
2846
|
atRule[ERRORS] = result.success ? [] : result.errors;
|
|
@@ -2967,11 +2855,13 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2967
2855
|
const tokens = trimArray(stream.slice(1));
|
|
2968
2856
|
const result = matchAllSyntaxes(syntax, createValidationContext(tokens), options);
|
|
2969
2857
|
if (result.errors.length > 0) {
|
|
2970
|
-
|
|
2858
|
+
for (const error of result.errors) {
|
|
2859
|
+
errors.push(error);
|
|
2860
|
+
}
|
|
2971
2861
|
}
|
|
2972
2862
|
// @ts-expect-error
|
|
2973
2863
|
options = { ...options, convertColor: false };
|
|
2974
|
-
atRule[
|
|
2864
|
+
atRule[LOCEND] = (tokens.at(-1) ?? atRule)[LOCEND];
|
|
2975
2865
|
atRule[TOKENS] = tokens;
|
|
2976
2866
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.ValidationFailed;
|
|
2977
2867
|
atRule[ERRORS] = result.success ? [] : result.errors;
|
|
@@ -2991,14 +2881,14 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
2991
2881
|
errors.push({
|
|
2992
2882
|
action: "drop",
|
|
2993
2883
|
node: atRule,
|
|
2994
|
-
location: options.source.getSourceLocation(atRule[
|
|
2995
|
-
message: `expected <keyframe-name
|
|
2884
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
2885
|
+
message: `expected <keyframe-name>`,
|
|
2996
2886
|
});
|
|
2997
2887
|
success = false;
|
|
2998
2888
|
}
|
|
2999
2889
|
// @ts-expect-error
|
|
3000
2890
|
options = { ...options, convertColor: false };
|
|
3001
|
-
atRule[
|
|
2891
|
+
atRule[LOCEND] = (tokens.at(-1) ?? atRule)[LOCEND];
|
|
3002
2892
|
atRule[TOKENS] = tokens;
|
|
3003
2893
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3004
2894
|
atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
|
|
@@ -3012,7 +2902,9 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3012
2902
|
case "namespace": {
|
|
3013
2903
|
const result = matchAllSyntaxes(syntax, createValidationContext(stream), options);
|
|
3014
2904
|
if (!result.success) {
|
|
3015
|
-
|
|
2905
|
+
for (const error of result.errors) {
|
|
2906
|
+
errors.push(error);
|
|
2907
|
+
}
|
|
3016
2908
|
}
|
|
3017
2909
|
// else {
|
|
3018
2910
|
// parseUrlToken(stream);
|
|
@@ -3041,7 +2933,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3041
2933
|
stream.splice(start - 1, end - start + 2, ...stream.slice(start, end));
|
|
3042
2934
|
}
|
|
3043
2935
|
}
|
|
3044
|
-
atRule[
|
|
2936
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3045
2937
|
atRule[TOKENS] = stream.slice();
|
|
3046
2938
|
atRule[STATE] = valid ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3047
2939
|
atRule[ERRORS] = valid ? [] : result.errors;
|
|
@@ -3061,7 +2953,9 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3061
2953
|
case "import": {
|
|
3062
2954
|
const result = matchAtRuleImportSyntax(atRule, stream, context, options);
|
|
3063
2955
|
if (result.errors.length > 0) {
|
|
3064
|
-
|
|
2956
|
+
for (const error of result.errors) {
|
|
2957
|
+
errors.push(error);
|
|
2958
|
+
}
|
|
3065
2959
|
}
|
|
3066
2960
|
else {
|
|
3067
2961
|
if (stream[0]?.typ == EnumToken.UrlFunctionTokenType &&
|
|
@@ -3069,8 +2963,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3069
2963
|
stream.splice(0, 1, ...stream[0].chi);
|
|
3070
2964
|
}
|
|
3071
2965
|
}
|
|
3072
|
-
|
|
3073
|
-
atRule[LOC].end = stream.at(-1)?.[LOC]?.end ?? atRule[LOC].end;
|
|
2966
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3074
2967
|
atRule[TOKENS] = stream.slice();
|
|
3075
2968
|
atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3076
2969
|
atRule[ERRORS] = result.success ? [] : result.errors;
|
|
@@ -3094,7 +2987,9 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3094
2987
|
? parseAtRuleSupportSyntax(stream, atRule, options)
|
|
3095
2988
|
: matchAtRuleWhenElseSyntax(stream, atRule, options);
|
|
3096
2989
|
if (result.errors.length > 0) {
|
|
3097
|
-
|
|
2990
|
+
for (const error of result.errors) {
|
|
2991
|
+
errors.push(error);
|
|
2992
|
+
}
|
|
3098
2993
|
}
|
|
3099
2994
|
let success = result.success;
|
|
3100
2995
|
if (atRule.nam === "else") {
|
|
@@ -3114,10 +3009,13 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3114
3009
|
let definedAfterLastElse = false;
|
|
3115
3010
|
if (sibling == null || sibling.typ !== EnumToken.AtRuleNodeType) {
|
|
3116
3011
|
missingWhen = true;
|
|
3012
|
+
// @ts-expect-error
|
|
3117
3013
|
}
|
|
3118
3014
|
else if (sibling.nam !== "when") {
|
|
3015
|
+
// @ts-expect-error
|
|
3119
3016
|
if (sibling.nam !== "else") {
|
|
3120
3017
|
missingWhen = true;
|
|
3018
|
+
// @ts-expect-error
|
|
3121
3019
|
}
|
|
3122
3020
|
else if (sibling.val === "") {
|
|
3123
3021
|
definedAfterLastElse = true;
|
|
@@ -3128,7 +3026,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3128
3026
|
errors.push({
|
|
3129
3027
|
action: "drop",
|
|
3130
3028
|
node: atRule,
|
|
3131
|
-
location: options.source.getSourceLocation(atRule[
|
|
3029
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
3132
3030
|
message: "at-rule @when is required before @else block",
|
|
3133
3031
|
});
|
|
3134
3032
|
}
|
|
@@ -3137,14 +3035,14 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3137
3035
|
errors.push({
|
|
3138
3036
|
action: "drop",
|
|
3139
3037
|
node: atRule,
|
|
3140
|
-
location: options.source.getSourceLocation(atRule[
|
|
3038
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
3141
3039
|
message: "at-rule @else block is defined after last @else block",
|
|
3142
3040
|
});
|
|
3143
3041
|
}
|
|
3144
3042
|
}
|
|
3145
3043
|
// @ts-expect-error
|
|
3146
3044
|
options = { ...options, minify: false, convertColor: false };
|
|
3147
|
-
atRule[
|
|
3045
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3148
3046
|
atRule[TOKENS] = stream.slice();
|
|
3149
3047
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3150
3048
|
atRule[ERRORS] = result.success ? [] : [errors[errors.length - 1]].concat(result.errors);
|
|
@@ -3159,9 +3057,11 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3159
3057
|
options = { ...options, parseColor: false };
|
|
3160
3058
|
const result = parseMediaqueryList(stream, options);
|
|
3161
3059
|
if (result.errors.length > 0) {
|
|
3162
|
-
|
|
3060
|
+
for (const error of result.errors) {
|
|
3061
|
+
errors.push(error);
|
|
3062
|
+
}
|
|
3163
3063
|
}
|
|
3164
|
-
atRule[
|
|
3064
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3165
3065
|
atRule[TOKENS] = stream.slice();
|
|
3166
3066
|
atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3167
3067
|
atRule[ERRORS] = result.success ? [] : result.errors;
|
|
@@ -3181,7 +3081,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3181
3081
|
errors.push({
|
|
3182
3082
|
action: "drop",
|
|
3183
3083
|
node: range[0] ?? atRule,
|
|
3184
|
-
location: options.source.getSourceLocation((range[0] ?? atRule)[
|
|
3084
|
+
location: options.source.getSourceLocation((range[0] ?? atRule)[LOCSTA]),
|
|
3185
3085
|
message: "expected '(' at start of @scope block",
|
|
3186
3086
|
});
|
|
3187
3087
|
success = false;
|
|
@@ -3190,7 +3090,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3190
3090
|
errors.push({
|
|
3191
3091
|
action: "drop",
|
|
3192
3092
|
node: range.at(-1) ?? atRule,
|
|
3193
|
-
location: options.source.getSourceLocation((range.at(-1) ?? atRule)[
|
|
3093
|
+
location: options.source.getSourceLocation((range.at(-1) ?? atRule)[LOCSTA]),
|
|
3194
3094
|
message: "expected ')' at end of @scope block",
|
|
3195
3095
|
});
|
|
3196
3096
|
success = false;
|
|
@@ -3216,7 +3116,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3216
3116
|
errors.push({
|
|
3217
3117
|
action: "drop",
|
|
3218
3118
|
node: stream[index],
|
|
3219
|
-
location: options.source.getSourceLocation(stream[index]?.[
|
|
3119
|
+
location: options.source.getSourceLocation(stream[index]?.[LOCSTA]),
|
|
3220
3120
|
message: "expected 'to' at end of @scope block",
|
|
3221
3121
|
});
|
|
3222
3122
|
success = false;
|
|
@@ -3229,7 +3129,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3229
3129
|
errors.push({
|
|
3230
3130
|
action: "drop",
|
|
3231
3131
|
node: stream[index],
|
|
3232
|
-
location: options.source.getSourceLocation(stream[index]?.[
|
|
3132
|
+
location: options.source.getSourceLocation(stream[index]?.[LOCSTA]),
|
|
3233
3133
|
message: "expected 'to' at end of @scope block",
|
|
3234
3134
|
});
|
|
3235
3135
|
success = false;
|
|
@@ -3242,7 +3142,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3242
3142
|
errors.push({
|
|
3243
3143
|
action: "drop",
|
|
3244
3144
|
node: range.at(-1) ?? atRule,
|
|
3245
|
-
location: options.source.getSourceLocation((range.at(-1) ?? atRule)[
|
|
3145
|
+
location: options.source.getSourceLocation((range.at(-1) ?? atRule)[LOCSTA]),
|
|
3246
3146
|
message: "expected ')' at end of @scope block",
|
|
3247
3147
|
});
|
|
3248
3148
|
success = false;
|
|
@@ -3261,8 +3161,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3261
3161
|
}
|
|
3262
3162
|
}
|
|
3263
3163
|
}
|
|
3264
|
-
|
|
3265
|
-
atRule[LOC].end = stream.at(-1)?.[LOC]?.end ?? atRule[LOC].end;
|
|
3164
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3266
3165
|
atRule[TOKENS] = stream.slice();
|
|
3267
3166
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3268
3167
|
atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
|
|
@@ -3275,7 +3174,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3275
3174
|
}
|
|
3276
3175
|
case "page": {
|
|
3277
3176
|
trimArray(stream);
|
|
3278
|
-
atRule[
|
|
3177
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3279
3178
|
atRule[TOKENS] = stream.slice();
|
|
3280
3179
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3281
3180
|
atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
|
|
@@ -3306,8 +3205,8 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3306
3205
|
errors.push({
|
|
3307
3206
|
action: "drop",
|
|
3308
3207
|
node: atRule,
|
|
3309
|
-
location: options.source.getSourceLocation(atRule[
|
|
3310
|
-
message: "node is
|
|
3208
|
+
location: options.source.getSourceLocation(atRule[LOCSTA]),
|
|
3209
|
+
message: "node is allowed only in @page rule",
|
|
3311
3210
|
});
|
|
3312
3211
|
}
|
|
3313
3212
|
else {
|
|
@@ -3319,14 +3218,14 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3319
3218
|
errors.push({
|
|
3320
3219
|
action: "drop",
|
|
3321
3220
|
node: stream[i],
|
|
3322
|
-
location: options.source.getSourceLocation(stream[i][
|
|
3221
|
+
location: options.source.getSourceLocation(stream[i][LOCSTA]),
|
|
3323
3222
|
message: "expected whitespace or comment",
|
|
3324
3223
|
});
|
|
3325
3224
|
break;
|
|
3326
3225
|
}
|
|
3327
3226
|
}
|
|
3328
3227
|
}
|
|
3329
|
-
atRule[
|
|
3228
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3330
3229
|
atRule[TOKENS] = stream.slice();
|
|
3331
3230
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3332
3231
|
atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
|
|
@@ -3348,7 +3247,9 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3348
3247
|
});
|
|
3349
3248
|
stream.splice(index, 0, {
|
|
3350
3249
|
typ: EnumToken.ColonTokenType,
|
|
3351
|
-
[
|
|
3250
|
+
[LOCSRCID]: stream[index][LOCSRCID],
|
|
3251
|
+
[LOCSTA]: stream[index][LOCSTA],
|
|
3252
|
+
[LOCEND]: stream[index][LOCEND],
|
|
3352
3253
|
});
|
|
3353
3254
|
isVarDeclaration = true;
|
|
3354
3255
|
break;
|
|
@@ -3370,14 +3271,15 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3370
3271
|
atRule[STATE] = success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3371
3272
|
atRule[ERRORS] = success ? [] : [errors[errors.length - 1]];
|
|
3372
3273
|
if (!result.success) {
|
|
3373
|
-
|
|
3274
|
+
for (const error of result.errors) {
|
|
3275
|
+
errors.push(error);
|
|
3276
|
+
}
|
|
3374
3277
|
return {
|
|
3375
3278
|
typ: EnumToken.AtRuleNodeType,
|
|
3376
3279
|
val: renderTokens(stream, options),
|
|
3377
|
-
[
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
},
|
|
3280
|
+
[LOCSRCID]: atRule[LOCSRCID],
|
|
3281
|
+
[LOCSTA]: atRule[LOCSTA],
|
|
3282
|
+
[LOCEND]: stream.at(-1)?.[LOCEND] ?? atRule[LOCEND],
|
|
3381
3283
|
[TOKENS]: stream,
|
|
3382
3284
|
[STATE]: EnumAstNodeStatus.Invalid,
|
|
3383
3285
|
[ERRORS]: result.errors,
|
|
@@ -3392,10 +3294,9 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3392
3294
|
typ: EnumToken.CssVariableImportTokenType,
|
|
3393
3295
|
nam: nam.val,
|
|
3394
3296
|
val: value,
|
|
3395
|
-
[
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
},
|
|
3297
|
+
[LOCSRCID]: atRule[LOCSRCID],
|
|
3298
|
+
[LOCSTA]: atRule[LOCSTA],
|
|
3299
|
+
[LOCEND]: stream.at(-1)?.[LOCEND] ?? atRule[LOCEND],
|
|
3399
3300
|
[TOKENS]: stream,
|
|
3400
3301
|
[STATE]: EnumAstNodeStatus.Validated,
|
|
3401
3302
|
[ERRORS]: [],
|
|
@@ -3406,19 +3307,15 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3406
3307
|
typ: EnumToken.CssVariableTokenType,
|
|
3407
3308
|
nam: nam.val,
|
|
3408
3309
|
val: value,
|
|
3409
|
-
[
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
},
|
|
3310
|
+
[LOCSRCID]: atRule[LOCSRCID],
|
|
3311
|
+
[LOCSTA]: atRule[LOCSTA],
|
|
3312
|
+
[LOCEND]: stream.at(-1)?.[LOCEND] ?? atRule[LOCEND],
|
|
3413
3313
|
[TOKENS]: stream,
|
|
3414
3314
|
[STATE]: EnumAstNodeStatus.Validated,
|
|
3415
3315
|
[ERRORS]: [],
|
|
3416
3316
|
};
|
|
3417
3317
|
}
|
|
3418
|
-
atRule[
|
|
3419
|
-
...atRule[LOC],
|
|
3420
|
-
end: stream.at(-1)?.[LOC]?.end ?? atRule[LOC].end,
|
|
3421
|
-
};
|
|
3318
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3422
3319
|
atRule[STATE] = EnumAstNodeStatus.Validated;
|
|
3423
3320
|
atRule[ERRORS] = [];
|
|
3424
3321
|
// @ts-expect-error
|
|
@@ -3438,17 +3335,18 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3438
3335
|
// check or and and
|
|
3439
3336
|
result = matchGenericSyntax(stream, options);
|
|
3440
3337
|
if (result.errors.length > 0) {
|
|
3441
|
-
|
|
3338
|
+
for (const error of result.errors) {
|
|
3339
|
+
errors.push(error);
|
|
3340
|
+
}
|
|
3442
3341
|
}
|
|
3443
3342
|
}
|
|
3444
3343
|
else {
|
|
3445
3344
|
result = matchAtRuleSyntax(atRule, stream, options);
|
|
3446
3345
|
if (result.errors.length > 0) {
|
|
3447
|
-
|
|
3346
|
+
for (const error of result.errors) {
|
|
3347
|
+
errors.push(error);
|
|
3348
|
+
}
|
|
3448
3349
|
}
|
|
3449
|
-
// else if (atRuleName === "document") {
|
|
3450
|
-
// parseUrlToken(stream);
|
|
3451
|
-
// }
|
|
3452
3350
|
if (result.success) {
|
|
3453
3351
|
let i = 0;
|
|
3454
3352
|
const stack = [];
|
|
@@ -3459,7 +3357,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3459
3357
|
}
|
|
3460
3358
|
if (stream[i].typ === EnumToken.EndParensTokenType && stack.length > 0) {
|
|
3461
3359
|
const index = stream.indexOf(stack[stack.length - 1]);
|
|
3462
|
-
stream[index][
|
|
3360
|
+
stream[index][LOCEND] = stream[i][LOCEND];
|
|
3463
3361
|
Object.assign(stream[index], {
|
|
3464
3362
|
typ: tokensfuncDefMap.get(stream[index].typ),
|
|
3465
3363
|
chi: stream.splice(index + 1, i - index - 1),
|
|
@@ -3467,15 +3365,11 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3467
3365
|
i = index;
|
|
3468
3366
|
stream.splice(index + 1, 1);
|
|
3469
3367
|
stack.pop();
|
|
3470
|
-
// continue;
|
|
3471
3368
|
}
|
|
3472
3369
|
}
|
|
3473
3370
|
}
|
|
3474
3371
|
}
|
|
3475
|
-
atRule[
|
|
3476
|
-
...atRule[LOC],
|
|
3477
|
-
end: stream.at(-1)?.[LOC]?.end ?? atRule[LOC].end,
|
|
3478
|
-
};
|
|
3372
|
+
atRule[LOCEND] = stream.at(-1)?.[LOCEND] ?? atRule[LOCEND];
|
|
3479
3373
|
atRule[TOKENS] = stream.slice();
|
|
3480
3374
|
atRule[STATE] = result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid;
|
|
3481
3375
|
atRule[ERRORS] = result.errors;
|
|
@@ -3501,13 +3395,12 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) {
|
|
|
3501
3395
|
*/
|
|
3502
3396
|
async function parseDeclarations(declaration) {
|
|
3503
3397
|
const stream = `.x{${declaration}}`;
|
|
3504
|
-
return doParse(
|
|
3398
|
+
return doParse(new Tokenizer({
|
|
3505
3399
|
stream,
|
|
3506
|
-
buffer: "",
|
|
3507
3400
|
offset: 0,
|
|
3508
3401
|
position: 0,
|
|
3509
3402
|
source: new SourceFile(stream, [], ""),
|
|
3510
|
-
currentPosition:
|
|
3403
|
+
currentPosition: 0,
|
|
3511
3404
|
}), { setParent: false, minify: false, validation: false }).then((result) => {
|
|
3512
3405
|
return result.ast.chi[0].chi.filter((t) => t.typ == EnumToken.DeclarationNodeType || t.typ == EnumToken.CommentNodeType);
|
|
3513
3406
|
});
|
|
@@ -3535,21 +3428,61 @@ async function parseDeclarations(declaration) {
|
|
|
3535
3428
|
* ```
|
|
3536
3429
|
*/
|
|
3537
3430
|
function parseString(src, options = { parseColor: true }, errors) {
|
|
3538
|
-
const parseInfo = {
|
|
3431
|
+
// const parseInfo: ParseInfo = {
|
|
3432
|
+
// stream: src,
|
|
3433
|
+
// offset: 0,
|
|
3434
|
+
// time: 0,
|
|
3435
|
+
// source: new SourceFile(src, [], ""),
|
|
3436
|
+
// position: 0,
|
|
3437
|
+
// currentPosition: 0,
|
|
3438
|
+
// };
|
|
3439
|
+
const tokenizer = new Tokenizer({
|
|
3539
3440
|
stream: src,
|
|
3540
3441
|
buffer: "",
|
|
3442
|
+
src: options?.src ?? "",
|
|
3541
3443
|
offset: 0,
|
|
3542
3444
|
time: 0,
|
|
3543
|
-
source: new SourceFile(src, [], ""),
|
|
3445
|
+
source: new SourceFile(src, [], options?.src ?? ""),
|
|
3544
3446
|
position: 0,
|
|
3545
|
-
currentPosition:
|
|
3546
|
-
};
|
|
3547
|
-
const
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3447
|
+
currentPosition: 0,
|
|
3448
|
+
});
|
|
3449
|
+
const mapped = [];
|
|
3450
|
+
let token;
|
|
3451
|
+
while (!tokenizer.done()) {
|
|
3452
|
+
tokenizer.next();
|
|
3453
|
+
if (tokenizer.unit != null) {
|
|
3454
|
+
token = {
|
|
3455
|
+
typ: tokenizer.typ,
|
|
3456
|
+
val: tokenizer.val,
|
|
3457
|
+
unit: tokenizer.unit,
|
|
3458
|
+
};
|
|
3459
|
+
}
|
|
3460
|
+
else if (tokenizer.val === null) {
|
|
3461
|
+
token = {
|
|
3462
|
+
typ: tokenizer.typ,
|
|
3463
|
+
};
|
|
3464
|
+
}
|
|
3465
|
+
else if (tokenizer.kin != null) {
|
|
3466
|
+
token = {
|
|
3467
|
+
typ: tokenizer.typ,
|
|
3468
|
+
val: tokenizer.val,
|
|
3469
|
+
kin: tokenizer.kin,
|
|
3470
|
+
};
|
|
3471
|
+
}
|
|
3472
|
+
else {
|
|
3473
|
+
token = {
|
|
3474
|
+
typ: tokenizer.typ,
|
|
3475
|
+
val: tokenizer.val,
|
|
3476
|
+
};
|
|
3477
|
+
}
|
|
3478
|
+
token[LOCSRCID] = tokenizer.source.id;
|
|
3479
|
+
token[LOCEND] = tokenizer.end;
|
|
3480
|
+
token[LOCSTA] = tokenizer.sta;
|
|
3481
|
+
mapped.push(token);
|
|
3552
3482
|
}
|
|
3483
|
+
const result = parseTokens(mapped, options, errors);
|
|
3484
|
+
// remove EOF token
|
|
3485
|
+
result.splice(result.length - (result[result.length - 2]?.typ === EnumToken.WhitespaceTokenType ? 2 : 1), 2);
|
|
3553
3486
|
return result;
|
|
3554
3487
|
}
|
|
3555
3488
|
/**
|
|
@@ -3591,7 +3524,7 @@ function parseTokens(tokens, options, errors) {
|
|
|
3591
3524
|
val: (tokens[i - 1].typ === EnumToken.ColonTokenType ? ":" : "::") +
|
|
3592
3525
|
tokens[i].val,
|
|
3593
3526
|
});
|
|
3594
|
-
t[
|
|
3527
|
+
t[LOCEND] = tokens[i][LOCEND];
|
|
3595
3528
|
tokens.splice(i--, 1);
|
|
3596
3529
|
}
|
|
3597
3530
|
}
|
|
@@ -3610,7 +3543,7 @@ function parseTokens(tokens, options, errors) {
|
|
|
3610
3543
|
action: "drop",
|
|
3611
3544
|
message: `Unbalanced token ')'`,
|
|
3612
3545
|
node,
|
|
3613
|
-
location: options.source.getSourceLocation(node[
|
|
3546
|
+
location: options.source.getSourceLocation(node[LOCSTA]),
|
|
3614
3547
|
});
|
|
3615
3548
|
// return [];
|
|
3616
3549
|
continue;
|
|
@@ -3638,14 +3571,13 @@ function parseTokens(tokens, options, errors) {
|
|
|
3638
3571
|
action: "drop",
|
|
3639
3572
|
message: `Unbalanced token ']'`,
|
|
3640
3573
|
node,
|
|
3641
|
-
location: options.source.getSourceLocation(node[
|
|
3574
|
+
location: options.source.getSourceLocation(node[LOCSTA]),
|
|
3642
3575
|
});
|
|
3643
|
-
// return [];
|
|
3644
3576
|
continue;
|
|
3645
3577
|
}
|
|
3646
3578
|
index = tokens.indexOf(stack.at(-1));
|
|
3647
3579
|
const attr = stack.at(-1);
|
|
3648
|
-
attr[
|
|
3580
|
+
attr[LOCEND] = t[LOCEND];
|
|
3649
3581
|
tokens.splice(i, 1);
|
|
3650
3582
|
Object.assign(attr, {
|
|
3651
3583
|
typ: EnumToken.AttrTokenType,
|
|
@@ -3761,9 +3693,8 @@ function parseTokens(tokens, options, errors) {
|
|
|
3761
3693
|
action: "drop",
|
|
3762
3694
|
message: `Unbalanced token. Expecting ${node.typ === EnumToken.AttrStartTokenType ? "']'" : ")"}'`,
|
|
3763
3695
|
node,
|
|
3764
|
-
location: options.source.getSourceLocation(node[
|
|
3696
|
+
location: options.source.getSourceLocation(node[LOCSTA]),
|
|
3765
3697
|
});
|
|
3766
|
-
// return [];
|
|
3767
3698
|
}
|
|
3768
3699
|
return tokens;
|
|
3769
3700
|
}
|