JSON formatter: Escape for code gives broken Python when the text has a run of single quotes
维护者通常 1 天内回复
评估
这个 Issue 还没有评估数据。
描述
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.
- 主要语言
- TypeScript
- 星标
- 1
- 派生
- 9
- 平均合并
- 9 小时 50 分钟
- 30 天内合并 PR
- 105
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
codercops/toolbelt 的其他 Issue
-
JSONPath: big numbers in the results are rounded可能已有人在做 @arxh80 今天认领。 未关闭bug hacktoberfest help wanted
难度 3/5 1-2 天 新手友好度 78/100
codercops/toolbelt#238 · 1 条评论 ·
维护者通常 1 天内回复
-
Color converter: the contrast ratio shows `4.50 : 1` while Normal AA says FAIL可能已有人在做 @bercedev 今天认领。 未关闭bug good first issue hacktoberfest
codercops/toolbelt#235 · 5 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
JSON to YAML: control characters are written raw, so PyYAML and js-yaml can't read the output可能已有人在做 @anurag2787 今天认领。 未关闭bug good first issue hacktoberfest
codercops/toolbelt#234 · 2 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
Add the URL encoder to the README, the home share card and package.json可能已有人在做 @Abhishek764 于 2 天前认领。 未关闭documentation good first issue hacktoberfest
codercops/toolbelt#153 · 4 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
-
enhancement good first issue hacktoberfest new tool
codercops/toolbelt#128 · 9 条评论 · 已指派 1 人 ·
维护者通常 1 天内回复
查看 codercops/toolbelt 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
难度 1/5 1-3 小时 新手友好度 72/100
zts212653/clowder-ai#1678 ·
维护者通常 1 天内回复
-
good first issue
难度 2/5 1-3 小时 新手友好度 64/100
Progressive-Victory/the-website#680 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 68/100