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 9m
- PR unite (30g)
- 118
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
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 55/100
I maintainer di solito rispondono entro 1 giorno
-
JWT encoder signs payloads that aren't JSON objects, and the decoder then rejects themForse già presa @MugdhoAI l’ha presa 1 giorno fa. Apertabug good first issue hacktoberfest
codercops/toolbelt#253 · 2 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 2 giorni fa. 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 4 giorni fa. Apertadocumentation good first issue hacktoberfest
codercops/toolbelt#153 · 4 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
hacktoberfest
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 1/100
codercops/toolbelt#101 · 1 commento ·
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 76/100
Nimblesite/SharpLsp#468 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Bundled uv is never updated and trusts the compromised eMudhra certificatesForse già presa @FabienDanieau l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
bg_wait's blocking mode (stopOnAttention: false) is under-documented and effectively undiscoverableAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
nicobailon/pi-subagents#3019 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
db-ux-design-system/core-web#8613 ·
I maintainer di solito rispondono entro 1 giorno