fix(es/parser): Fix conditional compilation (#8343)

**Description:**

`swc_ecma_parser` crate has a `typescript` feature which enables TS parsing, the `Syntax::Typescript` variant is behind the `typescript` feature, compiling without this feature fails as `Syntax::Typescript` is referenced at many places without `#[cfg(feature = "typescript")]`
This commit is contained in:
Han Seung Min - 한승민 2023-11-26 20:36:05 +09:00 committed by GitHub
parent 7e8b6c0f04
commit a423681df8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 17 deletions

View File

@ -171,6 +171,7 @@ impl Syntax {
auto_accessors: true, auto_accessors: true,
.. ..
}) => true, }) => true,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true, Syntax::Typescript(_) => true,
_ => false, _ => false,
} }
@ -181,16 +182,19 @@ impl Syntax {
Syntax::Es(EsConfig { Syntax::Es(EsConfig {
import_attributes, .. import_attributes, ..
}) => import_attributes, }) => import_attributes,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true, Syntax::Typescript(_) => true,
} }
} }
/// Should we parse jsx? /// Should we parse jsx?
pub fn jsx(self) -> bool { pub fn jsx(self) -> bool {
matches!( match self {
self, Syntax::Es(EsConfig { jsx: true, .. }) => true,
Syntax::Es(EsConfig { jsx: true, .. }) | Syntax::Typescript(TsConfig { tsx: true, .. }) #[cfg(feature = "typescript")]
) Syntax::Typescript(TsConfig { tsx: true, .. }) => true,
_ => false,
}
} }
pub fn fn_bind(self) -> bool { pub fn fn_bind(self) -> bool {
@ -198,26 +202,28 @@ impl Syntax {
} }
pub fn decorators(self) -> bool { pub fn decorators(self) -> bool {
matches!( match self {
self,
Syntax::Es(EsConfig { Syntax::Es(EsConfig {
decorators: true, decorators: true, ..
.. }) => true,
}) | Syntax::Typescript(TsConfig { #[cfg(feature = "typescript")]
decorators: true, Syntax::Typescript(TsConfig {
.. decorators: true, ..
}) }) => true,
) _ => false,
}
} }
pub fn decorators_before_export(self) -> bool { pub fn decorators_before_export(self) -> bool {
matches!( match self {
self,
Syntax::Es(EsConfig { Syntax::Es(EsConfig {
decorators_before_export: true, decorators_before_export: true,
.. ..
}) | Syntax::Typescript(..) }) => true,
) #[cfg(feature = "typescript")]
Syntax::Typescript(..) => true,
_ => false,
}
} }
/// Should we parse typescript? /// Should we parse typescript?
@ -244,6 +250,7 @@ impl Syntax {
pub fn dts(self) -> bool { pub fn dts(self) -> bool {
match self { match self {
#[cfg(feature = "typescript")]
Syntax::Typescript(t) => t.dts, Syntax::Typescript(t) => t.dts,
_ => false, _ => false,
} }
@ -255,6 +262,7 @@ impl Syntax {
allow_super_outside_method, allow_super_outside_method,
.. ..
}) => allow_super_outside_method, }) => allow_super_outside_method,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true, Syntax::Typescript(_) => true,
} }
} }
@ -265,12 +273,14 @@ impl Syntax {
allow_return_outside_function, allow_return_outside_function,
.. ..
}) => allow_return_outside_function, }) => allow_return_outside_function,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => false, Syntax::Typescript(_) => false,
} }
} }
pub(crate) fn early_errors(self) -> bool { pub(crate) fn early_errors(self) -> bool {
match self { match self {
#[cfg(feature = "typescript")]
Syntax::Typescript(t) => !t.no_early_errors, Syntax::Typescript(t) => !t.no_early_errors,
Syntax::Es(..) => true, Syntax::Es(..) => true,
} }
@ -278,6 +288,7 @@ impl Syntax {
fn disallow_ambiguous_jsx_like(self) -> bool { fn disallow_ambiguous_jsx_like(self) -> bool {
match self { match self {
#[cfg(feature = "typescript")]
Syntax::Typescript(t) => t.disallow_ambiguous_jsx_like, Syntax::Typescript(t) => t.disallow_ambiguous_jsx_like,
_ => false, _ => false,
} }
@ -289,6 +300,7 @@ impl Syntax {
explicit_resource_management: using_decl, explicit_resource_management: using_decl,
.. ..
}) => *using_decl, }) => *using_decl,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true, Syntax::Typescript(_) => true,
} }
} }

View File

@ -34,6 +34,7 @@ mod pat;
mod stmt; mod stmt;
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
#[cfg(feature = "typescript")]
mod typescript; mod typescript;
mod util; mod util;
@ -66,10 +67,13 @@ impl<'a> Parser<Lexer<'a>> {
impl<I: Tokens> Parser<I> { impl<I: Tokens> Parser<I> {
pub fn new_from(mut input: I) -> Self { pub fn new_from(mut input: I) -> Self {
#[cfg(feature = "typescript")]
let in_declare = matches!( let in_declare = matches!(
input.syntax(), input.syntax(),
Syntax::Typescript(TsConfig { dts: true, .. }) Syntax::Typescript(TsConfig { dts: true, .. })
); );
#[cfg(not(feature = "typescript"))]
let in_declare = false;
let ctx = Context { let ctx = Context {
in_declare, in_declare,
..input.ctx() ..input.ctx()