Skip to content

Commit 3db5e5a

Browse files
authored
css-tokenizer: improve ParseError (#1420)
1 parent ad7a343 commit 3db5e5a

20 files changed

+676
-75
lines changed

packages/css-tokenizer/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changes to CSS Tokenizer
22

3+
### Unreleased (minor)
4+
5+
- Expose `ParseErrorMessage`, the list of known parser error messages object to facilitate detection of specific cases
6+
- Add a specific `ParseErrorWithToken` subclass. This contains the associated token.
7+
38
### 2.3.3
49

510
_July 3, 2024_

packages/css-tokenizer/dist/index.cjs

+1-1
Large diffs are not rendered by default.

packages/css-tokenizer/dist/index.d.ts

+19
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,28 @@ export declare class ParseError extends Error {
212212
sourceEnd: number;
213213
/** The parser steps that preceded the error. */
214214
parserState: Array<string>;
215+
/** The associated AST node. */
216+
astNode?: unknown;
215217
constructor(message: string, sourceStart: number, sourceEnd: number, parserState: Array<string>);
216218
}
217219

220+
export declare const ParseErrorMessage: {
221+
UnexpectedNewLineInString: string;
222+
UnexpectedEOFInString: string;
223+
UnexpectedEOFInComment: string;
224+
UnexpectedEOFInURL: string;
225+
UnexpectedEOFInEscapedCodePoint: string;
226+
UnexpectedCharacterInURL: string;
227+
InvalidEscapeSequenceInURL: string;
228+
InvalidEscapeSequenceAfterBackslash: string;
229+
};
230+
231+
export declare class ParseErrorWithToken extends ParseError {
232+
/** The associated token. */
233+
token: CSSToken;
234+
constructor(message: string, sourceStart: number, sourceEnd: number, parserState: Array<string>, token: CSSToken);
235+
}
236+
218237
/**
219238
* @internal
220239
*/

packages/css-tokenizer/dist/index.mjs

+1-1
Large diffs are not rendered by default.

packages/css-tokenizer/docs/css-tokenizer.api.json

+218
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,36 @@
28002800
}
28012801
]
28022802
},
2803+
{
2804+
"kind": "Property",
2805+
"canonicalReference": "@csstools/css-tokenizer!ParseError#astNode:member",
2806+
"docComment": "/**\n * The associated AST node.\n */\n",
2807+
"excerptTokens": [
2808+
{
2809+
"kind": "Content",
2810+
"text": "astNode?: "
2811+
},
2812+
{
2813+
"kind": "Content",
2814+
"text": "unknown"
2815+
},
2816+
{
2817+
"kind": "Content",
2818+
"text": ";"
2819+
}
2820+
],
2821+
"isReadonly": false,
2822+
"isOptional": true,
2823+
"releaseTag": "Public",
2824+
"name": "astNode",
2825+
"propertyTypeTokenRange": {
2826+
"startIndex": 1,
2827+
"endIndex": 2
2828+
},
2829+
"isStatic": false,
2830+
"isProtected": false,
2831+
"isAbstract": false
2832+
},
28032833
{
28042834
"kind": "Property",
28052835
"canonicalReference": "@csstools/css-tokenizer!ParseError#parserState:member",
@@ -2902,6 +2932,194 @@
29022932
},
29032933
"implementsTokenRanges": []
29042934
},
2935+
{
2936+
"kind": "Variable",
2937+
"canonicalReference": "@csstools/css-tokenizer!ParseErrorMessage:var",
2938+
"docComment": "",
2939+
"excerptTokens": [
2940+
{
2941+
"kind": "Content",
2942+
"text": "ParseErrorMessage: "
2943+
},
2944+
{
2945+
"kind": "Content",
2946+
"text": "{\n UnexpectedNewLineInString: string;\n UnexpectedEOFInString: string;\n UnexpectedEOFInComment: string;\n UnexpectedEOFInURL: string;\n UnexpectedEOFInEscapedCodePoint: string;\n UnexpectedCharacterInURL: string;\n InvalidEscapeSequenceInURL: string;\n InvalidEscapeSequenceAfterBackslash: string;\n}"
2947+
}
2948+
],
2949+
"fileUrlPath": "dist/_types/interfaces/error.d.ts",
2950+
"isReadonly": true,
2951+
"releaseTag": "Public",
2952+
"name": "ParseErrorMessage",
2953+
"variableTypeTokenRange": {
2954+
"startIndex": 1,
2955+
"endIndex": 2
2956+
}
2957+
},
2958+
{
2959+
"kind": "Class",
2960+
"canonicalReference": "@csstools/css-tokenizer!ParseErrorWithToken:class",
2961+
"docComment": "",
2962+
"excerptTokens": [
2963+
{
2964+
"kind": "Content",
2965+
"text": "export declare class ParseErrorWithToken extends "
2966+
},
2967+
{
2968+
"kind": "Reference",
2969+
"text": "ParseError",
2970+
"canonicalReference": "@csstools/css-tokenizer!ParseError:class"
2971+
},
2972+
{
2973+
"kind": "Content",
2974+
"text": " "
2975+
}
2976+
],
2977+
"fileUrlPath": "dist/_types/interfaces/error.d.ts",
2978+
"releaseTag": "Public",
2979+
"isAbstract": false,
2980+
"name": "ParseErrorWithToken",
2981+
"preserveMemberOrder": false,
2982+
"members": [
2983+
{
2984+
"kind": "Constructor",
2985+
"canonicalReference": "@csstools/css-tokenizer!ParseErrorWithToken:constructor(1)",
2986+
"docComment": "/**\n * Constructs a new instance of the `ParseErrorWithToken` class\n */\n",
2987+
"excerptTokens": [
2988+
{
2989+
"kind": "Content",
2990+
"text": "constructor(message: "
2991+
},
2992+
{
2993+
"kind": "Content",
2994+
"text": "string"
2995+
},
2996+
{
2997+
"kind": "Content",
2998+
"text": ", sourceStart: "
2999+
},
3000+
{
3001+
"kind": "Content",
3002+
"text": "number"
3003+
},
3004+
{
3005+
"kind": "Content",
3006+
"text": ", sourceEnd: "
3007+
},
3008+
{
3009+
"kind": "Content",
3010+
"text": "number"
3011+
},
3012+
{
3013+
"kind": "Content",
3014+
"text": ", parserState: "
3015+
},
3016+
{
3017+
"kind": "Reference",
3018+
"text": "Array",
3019+
"canonicalReference": "!Array:interface"
3020+
},
3021+
{
3022+
"kind": "Content",
3023+
"text": "<string>"
3024+
},
3025+
{
3026+
"kind": "Content",
3027+
"text": ", token: "
3028+
},
3029+
{
3030+
"kind": "Reference",
3031+
"text": "CSSToken",
3032+
"canonicalReference": "@csstools/css-tokenizer!CSSToken:type"
3033+
},
3034+
{
3035+
"kind": "Content",
3036+
"text": ");"
3037+
}
3038+
],
3039+
"releaseTag": "Public",
3040+
"isProtected": false,
3041+
"overloadIndex": 1,
3042+
"parameters": [
3043+
{
3044+
"parameterName": "message",
3045+
"parameterTypeTokenRange": {
3046+
"startIndex": 1,
3047+
"endIndex": 2
3048+
},
3049+
"isOptional": false
3050+
},
3051+
{
3052+
"parameterName": "sourceStart",
3053+
"parameterTypeTokenRange": {
3054+
"startIndex": 3,
3055+
"endIndex": 4
3056+
},
3057+
"isOptional": false
3058+
},
3059+
{
3060+
"parameterName": "sourceEnd",
3061+
"parameterTypeTokenRange": {
3062+
"startIndex": 5,
3063+
"endIndex": 6
3064+
},
3065+
"isOptional": false
3066+
},
3067+
{
3068+
"parameterName": "parserState",
3069+
"parameterTypeTokenRange": {
3070+
"startIndex": 7,
3071+
"endIndex": 9
3072+
},
3073+
"isOptional": false
3074+
},
3075+
{
3076+
"parameterName": "token",
3077+
"parameterTypeTokenRange": {
3078+
"startIndex": 10,
3079+
"endIndex": 11
3080+
},
3081+
"isOptional": false
3082+
}
3083+
]
3084+
},
3085+
{
3086+
"kind": "Property",
3087+
"canonicalReference": "@csstools/css-tokenizer!ParseErrorWithToken#token:member",
3088+
"docComment": "/**\n * The associated token.\n */\n",
3089+
"excerptTokens": [
3090+
{
3091+
"kind": "Content",
3092+
"text": "token: "
3093+
},
3094+
{
3095+
"kind": "Reference",
3096+
"text": "CSSToken",
3097+
"canonicalReference": "@csstools/css-tokenizer!CSSToken:type"
3098+
},
3099+
{
3100+
"kind": "Content",
3101+
"text": ";"
3102+
}
3103+
],
3104+
"isReadonly": false,
3105+
"isOptional": false,
3106+
"releaseTag": "Public",
3107+
"name": "token",
3108+
"propertyTypeTokenRange": {
3109+
"startIndex": 1,
3110+
"endIndex": 2
3111+
},
3112+
"isStatic": false,
3113+
"isProtected": false,
3114+
"isAbstract": false
3115+
}
3116+
],
3117+
"extendsTokenRange": {
3118+
"startIndex": 1,
3119+
"endIndex": 2
3120+
},
3121+
"implementsTokenRanges": []
3122+
},
29053123
{
29063124
"kind": "Function",
29073125
"canonicalReference": "@csstools/css-tokenizer!stringify:function(1)",

packages/css-tokenizer/docs/css-tokenizer.md

+33
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ Description
5656
The CSS Tokenizer is forgiving and will never throw on invalid input. Any errors are reported through the `onParseError` callback.
5757

5858

59+
</td></tr>
60+
<tr><td>
61+
62+
[ParseErrorWithToken](./css-tokenizer.parseerrorwithtoken.md)
63+
64+
65+
</td><td>
66+
67+
5968
</td></tr>
6069
</tbody></table>
6170

@@ -752,6 +761,30 @@ The CSS Token interface
752761
</td><td>
753762

754763

764+
</td></tr>
765+
</tbody></table>
766+
767+
## Variables
768+
769+
<table><thead><tr><th>
770+
771+
Variable
772+
773+
774+
</th><th>
775+
776+
Description
777+
778+
779+
</th></tr></thead>
780+
<tbody><tr><td>
781+
782+
[ParseErrorMessage](./css-tokenizer.parseerrormessage.md)
783+
784+
785+
</td><td>
786+
787+
755788
</td></tr>
756789
</tbody></table>
757790

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@csstools/css-tokenizer](./css-tokenizer.md) &gt; [ParseError](./css-tokenizer.parseerror.md) &gt; [astNode](./css-tokenizer.parseerror.astnode.md)
4+
5+
## ParseError.astNode property
6+
7+
The associated AST node.
8+
9+
**Signature:**
10+
11+
```typescript
12+
astNode?: unknown;
13+
```

