Skip to content

Commit d1d9cb8

Browse files
committed
chore: keep fixing
1 parent bcfe500 commit d1d9cb8

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

crates/rspack_core/src/concatenated_module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rspack_collections::{
2020
use rspack_error::{Diagnosable, Diagnostic, Error, Result, ToStringResultToRspackResultExt};
2121
use rspack_hash::{HashDigest, HashFunction, RspackHash, RspackHashDigest};
2222
use rspack_hook::define_hook;
23-
use rspack_javascript_compiler::ast::Ast;
23+
use rspack_javascript_compiler::{ast::Ast, transform::parse_file_as_module};
2424
use rspack_sources::{
2525
BoxSource, CachedSource, ConcatSource, RawStringSource, ReplaceSource, Source, SourceExt,
2626
};
@@ -33,7 +33,7 @@ use swc_core::{
3333
ecma::{
3434
ast::{EsVersion, Program},
3535
atoms::Atom,
36-
parser::{Syntax, parse_file_as_module},
36+
parser::Syntax,
3737
transforms::base::resolver,
3838
},
3939
};

crates/rspack_javascript_compiler/src/compiler/transform.rs

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ use swc_core::{
3434
FileName, GLOBALS, Mark, SourceFile, SourceMap,
3535
comments::{Comments, SingleThreadedComments},
3636
errors::Handler,
37+
input::SourceFileInput,
3738
},
3839
ecma::{
39-
ast::{EsVersion, Pass, Program},
40-
parser::{
41-
Syntax, parse_file_as_commonjs, parse_file_as_module, parse_file_as_program,
42-
parse_file_as_script,
43-
},
40+
ast::{EsVersion, Module, Pass, Program, Script},
41+
parser::{Lexer, PResult, Parser, Syntax, unstable::Capturing},
4442
transforms::base::helpers::{self, Helpers},
4543
},
4644
};
45+
use swc_ecma_lexer::error::Error;
4746
use swc_error_reporters::handler::try_with_handler;
4847
use url::Url;
4948

@@ -650,3 +649,68 @@ impl<'a> JavaScriptTransformer<'a> {
650649
}
651650
}
652651
}
652+
653+
pub fn parse_file_as_module(
654+
fm: &SourceFile,
655+
syntax: Syntax,
656+
target: EsVersion,
657+
comments: Option<&dyn Comments>,
658+
recovered_errors: &mut Vec<Error>,
659+
) -> PResult<Module> {
660+
with_file_parser(fm, syntax, target, comments, recovered_errors, |p| {
661+
p.parse_module()
662+
})
663+
}
664+
665+
pub fn with_file_parser<T>(
666+
fm: &SourceFile,
667+
syntax: Syntax,
668+
target: EsVersion,
669+
comments: Option<&dyn Comments>,
670+
recovered_errors: &mut Vec<Error>,
671+
op: impl for<'aa> FnOnce(&mut Parser<Capturing<Lexer>>) -> PResult<T>,
672+
) -> PResult<T> {
673+
let lexer = self::Lexer::new(syntax, target, SourceFileInput::from(fm), comments);
674+
let mut p = Parser::new_from(Capturing::new(lexer));
675+
let ret = op(&mut p);
676+
677+
recovered_errors.append(&mut p.take_errors());
678+
679+
ret
680+
}
681+
682+
pub fn parse_file_as_script(
683+
fm: &SourceFile,
684+
syntax: Syntax,
685+
target: EsVersion,
686+
comments: Option<&dyn Comments>,
687+
recovered_errors: &mut Vec<Error>,
688+
) -> PResult<Script> {
689+
with_file_parser(fm, syntax, target, comments, recovered_errors, |p| {
690+
p.parse_script()
691+
})
692+
}
693+
694+
pub fn parse_file_as_commonjs(
695+
fm: &SourceFile,
696+
syntax: Syntax,
697+
target: EsVersion,
698+
comments: Option<&dyn Comments>,
699+
recovered_errors: &mut Vec<Error>,
700+
) -> PResult<Script> {
701+
with_file_parser(fm, syntax, target, comments, recovered_errors, |p| {
702+
p.parse_commonjs()
703+
})
704+
}
705+
706+
pub fn parse_file_as_program(
707+
fm: &SourceFile,
708+
syntax: Syntax,
709+
target: EsVersion,
710+
comments: Option<&dyn Comments>,
711+
recovered_errors: &mut Vec<Error>,
712+
) -> PResult<Program> {
713+
with_file_parser(fm, syntax, target, comments, recovered_errors, |p| {
714+
p.parse_program()
715+
})
716+
}

0 commit comments

Comments
 (0)