summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitson Kelly <me@kitsonkelly.com>2021-10-27 17:18:53 +1100
committerGitHub <noreply@github.com>2021-10-27 17:18:53 +1100
commitb44b26c8842522e0e952c31aeb42c7fb85a5c7a7 (patch)
tree9a80196aed6b669c750311dc35b4b1f4db81fd6b
parent1c739470b590ea13dc0aa67c0ecc7ea6f49b5746 (diff)
fix(cli): no-check respects inlineSources compiler option (#12559)
Fixes #12064
-rw-r--r--cli/ast/mod.rs26
-rw-r--r--cli/emit.rs6
-rw-r--r--cli/tests/testdata/compiler_api_test.ts28
3 files changed, 57 insertions, 3 deletions
diff --git a/cli/ast/mod.rs b/cli/ast/mod.rs
index 894a42d4e..626e75b45 100644
--- a/cli/ast/mod.rs
+++ b/cli/ast/mod.rs
@@ -203,6 +203,27 @@ fn strip_config_from_emit_options(
}
}
+/// Implements a configuration trait for source maps that reflects the logic
+/// to embed sources in the source map or not.
+#[derive(Debug)]
+pub(crate) struct SourceMapConfig {
+ pub inline_sources: bool,
+}
+
+impl deno_ast::swc::common::source_map::SourceMapGenConfig for SourceMapConfig {
+ fn file_name_to_source(&self, f: &FileName) -> String {
+ f.to_string()
+ }
+
+ fn inline_sources_content(&self, f: &FileName) -> bool {
+ match f {
+ FileName::Real(..) | FileName::Custom(..) => false,
+ FileName::Url(..) => self.inline_sources,
+ _ => true,
+ }
+ }
+}
+
/// Transform a TypeScript file into a JavaScript file, based on the supplied
/// options.
///
@@ -213,6 +234,9 @@ pub fn transpile(
) -> Result<(String, Option<String>), AnyError> {
let program: Program = (*parsed_source.program()).clone();
let source_map = Rc::new(SourceMap::default());
+ let source_map_config = SourceMapConfig {
+ inline_sources: options.inline_sources,
+ };
let specifier = resolve_url_or_path(parsed_source.specifier())?;
let file_name = FileName::Url(specifier);
source_map
@@ -252,7 +276,7 @@ pub fn transpile(
{
let mut buf = Vec::new();
source_map
- .build_source_map_from(&mut src_map_buf, None)
+ .build_source_map_with_config(&mut src_map_buf, None, source_map_config)
.to_writer(&mut buf)?;
if options.inline_source_map {
diff --git a/cli/emit.rs b/cli/emit.rs
index 7b25713fa..a4713f532 100644
--- a/cli/emit.rs
+++ b/cli/emit.rs
@@ -184,7 +184,6 @@ pub(crate) fn get_ts_config(
"emitDecoratorMetadata": false,
"importsNotUsedAsValues": "remove",
"inlineSourceMap": true,
- // TODO(@kitsonk) make this actually work when https://github.com/swc-project/swc/issues/2218 addressed.
"inlineSources": true,
"sourceMap": false,
"jsx": "react",
@@ -519,6 +518,9 @@ pub(crate) fn bundle(
let globals = swc::common::Globals::new();
deno_ast::swc::common::GLOBALS.set(&globals, || {
let emit_options: ast::EmitOptions = options.ts_config.into();
+ let source_map_config = ast::SourceMapConfig {
+ inline_sources: emit_options.inline_sources,
+ };
let cm = Rc::new(swc::common::SourceMap::new(
swc::common::FilePathMapping::empty(),
@@ -577,7 +579,7 @@ pub(crate) fn bundle(
let mut maybe_map: Option<String> = None;
{
let mut buf = Vec::new();
- cm.build_source_map_from(&mut srcmap, None)
+ cm.build_source_map_with_config(&mut srcmap, None, source_map_config)
.to_writer(&mut buf)?;
if emit_options.inline_source_map {
let encoded_map = format!(
diff --git a/cli/tests/testdata/compiler_api_test.ts b/cli/tests/testdata/compiler_api_test.ts
index 914147f76..9870908d1 100644
--- a/cli/tests/testdata/compiler_api_test.ts
+++ b/cli/tests/testdata/compiler_api_test.ts
@@ -529,3 +529,31 @@ Deno.test({
);
},
});
+
+Deno.test({
+ name: "Deno.emit() - no check respects inlineSources compiler option",
+ async fn() {
+ const { files } = await Deno.emit(
+ "file:///a.ts",
+ {
+ check: false,
+ compilerOptions: {
+ types: ["file:///b.d.ts"],
+ inlineSources: true,
+ },
+ sources: {
+ "file:///a.ts": `const b = new B();
+ console.log(b.b);`,
+ "file:///b.d.ts": `declare class B {
+ b: string;
+ }`,
+ },
+ },
+ );
+ const sourceMap: { sourcesContent?: string[] } = JSON.parse(
+ files["file:///a.ts.js.map"],
+ );
+ assert(sourceMap.sourcesContent);
+ assertEquals(sourceMap.sourcesContent.length, 1);
+ },
+});