Skip to content

Commit e51ad70

Browse files
committed
polish transpilers, add support for error reporter
1 parent 732d304 commit e51ad70

File tree

6 files changed

+450
-569
lines changed

6 files changed

+450
-569
lines changed

rust/cubetranspilers/src/check_dup_prop_transpiler.rs

Lines changed: 60 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
11
use std::collections::HashSet;
22

3+
use swc_core::common::errors::Handler;
34
use swc_core::common::{BytePos, Span, DUMMY_SP};
45
use swc_core::ecma::visit::VisitMutWith;
5-
use swc_core::plugin::errors::HANDLER;
6-
use swc_core::plugin::proxies::TransformPluginProgramMetadata;
76
use swc_core::{
8-
ecma::{
9-
ast::*,
10-
visit::{visit_mut_pass, VisitMut},
11-
},
7+
ecma::{ast::*, visit::VisitMut},
128
plugin::proxies::PluginSourceMapProxy,
139
};
1410

15-
pub struct CheckDupPropTransformVisitor {
11+
pub struct CheckDupPropTransformVisitor<'a> {
1612
pub(crate) source_map: Option<PluginSourceMapProxy>,
13+
handler: &'a Handler,
1714
}
1815

19-
impl CheckDupPropTransformVisitor {
20-
pub fn new(source_map: Option<PluginSourceMapProxy>) -> Self {
21-
CheckDupPropTransformVisitor { source_map }
16+
impl<'a> CheckDupPropTransformVisitor<'a> {
17+
pub fn new(source_map: Option<PluginSourceMapProxy>, handler: &'a Handler) -> Self {
18+
CheckDupPropTransformVisitor {
19+
source_map,
20+
handler,
21+
}
2222
}
2323

2424
fn emit_error(&mut self, span: Span, message: &str) {
25-
HANDLER.with(|handler| {
26-
handler
27-
.struct_span_err(span, &self.format_msg(span, message))
28-
.emit();
29-
});
25+
self.handler
26+
.struct_span_err(span, &self.format_msg(span, message))
27+
.emit();
3028
}
3129

3230
fn format_msg(&self, span: Span, message: &str) -> String {
@@ -87,7 +85,7 @@ impl CheckDupPropTransformVisitor {
8785
}
8886
}
8987

90-
impl VisitMut for CheckDupPropTransformVisitor {
88+
impl VisitMut for CheckDupPropTransformVisitor<'_> {
9189
// Implement necessary visit_mut_* methods for actual custom transform.
9290
// A comprehensive list of possible visitor methods can be found here:
9391
// https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
@@ -108,12 +106,6 @@ impl VisitMut for CheckDupPropTransformVisitor {
108106
}
109107
}
110108

111-
pub fn process_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program {
112-
program.apply(&mut visit_mut_pass(CheckDupPropTransformVisitor {
113-
source_map: Some(metadata.source_map),
114-
}))
115-
}
116-
117109
#[cfg(test)]
118110
mod tests {
119111
// Recommended strategy to test plugin's transform is verify
@@ -128,7 +120,7 @@ mod tests {
128120
common::{
129121
errors::{DiagnosticBuilder, Emitter, Handler, HandlerFlags},
130122
sync::Lrc,
131-
FileName, Globals, SourceMap,
123+
FileName, SourceMap,
132124
},
133125
ecma::visit::VisitMutWith,
134126
};
@@ -147,7 +139,6 @@ mod tests {
147139

148140
#[test]
149141
fn test_errors_for_duplicates_first_level() {
150-
let globals = Globals::new();
151142
let cm: Lrc<SourceMap> = Default::default();
152143
let diagnostics = Arc::new(Mutex::new(Vec::new()));
153144
let emitter = Box::new(TestEmitter {
@@ -210,26 +201,21 @@ mod tests {
210201
});
211202
"#;
212203

213-
swc_core::common::GLOBALS.set(&globals, || {
214-
HANDLER.set(&handler, || {
215-
let fm = cm.new_source_file(
216-
Arc::new(FileName::Custom("input.js".into())),
217-
js_code.into(),
218-
);
219-
let lexer = Lexer::new(
220-
Syntax::Es(Default::default()),
221-
EsVersion::Es2020,
222-
StringInput::from(&*fm),
223-
None,
224-
);
225-
let mut parser = Parser::new_from(lexer);
226-
let mut program: Program =
227-
parser.parse_program().expect("Failed to parse the JS code");
228-
229-
let mut visitor = CheckDupPropTransformVisitor { source_map: None };
230-
program.visit_mut_with(&mut visitor);
231-
});
232-
});
204+
let fm = cm.new_source_file(
205+
Arc::new(FileName::Custom("input.js".into())),
206+
js_code.into(),
207+
);
208+
let lexer = Lexer::new(
209+
Syntax::Es(Default::default()),
210+
EsVersion::Es2020,
211+
StringInput::from(&*fm),
212+
None,
213+
);
214+
let mut parser = Parser::new_from(lexer);
215+
let mut program: Program = parser.parse_program().expect("Failed to parse the JS code");
216+
217+
let mut visitor = CheckDupPropTransformVisitor::new(None, &handler);
218+
program.visit_mut_with(&mut visitor);
233219

234220
let diags = diagnostics.lock().unwrap();
235221
let msgs: Vec<_> = diags
@@ -241,7 +227,6 @@ mod tests {
241227

242228
#[test]
243229
fn test_errors_for_duplicates_deep_level() {
244-
let globals = Globals::new();
245230
let cm: Lrc<SourceMap> = Default::default();
246231
let diagnostics = Arc::new(Mutex::new(Vec::new()));
247232
let emitter = Box::new(TestEmitter {
@@ -304,26 +289,21 @@ mod tests {
304289
});
305290
"#;
306291

307-
swc_core::common::GLOBALS.set(&globals, || {
308-
HANDLER.set(&handler, || {
309-
let fm = cm.new_source_file(
310-
Arc::new(FileName::Custom("input.js".into())),
311-
js_code.into(),
312-
);
313-
let lexer = Lexer::new(
314-
Syntax::Es(Default::default()),
315-
EsVersion::Es2020,
316-
StringInput::from(&*fm),
317-
None,
318-
);
319-
let mut parser = Parser::new_from(lexer);
320-
let mut program: Program =
321-
parser.parse_program().expect("Failed to parse the JS code");
322-
323-
let mut visitor = CheckDupPropTransformVisitor { source_map: None };
324-
program.visit_mut_with(&mut visitor);
325-
});
326-
});
292+
let fm = cm.new_source_file(
293+
Arc::new(FileName::Custom("input.js".into())),
294+
js_code.into(),
295+
);
296+
let lexer = Lexer::new(
297+
Syntax::Es(Default::default()),
298+
EsVersion::Es2020,
299+
StringInput::from(&*fm),
300+
None,
301+
);
302+
let mut parser = Parser::new_from(lexer);
303+
let mut program: Program = parser.parse_program().expect("Failed to parse the JS code");
304+
305+
let mut visitor = CheckDupPropTransformVisitor::new(None, &handler);
306+
program.visit_mut_with(&mut visitor);
327307

328308
let diags = diagnostics.lock().unwrap();
329309
let msgs: Vec<_> = diags
@@ -335,7 +315,6 @@ mod tests {
335315

336316
#[test]
337317
fn test_no_errors() {
338-
let globals = Globals::new();
339318
let cm: Lrc<SourceMap> = Default::default();
340319
let diagnostics = Arc::new(Mutex::new(Vec::new()));
341320
let emitter = Box::new(TestEmitter {
@@ -391,26 +370,21 @@ mod tests {
391370
});
392371
"#;
393372

394-
swc_core::common::GLOBALS.set(&globals, || {
395-
HANDLER.set(&handler, || {
396-
let fm = cm.new_source_file(
397-
Arc::new(FileName::Custom("input.js".into())),
398-
js_code.into(),
399-
);
400-
let lexer = Lexer::new(
401-
Syntax::Es(Default::default()),
402-
EsVersion::Es2020,
403-
StringInput::from(&*fm),
404-
None,
405-
);
406-
let mut parser = Parser::new_from(lexer);
407-
let mut program: Program =
408-
parser.parse_program().expect("Failed to parse the JS code");
409-
410-
let mut visitor = CheckDupPropTransformVisitor { source_map: None };
411-
program.visit_mut_with(&mut visitor);
412-
});
413-
});
373+
let fm = cm.new_source_file(
374+
Arc::new(FileName::Custom("input.js".into())),
375+
js_code.into(),
376+
);
377+
let lexer = Lexer::new(
378+
Syntax::Es(Default::default()),
379+
EsVersion::Es2020,
380+
StringInput::from(&*fm),
381+
None,
382+
);
383+
let mut parser = Parser::new_from(lexer);
384+
let mut program: Program = parser.parse_program().expect("Failed to parse the JS code");
385+
386+
let mut visitor = CheckDupPropTransformVisitor::new(None, &handler);
387+
program.visit_mut_with(&mut visitor);
414388

415389
let diags = diagnostics.lock().unwrap();
416390
assert!(diags.is_empty(), "Should not emit errors",);

0 commit comments

Comments
 (0)