mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 03:16:37 +00:00
[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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user