Skip to content

Handle non-printable control characters when escaping JSON#7435

Merged
zth merged 5 commits intorescript-lang:masterfrom
mediremi:escape-json-control-characters
May 7, 2025
Merged

Handle non-printable control characters when escaping JSON#7435
zth merged 5 commits intorescript-lang:masterfrom
mediremi:escape-json-control-characters

Conversation

@mediremi
Copy link
Copy Markdown
Member

@mediremi mediremi commented May 6, 2025

When the VS Code extension calls rescript-editor-analysis, it replies with JSON serialised by analysis/vendor/json/Json.ml.

While Json.escape escapes printable characters correctly, it did not handle non-printable control characters (i.e. characters with a char code < 0x20). This meant that JSON sent to VS code was not always escaped properly and prevented code actions from being available in certain situations.

For example, opening up https://github.com/rescript-lang/rescript-lang.org/blob/master/src/bindings/RescriptCompilerApi.res in VS Code currently results in SyntaxError: Bad control character in string literal in JSON at position 1319 (line 15 column 751):

image

In the example above, this is caused by Warning.toCompactErrorLine containing a string with control character 0x27.

With the escaping logic I've added, this issue no longer occurs.

Changes I've made

  • Handled control characters in analysis/vendor/json/Json.ml
  • Renamed tests/ounit_tests/ounit_json_tests.ml to tests/ounit_tests/ounit_ext_json_tests.ml
  • Created a tests/ounit_tests/ounit_json_tests.ml test suite for analysis/vendor/json/Json.ml
  • Handled control characters in compiler/ext/ext_json_noloc.ml, which is used by the bsb watcher

Future work

  • analysis/vendor/json/Json.ml and compiler/ext/ext_json should probably be eventually replaced with Yojson

Comment thread analysis/vendor/json/Json.ml
@@ -1,153 +1,14 @@
let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: ))
type t = Ext_json_noloc.t
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The existing Ext_json tests were moved to tests/ounit_tests/ounit_ext_json_tests.ml

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 6, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7435

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7435

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7435

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7435

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7435

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7435

commit: 3c376bc

Copy link
Copy Markdown
Member

@zth zth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good to me, thank you! @cristianoc any thoughts?

@cristianoc
Copy link
Copy Markdown
Collaborator

This is looking good to me, thank you! @cristianoc any thoughts?

Looks great!

@zth zth merged commit 90d5daf into rescript-lang:master May 7, 2025
21 checks passed
@mediremi mediremi deleted the escape-json-control-characters branch May 24, 2025 16:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants