@@ -11,22 +11,32 @@ import {
1111import { type CommentLine } from './types.ts' ;
1212import { sourceSpanToLocationInformation } from './utils.ts' ;
1313
14+ let parseSourceSpan : ParseSourceSpan ;
1415// https://github.com/angular/angular/blob/5e9707dc84e6590ec8c9d41e7d3be7deb2fa7c53/packages/compiler/test/expression_parser/utils/span.ts
15- function getFakeSpan ( fileName = 'test.html' ) {
16- const file = new ParseSourceFile ( '' , fileName ) ;
17- const location = new ParseLocation ( file , 0 , 0 , 0 ) ;
18- return new ParseSourceSpan ( location , location ) ;
16+ function getParseSourceSpan ( ) {
17+ if ( ! parseSourceSpan ) {
18+ const file = new ParseSourceFile ( '' , 'test.html' ) ;
19+ const location = new ParseLocation ( file , - 1 , - 1 , - 1 ) ;
20+ parseSourceSpan = new ParseSourceSpan ( location , location ) ;
21+ }
22+
23+ return parseSourceSpan ;
24+ }
25+
26+ let parser : Parser ;
27+ function getParser ( ) {
28+ return ( parser ??= new Parser ( new Lexer ( ) ) ) ;
1929}
2030
2131const getCommentStart = ( text : string ) : number | null =>
2232 // @ts -expect-error -- need to call private _commentStart
2333 Parser . prototype . _commentStart ( text ) ;
2434
25- function extractComments ( text : string , shouldExtractComment : boolean ) {
26- const commentStart = shouldExtractComment ? getCommentStart ( text ) : null ;
35+ function extractComments ( text : string ) {
36+ const commentStart = getCommentStart ( text ) ;
2737
2838 if ( commentStart === null ) {
29- return { text , comments : [ ] } ;
39+ return [ ] ;
3040 }
3141
3242 const comment : CommentLine = {
@@ -38,53 +48,48 @@ function extractComments(text: string, shouldExtractComment: boolean) {
3848 } ) ,
3949 } ;
4050
41- return { text , comments : [ comment ] } ;
51+ return [ comment ] ;
4252}
4353
44- function createAngularParseFunction <
45- T extends ASTWithSource | TemplateBindingParseResult ,
46- > ( parse : ( text : string , parser : Parser ) => T , shouldExtractComment = true ) {
47- return ( originalText : string ) => {
48- const lexer = new Lexer ( ) ;
49- const parser = new Parser ( lexer ) ;
50-
51- const { text, comments } = extractComments (
52- originalText ,
53- shouldExtractComment ,
54+ function throwErrors <
55+ ResultType extends ASTWithSource | TemplateBindingParseResult ,
56+ > ( result : ResultType ) {
57+ if ( result . errors . length !== 0 ) {
58+ const [ { message } ] = result . errors ;
59+ throw new SyntaxError (
60+ message . replace ( / ^ P a r s e r E r r o r : | a t c o l u m n \d + i n [ ^ ] * $ / g, '' ) ,
5461 ) ;
55- const result = parse ( text , parser ) ;
56-
57- if ( result . errors . length !== 0 ) {
58- const [ { message } ] = result . errors ;
59- throw new SyntaxError (
60- message . replace ( / ^ P a r s e r E r r o r : | a t c o l u m n \d + i n [ ^ ] * $ / g, '' ) ,
61- ) ;
62- }
62+ }
6363
64- return { result, comments, text } ;
65- } ;
64+ return result ;
6665}
6766
68- export const parseBinding = createAngularParseFunction ( ( text , parser ) =>
69- parser . parseBinding ( text , getFakeSpan ( ) , 0 ) ,
67+ const createAstParser =
68+ (
69+ name :
70+ | 'parseBinding'
71+ | 'parseSimpleBinding'
72+ | 'parseAction'
73+ | 'parseInterpolationExpression' ,
74+ ) =>
75+ ( text : string ) => ( {
76+ result : throwErrors < ASTWithSource > (
77+ getParser ( ) [ name ] ( text , getParseSourceSpan ( ) , 0 ) ,
78+ ) ,
79+ text,
80+ comments : extractComments ( text ) ,
81+ } ) ;
82+
83+ export const parseAction = createAstParser ( 'parseAction' ) ;
84+ export const parseBinding = createAstParser ( 'parseBinding' ) ;
85+ export const parseSimpleBinding = createAstParser ( 'parseSimpleBinding' ) ;
86+ export const parseInterpolationExpression = createAstParser (
87+ 'parseInterpolationExpression' ,
7088) ;
71-
72- export const parseSimpleBinding = createAngularParseFunction ( ( text , parser ) =>
73- parser . parseSimpleBinding ( text , getFakeSpan ( ) , 0 ) ,
74- ) ;
75-
76- export const parseAction = createAngularParseFunction ( ( text , parser ) =>
77- parser . parseAction ( text , getFakeSpan ( ) , 0 ) ,
78- ) ;
79-
80- export const parseInterpolationExpression = createAngularParseFunction (
81- ( text , parser ) => parser . parseInterpolationExpression ( text , getFakeSpan ( ) , 0 ) ,
82- ) ;
83-
84- export const parseTemplateBindings = createAngularParseFunction (
85- ( text , parser ) => parser . parseTemplateBindings ( '' , text , getFakeSpan ( ) , 0 , 0 ) ,
86- /* shouldExtractComment */ false ,
87- ) ;
88-
89- export type AstParseResult = ReturnType < typeof parseBinding > ;
90- export type MicroSyntaxParseResult = ReturnType < typeof parseTemplateBindings > ;
89+ export const parseTemplateBindings = ( text : string ) => ( {
90+ result : throwErrors < TemplateBindingParseResult > (
91+ getParser ( ) . parseTemplateBindings ( '' , text , getParseSourceSpan ( ) , 0 , 0 ) ,
92+ ) ,
93+ text,
94+ comments : [ ] ,
95+ } ) ;
0 commit comments