|
| 1 | +import {AST} from '../expression_parser/ast'; |
| 2 | +import * as html from '../ml_parser/ast'; |
| 3 | +import {ParseError, ParseSourceSpan} from '../parse_util'; |
| 4 | +import {BindingParser} from '../template_parser/binding_parser'; |
| 5 | + |
| 6 | +import * as t from './r3_ast'; |
| 7 | + |
| 8 | +/** Pattern used to identify a boundary `let` parameter. */ |
| 9 | +const LET_PATTERN = /^(let\s+)(.*)/; |
| 10 | + |
| 11 | +/** Pattern used to identify a boundary `when` expression. */ |
| 12 | +const WHEN_PATTERN = /^(when\s+)(.*)/; |
| 13 | + |
| 14 | +/** Pattern used to validate a JavaScript identifier. */ |
| 15 | +const IDENTIFIER_PATTERN = /^[$A-Z_][0-9A-Z_$]*$/i; |
| 16 | + |
| 17 | +export function isConnectedBoundaryErrorBlock(name: string): boolean { |
| 18 | + return name === 'error'; |
| 19 | +} |
| 20 | + |
| 21 | +export function createBoundaryBlock( |
| 22 | + ast: html.Block, |
| 23 | + connectedBlocks: html.Block[], |
| 24 | + visitor: html.Visitor, |
| 25 | + bindingParser: BindingParser, |
| 26 | +): {node: t.BoundaryBlock | null; errors: ParseError[]} { |
| 27 | + const errors: ParseError[] = []; |
| 28 | + const errorBlocks: t.BoundaryErrorBlock[] = []; |
| 29 | + |
| 30 | + for (const block of connectedBlocks) { |
| 31 | + if (block.name === 'error') { |
| 32 | + const contextVariables: t.Variable[] = []; |
| 33 | + let expression: AST | null = null; |
| 34 | + |
| 35 | + for (const param of block.parameters) { |
| 36 | + const letMatch = param.expression.match(LET_PATTERN); |
| 37 | + if (letMatch) { |
| 38 | + const variablesExpression = letMatch[2].trim(); |
| 39 | + const parts = variablesExpression.split(','); |
| 40 | + |
| 41 | + for (const part of parts) { |
| 42 | + const expressionParts = part.split('='); |
| 43 | + const name = expressionParts[0].trim(); |
| 44 | + const variableName = |
| 45 | + expressionParts.length === 2 ? expressionParts[1].trim() : '$error'; |
| 46 | + |
| 47 | + if (name.length === 0) { |
| 48 | + errors.push( |
| 49 | + new ParseError( |
| 50 | + param.sourceSpan, |
| 51 | + `Invalid @error block "let" parameter. Variable name cannot be empty`, |
| 52 | + ), |
| 53 | + ); |
| 54 | + } else if (!IDENTIFIER_PATTERN.test(name)) { |
| 55 | + errors.push( |
| 56 | + new ParseError( |
| 57 | + param.sourceSpan, |
| 58 | + `"let" parameter must be a valid JavaScript identifier`, |
| 59 | + ), |
| 60 | + ); |
| 61 | + } else if (variableName !== '$error' && variableName !== '$retry') { |
| 62 | + errors.push( |
| 63 | + new ParseError( |
| 64 | + param.sourceSpan, |
| 65 | + `Unknown context variable "${variableName}". Only "$error" and "$retry" are allowed`, |
| 66 | + ), |
| 67 | + ); |
| 68 | + } else if (contextVariables.some((v) => v.name === name)) { |
| 69 | + errors.push( |
| 70 | + new ParseError(param.sourceSpan, `Duplicate "let" parameter variable "${name}"`), |
| 71 | + ); |
| 72 | + } else { |
| 73 | + const varSpan = param.sourceSpan; |
| 74 | + contextVariables.push(new t.Variable(name, variableName, varSpan, varSpan)); |
| 75 | + } |
| 76 | + } |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + const whenMatch = param.expression.match(WHEN_PATTERN); |
| 81 | + if (whenMatch) { |
| 82 | + if (expression !== null) { |
| 83 | + errors.push( |
| 84 | + new ParseError(param.sourceSpan, '@error block can only have one "when" expression'), |
| 85 | + ); |
| 86 | + } else { |
| 87 | + const start = param.expression.indexOf(whenMatch[2]); |
| 88 | + const end = start + whenMatch[2].length; |
| 89 | + const expressionAST = bindingParser.parseBinding( |
| 90 | + param.expression.slice(start, end), |
| 91 | + false, |
| 92 | + param.sourceSpan, |
| 93 | + param.sourceSpan.start.offset + start, |
| 94 | + ); |
| 95 | + expression = expressionAST.ast; |
| 96 | + } |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + errors.push( |
| 101 | + new ParseError( |
| 102 | + param.sourceSpan, |
| 103 | + `Unrecognized @error block parameter "${param.expression}"`, |
| 104 | + ), |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + errorBlocks.push( |
| 109 | + new t.BoundaryErrorBlock( |
| 110 | + html.visitAll(visitor, block.children, block.children), |
| 111 | + contextVariables, |
| 112 | + expression, |
| 113 | + block.nameSpan, |
| 114 | + block.sourceSpan, |
| 115 | + block.startSourceSpan, |
| 116 | + block.endSourceSpan, |
| 117 | + block.i18n, |
| 118 | + ), |
| 119 | + ); |
| 120 | + } else { |
| 121 | + errors.push( |
| 122 | + new ParseError(block.sourceSpan, `Unrecognized @boundary connected block @${block.name}`), |
| 123 | + ); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + const node = new t.BoundaryBlock( |
| 128 | + html.visitAll(visitor, ast.children, ast.children), |
| 129 | + errorBlocks, |
| 130 | + ast.nameSpan, |
| 131 | + ast.sourceSpan, |
| 132 | + ast.startSourceSpan, |
| 133 | + ast.endSourceSpan, |
| 134 | + ast.i18n, |
| 135 | + ); |
| 136 | + |
| 137 | + return {node, errors}; |
| 138 | +} |
0 commit comments