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

View File

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