Skip to content

Commit 117e2bb

Browse files
committed
Rethrow parse errors
1 parent 03fb261 commit 117e2bb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ export function getAST(cssText: string) {
3131
return parse(cssText, {
3232
parseAtrulePrelude: false,
3333
parseCustomProperty: true,
34+
onParseError: (err) => {
35+
const errorPrelude =
36+
'Invalid CSS could not be parsed. CSS Anchor Positioning Polyfill was not applied.\n\n';
37+
throw new Error(errorPrelude + err.formattedMessage, { cause: err });
38+
},
3439
});
3540
}
3641

tests/unit/utils.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,23 @@ describe('splitCommaList', () => {
2020
]);
2121
});
2222
});
23+
describe('getAST', () => {
24+
it('parses valid CSS', () => {
25+
const cssText = 'a { color: red; }';
26+
const ast = getAST(cssText);
27+
expect(ast.type).toBe('StyleSheet');
28+
});
29+
30+
it('throws on invalid declaration', () => {
31+
const cssText = 'a { color; red; } ';
32+
expect(() => getAST(cssText)).toThrowError(
33+
/Invalid CSS could not be parsed/,
34+
);
35+
});
36+
it('throws on invalid selector', () => {
37+
const cssText = 'a-[1] { color: red; } ';
38+
expect(() => getAST(cssText)).toThrowError(
39+
/Invalid CSS could not be parsed/,
40+
);
41+
});
42+
});

0 commit comments

Comments
 (0)