Skip to content

Commit

Permalink
fix: compute whether script (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Oct 31, 2024
1 parent be80692 commit 040709e
Show file tree
Hide file tree
Showing 3 changed files with 372 additions and 12 deletions.
41 changes: 41 additions & 0 deletions src/parsed_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,40 @@ pub enum ProgramRef<'a> {
}

impl<'a> ProgramRef<'a> {
/// Computes whether the program is a script.
pub fn compute_is_script(&self) -> bool {
// Necessary because swc will make a program a module when it contains
// typescript specific CJS imports/exports like `import add = require('./add');`.
match self {
ProgramRef::Module(m) => {
for m in m.body.iter() {
match m {
ModuleItem::ModuleDecl(m) => match m {
ModuleDecl::Import(_)
| ModuleDecl::ExportDecl(_)
| ModuleDecl::ExportNamed(_)
| ModuleDecl::ExportDefaultDecl(_)
| ModuleDecl::ExportDefaultExpr(_)
| ModuleDecl::ExportAll(_) => return false,
// the prescence of these means it's a script
ModuleDecl::TsImportEquals(_)
| ModuleDecl::TsExportAssignment(_) => {
return true;
}
ModuleDecl::TsNamespaceExport(_) => {
// ignore `export as namespace x;` as it's type only
}
},
ModuleItem::Stmt(_) => {}
}
}

false
}
ProgramRef::Script(_) => true,
}
}

pub fn unwrap_module(&self) -> &Module {
match self {
ProgramRef::Module(m) => m,
Expand Down Expand Up @@ -386,14 +420,21 @@ impl ParsedSource {
}

/// Gets if this source is a module.
#[deprecated(note = "use compute_is_script() instead")]
pub fn is_module(&self) -> bool {
matches!(self.program_ref(), ProgramRef::Module(_))
}

/// Gets if this source is a script.
#[deprecated(note = "use compute_is_script() instead")]
pub fn is_script(&self) -> bool {
matches!(self.program_ref(), ProgramRef::Script(_))
}

/// Computes whether this program should be treated as a script.
pub fn compute_is_script(&self) -> bool {
self.program_ref().compute_is_script()
}
}

impl<'a> SourceTextProvider<'a> for &'a ParsedSource {
Expand Down
Loading

0 comments on commit 040709e

Please sign in to comment.