[core] Review cleanup: docstring accuracy, rationale comments, unsafe+no_split test

- Fix the ComponentMarker docstring's incomplete 'either placement-news or mutates a global' claim — acknowledge that function-local patterns also exist and note the bare-local detection covers them.
- Document that _emits_bare_local's RawExpression detection is intentionally safety-biased: false negatives break compilation, false positives just keep a slightly larger IIFE. Note the CallExpression(..., RawExpression) negative case explicitly.
- Explain the mutable-list-flag pattern in cpp_main_section — dataclass would read cleaner but the pattern is localized.
- Add regression test for a group with BOTH IIFEUnsafeStatement and a bare-local: unsafe wins (flat emission) because a return inside any IIFE, even a single big one, only exits the lambda.
This commit is contained in:
J. Nick Koston
2026-04-17 18:55:39 -05:00
parent 3ab935bebb
commit 093c34d4a4
3 changed files with 43 additions and 4 deletions
+18
View File
@@ -1107,6 +1107,24 @@ def test_cpp_main_section_typed_assignment_disables_sub_split() -> None:
assert out.count("[]() {") == 1
def test_cpp_main_section_iife_unsafe_wins_over_no_split() -> None:
# A group that triggers BOTH flags (IIFEUnsafeStatement present AND
# a bare-local emission) must still be emitted flat — the unsafe
# flag wins because a `return` inside any IIFE, even a single big
# one, only exits the lambda.
target = core.EsphomeCore()
target.main_statements = [
ComponentMarker("safe_mode_with_local"),
RawStatement("{"), # would trigger no_split
RawStatement("new_foo();"),
RawStatement("}"),
IIFEUnsafeStatement(RawExpression("if (cond) return")),
]
out = target.cpp_main_section
assert "[]() {" not in out
assert "if (cond) return;" in out
def test_cpp_main_section_iife_unsafe_statement_emits_component_flat() -> None:
# A component that emits IIFEUnsafeStatement (e.g. safe_mode with
# `if (...) return;`) must be emitted flat — a `return` inside an