-
-
Notifications
You must be signed in to change notification settings - Fork 525
/
Copy pathtypes.rs
263 lines (233 loc) · 6.46 KB
/
types.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use std::mem;
use napi_derive::napi;
use oxc_napi::OxcError;
#[napi(object)]
#[derive(Default)]
pub struct ParserOptions {
#[napi(ts_type = "'script' | 'module' | 'unambiguous' | undefined")]
pub source_type: Option<String>,
/// Treat the source text as `js`, `jsx`, `ts`, or `tsx`.
#[napi(ts_type = "'js' | 'jsx' | 'ts' | 'tsx'")]
pub lang: Option<String>,
/// Return an AST which includes TypeScript-related properties, or excludes them.
///
/// `'js'` is default for JS / JSX files.
/// `'ts'` is default for TS / TSX files.
/// The type of the file is determined from `lang` option, or extension of provided `filename`.
#[napi(ts_type = "'js' | 'ts'")]
pub ast_type: Option<String>,
/// Emit `ParenthesizedExpression` and `TSParenthesizedType` in AST.
///
/// If this option is true, parenthesized expressions are represented by
/// (non-standard) `ParenthesizedExpression` and `TSParenthesizedType` nodes that
/// have a single `expression` property containing the expression inside parentheses.
///
/// @default true
pub preserve_parens: Option<bool>,
/// Produce semantic errors with an additional AST pass.
/// Semantic errors depend on symbols and scopes, where the parser does not construct.
/// This adds a small performance overhead.
///
/// @default false
pub show_semantic_errors: Option<bool>,
}
#[napi]
pub struct ParseResult {
pub(crate) program: String,
pub(crate) module: EcmaScriptModule,
pub(crate) comments: Vec<Comment>,
pub(crate) errors: Vec<OxcError>,
}
#[napi]
impl ParseResult {
#[napi(getter, ts_return_type = "import(\"@oxc-project/types\").Program")]
pub fn get_program(&mut self) -> String {
mem::take(&mut self.program)
}
#[napi(getter)]
pub fn module(&mut self) -> EcmaScriptModule {
mem::take(&mut self.module)
}
#[napi(getter)]
pub fn comments(&mut self) -> Vec<Comment> {
mem::take(&mut self.comments)
}
#[napi(getter)]
pub fn errors(&mut self) -> Vec<OxcError> {
mem::take(&mut self.errors)
}
}
#[napi(object)]
pub struct Comment {
#[napi(ts_type = "'Line' | 'Block'")]
pub r#type: String,
pub value: String,
pub start: u32,
pub end: u32,
}
#[napi(object)]
#[derive(Default)]
pub struct EcmaScriptModule {
/// Has ESM syntax.
///
/// i.e. `import` and `export` statements, and `import.meta`.
///
/// Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
pub has_module_syntax: bool,
/// Import statements.
pub static_imports: Vec<StaticImport>,
/// Export statements.
pub static_exports: Vec<StaticExport>,
/// Dynamic import expressions.
pub dynamic_imports: Vec<DynamicImport>,
/// Span positions` of `import.meta`
pub import_metas: Vec<Span>,
}
#[napi(object)]
pub struct Span {
pub start: u32,
pub end: u32,
}
#[napi(object)]
pub struct ValueSpan {
pub value: String,
pub start: u32,
pub end: u32,
}
#[napi(object)]
pub struct StaticImport {
/// Start of import statement.
pub start: u32,
/// End of import statement.
pub end: u32,
/// Import source.
///
/// ```js
/// import { foo } from "mod";
/// // ^^^
/// ```
pub module_request: ValueSpan,
/// Import specifiers.
///
/// Empty for `import "mod"`.
pub entries: Vec<StaticImportEntry>,
}
#[napi(object)]
pub struct StaticImportEntry {
/// The name under which the desired binding is exported by the module.
///
/// ```js
/// import { foo } from "mod";
/// // ^^^
/// import { foo as bar } from "mod";
/// // ^^^
/// ```
pub import_name: ImportName,
/// The name that is used to locally access the imported value from within the importing module.
/// ```js
/// import { foo } from "mod";
/// // ^^^
/// import { foo as bar } from "mod";
/// // ^^^
/// ```
pub local_name: ValueSpan,
/// Whether this binding is for a TypeScript type-only import.
///
/// `true` for the following imports:
/// ```ts
/// import type { foo } from "mod";
/// import { type foo } from "mod";
/// ```
pub is_type: bool,
}
#[napi(string_enum)]
pub enum ImportNameKind {
/// `import { x } from "mod"`
Name,
/// `import * as ns from "mod"`
NamespaceObject,
/// `import defaultExport from "mod"`
Default,
}
#[napi(object)]
pub struct ImportName {
pub kind: ImportNameKind,
pub name: Option<String>,
pub start: Option<u32>,
pub end: Option<u32>,
}
#[napi(object)]
pub struct StaticExportEntry {
pub start: u32,
pub end: u32,
pub module_request: Option<ValueSpan>,
/// The name under which the desired binding is exported by the module`.
pub import_name: ExportImportName,
/// The name used to export this binding by this module.
pub export_name: ExportExportName,
/// The name that is used to locally access the exported value from within the importing module.
pub local_name: ExportLocalName,
}
#[napi(object)]
pub struct StaticExport {
pub start: u32,
pub end: u32,
pub entries: Vec<StaticExportEntry>,
}
#[napi(string_enum)]
pub enum ExportImportNameKind {
/// `export { name }
Name,
/// `export * as ns from "mod"`
All,
/// `export * from "mod"`
AllButDefault,
/// Does not have a specifier.
None,
}
#[napi(object)]
pub struct ExportImportName {
pub kind: ExportImportNameKind,
pub name: Option<String>,
pub start: Option<u32>,
pub end: Option<u32>,
}
#[napi(string_enum)]
pub enum ExportExportNameKind {
/// `export { name }
Name,
/// `export default expression`
Default,
/// `export * from "mod"
None,
}
#[napi(object)]
pub struct ExportExportName {
pub kind: ExportExportNameKind,
pub name: Option<String>,
pub start: Option<u32>,
pub end: Option<u32>,
}
#[napi(object)]
pub struct ExportLocalName {
pub kind: ExportLocalNameKind,
pub name: Option<String>,
pub start: Option<u32>,
pub end: Option<u32>,
}
#[napi(string_enum)]
pub enum ExportLocalNameKind {
/// `export { name }
Name,
/// `export default expression`
Default,
/// If the exported value is not locally accessible from within the module.
/// `export default function () {}`
None,
}
#[napi(object)]
pub struct DynamicImport {
pub start: u32,
pub end: u32,
pub module_request: Span,
}