Skip to content

Commit 1c8c2ba

Browse files
authored
chore: tests for compute_script (#294)
1 parent 8873b45 commit 1c8c2ba

File tree

1 file changed

+59
-6
lines changed

1 file changed

+59
-6
lines changed

src/parsed_source.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a> ProgramRef<'a> {
121121
| ModuleDecl::ExportDefaultDecl(_)
122122
| ModuleDecl::ExportDefaultExpr(_)
123123
| ModuleDecl::ExportAll(_) => return false,
124-
// the prescence of these means it's a script
124+
// the presence of these means it's a script
125125
ModuleDecl::TsImportEquals(_)
126126
| ModuleDecl::TsExportAssignment(_) => {
127127
return true;
@@ -471,7 +471,13 @@ impl ParsedSource {
471471

472472
/// Computes whether this program should be treated as a script.
473473
pub fn compute_is_script(&self) -> bool {
474-
self.program_ref().compute_is_script()
474+
if self.media_type().is_typed() {
475+
// for typescript, we need to compute whether it's a script
476+
// because swc parses TsImportEquals as a module
477+
self.program_ref().compute_is_script()
478+
} else {
479+
matches!(self.program_ref(), ProgramRef::Script(_))
480+
}
475481
}
476482
}
477483

@@ -536,15 +542,15 @@ impl ParsedSource {
536542

537543
#[cfg(test)]
538544
mod test {
545+
use super::*;
546+
use crate::parse_program;
547+
use crate::ParseParams;
548+
539549
#[cfg(feature = "view")]
540550
#[test]
541551
fn should_parse_program() {
542-
use crate::parse_program;
543552
use crate::view::NodeTrait;
544553
use crate::ModuleSpecifier;
545-
use crate::ParseParams;
546-
547-
use super::*;
548554

549555
let program = parse_program(ParseParams {
550556
specifier: ModuleSpecifier::parse("file:///my_file.js").unwrap(),
@@ -565,4 +571,51 @@ mod test {
565571

566572
assert_eq!(result, 2);
567573
}
574+
575+
#[test]
576+
fn compute_is_script() {
577+
fn get(text: &str, ext: &str) -> bool {
578+
let specifier =
579+
ModuleSpecifier::parse(&format!("file:///my_file.{}", ext)).unwrap();
580+
let media_type = MediaType::from_specifier(&specifier);
581+
let program = parse_program(ParseParams {
582+
specifier,
583+
text: text.into(),
584+
media_type,
585+
capture_tokens: true,
586+
maybe_syntax: None,
587+
scope_analysis: false,
588+
})
589+
.unwrap();
590+
let is_script = program.compute_is_script();
591+
assert_eq!(
592+
program.program_ref().compute_is_script(),
593+
is_script,
594+
"text: {}",
595+
text
596+
);
597+
is_script
598+
}
599+
600+
// false, tla
601+
assert!(!get(
602+
"const mod = await import('./soljson.js');\nconsole.log(mod)",
603+
"js"
604+
));
605+
assert!(!get(
606+
"const mod = await import('./soljson.js');\nconsole.log(mod)",
607+
"js"
608+
));
609+
610+
// false, import
611+
assert!(!get("import './test';", "js"));
612+
assert!(!get("import './test';", "ts"));
613+
614+
// true, require
615+
assert!(get("require('test')", "js"));
616+
assert!(get("require('test')", "ts"));
617+
618+
// true, ts import equals
619+
assert!(get("import value = require('test');", "ts"));
620+
}
568621
}

0 commit comments

Comments
 (0)