Skip to content

Commit 7b5625e

Browse files
crisbetothePunderWoman
authored andcommitted
refactor(compiler): isolate arrow function processing
Isolates the logic that fixes references to arrow function parameters so that we don't have do pass extra parameters for every `convertAst` call.
1 parent 4cf1a92 commit 7b5625e

2 files changed

Lines changed: 79 additions & 61 deletions

File tree

  • packages
    • compiler-cli/test/compliance/test_cases/r3_view_compiler_arrow_functions
    • compiler/src/template/pipeline/src

‎packages/compiler-cli/test/compliance/test_cases/r3_view_compiler_arrow_functions/TEST_CASES.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"$schema": "../test_case_schema.json",
33
"cases": [
44
{
5-
"description": "should handle arrow functions that do not depend on ctext",
5+
"description": "should handle arrow functions that do not depend on context",
66
"inputFiles": ["arrow_function_no_context.ts"],
77
"expectations": [
88
{

‎packages/compiler/src/template/pipeline/src/ingest.ts‎

Lines changed: 78 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ export function ingestDomProperty(
152152
if (ast instanceof e.Interpolation) {
153153
expression = new ir.Interpolation(
154154
ast.strings,
155-
ast.expressions.map((expr) => convertAst(expr, job, property.sourceSpan, null)),
155+
ast.expressions.map((expr) => convertAst(expr, job, property.sourceSpan)),
156156
[],
157157
);
158158
} else {
159-
expression = convertAst(ast, job, property.sourceSpan, null);
159+
expression = convertAst(ast, job, property.sourceSpan);
160160
}
161161
job.root.update.push(
162162
ir.createBindingOp(
@@ -507,7 +507,7 @@ function ingestBoundText(
507507
textXref,
508508
new ir.Interpolation(
509509
value.strings,
510-
value.expressions.map((expr) => convertAst(expr, unit.job, baseSourceSpan, null)),
510+
value.expressions.map((expr) => convertAst(expr, unit.job, baseSourceSpan)),
511511
i18nPlaceholders,
512512
),
513513
text.sourceSpan,
@@ -556,7 +556,7 @@ function ingestIfBlock(unit: ViewCompilationUnit, ifBlock: t.IfBlock): void {
556556
firstXref = cView.xref;
557557
}
558558

559-
const caseExpr = ifCase.expression ? convertAst(ifCase.expression, unit.job, null, null) : null;
559+
const caseExpr = ifCase.expression ? convertAst(ifCase.expression, unit.job, null) : null;
560560
const conditionalCaseExpr = new ir.ConditionalCaseExpr(
561561
caseExpr,
562562
conditionalCreateOp.xref,
@@ -614,7 +614,7 @@ function ingestSwitchBlock(unit: ViewCompilationUnit, switchBlock: t.SwitchBlock
614614

615615
for (const switchCase of switchCaseGroup.cases) {
616616
const caseExpr = switchCase.expression
617-
? convertAst(switchCase.expression, unit.job, switchBlock.startSourceSpan, null)
617+
? convertAst(switchCase.expression, unit.job, switchBlock.startSourceSpan)
618618
: null;
619619
const conditionalCaseExpr = new ir.ConditionalCaseExpr(
620620
caseExpr,
@@ -628,7 +628,7 @@ function ingestSwitchBlock(unit: ViewCompilationUnit, switchBlock: t.SwitchBlock
628628
unit.update.push(
629629
ir.createConditionalOp(
630630
firstXref!,
631-
convertAst(switchBlock.expression, unit.job, null, null),
631+
convertAst(switchBlock.expression, unit.job, null),
632632
conditions,
633633
switchBlock.sourceSpan,
634634
),
@@ -865,7 +865,7 @@ function ingestDeferTriggers(
865865
targetView: null,
866866
targetSlotViewSteps: null,
867867
options: triggers.viewport.options
868-
? convertAst(triggers.viewport.options, unit.job, triggers.viewport.sourceSpan, null)
868+
? convertAst(triggers.viewport.options, unit.job, triggers.viewport.sourceSpan)
869869
: null,
870870
},
871871
modifier,
@@ -890,7 +890,7 @@ function ingestDeferTriggers(
890890
}
891891
const deferOnOp = ir.createDeferWhenOp(
892892
deferXref,
893-
convertAst(triggers.when.value, unit.job, triggers.when.sourceSpan, null),
893+
convertAst(triggers.when.value, unit.job, triggers.when.sourceSpan),
894894
modifier,
895895
triggers.when.sourceSpan,
896896
);
@@ -952,7 +952,7 @@ function ingestForBlock(unit: ViewCompilationUnit, forBlock: t.ForLoopBlock): vo
952952
}
953953

954954
const sourceSpan = convertSourceSpan(forBlock.trackBy.span, forBlock.sourceSpan);
955-
const track = convertAst(forBlock.trackBy, unit.job, sourceSpan, null);
955+
const track = convertAst(forBlock.trackBy, unit.job, sourceSpan);
956956

957957
ingestNodes(repeaterView, forBlock.children);
958958

@@ -1000,7 +1000,6 @@ function ingestForBlock(unit: ViewCompilationUnit, forBlock: t.ForLoopBlock): vo
10001000
forBlock.expression,
10011001
unit.job,
10021002
convertSourceSpan(forBlock.expression.span, forBlock.sourceSpan),
1003-
null,
10041003
);
10051004
const repeater = ir.createRepeaterOp(
10061005
repeaterCreate.xref,
@@ -1056,7 +1055,7 @@ function ingestLetDeclaration(unit: ViewCompilationUnit, node: t.LetDeclaration)
10561055
ir.createStoreLetOp(
10571056
target,
10581057
node.name,
1059-
convertAst(node.value, unit.job, node.valueSpan, null),
1058+
convertAst(node.value, unit.job, node.valueSpan),
10601059
node.sourceSpan,
10611060
),
10621061
);
@@ -1069,18 +1068,15 @@ function convertAst(
10691068
ast: e.AST,
10701069
job: CompilationJob,
10711070
baseSourceSpan: ParseSourceSpan | null,
1072-
localSymbols: Set<string> | null,
10731071
): o.Expression {
10741072
if (ast instanceof e.ASTWithSource) {
1075-
return convertAst(ast.ast, job, baseSourceSpan, localSymbols);
1073+
return convertAst(ast.ast, job, baseSourceSpan);
10761074
} else if (ast instanceof e.PropertyRead) {
10771075
if (ast.receiver instanceof e.ImplicitReceiver) {
1078-
return localSymbols !== null && localSymbols.has(ast.name)
1079-
? o.variable(ast.name)
1080-
: new ir.LexicalReadExpr(ast.name);
1076+
return new ir.LexicalReadExpr(ast.name);
10811077
} else {
10821078
return new o.ReadPropExpr(
1083-
convertAst(ast.receiver, job, baseSourceSpan, localSymbols),
1079+
convertAst(ast.receiver, job, baseSourceSpan),
10841080
ast.name,
10851081
null,
10861082
convertSourceSpan(ast.span, baseSourceSpan),
@@ -1091,8 +1087,8 @@ function convertAst(
10911087
throw new Error(`Unexpected ImplicitReceiver`);
10921088
} else {
10931089
return new o.InvokeFunctionExpr(
1094-
convertAst(ast.receiver, job, baseSourceSpan, localSymbols),
1095-
ast.args.map((arg) => convertAst(arg, job, baseSourceSpan, localSymbols)),
1090+
convertAst(ast.receiver, job, baseSourceSpan),
1091+
ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)),
10961092
undefined,
10971093
convertSourceSpan(ast.span, baseSourceSpan),
10981094
);
@@ -1104,14 +1100,14 @@ function convertAst(
11041100
case '+':
11051101
return new o.UnaryOperatorExpr(
11061102
o.UnaryOperator.Plus,
1107-
convertAst(ast.expr, job, baseSourceSpan, localSymbols),
1103+
convertAst(ast.expr, job, baseSourceSpan),
11081104
undefined,
11091105
convertSourceSpan(ast.span, baseSourceSpan),
11101106
);
11111107
case '-':
11121108
return new o.UnaryOperatorExpr(
11131109
o.UnaryOperator.Minus,
1114-
convertAst(ast.expr, job, baseSourceSpan, localSymbols),
1110+
convertAst(ast.expr, job, baseSourceSpan),
11151111
undefined,
11161112
convertSourceSpan(ast.span, baseSourceSpan),
11171113
);
@@ -1125,8 +1121,8 @@ function convertAst(
11251121
}
11261122
return new o.BinaryOperatorExpr(
11271123
operator,
1128-
convertAst(ast.left, job, baseSourceSpan, localSymbols),
1129-
convertAst(ast.right, job, baseSourceSpan, localSymbols),
1124+
convertAst(ast.left, job, baseSourceSpan),
1125+
convertAst(ast.right, job, baseSourceSpan),
11301126
undefined,
11311127
convertSourceSpan(ast.span, baseSourceSpan),
11321128
);
@@ -1135,16 +1131,16 @@ function convertAst(
11351131
return new ir.ContextExpr(job.root.xref);
11361132
} else if (ast instanceof e.KeyedRead) {
11371133
return new o.ReadKeyExpr(
1138-
convertAst(ast.receiver, job, baseSourceSpan, localSymbols),
1139-
convertAst(ast.key, job, baseSourceSpan, localSymbols),
1134+
convertAst(ast.receiver, job, baseSourceSpan),
1135+
convertAst(ast.key, job, baseSourceSpan),
11401136
undefined,
11411137
convertSourceSpan(ast.span, baseSourceSpan),
11421138
);
11431139
} else if (ast instanceof e.Chain) {
11441140
throw new Error(`AssertionError: Chain in unknown context`);
11451141
} else if (ast instanceof e.LiteralMap) {
11461142
const entries = ast.keys.map((key, idx) => {
1147-
const value = convertAst(ast.values[idx], job, baseSourceSpan, localSymbols);
1143+
const value = convertAst(ast.values[idx], job, baseSourceSpan);
11481144

11491145
// TODO: should literals have source maps, or do we just map the whole surrounding
11501146
// expression?
@@ -1156,83 +1152,80 @@ function convertAst(
11561152
} else if (ast instanceof e.LiteralArray) {
11571153
// TODO: should literals have source maps, or do we just map the whole surrounding expression?
11581154
return new o.LiteralArrayExpr(
1159-
ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan, localSymbols)),
1155+
ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan)),
11601156
);
11611157
} else if (ast instanceof e.Conditional) {
11621158
return new o.ConditionalExpr(
1163-
convertAst(ast.condition, job, baseSourceSpan, localSymbols),
1164-
convertAst(ast.trueExp, job, baseSourceSpan, localSymbols),
1165-
convertAst(ast.falseExp, job, baseSourceSpan, localSymbols),
1159+
convertAst(ast.condition, job, baseSourceSpan),
1160+
convertAst(ast.trueExp, job, baseSourceSpan),
1161+
convertAst(ast.falseExp, job, baseSourceSpan),
11661162
undefined,
11671163
convertSourceSpan(ast.span, baseSourceSpan),
11681164
);
11691165
} else if (ast instanceof e.NonNullAssert) {
11701166
// A non-null assertion shouldn't impact generated instructions, so we can just drop it.
1171-
return convertAst(ast.expression, job, baseSourceSpan, localSymbols);
1167+
return convertAst(ast.expression, job, baseSourceSpan);
11721168
} else if (ast instanceof e.BindingPipe) {
11731169
// TODO: pipes should probably have source maps; figure out details.
11741170
return new ir.PipeBindingExpr(job.allocateXrefId(), new ir.SlotHandle(), ast.name, [
1175-
convertAst(ast.exp, job, baseSourceSpan, localSymbols),
1176-
...ast.args.map((arg) => convertAst(arg, job, baseSourceSpan, localSymbols)),
1171+
convertAst(ast.exp, job, baseSourceSpan),
1172+
...ast.args.map((arg) => convertAst(arg, job, baseSourceSpan)),
11771173
]);
11781174
} else if (ast instanceof e.SafeKeyedRead) {
11791175
return new ir.SafeKeyedReadExpr(
1180-
convertAst(ast.receiver, job, baseSourceSpan, localSymbols),
1181-
convertAst(ast.key, job, baseSourceSpan, localSymbols),
1176+
convertAst(ast.receiver, job, baseSourceSpan),
1177+
convertAst(ast.key, job, baseSourceSpan),
11821178
convertSourceSpan(ast.span, baseSourceSpan),
11831179
);
11841180
} else if (ast instanceof e.SafePropertyRead) {
11851181
// TODO: source span
1186-
return new ir.SafePropertyReadExpr(
1187-
convertAst(ast.receiver, job, baseSourceSpan, localSymbols),
1188-
ast.name,
1189-
);
1182+
return new ir.SafePropertyReadExpr(convertAst(ast.receiver, job, baseSourceSpan), ast.name);
11901183
} else if (ast instanceof e.SafeCall) {
11911184
// TODO: source span
11921185
return new ir.SafeInvokeFunctionExpr(
1193-
convertAst(ast.receiver, job, baseSourceSpan, localSymbols),
1194-
ast.args.map((a) => convertAst(a, job, baseSourceSpan, localSymbols)),
1186+
convertAst(ast.receiver, job, baseSourceSpan),
1187+
ast.args.map((a) => convertAst(a, job, baseSourceSpan)),
11951188
);
11961189
} else if (ast instanceof e.EmptyExpr) {
11971190
return new ir.EmptyExpr(convertSourceSpan(ast.span, baseSourceSpan));
11981191
} else if (ast instanceof e.PrefixNot) {
11991192
return o.not(
1200-
convertAst(ast.expression, job, baseSourceSpan, localSymbols),
1193+
convertAst(ast.expression, job, baseSourceSpan),
12011194
convertSourceSpan(ast.span, baseSourceSpan),
12021195
);
12031196
} else if (ast instanceof e.TypeofExpression) {
1204-
return o.typeofExpr(convertAst(ast.expression, job, baseSourceSpan, localSymbols));
1197+
return o.typeofExpr(convertAst(ast.expression, job, baseSourceSpan));
12051198
} else if (ast instanceof e.VoidExpression) {
12061199
return new o.VoidExpr(
1207-
convertAst(ast.expression, job, baseSourceSpan, localSymbols),
1200+
convertAst(ast.expression, job, baseSourceSpan),
12081201
undefined,
12091202
convertSourceSpan(ast.span, baseSourceSpan),
12101203
);
12111204
} else if (ast instanceof e.TemplateLiteral) {
1212-
return convertTemplateLiteral(ast, job, baseSourceSpan, localSymbols);
1205+
return convertTemplateLiteral(ast, job, baseSourceSpan);
12131206
} else if (ast instanceof e.TaggedTemplateLiteral) {
12141207
return new o.TaggedTemplateLiteralExpr(
1215-
convertAst(ast.tag, job, baseSourceSpan, localSymbols),
1216-
convertTemplateLiteral(ast.template, job, baseSourceSpan, localSymbols),
1208+
convertAst(ast.tag, job, baseSourceSpan),
1209+
convertTemplateLiteral(ast.template, job, baseSourceSpan),
12171210
undefined,
12181211
convertSourceSpan(ast.span, baseSourceSpan),
12191212
);
12201213
} else if (ast instanceof e.ParenthesizedExpression) {
12211214
return new o.ParenthesizedExpr(
1222-
convertAst(ast.expression, job, baseSourceSpan, localSymbols),
1215+
convertAst(ast.expression, job, baseSourceSpan),
12231216
undefined,
12241217
convertSourceSpan(ast.span, baseSourceSpan),
12251218
);
12261219
} else if (ast instanceof e.RegularExpressionLiteral) {
12271220
return new o.RegularExpressionLiteralExpr(ast.body, ast.flags, baseSourceSpan);
12281221
} else if (ast instanceof e.SpreadElement) {
1229-
return new o.SpreadElementExpr(convertAst(ast.expression, job, baseSourceSpan, localSymbols));
1222+
return new o.SpreadElementExpr(convertAst(ast.expression, job, baseSourceSpan));
12301223
} else if (ast instanceof e.ArrowFunction) {
1231-
localSymbols ??= new Set();
1232-
ast.parameters.forEach((arg) => localSymbols!.add(arg.name));
1233-
return o.arrowFn(
1234-
ast.parameters.map((arg) => new o.FnParam(arg.name)),
1235-
convertAst(ast.body, job, baseSourceSpan, localSymbols),
1224+
return updateParameterReferences(
1225+
o.arrowFn(
1226+
ast.parameters.map((arg) => new o.FnParam(arg.name)),
1227+
convertAst(ast.body, job, baseSourceSpan),
1228+
),
12361229
);
12371230
} else {
12381231
throw new Error(
@@ -1245,13 +1238,12 @@ function convertTemplateLiteral(
12451238
ast: e.TemplateLiteral,
12461239
job: CompilationJob,
12471240
baseSourceSpan: ParseSourceSpan | null,
1248-
localSymbols: Set<string> | null,
12491241
) {
12501242
return new o.TemplateLiteralExpr(
12511243
ast.elements.map((el) => {
12521244
return new o.TemplateLiteralElementExpr(el.text, convertSourceSpan(el.span, baseSourceSpan));
12531245
}),
1254-
ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan, localSymbols)),
1246+
ast.expressions.map((expr) => convertAst(expr, job, baseSourceSpan)),
12551247
convertSourceSpan(ast.span, baseSourceSpan),
12561248
);
12571249
}
@@ -1266,11 +1258,11 @@ function convertAstWithInterpolation(
12661258
if (value instanceof e.Interpolation) {
12671259
expression = new ir.Interpolation(
12681260
value.strings,
1269-
value.expressions.map((e) => convertAst(e, job, sourceSpan ?? null, null)),
1261+
value.expressions.map((e) => convertAst(e, job, sourceSpan ?? null)),
12701262
Object.keys(asMessage(i18nMeta)?.placeholders ?? {}),
12711263
);
12721264
} else if (value instanceof e.AST) {
1273-
expression = convertAst(value, job, sourceSpan ?? null, null);
1265+
expression = convertAst(value, job, sourceSpan ?? null);
12741266
} else {
12751267
expression = o.literal(value);
12761268
}
@@ -1742,7 +1734,7 @@ function makeListenerHandlerOps(
17421734
if (handlerExprs.length === 0) {
17431735
throw new Error('Expected listener to have non-empty expression list.');
17441736
}
1745-
const expressions = handlerExprs.map((expr) => convertAst(expr, unit.job, handlerSpan, null));
1737+
const expressions = handlerExprs.map((expr) => convertAst(expr, unit.job, handlerSpan));
17461738
const returnExpr = expressions.pop()!;
17471739
handlerOps.push(
17481740
...expressions.map((e) =>
@@ -1770,7 +1762,7 @@ function makeTwoWayListenerHandlerOps(
17701762
}
17711763
}
17721764

1773-
const handlerExpr = convertAst(handler, unit.job, handlerSpan, null);
1765+
const handlerExpr = convertAst(handler, unit.job, handlerSpan);
17741766
const eventReference = new ir.LexicalReadExpr('$event');
17751767
const twoWaySetExpr = new ir.TwoWayBindingSetExpr(handlerExpr, eventReference);
17761768

@@ -1938,3 +1930,29 @@ function ingestControlFlowInsertionPoint(
19381930

19391931
return null;
19401932
}
1933+
1934+
/**
1935+
* When an arrow function in the expression AST is converted into the output AST, all of its
1936+
* top-level reads become `LexicalReadExpr` because the output AST doesn't have a concept of a
1937+
* variable read. This function corrects the ones that point to parameters.
1938+
*
1939+
* @param root Root arrow function.
1940+
*/
1941+
function updateParameterReferences(root: o.ArrowFunctionExpr): o.ArrowFunctionExpr {
1942+
const parameterNames = new Set(root.params.map((param) => param.name));
1943+
1944+
return ir.transformExpressionsInExpression(
1945+
root,
1946+
(expr) => {
1947+
if (expr instanceof o.ArrowFunctionExpr) {
1948+
for (const param of expr.params) {
1949+
parameterNames.add(param.name);
1950+
}
1951+
} else if (expr instanceof ir.LexicalReadExpr && parameterNames.has(expr.name)) {
1952+
return o.variable(expr.name);
1953+
}
1954+
return expr;
1955+
},
1956+
ir.VisitorContextFlag.None,
1957+
) as o.ArrowFunctionExpr;
1958+
}

0 commit comments

Comments
 (0)