@eslint/css-tree 3.2.0 → 3.3.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/dist/version.cjs CHANGED
@@ -1 +1 @@
1
- module.exports = "3.2.0";
1
+ module.exports = "3.3.0";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "3.2.0";
1
+ export const version = "3.3.0";
package/lib/index.d.ts CHANGED
@@ -996,6 +996,121 @@ export const tokenNames: ReadonlyArray<string>;
996
996
  */
997
997
  export type CssTokenizerCallback = (token: number, start: number, end: number) => void;
998
998
 
999
+ /**
1000
+ * Represents the API for iterating over tokens in a CSS source string.
1001
+ */
1002
+ export interface TokenIterateAPI {
1003
+ /**
1004
+ * The name of the file being parsed.
1005
+ */
1006
+ filename: string;
1007
+
1008
+ /**
1009
+ * The CSS source string being tokenized.
1010
+ */
1011
+ source: string;
1012
+
1013
+ /**
1014
+ * The total number of tokens in the stream.
1015
+ */
1016
+ tokenCount: number;
1017
+
1018
+ /**
1019
+ * Gets the type of the token at the specified index.
1020
+ *
1021
+ * @param index - The index of the token.
1022
+ * @returns The numeric type of the token.
1023
+ */
1024
+ getTokenType(index: number): number;
1025
+
1026
+ /**
1027
+ * Gets the name of the token type at the specified index.
1028
+ *
1029
+ * @param index - The index of the token.
1030
+ * @returns The string name of the token type.
1031
+ */
1032
+ getTokenTypeName(index: number): string;
1033
+
1034
+ /**
1035
+ * Gets the start position of the token at the specified index.
1036
+ *
1037
+ * @param index - The index of the token.
1038
+ * @returns The starting character position of the token.
1039
+ */
1040
+ getTokenStart(index: number): number;
1041
+
1042
+ /**
1043
+ * Gets the end position of the token at the specified index.
1044
+ *
1045
+ * @param index - The index of the token.
1046
+ * @returns The ending character position of the token.
1047
+ */
1048
+ getTokenEnd(index: number): number;
1049
+
1050
+ /**
1051
+ * Gets the value of the token at the specified index.
1052
+ *
1053
+ * @param index - The index of the token.
1054
+ * @returns The string value of the token.
1055
+ */
1056
+ getTokenValue(index: number): string;
1057
+
1058
+ /**
1059
+ * Gets a substring from the source string.
1060
+ *
1061
+ * @param start - The starting index.
1062
+ * @param end - The ending index.
1063
+ * @returns The substring from the source.
1064
+ */
1065
+ substring(start: number, end: number): string;
1066
+
1067
+ /**
1068
+ * A Uint32Array containing balance information for tokens.
1069
+ */
1070
+ balance: Uint32Array;
1071
+
1072
+ /**
1073
+ * Checks if a token type represents a block opener.
1074
+ *
1075
+ * @param type - The token type to check.
1076
+ * @returns True if the token type is a block opener.
1077
+ */
1078
+ isBlockOpenerTokenType(type: number): boolean;
1079
+
1080
+ /**
1081
+ * Checks if a token type represents a block closer.
1082
+ *
1083
+ * @param type - The token type to check.
1084
+ * @returns True if the token type is a block closer.
1085
+ */
1086
+ isBlockCloserTokenType(type: number): boolean;
1087
+
1088
+ /**
1089
+ * Gets the index of the matching pair token for a block token.
1090
+ *
1091
+ * @param index - The index of the block token.
1092
+ * @returns The index of the matching pair token.
1093
+ */
1094
+ getBlockTokenPairIndex(index: number): number;
1095
+
1096
+ /**
1097
+ * Gets the location information for a position in the source.
1098
+ *
1099
+ * @param offset - The character offset in the source.
1100
+ * @returns The location information.
1101
+ */
1102
+ getLocation(offset: number): CssLocation;
1103
+
1104
+ /**
1105
+ * Gets the location range information for a range in the source.
1106
+ *
1107
+ * @param start - The starting offset.
1108
+ * @param end - The ending offset.
1109
+ * @returns The location range information.
1110
+ */
1111
+ getRangeLocation(start: number, end: number): CssLocationRange;
1112
+ }
1113
+
999
1114
  /**
1000
1115
  * A function used to tokenize CSS source code.
1001
1116
  *
@@ -1306,6 +1421,16 @@ export type OnParseCommentCallback = (value: string, loc: CssLocationRange) => v
1306
1421
  */
