Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ability to specify if transpiling cjs #284

Merged
4 changes: 0 additions & 4 deletions src/cjs_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ impl ParsedSource {
///
/// Note: This will panic if called on a non-script.
pub fn analyze_cjs(&self) -> CjsAnalysis {
if !self.is_script() {
panic!("Cannot analyze non-script: {}", self.specifier())
}

let mut visitor = CjsVisitor::default();
match self.program_ref() {
ProgramRef::Module(n) => visitor.visit_module(n),
Expand Down
33 changes: 33 additions & 0 deletions src/parsed_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ impl Globals {
}
}

/// If the module is an Es module or CommonJs module.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ModuleKind {
Esm,
Cjs,
}

impl ModuleKind {
#[inline(always)]
pub fn from_is_cjs(is_cjs: bool) -> Self {
if is_cjs {
ModuleKind::Cjs
} else {
ModuleKind::Esm
}
}

#[inline(always)]
pub fn from_is_esm(is_esm: bool) -> Self {
ModuleKind::from_is_cjs(!is_esm)
}

#[inline(always)]
pub fn is_cjs(&self) -> bool {
matches!(self, Self::Cjs)
}

#[inline(always)]
pub fn is_esm(&self) -> bool {
matches!(self, Self::Esm)
}
}

/// A reference to a Program.
///
/// It is generally preferable for functions to accept this over `&Program`
Expand Down
Loading