Skip to content

Handle non-printable control characters when escaping JSON #7435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025

Conversation

mediremi
Copy link
Contributor

@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

@@ -1,153 +1,14 @@
let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: ))
type t = Ext_json_noloc.t
Copy link
Contributor 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

Copy link

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
Collaborator

@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
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
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