Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 85197c2

Browse files
committed
added typescript definitions
1 parent 1a2e999 commit 85197c2

File tree

12 files changed

+285
-52
lines changed

12 files changed

+285
-52
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ jobs:
168168
cp win32.node/ruby_parser.node pkg/win32.node
169169
cp LICENSE pkg/
170170
cp README.md pkg/
171+
cp types.d.ts pkg/index.d.ts
171172
cd pkg
172173
173174
npm version "$GITHUB_TAG" --no-git-tag-version

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ src/message.cc
1010

1111
lib-ruby-parser.a
1212
src/lib-ruby-parser.h
13+
14+
types.d.ts
15+
16+
pkg/LICENSE
17+
pkg/README.md
18+
pkg/*.node
19+
pkg/index.d.ts

Makefile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ endif
1616
ifeq ($(DETECTED_OS), Windows)
1717
LIB_ASSET_NAME = lib-ruby-parser-x86_64-pc-windows-msvc.lib
1818
LOCAL_LIB_NAME = lib-ruby-parser.lib
19+
NODE_FILE_NAME = win32.node
1920
endif
2021
ifeq ($(DETECTED_OS), Linux)
2122
LIB_ASSET_NAME = lib-ruby-parser-x86_64-unknown-linux-gnu.a
2223
LOCAL_LIB_NAME = lib-ruby-parser.a
24+
NODE_FILE_NAME = linux.node
2325
endif
2426
ifeq ($(UNAME_S), Darwin)
2527
LIB_ASSET_NAME = lib-ruby-parser-x86_64-apple-darwin.a
2628
LOCAL_LIB_NAME = lib-ruby-parser.a
29+
NODE_FILE_NAME = darwin.node
2730
endif
2831

2932
VERSION = 3.0.0-3.6
@@ -47,20 +50,25 @@ LIB_URL = $(ASSET_PREFIX)/$(LIB_ASSET_NAME)
4750
setup:
4851
npm install --ignore-scripts
4952
npm install node-gyp --no-save
53+
cp LICENSE pkg/
54+
cp README.md pkg/
5055

5156
configure:
5257
node ./node_modules/node-gyp/bin/node-gyp.js configure
5358

5459
generate-node-bindings:
5560
cd build-convert && cargo build
5661

62+
GYP_OUTPUT = ./build/$(GYP_ENV)/ruby_parser.node
63+
5764
.PHONY: build
5865
build:
5966
node ./node_modules/node-gyp/bin/node-gyp.js build $(NODE_GYP_FLAGS)
67+
cp $(GYP_OUTPUT) pkg/$(NODE_FILE_NAME)
68+
cp types.d.ts pkg/index.d.ts
6069

61-
GYP_OUTPUT = ./build/$(GYP_ENV)/ruby_parser.node
6270
test:
63-
./test.js $(GYP_OUTPUT)
71+
npm run test
6472

6573
clean:
6674
rm -f $(GYP_OUTPUT)

build-convert/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ fn main() {
1616

1717
gen::MessageH::new(&messages).write();
1818
gen::MessageCc::new(&messages).write();
19+
20+
gen::TypedDTs::new(&nodes, &messages).write();
1921
}

build-convert/gen/message_cc.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use lib_ruby_parser_nodes::MessageField;
2-
31
pub(crate) struct MessageCc<'a> {
42
messages: &'a [lib_ruby_parser_nodes::Message],
53
}

build-convert/gen/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ mod message_h;
77
pub(crate) use message_h::MessageH;
88
mod message_cc;
99
pub(crate) use message_cc::MessageCc;
10+
11+
mod types_d_ts;
12+
pub(crate) use types_d_ts::TypedDTs;

build-convert/gen/types_d_ts.rs

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
pub(crate) struct TypedDTs<'a> {
2+
nodes: &'a [lib_ruby_parser_nodes::Node],
3+
messages: &'a [lib_ruby_parser_nodes::Message],
4+
}
5+
6+
impl<'a> TypedDTs<'a> {
7+
pub(crate) fn new(
8+
nodes: &'a [lib_ruby_parser_nodes::Node],
9+
messages: &'a [lib_ruby_parser_nodes::Message],
10+
) -> Self {
11+
Self { nodes, messages }
12+
}
13+
14+
pub(crate) fn write(&self) {
15+
std::fs::write("../types.d.ts", self.contents()).unwrap()
16+
}
17+
18+
fn contents(&self) -> String {
19+
format!(
20+
"// lib-ruby-parser
21+
22+
export class Loc {{
23+
begin: number
24+
end: number
25+
26+
constructor(begin: number, end: number);
27+
source(input: Input): Uint8Array;
28+
}}
29+
30+
export class Token {{
31+
name: string
32+
value: Uint8Array
33+
loc: Loc
34+
}}
35+
36+
export type DiagnosticLevel =
37+
| \"error\"
38+
| \"warning\";
39+
40+
export class Diagnostic {{
41+
level: DiagnosticLevel
42+
message: Message
43+
rendered: string
44+
loc: Loc
45+
}}
46+
47+
export type CommentKind =
48+
| \"inline\"
49+
| \"document\"
50+
| \"unknown\";
51+
52+
export class Comment {{
53+
kind: CommentKind
54+
location: Loc
55+
}}
56+
57+
export type MagicCommentKind =
58+
| \"encoding\"
59+
| \"frozen-string-literal\"
60+
| \"warn-indent\"
61+
| \"shareable-constant-value\";
62+
63+
export class MagicComment {{
64+
kind: MagicCommentKind
65+
key_l: Loc
66+
value_l: Loc
67+
}}
68+
69+
export class Input {{}}
70+
71+
export class ParserResult {{
72+
ast: Node | null
73+
tokens: Token[]
74+
diagnostics: Diagnostic[]
75+
comments: Comment[]
76+
magic_comments: MagicComment[]
77+
input: Input
78+
}}
79+
80+
export interface ParserOptions {{}}
81+
82+
// Namespace with all kinds of AST nodes
83+
{node_ifaces}
84+
85+
// Namespace with all kinds of AST diagnostic messages
86+
{message_ifaces}
87+
88+
export type Node =
89+
{nodes_sum};
90+
91+
export type Message =
92+
{messages_sum};
93+
94+
export function bytes_to_utf8_lossy(bytes: Uint8Array): string;
95+
export function parse(input: Uint8Array, options: ParserOptions): ParserResult;
96+
",
97+
node_ifaces = self.node_ifaces().join("\n\n"),
98+
message_ifaces = self.message_ifaces().join("\n\n"),
99+
nodes_sum = self.nodes_sum().join("\n "),
100+
messages_sum = self.messages_sum().join("\n ")
101+
)
102+
}
103+
104+
fn node_ifaces(&self) -> Vec<String> {
105+
self.nodes.iter().map(node_iface).collect()
106+
}
107+
fn message_ifaces(&self) -> Vec<String> {
108+
self.messages.iter().map(message_iface).collect()
109+
}
110+
fn nodes_sum(&self) -> Vec<String> {
111+
self.nodes.iter().map(nodes_sum_item).collect()
112+
}
113+
fn messages_sum(&self) -> Vec<String> {
114+
self.messages.iter().map(messages_sum_item).collect()
115+
}
116+
}
117+
118+
fn node_iface(node: &lib_ruby_parser_nodes::Node) -> String {
119+
let fields = node
120+
.fields
121+
.iter()
122+
.map(|f| {
123+
let field_type = match f.field_type {
124+
lib_ruby_parser_nodes::FieldType::Node => "Node",
125+
lib_ruby_parser_nodes::FieldType::MaybeNode => "Node | null",
126+
lib_ruby_parser_nodes::FieldType::Nodes => "Node[]",
127+
lib_ruby_parser_nodes::FieldType::Loc => "Loc",
128+
lib_ruby_parser_nodes::FieldType::MaybeLoc => "Loc | null",
129+
lib_ruby_parser_nodes::FieldType::Str => "string",
130+
lib_ruby_parser_nodes::FieldType::MaybeStr => "string | null",
131+
lib_ruby_parser_nodes::FieldType::Chars => "string | null",
132+
lib_ruby_parser_nodes::FieldType::StringValue => "Uint8Array",
133+
lib_ruby_parser_nodes::FieldType::U8 => "number",
134+
lib_ruby_parser_nodes::FieldType::Usize => "number",
135+
lib_ruby_parser_nodes::FieldType::RawString => "string",
136+
lib_ruby_parser_nodes::FieldType::RegexOptions => "Node",
137+
};
138+
format!(
139+
"{comment}
140+
{field_name}: {field_type};",
141+
comment = comment(&f.comment, 4).join("\n"),
142+
field_name = f.field_name,
143+
field_type = field_type
144+
)
145+
})
146+
.collect::<Vec<_>>();
147+
148+
format!(
149+
"{comment}
150+
export class {name} {{
151+
{fields}
152+
}}",
153+
comment = comment(&node.comment, 0).join("\n"),
154+
name = node.struct_name,
155+
fields = fields.join("\n\n")
156+
)
157+
}
158+
fn message_iface(message: &lib_ruby_parser_nodes::Message) -> String {
159+
let fields = message
160+
.fields
161+
.iter()
162+
.map(|f| {
163+
let field_type = match f.field_type {
164+
lib_ruby_parser_nodes::MessageFieldType::Str => "string",
165+
lib_ruby_parser_nodes::MessageFieldType::Byte => "string",
166+
};
167+
format!(
168+
"{comment}
169+
{field_name}: {field_type}",
170+
comment = comment(&f.comment, 4).join("\n"),
171+
field_name = f.name,
172+
field_type = field_type
173+
)
174+
})
175+
.collect::<Vec<_>>();
176+
177+
format!(
178+
"{comment}
179+
export class {name} {{
180+
{fields}
181+
}}",
182+
comment = comment(&message.comment, 0).join("\n"),
183+
name = message.name,
184+
fields = fields.join("\n\n")
185+
)
186+
}
187+
fn nodes_sum_item(node: &lib_ruby_parser_nodes::Node) -> String {
188+
format!("| {}", node.struct_name)
189+
}
190+
fn messages_sum_item(message: &lib_ruby_parser_nodes::Message) -> String {
191+
format!("| {}", message.name)
192+
}
193+
194+
fn comment(s: &str, spaces: usize) -> Vec<String> {
195+
s.lines()
196+
.map(|l| {
197+
format!("{}// {}", " ".repeat(spaces), l)
198+
.trim_end()
199+
.to_owned()
200+
})
201+
.collect()
202+
}

package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
{
2-
"name": "lib-ruby-parser",
2+
"name": "lib-ruby-parser-dev",
33
"version": "0.0.0",
44
"description": "bindings to lib-ruby-parser",
55
"dependencies": {
6-
"node-addon-api": "^3.1.0"
6+
"@types/node": "^14.14.31",
7+
"lib-ruby-parser": "./pkg/",
8+
"node-addon-api": "^3.1.0",
9+
"ts-node": "^9.1.0",
10+
"typescript": "^4.2.0"
11+
},
12+
"scripts": {
13+
"test": "ts-node test.ts"
714
}
815
}

pkg/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"version": "0.0.0",
44
"description": "bindings to lib-ruby-parser",
55
"repository": "github:lib-ruby-parser/node-bindings",
6-
"license": "MIT"
6+
"license": "MIT",
7+
"types": "index.d.ts"
78
}

src/diagnostic.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace lib_ruby_parser_node
1515
env,
1616
"Diagnostic",
1717
{
18-
InstanceValue("name", env.Null(), (napi_property_attributes)(napi_writable | napi_enumerable | napi_configurable)),
18+
InstanceValue("level", env.Null(), (napi_property_attributes)(napi_writable | napi_enumerable | napi_configurable)),
1919
InstanceValue("message", env.Null(), (napi_property_attributes)(napi_writable | napi_enumerable | napi_configurable)),
2020
InstanceValue("rendered", env.Null(), (napi_property_attributes)(napi_writable | napi_enumerable | napi_configurable)),
2121
InstanceValue("loc", env.Null(), (napi_property_attributes)(napi_writable | napi_enumerable | napi_configurable)),

0 commit comments

Comments
 (0)