MCPcopy Create free account
hub / github.com/angular/angular / parseLetParameters

Function parseLetParameters

packages/compiler/src/render3/util.ts:43–108  ·  view source on GitHub ↗
(
  sourceSpan: ParseSourceSpan,
  expression: string,
  span: ParseSourceSpan,
  context: t.Variable[],
  errors: ParseError[],
  validateLet: (name: string, variableName: string, sourceSpan: ParseSourceSpan) => void,
  messagePrefix: string,
  defaultImplicitVariableName?: string,
)

Source from the content-addressed store, hash-verified

41 * @param defaultImplicitVariableName An optional default variable name to use if no value is provided.
42 */
43export 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 )

Callers 2

parseForLoopParametersFunction · 0.90
createBoundaryBlockFunction · 0.90

Calls 4

moveByMethod · 0.80
pushMethod · 0.45
testMethod · 0.45
matchMethod · 0.45

Tested by

no test coverage detected