mirror of
https://github.com/esphome/esphome.git
synced 2026-10-01 08:50:22 +00:00
[core] Address Copilot review: robust brace depth, accurate docstrings
- Count { and } characters per line instead of matching whole-line
tokens. Current codegen only emits scope braces as standalone lines
(from cg.with_local_variable()), but the defensive change is robust
against future codegen emitting inline control flow like
`if (cond) {` or `} else {` on one line.
- Add a regression test covering those inline-brace patterns.
- Fix stale docstrings on ComponentMarker and cpp_main_section that
still claimed "stack frame released on return" and described the
IIFEs as "noinline". The IIFEs have no noinline attribute and rely
on scope-based lifetime shortening rather than guaranteed frames.
This commit is contained in:
@@ -934,6 +934,40 @@ def test_wrap_in_iifes_unbalanced_braces_fall_through() -> None:
|
||||
assert [line for line in result if line in lines] == lines
|
||||
|
||||
|
||||
def test_wrap_in_iifes_never_splits_inline_brace_lines() -> None:
|
||||
# Defensive: if codegen ever emits control flow with braces on the
|
||||
# same line (if/else/for), the depth tracker should keep the whole
|
||||
# scoped block together even with aggressive max_statements.
|
||||
lines = [
|
||||
"before();",
|
||||
"if (cond) {",
|
||||
"then_branch();",
|
||||
"} else {",
|
||||
"for (;;) {",
|
||||
"loop_body();",
|
||||
"}",
|
||||
"}",
|
||||
"after();",
|
||||
]
|
||||
assert core._wrap_in_iifes(lines, max_statements=1) == [
|
||||
"[]() {",
|
||||
"before();",
|
||||
"}();",
|
||||
"[]() {",
|
||||
"if (cond) {",
|
||||
"then_branch();",
|
||||
"} else {",
|
||||
"for (;;) {",
|
||||
"loop_body();",
|
||||
"}",
|
||||
"}",
|
||||
"}();",
|
||||
"[]() {",
|
||||
"after();",
|
||||
"}();",
|
||||
]
|
||||
|
||||
|
||||
def test_wrap_in_iifes_skips_comment_only_chunks() -> None:
|
||||
# Components that emit only a ComponentMarker + config dump (no C++
|
||||
# statements) should not be wrapped in an empty IIFE.
|
||||
|
||||
Reference in New Issue
Block a user