@markuplint/types 4.8.1 → 5.0.0-alpha.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/ARCHITECTURE.ja.md +256 -0
- package/ARCHITECTURE.md +256 -0
- package/CHANGELOG.md +18 -2
- package/README.md +37 -2
- package/SKILL.md +100 -0
- package/docs/check-pipeline.ja.md +494 -0
- package/docs/check-pipeline.md +494 -0
- package/docs/token-system.ja.md +584 -0
- package/docs/token-system.md +584 -0
- package/docs/type-system.ja.md +556 -0
- package/docs/type-system.md +556 -0
- package/docs/validators.ja.md +524 -0
- package/docs/validators.md +524 -0
- package/lib/check-base.d.ts +81 -1
- package/lib/check-base.js +87 -0
- package/lib/check-multi-types.d.ts +12 -1
- package/lib/check-multi-types.js +11 -0
- package/lib/check.d.ts +13 -0
- package/lib/check.js +13 -0
- package/lib/css-defs.d.ts +8 -0
- package/lib/css-defs.js +8 -0
- package/lib/css-overrides.d.ts +7 -0
- package/lib/css-overrides.js +7 -0
- package/lib/css-syntax.d.ts +11 -0
- package/lib/css-syntax.js +12 -1
- package/lib/css-tokenizers.d.ts +6 -0
- package/lib/css-tokenizers.js +6 -0
- package/lib/debug.d.ts +3 -0
- package/lib/debug.js +3 -0
- package/lib/defs.d.ts +8 -0
- package/lib/defs.js +26 -0
- package/lib/directive.d.ts +14 -0
- package/lib/directive.js +14 -0
- package/lib/enum.d.ts +11 -0
- package/lib/enum.js +11 -0
- package/lib/get-candidate.d.ts +11 -0
- package/lib/get-candidate.js +11 -0
- package/lib/index.d.ts +10 -1
- package/lib/index.js +9 -1
- package/lib/keyword-type.d.ts +13 -0
- package/lib/keyword-type.js +13 -0
- package/lib/list.d.ts +13 -0
- package/lib/list.js +13 -0
- package/lib/match-result.d.ts +22 -1
- package/lib/match-result.js +21 -0
- package/lib/number.d.ts +12 -0
- package/lib/number.js +12 -0
- package/lib/pattern.d.ts +12 -0
- package/lib/pattern.js +33 -0
- package/lib/primitive/is-float.d.ts +4 -1
- package/lib/primitive/is-float.js +4 -1
- package/lib/primitive/is-int.d.ts +4 -1
- package/lib/primitive/is-int.js +4 -1
- package/lib/primitive/is-non-zero-uint.d.ts +3 -2
- package/lib/primitive/is-non-zero-uint.js +3 -2
- package/lib/primitive/is-quantity.d.ts +5 -3
- package/lib/primitive/is-quantity.js +5 -3
- package/lib/primitive/is-uint.d.ts +5 -1
- package/lib/primitive/is-uint.js +5 -1
- package/lib/primitive/range.d.ts +5 -4
- package/lib/primitive/range.js +5 -4
- package/lib/primitive/split-unit.d.ts +3 -2
- package/lib/primitive/split-unit.js +3 -2
- package/lib/rfc/is-bcp-47.d.ts +2 -0
- package/lib/rfc/is-bcp-47.js +4 -2
- package/lib/token/token-collection.d.ts +108 -3
- package/lib/token/token-collection.js +110 -3
- package/lib/token/token.d.ts +66 -2
- package/lib/token/token.js +89 -21
- package/lib/token/types.d.ts +9 -0
- package/lib/types.d.ts +108 -1
- package/lib/types.schema.d.ts +4 -1
- package/lib/w3c/check-serialized-permissions-policy.d.ts +2 -0
- package/lib/w3c/check-serialized-permissions-policy.js +2 -0
- package/lib/whatwg/check-autocomplete.d.ts +10 -0
- package/lib/whatwg/check-autocomplete.js +214 -159
- package/lib/whatwg/check-datetime/date-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/date-string.js +2 -0
- package/lib/whatwg/check-datetime/datetime-tokens.d.ts +13 -0
- package/lib/whatwg/check-datetime/datetime-tokens.js +14 -1
- package/lib/whatwg/check-datetime/duration-string.d.ts +7 -0
- package/lib/whatwg/check-datetime/duration-string.js +7 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/global-date-and-time-string.js +2 -0
- package/lib/whatwg/check-datetime/index.d.ts +5 -0
- package/lib/whatwg/check-datetime/index.js +5 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.d.ts +4 -0
- package/lib/whatwg/check-datetime/local-date-and-time-string.js +4 -0
- package/lib/whatwg/check-datetime/month-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/month-string.js +2 -0
- package/lib/whatwg/check-datetime/time-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/time-string.js +2 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.d.ts +8 -0
- package/lib/whatwg/check-datetime/time-zone-offset-string.js +8 -0
- package/lib/whatwg/check-datetime/week-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/week-string.js +2 -0
- package/lib/whatwg/check-datetime/year-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/year-string.js +2 -0
- package/lib/whatwg/check-datetime/yearless-date-string.d.ts +2 -0
- package/lib/whatwg/check-datetime/yearless-date-string.js +2 -0
- package/lib/whatwg/check-link-type.d.ts +108 -1
- package/lib/whatwg/check-link-type.js +17 -7
- package/lib/whatwg/check-mime-type.d.ts +4 -1
- package/lib/whatwg/check-mime-type.js +6 -3
- package/lib/whatwg/is-abs-url.d.ts +2 -0
- package/lib/whatwg/is-abs-url.js +3 -4
- package/lib/whatwg/is-browser-context-name.d.ts +2 -2
- package/lib/whatwg/is-browser-context-name.js +2 -2
- package/lib/whatwg/is-custom-element-name.d.ts +1 -1
- package/lib/whatwg/is-custom-element-name.js +1 -1
- package/lib/whatwg/is-itemprop-name.d.ts +1 -0
- package/lib/whatwg/is-itemprop-name.js +1 -0
- package/lib/whatwg/is-navigable-target-name.d.ts +2 -0
- package/lib/whatwg/is-navigable-target-name.js +2 -0
- package/package.json +8 -6
- package/types.schema.json +10 -1
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { matched, unmatched } from '../match-result.js';
|
|
2
2
|
import { Token } from './token.js';
|
|
3
|
+
/**
|
|
4
|
+
* An ordered collection of tokens parsed from a string value.
|
|
5
|
+
*
|
|
6
|
+
* Extends `Array<Token>` with parsing, validation, and query capabilities.
|
|
7
|
+
* Supports configurable separators (space, comma, or custom), uniqueness
|
|
8
|
+
* constraints, ordering checks, and case sensitivity options.
|
|
9
|
+
*/
|
|
3
10
|
export class TokenCollection extends Array {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a TokenCollection by matching a value against a sequence of regex patterns.
|
|
13
|
+
*
|
|
14
|
+
* @param value - The source token or string to parse
|
|
15
|
+
* @param patterns - The regex patterns to match sequentially
|
|
16
|
+
* @param typeOptions - Optional collection configuration
|
|
17
|
+
* @returns A new TokenCollection containing the matched tokens
|
|
18
|
+
*/
|
|
4
19
|
static fromPatterns(value, patterns,
|
|
5
20
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
6
21
|
typeOptions) {
|
|
@@ -15,7 +30,7 @@ export class TokenCollection extends Array {
|
|
|
15
30
|
cumulativeOffset += tokenValue.length;
|
|
16
31
|
return token;
|
|
17
32
|
}
|
|
18
|
-
let isBroken
|
|
33
|
+
let isBroken;
|
|
19
34
|
do {
|
|
20
35
|
isBroken = false;
|
|
21
36
|
for (const pattern of patterns) {
|
|
@@ -49,6 +64,13 @@ export class TokenCollection extends Array {
|
|
|
49
64
|
static get [Symbol.species]() {
|
|
50
65
|
return Array;
|
|
51
66
|
}
|
|
67
|
+
allowEmpty;
|
|
68
|
+
caseInsensitive;
|
|
69
|
+
disallowToSurroundBySpaces;
|
|
70
|
+
number;
|
|
71
|
+
ordered;
|
|
72
|
+
separator;
|
|
73
|
+
unique;
|
|
52
74
|
constructor(value,
|
|
53
75
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
54
76
|
typeOptions) {
|
|
@@ -107,10 +129,23 @@ export class TokenCollection extends Array {
|
|
|
107
129
|
offset += v.length;
|
|
108
130
|
}
|
|
109
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* The concatenated string value of all tokens in this collection.
|
|
134
|
+
*/
|
|
110
135
|
get value() {
|
|
111
136
|
const value = this.map(t => t.value).join('');
|
|
112
137
|
return value;
|
|
113
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Validates the token collection structure against its configuration.
|
|
141
|
+
*
|
|
142
|
+
* Checks for unexpected spaces, consecutive commas, empty tokens,
|
|
143
|
+
* and duplicate values based on the collection's separator, allowEmpty,
|
|
144
|
+
* and unique settings.
|
|
145
|
+
*
|
|
146
|
+
* @param options - Optional validation settings including expected values and reference URL
|
|
147
|
+
* @returns The validation result
|
|
148
|
+
*/
|
|
114
149
|
check(options = {}) {
|
|
115
150
|
const { expects, ref } = options;
|
|
116
151
|
if (this.separator !== 'space' && this.disallowToSurroundBySpaces) {
|
|
@@ -187,6 +222,12 @@ export class TokenCollection extends Array {
|
|
|
187
222
|
}
|
|
188
223
|
return matched();
|
|
189
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* Splits this collection into chunks of the specified size.
|
|
227
|
+
*
|
|
228
|
+
* @param split - The number of tokens per chunk
|
|
229
|
+
* @returns An array of TokenCollection chunks
|
|
230
|
+
*/
|
|
190
231
|
chunk(split) {
|
|
191
232
|
const chunks = [];
|
|
192
233
|
const tokens = this.slice();
|
|
@@ -197,6 +238,12 @@ export class TokenCollection extends Array {
|
|
|
197
238
|
}
|
|
198
239
|
return chunks;
|
|
199
240
|
}
|
|
241
|
+
/**
|
|
242
|
+
* Iterates over consecutive token pairs, calling the callback for each pair.
|
|
243
|
+
*
|
|
244
|
+
* @param callback - A function receiving the previous and current token; return a token to stop iteration
|
|
245
|
+
* @returns The token returned by the callback, or `null` if iteration completes
|
|
246
|
+
*/
|
|
200
247
|
compareTokens(callback) {
|
|
201
248
|
const _tokens = this.slice();
|
|
202
249
|
let prev = _tokens.shift();
|
|
@@ -213,6 +260,12 @@ export class TokenCollection extends Array {
|
|
|
213
260
|
}
|
|
214
261
|
return null;
|
|
215
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Splits this collection into two at the given position.
|
|
265
|
+
*
|
|
266
|
+
* @param position - The index at which to split
|
|
267
|
+
* @returns A tuple of two TokenCollections: before and after the position
|
|
268
|
+
*/
|
|
216
269
|
divide(position) {
|
|
217
270
|
const _a = this.slice(0, position);
|
|
218
271
|
const _b = this.slice(position);
|
|
@@ -220,6 +273,15 @@ export class TokenCollection extends Array {
|
|
|
220
273
|
const b = TokenCollection._new(_b, this);
|
|
221
274
|
return [a, b];
|
|
222
275
|
}
|
|
276
|
+
/**
|
|
277
|
+
* Applies a sequence of check callbacks to consecutive tokens.
|
|
278
|
+
*
|
|
279
|
+
* Each callback receives the current head token and remaining tail.
|
|
280
|
+
* Tracks pass count and cumulative offset for error reporting.
|
|
281
|
+
*
|
|
282
|
+
* @param callbacks - The check functions to apply sequentially
|
|
283
|
+
* @returns The validation result
|
|
284
|
+
*/
|
|
223
285
|
eachCheck(...callbacks) {
|
|
224
286
|
let headAndTail = this.headAndTail();
|
|
225
287
|
let head = headAndTail.head;
|
|
@@ -286,9 +348,21 @@ export class TokenCollection extends Array {
|
|
|
286
348
|
}
|
|
287
349
|
return matched();
|
|
288
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Creates a new TokenCollection containing only tokens that pass the test.
|
|
353
|
+
*
|
|
354
|
+
* @param callback - The filter predicate function
|
|
355
|
+
* @returns A new TokenCollection with the filtered tokens
|
|
356
|
+
*/
|
|
289
357
|
filter(callback) {
|
|
290
358
|
return TokenCollection._new(super.filter(callback), this);
|
|
291
359
|
}
|
|
360
|
+
/**
|
|
361
|
+
* Finds the first occurrence of two consecutive tokens of the same type.
|
|
362
|
+
*
|
|
363
|
+
* @param tokenType - The token type number to check for consecutive occurrences
|
|
364
|
+
* @returns The second consecutive token, or `null` if none found
|
|
365
|
+
*/
|
|
292
366
|
getConsecutiveToken(tokenType) {
|
|
293
367
|
const resultToken = this.compareTokens((prev, current) => {
|
|
294
368
|
if (prev.type === tokenType && current.type === tokenType) {
|
|
@@ -297,6 +371,13 @@ export class TokenCollection extends Array {
|
|
|
297
371
|
});
|
|
298
372
|
return resultToken ?? null;
|
|
299
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Finds the first duplicated token in this collection.
|
|
376
|
+
*
|
|
377
|
+
* Comparison respects the `caseInsensitive` setting of this collection.
|
|
378
|
+
*
|
|
379
|
+
* @returns The duplicated token, or `null` if all tokens are unique
|
|
380
|
+
*/
|
|
300
381
|
getDuplicated() {
|
|
301
382
|
const aList = this.slice();
|
|
302
383
|
const bList = this.slice();
|
|
@@ -318,16 +399,28 @@ export class TokenCollection extends Array {
|
|
|
318
399
|
}
|
|
319
400
|
return null;
|
|
320
401
|
}
|
|
402
|
+
/**
|
|
403
|
+
* Returns only the identifier tokens, excluding whitespace and separators.
|
|
404
|
+
*
|
|
405
|
+
* @returns A new TokenCollection containing only Ident-type tokens
|
|
406
|
+
*/
|
|
321
407
|
getIdentTokens() {
|
|
322
408
|
return this.filter(token => token.type === Token.Ident);
|
|
323
409
|
}
|
|
324
410
|
/**
|
|
411
|
+
* Checks whether any token in this collection matches the given value.
|
|
325
412
|
*
|
|
326
|
-
* @param value The token value
|
|
413
|
+
* @param value - The token value, type number, regex, or array to check against
|
|
414
|
+
* @returns Whether any token matches
|
|
327
415
|
*/
|
|
328
416
|
has(value) {
|
|
329
417
|
return this.some(t => t.matches(value));
|
|
330
418
|
}
|
|
419
|
+
/**
|
|
420
|
+
* Splits this collection into a head token and a tail collection.
|
|
421
|
+
*
|
|
422
|
+
* @returns An object with `head` (first token or null) and `tail` (remaining tokens)
|
|
423
|
+
*/
|
|
331
424
|
headAndTail() {
|
|
332
425
|
const copy = this.slice();
|
|
333
426
|
const head = copy.shift();
|
|
@@ -338,8 +431,10 @@ export class TokenCollection extends Array {
|
|
|
338
431
|
return { head, tail };
|
|
339
432
|
}
|
|
340
433
|
/**
|
|
434
|
+
* Searches for the first token that includes the given value.
|
|
341
435
|
*
|
|
342
|
-
* @param value The token value
|
|
436
|
+
* @param value - The token value, type number, regex, or array to search for
|
|
437
|
+
* @returns The first matching token, or `null` if not found
|
|
343
438
|
*/
|
|
344
439
|
search(value) {
|
|
345
440
|
for (const token of this) {
|
|
@@ -349,6 +444,13 @@ export class TokenCollection extends Array {
|
|
|
349
444
|
}
|
|
350
445
|
return null;
|
|
351
446
|
}
|
|
447
|
+
/**
|
|
448
|
+
* Verifies that tokens alternate between the specified types in order.
|
|
449
|
+
*
|
|
450
|
+
* @param tokenNumbers - The expected repeating pattern of token types
|
|
451
|
+
* @param lastTokenNumber - Optional expected type for the last token
|
|
452
|
+
* @returns An error object if the pattern is violated, or `null` if valid
|
|
453
|
+
*/
|
|
352
454
|
takeTurns(tokenNumbers, lastTokenNumber) {
|
|
353
455
|
const tokens = this.slice();
|
|
354
456
|
for (let i = 0; i < tokens.length; i++) {
|
|
@@ -374,6 +476,11 @@ export class TokenCollection extends Array {
|
|
|
374
476
|
}
|
|
375
477
|
return null;
|
|
376
478
|
}
|
|
479
|
+
/**
|
|
480
|
+
* Converts all tokens in this collection to plain JSON-serializable objects.
|
|
481
|
+
*
|
|
482
|
+
* @returns An array of plain token objects
|
|
483
|
+
*/
|
|
377
484
|
toJSON() {
|
|
378
485
|
return this.map(t => t.toJSON());
|
|
379
486
|
}
|
package/lib/token/token.d.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
import type { TokenValue } from './types.js';
|
|
2
2
|
import type { UnmatchedResult, UnmatchedResultOptions, UnmatchedResultReason } from '../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Represents a single token within a parsed string value.
|
|
5
|
+
*
|
|
6
|
+
* Tracks the token's value, type (whitespace, comma, or identifier),
|
|
7
|
+
* offset position within the original string, and provides methods
|
|
8
|
+
* for matching and comparison operations.
|
|
9
|
+
*/
|
|
3
10
|
export declare class Token {
|
|
4
11
|
/**
|
|
5
12
|
* @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
|
|
@@ -26,11 +33,32 @@ export declare class Token {
|
|
|
26
33
|
* @deprecated Use {@link getPosition} instead. Will be removed in v5.0.0.
|
|
27
34
|
*/
|
|
28
35
|
static getLine(value: string, offset: number): number;
|
|
36
|
+
/**
|
|
37
|
+
* Calculates the line and column position at the given offset within a string.
|
|
38
|
+
*
|
|
39
|
+
* @param value - The source string
|
|
40
|
+
* @param offset - The character offset to calculate the position for
|
|
41
|
+
* @returns The line number (1-based) and column number (1-based)
|
|
42
|
+
*/
|
|
29
43
|
static getPosition(value: string, offset: number): {
|
|
30
44
|
line: number;
|
|
31
45
|
column: number;
|
|
32
46
|
};
|
|
47
|
+
/**
|
|
48
|
+
* Determines the token type based on the first character.
|
|
49
|
+
*
|
|
50
|
+
* @param value - The token string value
|
|
51
|
+
* @param separators - Optional separator characters to detect
|
|
52
|
+
* @returns The token type number (WhiteSpace, Comma, or Ident)
|
|
53
|
+
*/
|
|
33
54
|
static getType(value: string, separators?: readonly string[]): 1 | 18 | 13;
|
|
55
|
+
/**
|
|
56
|
+
* Calculates a new position by shifting from a token's offset.
|
|
57
|
+
*
|
|
58
|
+
* @param token - The base token to shift from
|
|
59
|
+
* @param offset - The additional offset to apply
|
|
60
|
+
* @returns The shifted offset, line, and column
|
|
61
|
+
*/
|
|
34
62
|
static shiftLocation(token: Readonly<Token>, offset: number): {
|
|
35
63
|
offset: number;
|
|
36
64
|
line: number;
|
|
@@ -40,25 +68,61 @@ export declare class Token {
|
|
|
40
68
|
readonly originalValue: string;
|
|
41
69
|
readonly type: number;
|
|
42
70
|
readonly value: string;
|
|
71
|
+
/**
|
|
72
|
+
* @param value - The token string value
|
|
73
|
+
* @param offset - The offset position within the original string
|
|
74
|
+
* @param originalValue - The complete original string this token was parsed from
|
|
75
|
+
* @param separators - Optional separator characters used for type detection
|
|
76
|
+
*/
|
|
43
77
|
constructor(value: string, offset: number, originalValue: string, separators?: readonly string[]);
|
|
78
|
+
/**
|
|
79
|
+
* The character length of the token value.
|
|
80
|
+
*/
|
|
44
81
|
get length(): number;
|
|
82
|
+
/**
|
|
83
|
+
* Creates a copy of this token.
|
|
84
|
+
*
|
|
85
|
+
* @returns A new Token instance with the same value, offset, and original value
|
|
86
|
+
*/
|
|
45
87
|
clone(): Token;
|
|
46
88
|
/**
|
|
89
|
+
* Checks whether this token's value contains the given value.
|
|
47
90
|
*
|
|
48
|
-
* @param value The token value
|
|
91
|
+
* @param value - The token value, type number, regex, or array to check against
|
|
92
|
+
* @param caseInsensitive - Whether to perform case-insensitive comparison
|
|
93
|
+
* @returns Whether this token includes the given value
|
|
49
94
|
*/
|
|
50
95
|
includes(value: TokenValue, caseInsensitive?: boolean): boolean;
|
|
51
96
|
/**
|
|
97
|
+
* Checks whether this token exactly matches the given value.
|
|
52
98
|
*
|
|
53
|
-
* @param value The token value
|
|
99
|
+
* @param value - The token value, type number, regex, or array to match against
|
|
100
|
+
* @param caseInsensitive - Whether to perform case-insensitive comparison
|
|
101
|
+
* @returns Whether this token matches the given value
|
|
54
102
|
*/
|
|
55
103
|
matches(value: TokenValue, caseInsensitive?: boolean): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Converts this token to a plain JSON-serializable object.
|
|
106
|
+
*
|
|
107
|
+
* @returns An object with type, value, and offset properties
|
|
108
|
+
*/
|
|
56
109
|
toJSON(): {
|
|
57
110
|
type: number;
|
|
58
111
|
value: string;
|
|
59
112
|
offset: number;
|
|
60
113
|
};
|
|
114
|
+
/**
|
|
115
|
+
* Parses the token value as a floating-point number.
|
|
116
|
+
*
|
|
117
|
+
* @returns The parsed number, or 0 if parsing fails
|
|
118
|
+
*/
|
|
61
119
|
toNumber(): number;
|
|
120
|
+
/**
|
|
121
|
+
* Creates an unmatched result positioned at this token's location.
|
|
122
|
+
*
|
|
123
|
+
* @param options - Optional settings including ref, reason, and expected values
|
|
124
|
+
* @returns An unmatched result with this token's position information
|
|
125
|
+
*/
|
|
62
126
|
unmatched(options?: UnmatchedResultOptions & {
|
|
63
127
|
readonly ref?: string;
|
|
64
128
|
readonly reason?: UnmatchedResultReason;
|
package/lib/token/token.js
CHANGED
|
@@ -1,4 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a single token within a parsed string value.
|
|
3
|
+
*
|
|
4
|
+
* Tracks the token's value, type (whitespace, comma, or identifier),
|
|
5
|
+
* offset position within the original string, and provides methods
|
|
6
|
+
* for matching and comparison operations.
|
|
7
|
+
*/
|
|
1
8
|
export class Token {
|
|
9
|
+
/**
|
|
10
|
+
* @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
|
|
11
|
+
*/
|
|
12
|
+
static Comma = 18;
|
|
13
|
+
static Ident = 1;
|
|
14
|
+
static WhiteSpace = 13;
|
|
15
|
+
/**
|
|
16
|
+
* ASCII whitespace is
|
|
17
|
+
* - U+0009 TAB
|
|
18
|
+
* - U+000A LF
|
|
19
|
+
* - U+000C FF
|
|
20
|
+
* - U+000D CR
|
|
21
|
+
* - U+0020 SPACE.
|
|
22
|
+
*
|
|
23
|
+
* @see https://infra.spec.whatwg.org/#ascii-whitespace
|
|
24
|
+
*/
|
|
25
|
+
static whitespace = ['\u0009', '\u000A', '\u000C', '\u000D', '\u0020'];
|
|
2
26
|
/**
|
|
3
27
|
* @deprecated Use {@link getPosition} instead. Will be removed in v5.0.0.
|
|
4
28
|
*/
|
|
@@ -12,12 +36,26 @@ export class Token {
|
|
|
12
36
|
static getLine(value, offset) {
|
|
13
37
|
return value.slice(0, offset).split(/\n/).length;
|
|
14
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Calculates the line and column position at the given offset within a string.
|
|
41
|
+
*
|
|
42
|
+
* @param value - The source string
|
|
43
|
+
* @param offset - The character offset to calculate the position for
|
|
44
|
+
* @returns The line number (1-based) and column number (1-based)
|
|
45
|
+
*/
|
|
15
46
|
static getPosition(value, offset) {
|
|
16
47
|
const lines = value.slice(0, offset).split(/\n/);
|
|
17
48
|
const line = lines.length;
|
|
18
49
|
const column = (lines.at(-1) ?? '').length + 1;
|
|
19
50
|
return { line, column };
|
|
20
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Determines the token type based on the first character.
|
|
54
|
+
*
|
|
55
|
+
* @param value - The token string value
|
|
56
|
+
* @param separators - Optional separator characters to detect
|
|
57
|
+
* @returns The token type number (WhiteSpace, Comma, or Ident)
|
|
58
|
+
*/
|
|
21
59
|
static getType(value, separators) {
|
|
22
60
|
if (Token.whitespace.includes(value[0] ?? '')) {
|
|
23
61
|
return Token.WhiteSpace;
|
|
@@ -31,6 +69,13 @@ export class Token {
|
|
|
31
69
|
}
|
|
32
70
|
return Token.Ident;
|
|
33
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Calculates a new position by shifting from a token's offset.
|
|
74
|
+
*
|
|
75
|
+
* @param token - The base token to shift from
|
|
76
|
+
* @param offset - The additional offset to apply
|
|
77
|
+
* @returns The shifted offset, line, and column
|
|
78
|
+
*/
|
|
34
79
|
static shiftLocation(token, offset) {
|
|
35
80
|
const shifted = token.offset + offset;
|
|
36
81
|
const { line, column } = Token.getPosition(token.originalValue, shifted);
|
|
@@ -40,25 +85,46 @@ export class Token {
|
|
|
40
85
|
column,
|
|
41
86
|
};
|
|
42
87
|
}
|
|
88
|
+
offset;
|
|
89
|
+
originalValue;
|
|
90
|
+
type;
|
|
91
|
+
value;
|
|
92
|
+
/**
|
|
93
|
+
* @param value - The token string value
|
|
94
|
+
* @param offset - The offset position within the original string
|
|
95
|
+
* @param originalValue - The complete original string this token was parsed from
|
|
96
|
+
* @param separators - Optional separator characters used for type detection
|
|
97
|
+
*/
|
|
43
98
|
constructor(value, offset, originalValue, separators) {
|
|
44
99
|
this.type = Token.getType(value, separators);
|
|
45
100
|
this.value = value;
|
|
46
101
|
this.offset = offset;
|
|
47
102
|
this.originalValue = originalValue;
|
|
48
103
|
}
|
|
104
|
+
/**
|
|
105
|
+
* The character length of the token value.
|
|
106
|
+
*/
|
|
49
107
|
get length() {
|
|
50
108
|
return this.value.length;
|
|
51
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Creates a copy of this token.
|
|
112
|
+
*
|
|
113
|
+
* @returns A new Token instance with the same value, offset, and original value
|
|
114
|
+
*/
|
|
52
115
|
clone() {
|
|
53
116
|
return new Token(this.value, this.offset, this.originalValue);
|
|
54
117
|
}
|
|
55
118
|
/**
|
|
119
|
+
* Checks whether this token's value contains the given value.
|
|
56
120
|
*
|
|
57
|
-
* @param value The token value
|
|
121
|
+
* @param value - The token value, type number, regex, or array to check against
|
|
122
|
+
* @param caseInsensitive - Whether to perform case-insensitive comparison
|
|
123
|
+
* @returns Whether this token includes the given value
|
|
58
124
|
*/
|
|
59
125
|
includes(value, caseInsensitive) {
|
|
60
126
|
if (Array.isArray(value)) {
|
|
61
|
-
return value.some(v => this.includes(v));
|
|
127
|
+
return value.some(v => this.includes(v, caseInsensitive));
|
|
62
128
|
}
|
|
63
129
|
if (typeof value === 'string') {
|
|
64
130
|
const a = caseInsensitive ? this.value.toLowerCase() : this.value;
|
|
@@ -72,12 +138,15 @@ export class Token {
|
|
|
72
138
|
return this.type === value;
|
|
73
139
|
}
|
|
74
140
|
/**
|
|
141
|
+
* Checks whether this token exactly matches the given value.
|
|
75
142
|
*
|
|
76
|
-
* @param value The token value
|
|
143
|
+
* @param value - The token value, type number, regex, or array to match against
|
|
144
|
+
* @param caseInsensitive - Whether to perform case-insensitive comparison
|
|
145
|
+
* @returns Whether this token matches the given value
|
|
77
146
|
*/
|
|
78
147
|
matches(value, caseInsensitive) {
|
|
79
148
|
if (Array.isArray(value)) {
|
|
80
|
-
return value.some(v => this.matches(v));
|
|
149
|
+
return value.some(v => this.matches(v, caseInsensitive));
|
|
81
150
|
}
|
|
82
151
|
if (typeof value === 'string') {
|
|
83
152
|
const a = caseInsensitive ? this.value.toLowerCase() : this.value;
|
|
@@ -90,6 +159,11 @@ export class Token {
|
|
|
90
159
|
}
|
|
91
160
|
return this.type === value;
|
|
92
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Converts this token to a plain JSON-serializable object.
|
|
164
|
+
*
|
|
165
|
+
* @returns An object with type, value, and offset properties
|
|
166
|
+
*/
|
|
93
167
|
toJSON() {
|
|
94
168
|
return {
|
|
95
169
|
type: this.type,
|
|
@@ -97,10 +171,21 @@ export class Token {
|
|
|
97
171
|
offset: this.offset,
|
|
98
172
|
};
|
|
99
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Parses the token value as a floating-point number.
|
|
176
|
+
*
|
|
177
|
+
* @returns The parsed number, or 0 if parsing fails
|
|
178
|
+
*/
|
|
100
179
|
toNumber() {
|
|
101
180
|
const num = Number.parseFloat(this.value);
|
|
102
181
|
return Number.isNaN(num) ? 0 : num;
|
|
103
182
|
}
|
|
183
|
+
/**
|
|
184
|
+
* Creates an unmatched result positioned at this token's location.
|
|
185
|
+
*
|
|
186
|
+
* @param options - Optional settings including ref, reason, and expected values
|
|
187
|
+
* @returns An unmatched result with this token's position information
|
|
188
|
+
*/
|
|
104
189
|
unmatched(options) {
|
|
105
190
|
const { line, column } = Token.getPosition(this.originalValue, this.offset);
|
|
106
191
|
return {
|
|
@@ -116,20 +201,3 @@ export class Token {
|
|
|
116
201
|
};
|
|
117
202
|
}
|
|
118
203
|
}
|
|
119
|
-
/**
|
|
120
|
-
* @see https://github.com/csstree/csstree/blob/master/lib/tokenizer/types.js
|
|
121
|
-
*/
|
|
122
|
-
Token.Comma = 18;
|
|
123
|
-
Token.Ident = 1;
|
|
124
|
-
Token.WhiteSpace = 13;
|
|
125
|
-
/**
|
|
126
|
-
* ASCII whitespace is
|
|
127
|
-
* - U+0009 TAB
|
|
128
|
-
* - U+000A LF
|
|
129
|
-
* - U+000C FF
|
|
130
|
-
* - U+000D CR
|
|
131
|
-
* - U+0020 SPACE.
|
|
132
|
-
*
|
|
133
|
-
* @see https://infra.spec.whatwg.org/#ascii-whitespace
|
|
134
|
-
*/
|
|
135
|
-
Token.whitespace = ['\u0009', '\u000A', '\u000C', '\u000D', '\u0020'];
|
package/lib/token/types.d.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single token value: a literal string, a regular expression pattern, or a token type number.
|
|
3
|
+
*/
|
|
1
4
|
export type TokenValueScalar = string | Readonly<RegExp> | number;
|
|
5
|
+
/**
|
|
6
|
+
* An array of token values for matching against multiple patterns.
|
|
7
|
+
*/
|
|
2
8
|
export type TokenValueArray = readonly TokenValue[];
|
|
9
|
+
/**
|
|
10
|
+
* A token value used for matching: either a scalar or an array of token values.
|
|
11
|
+
*/
|
|
3
12
|
export type TokenValue = TokenValueScalar | TokenValueArray;
|