Skip to content

Commit

Permalink
TypeScript nodes should send a TextIndex object instead of a `[numb…
Browse files Browse the repository at this point in the history
…er,number,number]` tuple (#513)

Fixes #509
  • Loading branch information
AntonyBlakey authored Jul 11, 2023
1 parent a54b88c commit 7e01250
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 94 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-ads-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"changelog": minor
---

Typescript API now has TextIndex and TextRange types that are returned from the appropriate methods rather than tuples.
68 changes: 43 additions & 25 deletions crates/codegen/syntax_templates/src/typescript/cst_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{
cst::{Node, RuleNode as RustRuleNode, TokenNode as RustTokenNode},
kinds::*,
text_index::TextIndex,
text_index::{TextIndex as RustTextIndex, TextRange as RustTextRange},
};

use std::rc::Rc;
Expand All @@ -10,7 +10,41 @@ use napi::bindgen_prelude::*;
use napi::JsObject;
use napi::NapiValue;

#[napi]
#[napi(object)]
#[derive(Copy, Clone)]
pub struct TextIndex {
pub utf8: u32,
pub utf16: u32,
pub char: u32,
}

impl From<&RustTextIndex> for TextIndex {
fn from(value: &RustTextIndex) -> Self {
Self {
utf8: value.utf8 as u32,
utf16: value.utf16 as u32,
char: value.char as u32,
}
}
}

#[napi(object)]
#[derive(Copy, Clone)]
pub struct TextRange {
pub start: TextIndex,
pub end: TextIndex,
}

impl From<&RustTextRange> for TextRange {
fn from(value: &RustTextRange) -> Self {
Self {
start: (&value.start).into(),
end: (&value.end).into(),
}
}
}

#[napi(object)]
pub enum NodeType {
Rule,
Token,
Expand All @@ -34,17 +68,9 @@ impl RuleNode {
self.0.kind
}

#[napi(
getter,
js_name = "textLength",
ts_return_type = "[ utf8: number, utf16: number, char: number]"
)]
pub fn text_len(&self) -> [u32; 3] {
[
self.0.text_len.utf8 as u32,
self.0.text_len.utf16 as u32,
self.0.text_len.char as u32,
]
#[napi(getter, js_name = "textLength")]
pub fn text_len(&self) -> TextIndex {
(&self.0.text_len).into()
}

#[napi(getter, ts_return_type = "(RuleNode | TokenNode)[]")]
Expand All @@ -69,18 +95,10 @@ impl TokenNode {
self.0.kind
}

#[napi(
getter,
js_name = "textLength",
ts_return_type = "[ utf8: number, utf16: number, char: number]"
)]
pub fn text_len(&self) -> [u32; 3] {
let text_len: TextIndex = (&self.0.text).into();
[
text_len.utf8 as u32,
text_len.utf16 as u32,
text_len.char as u32,
]
#[napi(getter, js_name = "textLength")]
pub fn text_len(&self) -> TextIndex {
let text_len: RustTextIndex = (&self.0.text).into();
(&text_len).into()
}
}

Expand Down
27 changes: 8 additions & 19 deletions crates/codegen/syntax_templates/src/typescript/parse_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::BTreeSet;

use super::{cst, kinds::TokenKind, parse_error::render_error_report, text_index::TextRange};
use super::{
cst, cst_types::TextRange, kinds::TokenKind, parse_error::render_error_report,
text_index::TextRange as RustTextRange,
};
use napi::bindgen_prelude::*;

#[napi]
Expand Down Expand Up @@ -30,29 +33,15 @@ impl ParseOutput {
#[napi]
#[derive(PartialEq, Clone)]
pub struct ParseError {
pub(crate) range: TextRange,
pub(crate) range: RustTextRange,
pub(crate) tokens_that_would_have_allowed_more_progress: Vec<TokenKind>,
}

#[napi]
impl ParseError {
#[napi(
getter,
ts_return_type = "[ start: [ utf8: number, utf16: number, char: number], end: [ utf8: number, utf16: number, char: number] ]"
)]
pub fn range(&self) -> [[u32; 3]; 2] {
return [
[
self.range.start.utf8 as u32,
self.range.start.utf16 as u32,
self.range.start.char as u32,
],
[
self.range.end.utf8 as u32,
self.range.end.utf16 as u32,
self.range.end.char as u32,
],
];
#[napi(getter)]
pub fn range(&self) -> TextRange {
(&self.range).into()
}

pub fn tokens_that_would_have_allowed_more_progress(&self) -> Vec<String> {
Expand Down
68 changes: 43 additions & 25 deletions crates/solidity/outputs/npm/crate/src/generated/cst_types.rs

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

27 changes: 8 additions & 19 deletions crates/solidity/outputs/npm/crate/src/generated/parse_output.rs

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

15 changes: 12 additions & 3 deletions crates/solidity/outputs/npm/package/generated/index.d.ts

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 @@ -2,4 +2,4 @@
* __SLANG_PUBLIC_API_SYNC__ (please keep in sync across all other instances)
*/

export { NodeType, RuleKind, RuleNode, TokenKind, TokenNode } from "../../generated";
export { NodeType, RuleKind, RuleNode, TextIndex, TextRange, TokenKind, TokenNode } from "../../generated";
4 changes: 2 additions & 2 deletions crates/solidity/outputs/npm/tests/src/cst-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ test("calculate both byte and char ranges", (t) => {

if (parseTree instanceof TokenNode) {
t.is(parseTree.kind, TokenKind.UnicodeStringLiteral);
t.deepEqual(parseTree.textLength[2], 21);
t.deepEqual(parseTree.textLength[0], 24);
t.deepEqual(parseTree.textLength.char, 21);
t.deepEqual(parseTree.textLength.utf8, 24);
} else {
t.fail("Expected TokenNode");
}
Expand Down

0 comments on commit 7e01250

Please sign in to comment.