JSON formatter: Escape for code gives broken Python when the text has a run of single quotes
I maintainer di solito rispondono entro 1 giorno
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
In the JSON formatter, open "Conversions · Validate · Query", pick "Escape for code" and set the target language to Python. With this JSON:
{"a":"'''''"}
the output is
'''{"a":"'\''''"}'''
and Python 3.10 rejects it:
SyntaxError: unterminated string literal (detected at line 1)
escapeForCode (lib/json-utils.ts, line 677) wraps the JSON in '''...''' and only handles runs of three quotes, by turning each ''' into '\'' (line 691). With five quotes, the two that are left over join the last ' of the replacement and make a new ''', which closes the string early. Eight quotes in a row break it the same way.
When the input isn't valid JSON, the function escapes the raw text instead (line 682), and that breaks whenever the text ends in a quote: it's' gives '''it's'''', where Python closes the string after it's and then finds a lone '.
Double quotes are fine. JSON already escapes them as \", and the backslash pass doubles the backslash.
What to do
- After the backslash pass, escape every single quote (
.replace(/'/g, "\\'")) instead of only'''. Inside'''...''',\'is always a plain quote, so the string can't close early. - Keep the backslash pass first, so the new backslashes don't get doubled.
- The other languages stay as they are.
Where to look
lib/json-utils.ts:escapeForCode(line 677) and its Python branch (line 691)lib/__tests__/json-utils.test.ts: theescapeForCode pythontests (line 214)app/(tools)/json-formatter/components/ConversionPanel.tsx: the language list (line 32) and the Escape for code mode (line 62), if you want to try it in the UI
Acceptance criteria
-
{"a":"'''''"}gives'''{"a":"\'\'\'\'\'"}''' -
{"a":"''''''''"}(eight quotes) gives'''{"a":"\'\'\'\'\'\'\'\'"}''' -
it's'gives'''it\'s\'''' - The existing backslash test still passes:
{"path":"C:\\temp"}gives'''{"path":"C:\\\\temp"}''' - Tests assert these exact strings (the ones that change fail on the current code). Each expected string evaluates in Python back to the input, so a quick check in
python3is an easy way to confirm a new case. -
npm run lint,npm run testandnpm run buildpass
Want to work on this? Comment below and I'll assign it to you. Please take one issue at a time, branch off develop, and open your PR against develop with Closes #233 in the description.
- Lingua principale
- TypeScript
- Stelle
- 1
- Fork
- 9
- Merge medio
- 9h 50m
- PR unite (30g)
- 105
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di codercops/toolbelt
-
JSONPath: big numbers in the results are roundedForse già presa @arxh80 l’ha presa oggi. Apertabug hacktoberfest help wanted
Difficoltà 3/5 1-2 giorni Idoneità per principianti 78/100
codercops/toolbelt#238 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Color converter: the contrast ratio shows `4.50 : 1` while Normal AA says FAILForse già presa @bercedev l’ha presa oggi. Apertabug good first issue hacktoberfest
codercops/toolbelt#235 · 5 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
JSON to YAML: control characters are written raw, so PyYAML and js-yaml can't read the outputForse già presa @anurag2787 l’ha presa oggi. Apertabug good first issue hacktoberfest
codercops/toolbelt#234 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
Add the URL encoder to the README, the home share card and package.jsonForse già presa @Abhishek764 l’ha presa 3 giorni fa. Apertadocumentation good first issue hacktoberfest
codercops/toolbelt#153 · 4 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement good first issue hacktoberfest new tool
codercops/toolbelt#128 · 9 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di codercops/toolbelt
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
kunchenguid/gh-axi#220 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 79/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
prerender: wait for env-runner worker readiness before crawlingForse già presa @sarankumar1325 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno