Skip to content

Commit fc13144

Browse files
crisbetodylhunn
authored andcommitted
refactor(compiler-cli): exit early when checking content projection (#54921)
Updates the logic that detects if a node should be checked for control flow content projection to exit as soon as it detects a second root node, instead of counting the total and then checking if it's more than one. PR Close #54921
1 parent a369f43 commit fc13144

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_block.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,23 +1035,26 @@ class TcbControlFlowContentProjectionOp extends TcbOp {
10351035
return false;
10361036
}
10371037

1038-
// Count the number of root nodes while skipping empty text where relevant.
1039-
const rootNodeCount = node.children.reduce((count, node) => {
1038+
let hasSeenRootNode = false;
1039+
1040+
// Check the number of root nodes while skipping empty text where relevant.
1041+
for (const child of node.children) {
10401042
// Normally `preserveWhitspaces` would have been accounted for during parsing, however
10411043
// in `ngtsc/annotations/component/src/resources.ts#parseExtractedTemplate` we enable
10421044
// `preserveWhitespaces` to preserve the accuracy of source maps diagnostics. This means
10431045
// that we have to account for it here since the presence of text nodes affects the
10441046
// content projection behavior.
1045-
if (!(node instanceof TmplAstText) || this.tcb.hostPreserveWhitespaces ||
1046-
node.value.trim().length > 0) {
1047-
count++;
1047+
if (!(child instanceof TmplAstText) || this.tcb.hostPreserveWhitespaces ||
1048+
child.value.trim().length > 0) {
1049+
// Content projection will be affected if there's more than one root node.
1050+
if (hasSeenRootNode) {
1051+
return true;
1052+
}
1053+
hasSeenRootNode = true;
10481054
}
1055+
}
10491056

1050-
return count;
1051-
}, 0);
1052-
1053-
// Content projection can only be affected if there is more than one root node.
1054-
return rootNodeCount > 1;
1057+
return false;
10551058
}
10561059
}
10571060

0 commit comments

Comments
 (0)