Skip to content

Commit

Permalink
fix(es/parser): Fix conditional compilation (#8343)
Browse files Browse the repository at this point in the history
**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")]`
  • Loading branch information
arHSM committed Nov 26, 2023
1 parent 7e8b6c0 commit a423681
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
46 changes: 29 additions & 17 deletions crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl Syntax {
auto_accessors: true,
..
}) => true,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true,
_ => false,
}
Expand All @@ -181,43 +182,48 @@ 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 {
matches!(self, Syntax::Es(EsConfig { fn_bind: true, .. }))
}

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?
Expand All @@ -244,6 +250,7 @@ impl Syntax {

pub fn dts(self) -> bool {
match self {
#[cfg(feature = "typescript")]
Syntax::Typescript(t) => t.dts,
_ => false,
}
Expand All @@ -255,6 +262,7 @@ impl Syntax {
allow_super_outside_method,
..
}) => allow_super_outside_method,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true,
}
}
Expand All @@ -265,19 +273,22 @@ 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,
}
}

fn disallow_ambiguous_jsx_like(self) -> bool {
match self {
#[cfg(feature = "typescript")]
Syntax::Typescript(t) => t.disallow_ambiguous_jsx_like,
_ => false,
}
Expand All @@ -289,6 +300,7 @@ impl Syntax {
explicit_resource_management: using_decl,
..
}) => *using_decl,
#[cfg(feature = "typescript")]
Syntax::Typescript(_) => true,
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod pat;
mod stmt;
#[cfg(test)]
mod tests;
#[cfg(feature = "typescript")]
mod typescript;
mod util;

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a423681

Please sign in to comment.