packages/css-tokenizer/docs/css-tokenizer.parseerror.md

+19
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ Description
7272
</th></tr></thead>
7373
<tbody><tr><td>
7474
75+
[astNode?](./css-tokenizer.parseerror.astnode.md)
76+
77+
78+
</td><td>
79+
80+
81+
</td><td>
82+
83+
unknown
84+
85+
86+
</td><td>
87+
88+
_(Optional)_ The associated AST node.
89+
90+
91+
</td></tr>
92+
<tr><td>
93+
7594
[parserState](./css-tokenizer.parseerror.parserstate.md)
7695
7796
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
2+
3+
[Home](./index.md) &gt; [@csstools/css-tokenizer](./css-tokenizer.md) &gt; [ParseErrorMessage](./css-tokenizer.parseerrormessage.md)
4+
5+
## ParseErrorMessage variable
6+
7+
**Signature:**
8+
9+
```typescript
10+
ParseErrorMessage: {
11+
UnexpectedNewLineInString: string;
12+
UnexpectedEOFInString: string;
13+
UnexpectedEOFInComment: string;
14+
UnexpectedEOFInURL: string;
15+
UnexpectedEOFInEscapedCodePoint: string;
16+
UnexpectedCharacterInURL: string;
17+
InvalidEscapeSequenceInURL: string;
18+
InvalidEscapeSequenceAfterBackslash: string;
19+
}
20+
```

0 commit comments

Comments
 (0)