Skip to content

Commit

Permalink
fix(cli): no-check respects inlineSources compiler option (denoland#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk authored Oct 27, 2021
1 parent 1c73947 commit b44b26c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
26 changes: 25 additions & 1 deletion cli/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions cli/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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!(
Expand Down
28 changes: 28 additions & 0 deletions cli/tests/testdata/compiler_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:https:///a.ts",
{
check: false,
compilerOptions: {
types: ["file:https:///b.d.ts"],
inlineSources: true,
},
sources: {
"file:https:///a.ts": `const b = new B();
console.log(b.b);`,
"file:https:///b.d.ts": `declare class B {
b: string;
}`,
},
},
);
const sourceMap: { sourcesContent?: string[] } = JSON.parse(
files["file:https:///a.ts.js.map"],
);
assert(sourceMap.sourcesContent);
assertEquals(sourceMap.sourcesContent.length, 1);
},
});

0 comments on commit b44b26c

Please sign in to comment.