Skip to content

Commit

Permalink
feat(napi): Add {Rule,Token}Node::toJSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Mar 27, 2024
1 parent 980f664 commit 9124880
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/warm-maps-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": minor
---

Add RuleNode/TokenNode::toJSON() in the TypeScript API
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/codegen/parser/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ napi = { workspace = true, optional = true }
napi-derive = { workspace = true, optional = true }
nom = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true, optional = true }
strum = { workspace = true }
strum_macros = { workspace = true }

# Since the source code is copied over as-is, we need to retain exact feature set with target crate,
# i.e `slang_solidity_node_addon`.
[features]
default = ["slang_napi_interfaces"]
slang_napi_interfaces = ["dep:napi", "dep:napi-derive"]
slang_napi_interfaces = ["dep:napi", "dep:napi-derive", "dep:serde_json"]

[lints]
workspace = true
Expand Down
16 changes: 16 additions & 0 deletions crates/codegen/parser/runtime/src/napi_interface/cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ impl RuleNode {
.into()
}

#[napi(catch_unwind, js_name = "toJSON")]
/// Serialize the token node to JSON.
///
/// This method is intended for debugging purposes and may not be stable.
pub fn to_json(&self) -> String {
serde_json::to_string(&self.0).unwrap()
}

#[napi(catch_unwind)]
pub fn unparse(&self) -> String {
self.0.clone().unparse()
Expand Down Expand Up @@ -134,6 +142,14 @@ impl TokenNode {
self.0.text.clone()
}

#[napi(catch_unwind, js_name = "toJSON")]
/// Serialize the token node to JSON.
///
/// This method is intended for debugging purposes and may not be stable.
pub fn to_json(&self) -> String {
serde_json::to_string(&self.0).unwrap()
}

#[napi(ts_return_type = "cursor.Cursor", catch_unwind)]
pub fn create_cursor(
&self,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ crate-type = ["cdylib"]
default = ["slang_napi_interfaces"]
slang_napi_interfaces = [
# This enables '#[napi]' attributes on the Rust types imported via [lib.path] above.
"dep:serde_json",
]

[build-dependencies]
Expand All @@ -43,6 +44,7 @@ napi-derive = { workspace = true }
nom = { workspace = true }
semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true, optional = true }
strum = { workspace = true }
strum_macros = { workspace = true }
thiserror = { workspace = true }
Expand Down
12 changes: 12 additions & 0 deletions crates/solidity/outputs/npm/package/src/generated/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ crate-type = ["cdylib"]
default = ["slang_napi_interfaces"]
slang_napi_interfaces = [
# This enables '#[napi]' attributes on the Rust types imported via [lib.path] above.
"dep:serde_json",
]

[build-dependencies]
Expand All @@ -43,6 +44,7 @@ napi-derive = { workspace = true }
nom = { workspace = true }
semver = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true, optional = true }
strum = { workspace = true }
strum_macros = { workspace = true }
thiserror = { workspace = true }
Expand Down
12 changes: 12 additions & 0 deletions crates/testlang/outputs/npm/package/src/generated/index.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9124880

Please sign in to comment.