highlightjs / highlightjs/highlight.js
(Python) f-string nested replacement field in a format spec closes `subst` at the wrong brace
- Dominant language
- JavaScript
- Stars
- 25k
- Forks
- 3.8k
- Avg merge
- 11h 35m
- Merged PRs (30d)
- 3
Description
**Which language:** Python (specified explicitly, not auto-detect)
**Version:** 11.12.0
**Using:** `highlight` (not `highlightAuto`)
**Describe the issue**
A nested replacement field inside an f-string format spec (dynamic width/precision, e.g. `f"{x:{w}}"`) closes the `subst` region on the *inner* `}`. The field's own closing `}` is then emitted as plain string text, and the `subst` span ends up brace-unbalanced.
**Sample Code to Reproduce**
```python
f"{x:{w}}"
```
**Actual** — `hljs.highlight('f"{x:{w}}"', {language:'python'}).value`:
```html
f"{x:{w}}"
```
The `subst` span is `{x:{w}` (an unmatched `{`) and the final `}` renders in string color instead of as part of the field. `f"{value:{width}.{prec}f}"` fails the same way.
**Expected behavior**
The whole `{x:{w}}` is a single replacement field, and the nested `{w}` is itself a field. This is valid Python — `{w}` sets the width dynamically (`f"{x:{w}}"` with `x=42, w=6` → `' 42'`). The closing `}` should belong to the field, not leak into the string, and the `subst` region should be brace-balanced.
**Cause**
`SUBST` in `src/languages/python.js` (`begin:/\{/, end:/\}/`) does not `contain` itself (`SUBST.contains = [STRING, NUMBER, PROMPT]`), so it ends at the first inner `}` and cannot handle nested replacement fields.
Assisted-by: Claude Opus 4 (high)
Contributor guide
Research direction
Start in src/languages/python.js, inspect the SUBST grammar and reproduce the issue with the f-string examples using highlight with the Python language selected. The work is done when nested replacement fields remain brace-balanced, the whole {x:{w}} field is highlighted correctly, and the final } no longer renders as plain string text.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 74/100