MCPcopy
hub / github.com/github/spec-kit / _is_single_expression

Function _is_single_expression

src/specify_cli/workflows/expressions.py:149–183  ·  view source on GitHub ↗

True when *stripped* is exactly one top-level ``{{ ... }}`` block. Scans the block body for a ``}}`` that would close it early, ignoring any braces inside string literals. This keeps a lone expression whose string argument contains a literal ``{{`` or ``}}`` (e.g. ``{{ inputs.text |

(stripped: str)

Source from the content-addressed store, hash-verified

147
148
149def _is_single_expression(stripped: str) -> bool:
150 """True when *stripped* is exactly one top-level ``{{ ... }}`` block.
151
152 Scans the block body for a ``}}`` that would close it early, ignoring any
153 braces inside string literals. This keeps a lone expression whose string
154 argument contains a literal ``{{`` or ``}}`` (e.g.
155 ``{{ inputs.text | contains('}}') }}``) on the typed fast path, while
156 ``{{ a }} {{ b }}`` and ``{{ a }}{{ b }}`` are correctly seen as
157 multi-expression. Mirrors the quote handling in
158 ``_split_top_level_commas``.
159
160 A regex span check cannot decide this: the pattern's non-greedy body stops
161 at the first ``}}``, so a literal ``}}`` inside a string argument would be
162 mistaken for the closing delimiter (issue #3208, follow-up review).
163 """
164 if not (stripped.startswith("{{") and stripped.endswith("}}")):
165 return False
166 inner = stripped[2:-2]
167 if not inner.strip():
168 return False
169 quote: str | None = None
170 i = 0
171 n = len(inner)
172 while i < n:
173 ch = inner[i]
174 if quote is not None:
175 if ch == quote:
176 quote = None
177 elif ch in ("'", '"'):
178 quote = ch
179 elif ch == "}" and i + 1 < n and inner[i + 1] == "}":
180 # A ``}}`` outside quotes closes the first block early.
181 return False
182 i += 1
183 return True
184
185
186def _split_top_level_commas(text: str) -> list[str]:

Callers 1

evaluate_expressionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected