diff --git a/crates/jsdoc/tests/fixture.rs b/crates/jsdoc/tests/fixture.rs index 71d212c6d47..97f716037a9 100644 --- a/crates/jsdoc/tests/fixture.rs +++ b/crates/jsdoc/tests/fixture.rs @@ -7,7 +7,7 @@ use swc_common::{ comments::{Comment, CommentKind, Comments}, BytePos, DUMMY_SP, }; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax}; +use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; use testing::NormalizedOutput; #[testing::fixture("tests/fixtures/**/*.js")] @@ -17,21 +17,21 @@ fn fixture(path: PathBuf) { let fm = cm.load_file(&path).expect("failed to load fixture file"); - let lexer = Lexer::new( + let mut errors = vec![]; + + if let Err(err) = parse_file_as_module( + &fm, Syntax::Es(EsConfig { jsx: true, ..Default::default() }), Default::default(), - StringInput::from(&*fm), Some(&comments), - ); - let mut p = Parser::new_from(lexer); - - if let Err(err) = p.parse_module() { + &mut errors, + ) { err.into_diagnostic(&handler).emit(); } - for err in p.take_errors() { + for err in errors { err.into_diagnostic(&handler).emit(); } if handler.has_errors() { diff --git a/crates/swc_bundler/examples/bundle.rs b/crates/swc_bundler/examples/bundle.rs index 10703eff328..4afe72949dd 100644 --- a/crates/swc_bundler/examples/bundle.rs +++ b/crates/swc_bundler/examples/bundle.rs @@ -30,7 +30,7 @@ use swc_ecma_loader::{ use swc_ecma_minifier::option::{ CompressOptions, ExtraOptions, MangleOptions, MinifyOptions, TopLevelOptions, }; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax}; +use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; use swc_ecma_transforms_base::fixer::fixer; use swc_ecma_visit::VisitMutWith; @@ -228,17 +228,16 @@ impl Load for Loader { _ => unreachable!(), }; - let lexer = Lexer::new( + let module = parse_file_as_module( + &fm, Syntax::Es(EsConfig { ..Default::default() }), EsVersion::Es2020, - StringInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - let module = parser.parse_module().unwrap_or_else(|err| { + &mut vec![], + ) + .unwrap_or_else(|err| { let handler = Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(self.cm.clone())); err.into_diagnostic(&handler).emit(); diff --git a/crates/swc_bundler/examples/path.rs b/crates/swc_bundler/examples/path.rs index 2cac4063ec9..539784e25f3 100644 --- a/crates/swc_bundler/examples/path.rs +++ b/crates/swc_bundler/examples/path.rs @@ -5,7 +5,7 @@ use swc_bundler::{BundleKind, Bundler, Config, Hook, Load, ModuleData, ModuleRec use swc_common::{sync::Lrc, FileName, FilePathMapping, Globals, SourceMap, Span}; use swc_ecma_ast::KeyValueProp; use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax}; +use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; fn main() { let _log = testing::init(); @@ -67,17 +67,17 @@ impl Load for PathLoader { }; let fm = self.cm.load_file(file)?; - let lexer = Lexer::new( + + let module = parse_file_as_module( + &fm, Syntax::Es(EsConfig { ..Default::default() }), Default::default(), - StringInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - let module = parser.parse_module().expect("This should not happen"); + &mut vec![], + ) + .expect("This should not happen"); Ok(ModuleData { fm, diff --git a/crates/swc_bundler/src/bundler/helpers/mod.rs b/crates/swc_bundler/src/bundler/helpers/mod.rs index abc7ead9be9..a39eb1fb875 100644 --- a/crates/swc_bundler/src/bundler/helpers/mod.rs +++ b/crates/swc_bundler/src/bundler/helpers/mod.rs @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicBool, Ordering::SeqCst}; use once_cell::sync::Lazy; use swc_common::{FileName, FilePathMapping, SourceMap}; use swc_ecma_ast::*; -use swc_ecma_parser::{lexer::Lexer, Parser, StringInput}; +use swc_ecma_parser::parse_file_as_module; use swc_ecma_utils::{drop_span, prepend_stmts}; #[derive(Debug, Default)] @@ -12,6 +12,21 @@ pub(crate) struct Helpers { pub require: AtomicBool, } +fn parse(code: &'static str, name: &'static str) -> Vec { + let cm = SourceMap::new(FilePathMapping::empty()); + let fm = cm.new_source_file(FileName::Custom(name.into()), code.into()); + parse_file_as_module( + &fm, + Default::default(), + Default::default(), + None, + &mut vec![], + ) + .map(|script| drop_span(script.body)) + .map_err(|_| {}) + .unwrap() +} + macro_rules! define { ( $( @@ -23,24 +38,7 @@ macro_rules! define { $( fn $build(to: &mut Vec) { static STMTS: Lazy> = Lazy::new(|| { - let cm = SourceMap::new(FilePathMapping::empty()); - let code = include_str!(concat!("_", stringify!($name), ".js")); - let fm = - cm.new_source_file(FileName::Custom(stringify!($name).into()), code.into()); - let lexer = Lexer::new( - Default::default(), - Default::default(), - StringInput::from(&*fm), - None, - ); - let stmts = Parser::new_from(lexer) - .parse_module() - .map(|script| drop_span(script.body)) - .map_err(|_| { - - }) - .unwrap(); - stmts + parse(include_str!(concat!("_", stringify!($name), ".js")), stringify!($name)) }); to.extend((*STMTS).clone()); diff --git a/crates/swc_bundler/tests/common/mod.rs b/crates/swc_bundler/tests/common/mod.rs index 939c50c4162..440dbc6af23 100644 --- a/crates/swc_bundler/tests/common/mod.rs +++ b/crates/swc_bundler/tests/common/mod.rs @@ -17,7 +17,7 @@ use swc_common::{ FileName, Mark, SourceMap, }; use swc_ecma_ast::EsVersion; -use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig}; +use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig}; use swc_ecma_transforms_base::resolver::resolver_with_mark; use swc_ecma_transforms_react::react; use swc_ecma_transforms_typescript::strip; @@ -109,19 +109,18 @@ impl Load for Loader { _ => unreachable!(), }; - let lexer = Lexer::new( + let module = parse_file_as_module( + &fm, Syntax::Typescript(TsConfig { decorators: true, tsx, ..Default::default() }), EsVersion::Es2020, - StringInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - let module = parser.parse_module().unwrap_or_else(|err| { + &mut vec![], + ) + .unwrap_or_else(|err| { let handler = Handler::with_tty_emitter(ColorConfig::Always, false, false, Some(self.cm.clone())); err.into_diagnostic(&handler).emit(); diff --git a/crates/swc_ecma_codegen/tests/fixture.rs b/crates/swc_ecma_codegen/tests/fixture.rs index 2acab339046..edf4fa6713c 100644 --- a/crates/swc_ecma_codegen/tests/fixture.rs +++ b/crates/swc_ecma_codegen/tests/fixture.rs @@ -1,12 +1,11 @@ use std::path::{Path, PathBuf}; -use swc_common::input::SourceFileInput; use swc_ecma_ast::EsVersion; use swc_ecma_codegen::{ text_writer::{JsWriter, WriteJs}, Emitter, }; -use swc_ecma_parser::{lexer::Lexer, Parser, Syntax}; +use swc_ecma_parser::{parse_file_as_module, Syntax}; use testing::{run_test2, NormalizedOutput}; fn run(input: &Path, minify: bool) { @@ -26,16 +25,14 @@ fn run(input: &Path, minify: bool) { run_test2(false, |cm, _| { let fm = cm.load_file(input).unwrap(); - let lexer = Lexer::new( + let m = parse_file_as_module( + &fm, Syntax::Typescript(Default::default()), EsVersion::latest(), - SourceFileInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - let m = parser - .parse_module() - .expect("failed to parse input as a module"); + &mut vec![], + ) + .expect("failed to parse input as a module"); let mut buf = vec![]; diff --git a/crates/swc_ecma_minifier/examples/minifier.rs b/crates/swc_ecma_minifier/examples/minifier.rs index b315d0d1db4..5e520a394bc 100644 --- a/crates/swc_ecma_minifier/examples/minifier.rs +++ b/crates/swc_ecma_minifier/examples/minifier.rs @@ -1,14 +1,16 @@ +#![deny(warnings)] + extern crate swc_node_base; use std::{env::args, fs, path::Path}; -use swc_common::{input::SourceFileInput, sync::Lrc, Mark, SourceMap}; +use swc_common::{sync::Lrc, Mark, SourceMap}; use swc_ecma_codegen::text_writer::JsWriter; use swc_ecma_minifier::{ optimize, option::{ExtraOptions, MangleOptions, MinifyOptions}, }; -use swc_ecma_parser::{lexer::Lexer, Parser}; +use swc_ecma_parser::parse_file_as_module; use swc_ecma_transforms::{fixer, hygiene, resolver_with_mark}; use swc_ecma_visit::FoldWith; @@ -22,21 +24,18 @@ fn main() { let top_level_mark = Mark::fresh(Mark::root()); - let lexer = Lexer::new( + let program = parse_file_as_module( + &fm, Default::default(), Default::default(), - SourceFileInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - let program = parser - .parse_module() - .map_err(|err| { - err.into_diagnostic(&handler).emit(); - }) - .map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark))) - .unwrap(); + &mut vec![], + ) + .map_err(|err| { + err.into_diagnostic(&handler).emit(); + }) + .map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark))) + .unwrap(); let output = optimize( program, diff --git a/crates/swc_ecma_minifier/src/analyzer/tests.rs b/crates/swc_ecma_minifier/src/analyzer/tests.rs index 995e6d9aac9..91952aeb2b0 100644 --- a/crates/swc_ecma_minifier/src/analyzer/tests.rs +++ b/crates/swc_ecma_minifier/src/analyzer/tests.rs @@ -1,8 +1,8 @@ use std::path::PathBuf; -use swc_common::{comments::SingleThreadedComments, input::SourceFileInput, Mark}; +use swc_common::{comments::SingleThreadedComments, Mark}; use swc_ecma_ast::Id; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, Syntax}; +use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; use swc_ecma_transforms::resolver_with_mark; use swc_ecma_visit::FoldWith; use testing::NormalizedOutput; @@ -22,25 +22,22 @@ fn snapshot(input: PathBuf) { let top_level_mark = Mark::fresh(Mark::root()); - let lexer = Lexer::new( + let marks = Marks::new(); + + let program = parse_file_as_module( + &fm, Syntax::Es(EsConfig { jsx: true, ..Default::default() }), Default::default(), - SourceFileInput::from(&*fm), Some(&comments), - ); - - let marks = Marks::new(); - - let mut parser = Parser::new_from(lexer); - let program = parser - .parse_module() - .map_err(|err| { - err.into_diagnostic(&handler).emit(); - }) - .map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark))); + &mut vec![], + ) + .map_err(|err| { + err.into_diagnostic(&handler).emit(); + }) + .map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark))); let program = match program { Ok(program) => program, diff --git a/crates/swc_ecma_minifier/src/compress/util/tests.rs b/crates/swc_ecma_minifier/src/compress/util/tests.rs index 8c8a7f5f660..554282993cf 100644 --- a/crates/swc_ecma_minifier/src/compress/util/tests.rs +++ b/crates/swc_ecma_minifier/src/compress/util/tests.rs @@ -1,6 +1,6 @@ -use swc_common::{input::SourceFileInput, util::take::Take, FileName}; +use swc_common::{util::take::Take, FileName}; use swc_ecma_ast::*; -use swc_ecma_parser::{lexer::Lexer, Parser}; +use swc_ecma_parser::parse_file_as_expr; use swc_ecma_transforms::fixer; use swc_ecma_visit::{noop_visit_mut_type, FoldWith, VisitMut, VisitMutWith}; use tracing::{info, warn}; @@ -25,16 +25,14 @@ fn assert_negate_cost(s: &str, in_bool_ctx: bool, is_ret_val_ignored: bool, expe testing::run_test2(false, |cm, handler| { let fm = cm.new_source_file(FileName::Anon, s.to_string()); - let lexer = Lexer::new( + let mut e = parse_file_as_expr( + &fm, Default::default(), swc_ecma_ast::EsVersion::latest(), - SourceFileInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - - let mut e = parser.parse_expr().map_err(|e| { + &mut vec![], + ) + .map_err(|e| { e.into_diagnostic(&handler).emit(); })?; diff --git a/crates/swc_ecma_minifier/tests/eval.rs b/crates/swc_ecma_minifier/tests/eval.rs index b5038314534..dacda485286 100644 --- a/crates/swc_ecma_minifier/tests/eval.rs +++ b/crates/swc_ecma_minifier/tests/eval.rs @@ -1,11 +1,13 @@ -use swc_common::{input::SourceFileInput, sync::Lrc, FileName, SourceMap}; +#![deny(warnings)] + +use swc_common::{sync::Lrc, FileName, SourceMap}; use swc_ecma_ast::*; use swc_ecma_codegen::{text_writer::JsWriter, Emitter}; use swc_ecma_minifier::{ eval::{EvalResult, Evaluator}, marks::Marks, }; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, Syntax}; +use swc_ecma_parser::{parse_file_as_expr, parse_file_as_module, EsConfig, Syntax}; use swc_ecma_transforms::resolver; use swc_ecma_utils::ExprExt; use swc_ecma_visit::{noop_visit_mut_type, VisitMut, VisitMutWith}; @@ -16,28 +18,26 @@ fn eval(module: &str, expr: &str) -> Option { let fm = cm.new_source_file(FileName::Anon, module.to_string()); let marks = Marks::new(); - let module_ast = { - let lexer = Lexer::new( - Default::default(), - EsVersion::latest(), - SourceFileInput::from(&*fm), - None, - ); - let mut parser = Parser::new_from(lexer); - parser.parse_module().unwrap() - }; + let module_ast = parse_file_as_module( + &fm, + Default::default(), + EsVersion::latest(), + None, + &mut vec![], + ) + .unwrap(); let expr_ast = { let fm = cm.new_source_file(FileName::Anon, expr.to_string()); - let lexer = Lexer::new( + parse_file_as_expr( + &fm, Default::default(), EsVersion::latest(), - SourceFileInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - parser.parse_expr().unwrap() + &mut vec![], + ) + .unwrap() }; let mut evaluator = Evaluator::new(module_ast, marks); @@ -81,19 +81,17 @@ impl PartialInliner { let fm = cm.new_source_file(FileName::Anon, src.to_string()); let marks = Marks::new(); - let mut module = { - let lexer = Lexer::new( - Syntax::Es(EsConfig { - jsx: true, - ..Default::default() - }), - EsVersion::latest(), - SourceFileInput::from(&*fm), - None, - ); - let mut parser = Parser::new_from(lexer); - parser.parse_module().unwrap() - }; + let mut module = parse_file_as_module( + &fm, + Syntax::Es(EsConfig { + jsx: true, + ..Default::default() + }), + EsVersion::latest(), + None, + &mut vec![], + ) + .unwrap(); module.visit_mut_with(&mut resolver()); let mut inliner = PartialInliner { @@ -115,17 +113,18 @@ impl PartialInliner { let expected_module = { let fm = cm.new_source_file(FileName::Anon, expected.to_string()); - let lexer = Lexer::new( + + parse_file_as_module( + &fm, Syntax::Es(EsConfig { jsx: true, ..Default::default() }), EsVersion::latest(), - SourceFileInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - parser.parse_module().unwrap() + &mut vec![], + ) + .unwrap() }; let expected = { let mut buf = vec![]; diff --git a/crates/swc_ecma_minifier/tests/exec.rs b/crates/swc_ecma_minifier/tests/exec.rs index f28febd2cac..5a36b2afafa 100644 --- a/crates/swc_ecma_minifier/tests/exec.rs +++ b/crates/swc_ecma_minifier/tests/exec.rs @@ -8,8 +8,7 @@ use ansi_term::Color; use anyhow::{bail, Context, Error}; use serde::Deserialize; use swc_common::{ - comments::SingleThreadedComments, errors::Handler, input::SourceFileInput, sync::Lrc, FileName, - Mark, SourceMap, + comments::SingleThreadedComments, errors::Handler, sync::Lrc, FileName, Mark, SourceMap, }; use swc_ecma_ast::Module; use swc_ecma_codegen::{ @@ -23,7 +22,7 @@ use swc_ecma_minifier::{ MinifyOptions, }, }; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, Syntax}; +use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax}; use swc_ecma_transforms::{fixer, hygiene, resolver_with_mark}; use swc_ecma_visit::{FoldWith, VisitMutWith}; use testing::DebugUsingDisplay; @@ -112,23 +111,20 @@ fn run( let top_level_mark = Mark::fresh(Mark::root()); - let lexer = Lexer::new( + let program = parse_file_as_module( + &fm, Syntax::Es(EsConfig { jsx: true, ..Default::default() }), Default::default(), - SourceFileInput::from(&*fm), Some(&comments), - ); - - let mut parser = Parser::new_from(lexer); - let program = parser - .parse_module() - .map_err(|err| { - err.into_diagnostic(handler).emit(); - }) - .map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark))); + &mut vec![], + ) + .map_err(|err| { + err.into_diagnostic(handler).emit(); + }) + .map(|module| module.fold_with(&mut resolver_with_mark(top_level_mark))); // Ignore parser errors. // diff --git a/crates/swc_ecma_minifier/tests/mangle.rs b/crates/swc_ecma_minifier/tests/mangle.rs index 49db8212ca1..5ac2d6499c8 100644 --- a/crates/swc_ecma_minifier/tests/mangle.rs +++ b/crates/swc_ecma_minifier/tests/mangle.rs @@ -2,7 +2,7 @@ use std::path::{Path, PathBuf}; -use swc_common::{input::SourceFileInput, sync::Lrc, FileName, Mark, SourceFile, SourceMap}; +use swc_common::{sync::Lrc, FileName, Mark, SourceFile, SourceMap}; use swc_ecma_ast::*; use swc_ecma_codegen::{ text_writer::{omit_trailing_semi, JsWriter, WriteJs}, @@ -12,7 +12,7 @@ use swc_ecma_minifier::{ optimize, option::{ExtraOptions, MangleOptions, ManglePropertiesOptions, MinifyOptions}, }; -use swc_ecma_parser::{lexer::Lexer, Parser}; +use swc_ecma_parser::parse_file_as_module; use swc_ecma_transforms::resolver_with_mark; use swc_ecma_visit::VisitMutWith; use testing::NormalizedOutput; @@ -46,15 +46,14 @@ fn parse(cm: Lrc, path: &Path) -> Module { } fn parse_fm(fm: Lrc) -> Module { - let lexer = Lexer::new( + parse_file_as_module( + &fm, Default::default(), EsVersion::latest(), - SourceFileInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - parser.parse_module().unwrap() + &mut vec![], + ) + .unwrap() } #[testing::fixture("tests/compress/fixture/**/output.js")] diff --git a/crates/swc_ecma_transforms_base/benches/deps.rs b/crates/swc_ecma_transforms_base/benches/deps.rs index cf4358a36c3..ba8b91e1bdd 100644 --- a/crates/swc_ecma_transforms_base/benches/deps.rs +++ b/crates/swc_ecma_transforms_base/benches/deps.rs @@ -3,7 +3,7 @@ extern crate test; use swc_common::{FileName, DUMMY_SP}; use swc_ecma_ast::*; -use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax}; +use swc_ecma_parser::{lexer::Lexer, parse_file_as_module, Parser, StringInput, Syntax}; use swc_ecma_transforms_base::pass::noop; use swc_ecma_utils::ExprFactory; use swc_ecma_visit::{FoldWith, Visit, VisitWith}; @@ -17,21 +17,21 @@ fn module_clone(b: &mut Bencher) { let _ = ::testing::run_test(false, |cm, handler| { let fm = cm.new_source_file(FileName::Anon, SOURCE.into()); - let lexer = Lexer::new( + + let mut errors = vec![]; + let module = parse_file_as_module( + &fm, Syntax::Typescript(Default::default()), Default::default(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - let module = parser - .parse_module() - .map_err(|e| { - e.into_diagnostic(handler).emit(); - }) - .unwrap(); + &mut errors, + ) + .map_err(|e| { + e.into_diagnostic(handler).emit(); + }) + .unwrap(); - for e in parser.take_errors() { + for e in errors { e.into_diagnostic(handler).emit(); } @@ -46,21 +46,21 @@ fn fold_empty(b: &mut Bencher) { let _ = ::testing::run_test(false, |cm, handler| { let fm = cm.new_source_file(FileName::Anon, SOURCE.into()); - let lexer = Lexer::new( + + let mut errors = vec![]; + let module = parse_file_as_module( + &fm, Syntax::Typescript(Default::default()), Default::default(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - let module = parser - .parse_module() - .map_err(|e| { - e.into_diagnostic(handler).emit(); - }) - .unwrap(); + &mut errors, + ) + .map_err(|e| { + e.into_diagnostic(handler).emit(); + }) + .unwrap(); - for e in parser.take_errors() { + for e in errors { e.into_diagnostic(handler).emit(); } @@ -79,19 +79,18 @@ fn fold_noop_impl_all(b: &mut Bencher) { let _ = ::testing::run_test(false, |cm, handler| { let fm = cm.new_source_file(FileName::Anon, SOURCE.into()); - let lexer = Lexer::new( + let mut errors = vec![]; + let module = parse_file_as_module( + &fm, Syntax::Typescript(Default::default()), Default::default(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - let module = parser - .parse_module() - .map_err(|e| e.into_diagnostic(handler).emit()) - .unwrap(); + &mut errors, + ) + .map_err(|e| e.into_diagnostic(handler).emit()) + .unwrap(); - for e in parser.take_errors() { + for e in errors { e.into_diagnostic(handler).emit(); } @@ -109,21 +108,20 @@ fn fold_noop_impl_vec(b: &mut Bencher) { let _ = ::testing::run_test(false, |cm, handler| { let fm = cm.new_source_file(FileName::Anon, SOURCE.into()); - let lexer = Lexer::new( + let mut errors = vec![]; + let module = parse_file_as_module( + &fm, Syntax::Typescript(Default::default()), Default::default(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - let module = parser - .parse_module() - .map_err(|e| { - e.into_diagnostic(handler).emit(); - }) - .unwrap(); + &mut errors, + ) + .map_err(|e| { + e.into_diagnostic(handler).emit(); + }) + .unwrap(); - for e in parser.take_errors() { + for e in errors { e.into_diagnostic(handler).emit(); } diff --git a/crates/swc_ecma_transforms_base/tests/ts_resolver.rs b/crates/swc_ecma_transforms_base/tests/ts_resolver.rs index 860f790a928..8d3207e8954 100644 --- a/crates/swc_ecma_transforms_base/tests/ts_resolver.rs +++ b/crates/swc_ecma_transforms_base/tests/ts_resolver.rs @@ -1,8 +1,10 @@ +#![deny(warnings)] + use std::path::PathBuf; -use swc_common::{input::SourceFileInput, Mark, SyntaxContext}; +use swc_common::{Mark, SyntaxContext}; use swc_ecma_ast::*; -use swc_ecma_parser::{lexer::Lexer, Parser, Syntax, TsConfig}; +use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig}; use swc_ecma_transforms_base::resolver::ts_resolver; use swc_ecma_visit::{FoldWith, Visit, VisitWith}; use testing::fixture; @@ -15,7 +17,8 @@ fn no_empty(input: PathBuf) { testing::run_test2(false, |cm, _handler| { let fm = cm.load_file(&input).expect("failed to load file"); - let lexer = Lexer::new( + let module = match parse_file_as_module( + &fm, Syntax::Typescript(TsConfig { tsx: input.ends_with("tsx"), decorators: true, @@ -23,12 +26,9 @@ fn no_empty(input: PathBuf) { ..Default::default() }), EsVersion::latest(), - SourceFileInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - let module = match parser.parse_module() { + &mut vec![], + ) { Ok(v) => v, // We are not testing parser Err(..) => return Ok(()), diff --git a/crates/swc_node_bundler/benches/bench.rs b/crates/swc_node_bundler/benches/bench.rs index e1af8e5412c..41f7a69bbaf 100644 --- a/crates/swc_node_bundler/benches/bench.rs +++ b/crates/swc_node_bundler/benches/bench.rs @@ -17,7 +17,7 @@ use swc_atoms::js_word; use swc_bundler::{Bundler, Load, ModuleData, ModuleRecord}; use swc_common::{sync::Lrc, FileName, Mark, SourceMap, Span, GLOBALS}; use swc_ecma_ast::*; -use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig}; +use swc_ecma_parser::{parse_file_as_module, Syntax, TsConfig}; use swc_ecma_transforms::{resolver_with_mark, typescript::strip}; use swc_ecma_visit::FoldWith; use test::Bencher; @@ -76,19 +76,18 @@ impl Load for Loader { _ => unreachable!(), }; - let lexer = Lexer::new( + let module = parse_file_as_module( + &fm, Syntax::Typescript(TsConfig { decorators: true, tsx, ..Default::default() }), EsVersion::Es2020, - StringInput::from(&*fm), None, - ); - - let mut parser = Parser::new_from(lexer); - let module = parser.parse_module().unwrap(); + &mut vec![], + ) + .unwrap(); let mark = Mark::fresh(Mark::root()); let module = module diff --git a/crates/swc_node_bundler/src/loaders/json.rs b/crates/swc_node_bundler/src/loaders/json.rs index 59b4172f98c..09651839cc1 100644 --- a/crates/swc_node_bundler/src/loaders/json.rs +++ b/crates/swc_node_bundler/src/loaders/json.rs @@ -1,20 +1,12 @@ use std::sync::Arc; use anyhow::{anyhow, Error}; -use swc_common::{input::SourceFileInput, SourceFile, DUMMY_SP}; +use swc_common::{SourceFile, DUMMY_SP}; use swc_ecma_ast::{EsVersion, *}; -use swc_ecma_parser::{lexer::Lexer, Parser, Syntax}; +use swc_ecma_parser::{parse_file_as_expr, Syntax}; pub(super) fn load_json_as_module(fm: &Arc) -> Result { - let lexer = Lexer::new( - Syntax::default(), - EsVersion::Es2020, - SourceFileInput::from(&**fm), - None, - ); - let mut parser = Parser::new_from(lexer); - let expr = parser - .parse_expr() + let expr = parse_file_as_expr(fm, Syntax::default(), EsVersion::Es2020, None, &mut vec![]) .map_err(|err| anyhow!("failed parse json as javascript object: {:#?}", err))?; let export = ModuleItem::Stmt(Stmt::Expr(ExprStmt { diff --git a/crates/swc_node_bundler/src/loaders/swc.rs b/crates/swc_node_bundler/src/loaders/swc.rs index d7ea1fa8b9c..8f5ae337c3c 100644 --- a/crates/swc_node_bundler/src/loaders/swc.rs +++ b/crates/swc_node_bundler/src/loaders/swc.rs @@ -15,7 +15,7 @@ use swc_common::{ FileName, DUMMY_SP, }; use swc_ecma_ast::{EsVersion, Expr, Lit, Module, Program, Str}; -use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax}; +use swc_ecma_parser::{parse_file_as_module, Syntax}; use swc_ecma_transforms::{ helpers, optimization::{ @@ -108,14 +108,15 @@ impl SwcLoader { .compiler .cm .new_source_file(name.clone(), "module.exports = {}".to_string()); - let lexer = Lexer::new( + + let module = parse_file_as_module( + &fm, Syntax::Es(Default::default()), Default::default(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - let module = parser.parse_module().unwrap(); + &mut vec![], + ) + .unwrap(); return Ok(ModuleData { fm, module, diff --git a/crates/swc_plugin_runner/tests/integration.rs b/crates/swc_plugin_runner/tests/integration.rs index b491406eda6..1bf1420e50c 100644 --- a/crates/swc_plugin_runner/tests/integration.rs +++ b/crates/swc_plugin_runner/tests/integration.rs @@ -7,7 +7,7 @@ use std::{ use anyhow::{anyhow, Error}; use swc_common::{errors::HANDLER, plugin::Serialized, sync::Lazy, FileName}; use swc_ecma_ast::{CallExpr, Callee, EsVersion, Expr, Lit, MemberExpr, Program, Str}; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax}; +use swc_ecma_parser::{parse_file_as_program, EsConfig, Syntax}; use swc_ecma_visit::{Visit, VisitWith}; use swc_plugin_runner::cache::PluginModuleCache; @@ -69,17 +69,16 @@ fn internal() -> Result<(), Error> { testing::run_test(false, |cm, _handler| { let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into()); - let lexer = Lexer::new( + let program = parse_file_as_program( + &fm, Syntax::Es(EsConfig { ..Default::default() }), EsVersion::latest(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - - let program = parser.parse_program().unwrap(); + &mut vec![], + ) + .unwrap(); let program = Serialized::serialize(&program).expect("Should serializable"); let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable"); @@ -116,17 +115,16 @@ fn internal() -> Result<(), Error> { testing::run_test2(false, |cm, handler| { let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into()); - let lexer = Lexer::new( + let program = parse_file_as_program( + &fm, Syntax::Es(EsConfig { ..Default::default() }), EsVersion::latest(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - - let program = parser.parse_program().unwrap(); + &mut vec![], + ) + .unwrap(); let program = Serialized::serialize(&program).expect("Should serializable"); let config = Serialized::serialize(&"{}".to_string()).expect("Should serializable"); @@ -156,17 +154,16 @@ fn internal() -> Result<(), Error> { testing::run_test(false, |cm, _handler| { let fm = cm.new_source_file(FileName::Anon, "console.log(foo)".into()); - let lexer = Lexer::new( + let program = parse_file_as_program( + &fm, Syntax::Es(EsConfig { ..Default::default() }), EsVersion::latest(), - StringInput::from(&*fm), None, - ); - let mut parser = Parser::new_from(lexer); - - let program = parser.parse_program().unwrap(); + &mut vec![], + ) + .unwrap(); let mut serialized_program = Serialized::serialize(&program).expect("Should serializable"); let cache: Lazy = Lazy::new(PluginModuleCache::new); diff --git a/crates/swc_webpack_ast/src/lib.rs b/crates/swc_webpack_ast/src/lib.rs index 3ef018eae42..7fba2e23652 100644 --- a/crates/swc_webpack_ast/src/lib.rs +++ b/crates/swc_webpack_ast/src/lib.rs @@ -7,7 +7,7 @@ use swc_common::{ GLOBALS, }; use swc_ecma_ast::*; -use swc_ecma_parser::{lexer::Lexer, EsConfig, Parser, StringInput, Syntax, TsConfig}; +use swc_ecma_parser::{parse_file_as_module, EsConfig, Syntax, TsConfig}; use swc_ecma_transforms_base::resolver::resolver_with_mark; use swc_ecma_visit::VisitMutWith; use swc_estree_ast::flavor::Flavor; @@ -62,15 +62,14 @@ where }; let module = { - let lexer = Lexer::new(syntax, EsVersion::latest(), StringInput::from(&*fm), None); - let mut parser = Parser::new_from(lexer); - - parser.parse_module().map_err(|err| { - HANDLER.with(|h| { - err.into_diagnostic(h).emit(); - anyhow!("failed to parse module") - }) - })? + parse_file_as_module(&fm, syntax, EsVersion::latest(), None, &mut vec![]).map_err( + |err| { + HANDLER.with(|h| { + err.into_diagnostic(h).emit(); + anyhow!("failed to parse module") + }) + }, + )? }; let ast = GLOBALS.set(&globals, || webpack_ast(cm.clone(), fm.clone(), module))?;