@eslint/css-tree 3.2.0 → 3.3.1

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.1";
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "3.2.0";
1
+ export const version = "3.3.1";
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
  *
@@ -1267,27 +1382,46 @@ export class OffsetToLocation {
1267
1382
  * Represents an error that occurs during CSS parsing. Extends the standard `SyntaxError`
1268
1383
  * to include additional details about the parsing error.
1269
1384
  */
1385
+
1386
+ /**
1387
+ * Represents a syntax error while parsing CSS code. In the actual code,
1388
+ * this is called `SyntaxError`, but that clashes with the global `SyntaxError` class.
1389
+ * This isn't exported separately but rather as a member of the `parse` function.
1390
+ */
1270
1391
  export interface SyntaxParseError extends SyntaxError {
1392
+
1271
1393
  /**
1272
- * The original input string that caused the error.
1394
+ * The source code where the error occurred.
1273
1395
  */
1274
- input: string;
1396
+ source: string;
1275
1397
 
1276
1398
  /**
1277
- * The character offset in the input string where the error occurred.
1399
+ * The character offset in the source code where the error occurred.
1278
1400
  */
1279
1401
  offset: number;
1280
1402
 
1281
1403
  /**
1282
- * The raw error message without formatting.
1404
+ * The line number (1-indexed) in the source code where the error occurred.
1283
1405
  */
1284
- rawMessage: string;
1406
+ line: number;
1285
1407
 
1286
1408
  /**
1287
- * The formatted error message, including contextual information such as
1288
- * the location in the input string.
1409
+ * The column number (1-indexed) in the source code where the error occurred.
1289
1410
  */
1290
- formattedMessage: string;
1411
+ column: number;
1412
+
1413
+ /**
1414
+ * The source code fragment around the error, including a specified number of extra lines.
1415
+ * @param extraLines The number of extra lines to include in the fragment.
1416
+ * @return A string containing the source code fragment around the error.
1417
+ * This fragment includes the error line and the specified number of lines before and after it.
1418
+ */
1419
+ sourceFragment(extraLines: number): string;
1420
+
1421
+ /**
1422
+ * The error message formatted with the source fragment.
1423
+ */
1424
+ readonly formattedMessage: string;
1291
1425
  }
1292
1426
 
1293
1427
  /**
@@ -1306,6 +1440,16 @@ export type OnParseCommentCallback = (value: string, loc: CssLocationRange) => v
1306
1440
  */
1307
1441
  export type OnParseErrorCallback = (error: SyntaxParseError, fallbackNode: CssNode) => void;
1308
1442
 
1443
+ /**
1444
+ * A callback function invoked for each token encountered during parsing.
1445
+ *
1446
+ * @param token - The numeric type of the token.
1447
+ * @param start - The starting index of the token in the source string.
1448
+ * @param end - The ending index (exclusive) of the token in the source string.
1449
+ * @param index - The index of the token in the stream.
1450
+ */
1451
+ export type OnTokenCallback = (this: TokenIterateAPI, token: number, start: number, end: number, index: number) => void;
1452
+
1309
1453
  /**
1310
1454
  * Options for controlling the behavior of the CSS parser.
1311
1455
  */
@@ -1335,6 +1479,11 @@ export interface ParseOptions {
1335
1479
  */
1336
1480
  onParseError?: OnParseErrorCallback;
1337
1481
 
1482
+ /**
1483
+ * A callback function invoked for each token encountered during parsing.
1484
+ */
1485
+ onToken?: OnTokenCallback;
1486
+
1338
1487
  /**
1339
1488
  * The name of the file being parsed, used for error reporting.
1340
1489
  */
@@ -1376,14 +1525,42 @@ export interface ParseOptions {
1376
1525
  parseCustomProperty?: boolean | undefined;
1377
1526
  }
1378
1527
 
1528
+ /**
1529
+ * Creates a new instance of a parse error.
1530
+ * @param message The error message describing the syntax error.
1531
+ * @param source The source code where the error occurred.
1532
+ * @param offset The character offset in the source code where the error occurred.
1533
+ * @param line The line number (1-indexed) in the source code where the error occurred.
1534
+ * @param column The column number (1-indexed) in the source code where the error occurred.
1535
+ * @param baseLine The base line number (1-indexed) for the error, used for relative positioning.
1536
+ * @param baseColumn The base column number (1-indexed) for the error, used for relative positioning.
1537
+ */
1538
+ export type SyntaxErrorCreator = (message: string, source: string, offset: number, line: number, column: number, baseLine?: number, baseColumn?: number) => SyntaxParseError;
1539
+
1379
1540
  /**
1380
1541
  * 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
1542
  */
1386
- export type ParseFunction = (source: string, options?: ParseOptions) => CssNode;
1543
+ export interface ParseFunction {
1544
+
1545
+ /**
1546
+ * Parses a CSS source string into an abstract syntax tree (AST).
1547
+ * @param source - The CSS source string to parse.
1548
+ * @param options - Optional configuration for the parser.
1549
+ * @returns The parsed CSS as a `CssNode`.
1550
+ * @throws {CSSSyntaxError} If a parsing error occurs, this error will be thrown.
1551
+ */
1552
+ (source: string, options?: ParseOptions): CssNode;
1553
+
1554
+ /**
1555
+ * The error class used for parsing errors.
1556
+ */
1557
+ SyntaxError: SyntaxErrorCreator;
1558
+
1559
+ /**
1560
+ * The configuration used by the parser.
1561
+ */
1562
+ config: ParseConfig;
1563
+ }
1387
1564
 
1388
1565
  /**
1389
1566
  * Parses a CSS string into an abstract syntax tree (AST).
@@ -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.1",
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",