1307
1422
  export type OnParseErrorCallback = (error: SyntaxParseError, fallbackNode: CssNode) => void;
1308
1423
 
1424
+ /**
1425
+ * A callback function invoked for each token encountered during parsing.
1426
+ *
1427
+ * @param token - The numeric type of the token.
1428
+ * @param start - The starting index of the token in the source string.
1429
+ * @param end - The ending index (exclusive) of the token in the source string.
1430
+ * @param index - The index of the token in the stream.
1431
+ */
1432
+ export type OnTokenCallback = (this: TokenIterateAPI, token: number, start: number, end: number, index: number) => void;
1433
+
1309
1434
  /**
1310
1435
  * Options for controlling the behavior of the CSS parser.
1311
1436
  */
@@ -1335,6 +1460,11 @@ export interface ParseOptions {
1335
1460
  */
1336
1461
  onParseError?: OnParseErrorCallback;
1337
1462
 
1463
+ /**
1464
+ * A callback function invoked for each token encountered during parsing.
1465
+ */
1466
+ onToken?: OnTokenCallback;
1467
+
1338
1468
  /**
1339
1469
  * The name of the file being parsed, used for error reporting.
1340
1470
  */
@@ -1378,12 +1508,28 @@ export interface ParseOptions {
1378
1508
 
1379
1509
  /**
1380
1510
  * A function that parses a CSS string into an abstract syntax tree (AST).
1381
- *
1382
- * @param source - The CSS source string to parse.
1383
- * @param options - Optional configuration for the parser.
1384
- * @returns The parsed CSS as a `CssNode`.
1385
1511
  */
1386
- export type ParseFunction = (source: string, options?: ParseOptions) => CssNode;
1512
+ export interface ParseFunction {
1513
+
1514
+ /**
1515
+ * Parses a CSS source string into an abstract syntax tree (AST).
1516
+ * @param source - The CSS source string to parse.
1517
+ * @param options - Optional configuration for the parser.
1518
+ * @returns The parsed CSS as a `CssNode`.
1519
+ * @throws {CSSSyntaxError} If a parsing error occurs, this error will be thrown.
1520
+ */
1521
+ (source: string, options?: ParseOptions): CssNode;
1522
+
1523
+ /**
1524
+ * The error class used for parsing errors.
1525
+ */
1526
+ SyntaxError: typeof CSSSyntaxError;
1527
+
1528
+ /**
1529
+ * The configuration used by the parser.
1530
+ */
1531
+ config: ParseConfig;
1532
+ }
1387
1533
 
1388
1534
  /**
1389
1535
  * Parses a CSS string into an abstract syntax tree (AST).
@@ -2120,6 +2266,59 @@ export const url: {
2120
2266
  // https://github.com/csstree/csstree/blob/master/lib/lexer/Lexer.js
2121
2267
  // ----------------------------------------------------------
2122
2268
 
2269
+ /**
2270
+ * Represents a syntax error while parsing CSS code. In the actual code,
2271
+ * this is called `SyntaxError`, but that clashes with the global `SyntaxError` class.
2272
+ * This isn't exported separately but rather as a member of the `parse` function.
2273
+ */
2274
+ declare class CSSSyntaxError extends SyntaxError {
2275
+
2276
+ /**
2277
+ * Creates a new instance
2278
+ * @param message The error message describing the syntax error.
2279
+ * @param source The source code where the error occurred.
2280
+ * @param offset The character offset in the source code where the error occurred.
2281
+ * @param line The line number (1-indexed) in the source code where the error occurred.
2282
+ * @param column The column number (1-indexed) in the source code where the error occurred.
2283
+ * @param baseLine The base line number (1-indexed) for the error, used for relative positioning.
2284
+ * @param baseColumn The base column number (1-indexed) for the error, used for relative positioning.
2285
+ */
2286
+ constructor(message: string, source: string, offset: number, line: number, column: number, baseLine?: number, baseColumn?: number);
2287
+
2288
+ /**
2289
+ * The source code where the error occurred.
2290
+ */
2291
+ source: string;
2292
+
2293
+ /**
2294
+ * The character offset in the source code where the error occurred.
2295
+ */
2296
+ offset: number;
2297
+
2298
+ /**
2299
+ * The line number (1-indexed) in the source code where the error occurred.
2300
+ */
2301
+ line: number;
2302
+
2303
+ /**
2304
+ * The column number (1-indexed) in the source code where the error occurred.
2305
+ */
2306
+ column: number;
2307
+
2308
+ /**
2309
+ * The source code fragment around the error, including a specified number of extra lines.
2310
+ * @param extraLines The number of extra lines to include in the fragment.
2311
+ * @return A string containing the source code fragment around the error.
2312
+ * This fragment includes the error line and the specified number of lines before and after it.
2313
+ */
2314
+ sourceFragment(extraLines: number): string;
2315
+
2316
+ /**
2317
+ * The error message formatted with the source fragment.
2318
+ */
2319
+ readonly formattedMessage: string;
2320
+ }
2321
+
2123
2322
  /**
2124
2323
  * Represents an error that occurs during the syntax matching process.
2125
2324
  * Extends the standard `SyntaxError` with additional properties specific to CSS parsing.
@@ -63,6 +63,16 @@ function syntaxHasTopLevelCommaMultiplier(syntax) {
63
63
  );
64
64
  }
65
65
 
66
+ function valueHasEnv(tokens) {
67
+ for (let i = 0; i < tokens.length; i++) {
68
+ if (tokens[i].value.toLowerCase() === 'env(') {
69
+ return true;
70
+ }
71
+ }
72
+
73
+ return false;
74
+ }
75
+
66
76
  function buildMatchResult(matched, error, iterations) {
67
77
  return {
68
78
  matched,
@@ -80,6 +90,10 @@ function matchSyntax(lexer, syntax, value, useCssWideKeywords) {
80
90
  return buildMatchResult(null, new Error('Matching for a tree with var() is not supported'));
81
91
  }
82
92
 
93
+ if (valueHasEnv(tokens)) {
94
+ return buildMatchResult(null, new Error('Matching for a tree with env() is not supported'));
95
+ }
96
+
83
97
  if (useCssWideKeywords) {
84
98
  result = matchAsTree(tokens, lexer.cssWideKeywordsSyntax, lexer);
85
99
  }
@@ -356,8 +370,10 @@ export class Lexer {
356
370
  return this.matchProperty(node.property, node.value);
357
371
  }
358
372
  matchProperty(propertyName, value) {
359
- // don't match syntax for a custom property at the moment
360
- if (names.property(propertyName).custom) {
373
+ if (
374
+ !this.getProperty(propertyName) &&
375
+ names.property(propertyName).custom
376
+ ) {
361
377
  return buildMatchResult(null, new Error('Lexer matching doesn\'t applicable for custom properties'));
362
378
  }
363
379
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eslint/css-tree",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "A tool set for CSS: fast detailed parser (CSS → AST), walker (AST traversal), generator (AST → CSS) and lexer (validation and matching) based on specs and browser implementations",
5
5
  "author": "Roman Dvornov <rdvornov@gmail.com> (https://github.com/lahmatiy)",
6
6
  "license": "MIT",