-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlib.rs
More file actions
159 lines (134 loc) · 4.64 KB
/
lib.rs
File metadata and controls
159 lines (134 loc) · 4.64 KB
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
// Napi value need to be passed as value
#![expect(clippy::needless_pass_by_value)]
#[cfg(all(feature = "allocator", not(target_arch = "arm"), not(target_family = "wasm")))]
#[global_allocator]
static ALLOC: mimalloc_safe::MiMalloc = mimalloc_safe::MiMalloc;
use std::mem;
use napi::{Task, bindgen_prelude::AsyncTask};
use napi_derive::napi;
use oxc::{
allocator::Allocator,
parser::{ParseOptions, Parser, ParserReturn},
semantic::SemanticBuilder,
span::SourceType,
};
use oxc_napi::{OxcError, convert_utf8_to_utf16};
mod convert;
mod raw_transfer;
mod raw_transfer_types;
mod types;
pub use raw_transfer::{get_buffer_offset, parse_sync_raw, raw_transfer_supported};
pub use types::{EcmaScriptModule, ParseResult, ParserOptions};
mod generated {
// Note: We intentionally don't import `generated/derive_estree.rs`. It's not needed.
#[cfg(debug_assertions)]
pub mod assert_layouts;
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum AstType {
JavaScript,
TypeScript,
}
fn get_source_and_ast_type(filename: &str, options: &ParserOptions) -> (SourceType, AstType) {
let source_type = match options.lang.as_deref() {
Some("js") => SourceType::mjs(),
Some("jsx") => SourceType::jsx(),
Some("ts") => SourceType::ts(),
Some("tsx") => SourceType::tsx(),
_ => {
let mut source_type = SourceType::from_path(filename).unwrap_or_default();
// Force `script` or `module`
match options.source_type.as_deref() {
Some("script") => source_type = source_type.with_script(true),
Some("module") => source_type = source_type.with_module(true),
_ => {}
}
source_type
}
};
let ast_type = match options.ast_type.as_deref() {
Some("js") => AstType::JavaScript,
Some("ts") => AstType::TypeScript,
_ => {
if source_type.is_javascript() {
AstType::JavaScript
} else {
AstType::TypeScript
}
}
};
(source_type, ast_type)
}
fn parse<'a>(
allocator: &'a Allocator,
source_type: SourceType,
source_text: &'a str,
options: &ParserOptions,
) -> ParserReturn<'a> {
Parser::new(allocator, source_text, source_type)
.with_options(ParseOptions {
preserve_parens: options.preserve_parens.unwrap_or(true),
..ParseOptions::default()
})
.parse()
}
fn parse_with_return(filename: &str, source_text: String, options: &ParserOptions) -> ParseResult {
let allocator = Allocator::default();
let (source_type, ast_type) = get_source_and_ast_type(filename, options);
let ret = parse(&allocator, source_type, &source_text, options);
let mut program = ret.program;
let mut module_record = ret.module_record;
let mut diagnostics = ret.errors;
if options.show_semantic_errors == Some(true) {
let semantic_ret = SemanticBuilder::new().with_check_syntax_error(true).build(&program);
diagnostics.extend(semantic_ret.errors);
}
let mut errors = OxcError::from_diagnostics(filename, &source_text, diagnostics);
let comments =
convert_utf8_to_utf16(&source_text, &mut program, &mut module_record, &mut errors);
let program = match ast_type {
AstType::JavaScript => program.to_estree_js_json(),
AstType::TypeScript => program.to_estree_ts_json(),
};
let module = EcmaScriptModule::from(&module_record);
ParseResult { program, module, comments, errors }
}
/// Parse synchronously.
#[napi]
pub fn parse_sync(
filename: String,
source_text: String,
options: Option<ParserOptions>,
) -> ParseResult {
let options = options.unwrap_or_default();
parse_with_return(&filename, source_text, &options)
}
pub struct ResolveTask {
filename: String,
source_text: String,
options: ParserOptions,
}
#[napi]
impl Task for ResolveTask {
type JsValue = ParseResult;
type Output = ParseResult;
fn compute(&mut self) -> napi::Result<Self::Output> {
let source_text = mem::take(&mut self.source_text);
Ok(parse_with_return(&self.filename, source_text, &self.options))
}
fn resolve(&mut self, _: napi::Env, result: Self::Output) -> napi::Result<Self::JsValue> {
Ok(result)
}
}
/// Parse asynchronously.
///
/// Note: This function can be slower than `parseSync` due to the overhead of spawning a thread.
#[napi]
pub fn parse_async(
filename: String,
source_text: String,
options: Option<ParserOptions>,
) -> AsyncTask<ResolveTask> {
let options = options.unwrap_or_default();
AsyncTask::new(ResolveTask { filename, source_text, options })
}