|
| 1 | +import { |
| 2 | + type ASTWithSource, |
| 3 | + Lexer, |
| 4 | + Parser, |
| 5 | + type TemplateBindingParseResult, |
| 6 | +} from '@angular/compiler'; |
| 7 | + |
| 8 | +import { type CommentLine } from './types.js'; |
| 9 | +import { sourceSpanToLocationInformation } from './utils.js'; |
| 10 | + |
| 11 | +const getCommentStart = (text: string): number | null => |
| 12 | + // @ts-expect-error -- need to call private _commentStart |
| 13 | + Parser.prototype._commentStart(text); |
| 14 | + |
| 15 | +function extractComments(text: string, shouldExtractComment: boolean) { |
| 16 | + const commentStart = shouldExtractComment ? getCommentStart(text) : null; |
| 17 | + |
| 18 | + if (commentStart === null) { |
| 19 | + return { text, comments: [] }; |
| 20 | + } |
| 21 | + |
| 22 | + const comment: CommentLine = { |
| 23 | + type: 'CommentLine', |
| 24 | + value: text.slice(commentStart + '//'.length), |
| 25 | + ...sourceSpanToLocationInformation({ |
| 26 | + start: commentStart, |
| 27 | + end: text.length, |
| 28 | + }), |
| 29 | + }; |
| 30 | + |
| 31 | + return { |
| 32 | + text: text.slice(0, commentStart), |
| 33 | + comments: [comment], |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function createAngularParseFunction< |
| 38 | + T extends ASTWithSource | TemplateBindingParseResult, |
| 39 | +>(parse: (text: string, parser: Parser) => T, shouldExtractComment = true) { |
| 40 | + return (originalText: string) => { |
| 41 | + const lexer = new Lexer(); |
| 42 | + const parser = new Parser(lexer); |
| 43 | + |
| 44 | + const { text, comments } = extractComments( |
| 45 | + originalText, |
| 46 | + shouldExtractComment, |
| 47 | + ); |
| 48 | + const result = parse(text, parser); |
| 49 | + |
| 50 | + if (result.errors.length !== 0) { |
| 51 | + const [{ message }] = result.errors; |
| 52 | + throw new SyntaxError( |
| 53 | + message.replace(/^Parser Error: | at column \d+ in [^]*$/g, ''), |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + return { result, comments, text }; |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +export const parseBinding = createAngularParseFunction((text, parser) => |
| 62 | + parser.parseBinding(text, '', 0), |
| 63 | +); |
| 64 | + |
| 65 | +export const parseSimpleBinding = createAngularParseFunction((text, parser) => |
| 66 | + parser.parseSimpleBinding(text, '', 0), |
| 67 | +); |
| 68 | + |
| 69 | +export const parseAction = createAngularParseFunction((text, parser) => |
| 70 | + parser.parseAction(text, '', 0), |
| 71 | +); |
| 72 | + |
| 73 | +export const parseInterpolationExpression = createAngularParseFunction( |
| 74 | + (text, parser) => parser.parseInterpolationExpression(text, '', 0), |
| 75 | +); |
| 76 | + |
| 77 | +export const parseTemplateBindings = createAngularParseFunction( |
| 78 | + (text, parser) => parser.parseTemplateBindings('', text, '', 0, 0), |
| 79 | + /* shouldExtractComment */ false, |
| 80 | +); |
| 81 | + |
| 82 | +export type AstParseResult = ReturnType<typeof parseBinding>; |
| 83 | +export type MicroSyntaxParseResult = ReturnType<typeof parseTemplateBindings>; |
0 commit comments