( sourceSpan: ParseSourceSpan, expression: string, span: ParseSourceSpan, context: t.Variable[], errors: ParseError[], validateLet: (name: string, variableName: string, sourceSpan: ParseSourceSpan) => void, messagePrefix: string, defaultImplicitVariableName?: string, )
| 41 | * @param defaultImplicitVariableName An optional default variable name to use if no value is provided. |
| 42 | */ |
| 43 | export function parseLetParameters( |
| 44 | sourceSpan: ParseSourceSpan, |
| 45 | expression: string, |
| 46 | span: ParseSourceSpan, |
| 47 | context: t.Variable[], |
| 48 | errors: ParseError[], |
| 49 | validateLet: (name: string, variableName: string, sourceSpan: ParseSourceSpan) => void, |
| 50 | messagePrefix: string, |
| 51 | defaultImplicitVariableName?: string, |
| 52 | ): void { |
| 53 | const parts = expression.split(','); |
| 54 | let startSpan = span.start; |
| 55 | for (const part of parts) { |
| 56 | const expressionParts = part.split('='); |
| 57 | const name = expressionParts[0].trim(); |
| 58 | let variableName = |
| 59 | expressionParts.length === 2 |
| 60 | ? expressionParts[1].trim() |
| 61 | : (defaultImplicitVariableName ?? ''); |
| 62 | |
| 63 | if (name.length === 0 || variableName.length === 0) { |
| 64 | errors.push( |
| 65 | new ParseError( |
| 66 | sourceSpan, |
| 67 | `Invalid ${messagePrefix} "let" parameter. Parameter should match the pattern "<name> = <variable name>"`, |
| 68 | ), |
| 69 | ); |
| 70 | } else if (!IDENTIFIER_PATTERN.test(name)) { |
| 71 | errors.push( |
| 72 | new ParseError(sourceSpan, `"let" parameter must be a valid JavaScript identifier`), |
| 73 | ); |
| 74 | } else { |
| 75 | validateLet(name, variableName, sourceSpan); |
| 76 | |
| 77 | const [, keyLeadingWhitespace, keyName] = |
| 78 | expressionParts[0].match(CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN) ?? []; |
| 79 | const keySpan = |
| 80 | keyLeadingWhitespace !== undefined |
| 81 | ? new ParseSourceSpan( |
| 82 | /* strip leading spaces */ |
| 83 | startSpan.moveBy(keyLeadingWhitespace.length), |
| 84 | /* advance to end of the variable name */ |
| 85 | startSpan.moveBy(keyLeadingWhitespace.length + keyName.length), |
| 86 | ) |
| 87 | : span; |
| 88 | |
| 89 | let valueSpan: ParseSourceSpan | undefined = undefined; |
| 90 | if (expressionParts.length === 2) { |
| 91 | const [, valueLeadingWhitespace, implicit] = |
| 92 | expressionParts[1].match(CHARACTERS_IN_SURROUNDING_WHITESPACE_PATTERN) ?? []; |
| 93 | valueSpan = |
| 94 | valueLeadingWhitespace !== undefined |
| 95 | ? new ParseSourceSpan( |
| 96 | startSpan.moveBy(expressionParts[0].length + 1 + valueLeadingWhitespace.length), |
| 97 | startSpan.moveBy( |
| 98 | expressionParts[0].length + 1 + valueLeadingWhitespace.length + implicit.length, |
| 99 | ), |
| 100 | ) |
no test coverage detected