Skip to content

Commit

Permalink
Fix TS api types and add some cursor tests (NomicFoundation#623)
Browse files Browse the repository at this point in the history
Co-authored-by: Igor Matuszewski <[email protected]>
  • Loading branch information
AntonyBlakey and Xanewok authored Oct 28, 2023
1 parent e37d803 commit 80114a8
Show file tree
Hide file tree
Showing 19 changed files with 562 additions and 89 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-rabbits-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/slang": patch
---

Correct the types in the TS api by adding the correct namespaces to type references
22 changes: 15 additions & 7 deletions crates/codegen/parser/runtime/src/napi/napi_cst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,21 @@ impl RuleNode {
NodeType::Rule
}

#[napi(getter)]
#[napi(getter, ts_return_type = "kinds.RuleKind")]
pub fn kind(&self) -> RuleKind {
self.0.kind
}

#[napi(getter, js_name = "textLength")]
#[napi(
getter,
js_name = "textLength",
ts_return_type = "text_index.TextIndex"
)]
pub fn text_len(&self) -> TextIndex {
(&self.0.text_len).into()
}

#[napi(getter, ts_return_type = "Array<RuleNode | TokenNode>")]
#[napi(getter, ts_return_type = "Array<cst.RuleNode | cst.TokenNode>")]
pub fn children(&self, env: Env) -> Vec<JsObject> {
self.0
.children
Expand All @@ -47,7 +51,7 @@ impl RuleNode {
.collect()
}

#[napi(getter)]
#[napi(getter, ts_return_type = "cursor.Cursor")]
pub fn cursor(&self) -> Cursor {
Cursor::new(RustNode::Rule(self.0.clone()).cursor())
}
Expand All @@ -60,11 +64,15 @@ impl TokenNode {
NodeType::Token
}

#[napi(getter)]
#[napi(getter, ts_return_type = "kinds.TokenKind")]
pub fn kind(&self) -> TokenKind {
self.0.kind
}
#[napi(getter, js_name = "textLength")]
#[napi(
getter,
js_name = "textLength",
ts_return_type = "text_index.TextIndex"
)]
pub fn text_len(&self) -> TextIndex {
let text_len: RustTextIndex = (&self.0.text).into();
(&text_len).into()
Expand All @@ -75,7 +83,7 @@ impl TokenNode {
self.0.text.clone()
}

#[napi(getter)]
#[napi(getter, ts_return_type = "cursor.Cursor")]
pub fn cursor(&self) -> Cursor {
Cursor::new(RustNode::Token(self.0.clone()).cursor())
}
Expand Down
28 changes: 18 additions & 10 deletions crates/codegen/parser/runtime/src/napi/napi_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ impl Cursor {
Self(Box::new(cursor))
}

#[napi(getter)]
#[napi]
pub fn reset(&mut self) {
self.0.reset()
}

#[napi(getter)]
#[napi]
pub fn complete(&mut self) {
self.0.complete()
}
Expand All @@ -41,22 +41,22 @@ impl Cursor {
self.0.is_completed()
}

#[napi(getter, ts_return_type = "RuleNode | TokenNode")]
#[napi(getter, ts_return_type = "cst.RuleNode | cst.TokenNode")]
pub fn node(&self, env: Env) -> JsObject {
self.0.node().to_js(&env)
}

#[napi(getter)]
#[napi(getter, ts_return_type = "text_index.TextIndex")]
pub fn text_offset(&self) -> TextIndex {
(&self.0.text_offset()).into()
}

#[napi(getter)]
#[napi(getter, ts_return_type = "text_index.TextRange")]
pub fn text_range(&self) -> TextRange {
(&self.0.text_range()).into()
}

#[napi(getter, ts_return_type = "Array<RuleNode>")]
#[napi(getter, ts_return_type = "Array<cst.RuleNode>")]
pub fn path_rule_nodes(&self, env: Env) -> Vec<JsObject> {
self.0
.path_rule_nodes()
Expand Down Expand Up @@ -111,17 +111,25 @@ impl Cursor {
}

// TODO: find_matching (taking JS function)
#[napi(ts_return_type = "TokenNode | null")]
pub fn find_token_with_kind(&mut self, kinds: Vec<TokenKind>, env: Env) -> Option<JsObject> {
#[napi(ts_return_type = "cst.TokenNode | null")]
pub fn find_token_with_kind(
&mut self,
#[napi(ts_arg_type = "Array<kinds.TokenKind>")] kinds: Vec<TokenKind>,
env: Env,
) -> Option<JsObject> {
self.0
.find_token_with_kind(&kinds[..])
.map(|token_node| token_node.to_js(&env))
}

// TODO: find_token_matching (taking JS function)

#[napi(ts_return_type = "RuleNode | null")]
pub fn find_rule_with_kind(&mut self, kinds: Vec<RuleKind>, env: Env) -> Option<JsObject> {
#[napi(ts_return_type = "cst.RuleNode | null")]
pub fn find_rule_with_kind(
&mut self,
#[napi(ts_arg_type = "Array<kinds.RuleKind>")] kinds: Vec<RuleKind>,
env: Env,
) -> Option<JsObject> {
self.0
.find_rule_with_kind(&kinds[..])
.map(|token_node| token_node.to_js(&env))
Expand Down
2 changes: 1 addition & 1 deletion crates/codegen/parser/runtime/src/napi/napi_parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl From<RustParseError> for ParseError {

#[napi(namespace = "parse_error")]
impl ParseError {
#[napi(getter)]
#[napi(getter, ts_return_type = "text_index.TextRange")]
pub fn text_range(&self) -> TextRange {
self.0.text_range().into()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/parser/runtime/src/napi/napi_parse_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ impl From<RustParseOutput> for ParseOutput {

#[napi(namespace = "parse_output")]
impl ParseOutput {
#[napi(getter, ts_return_type = "RuleNode | TokenNode")]
#[napi(getter, ts_return_type = "cst.RuleNode | cst.TokenNode")]
pub fn parse_tree(&self, env: Env) -> napi::JsObject {
return self.0.parse_tree().to_js(&env);
}

#[napi(getter)]
#[napi(getter, ts_return_type = "Array<parse_error.ParseError>")]
pub fn errors(&self) -> Vec<napi_parse_error::ParseError> {
self.0.errors().iter().map(|x| x.clone().into()).collect()
}
Expand Down
10 changes: 7 additions & 3 deletions crates/codegen/parser/runtime/src/templates/language.rs.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,17 @@ impl Language {
return Self::SUPPORTED_VERSIONS.iter().map(|v| v.to_string()).collect();
}

#[napi(js_name = "scan")]
#[napi(js_name = "scan", ts_return_type = "kinds.TokenKind | null")]
pub fn scan_napi(&self, lexical_context: LexicalContext, input: String) -> Option<TokenKind> {
self.scan(lexical_context, input.as_str())
}

#[napi(js_name = "parse", ts_return_type = "ParseOutput")]
pub fn parse_napi(&self, production_kind: ProductionKind, input: String) -> NAPIParseOutput {
#[napi(js_name = "parse", ts_return_type = "parse_output.ParseOutput")]
pub fn parse_napi(
&self,
#[napi(ts_arg_type = "kinds.ProductionKind")] production_kind: ProductionKind,
input: String
) -> NAPIParseOutput {
self.parse(production_kind, input.as_str()).into()
}

Expand Down
10 changes: 7 additions & 3 deletions crates/solidity/outputs/cargo/crate/src/generated/language.rs

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

22 changes: 15 additions & 7 deletions crates/solidity/outputs/cargo/crate/src/generated/napi/napi_cst.rs

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.

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.

10 changes: 7 additions & 3 deletions crates/solidity/outputs/npm/crate/src/generated/language.rs

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

Loading

0 comments on commit 80114a8

Please sign in to comment.