Skip to content

Commit

Permalink
refactor: use new deno_ast emitter API (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato authored Apr 11, 2024
1 parent 523d1e2 commit 2b1b063
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 213 deletions.
203 changes: 80 additions & 123 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ wasm_executor = []
anyhow = "1.0.43"
async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.35.0", features = ["dep_analysis"] }
deno_ast = { version = "0.36.0", features = ["dep_analysis", "emit"] }
deno_semver = "0.5.4"
deno_unsync = { version = "0.3.2", optional = true }
encoding_rs = "0.8.33"
Expand Down
51 changes: 0 additions & 51 deletions src/fast_check/swc_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use std::rc::Rc;

use deno_ast::swc::ast::*;
use deno_ast::swc::codegen::text_writer::JsWriter;
use deno_ast::swc::codegen::Node;
use deno_ast::swc::common::comments::SingleThreadedComments;
use deno_ast::swc::common::FileName;
use deno_ast::swc::common::SourceMap;
use deno_ast::swc::common::DUMMY_SP;
use deno_ast::swc_codegen_config;
use deno_ast::ModuleSpecifier;
use deno_ast::SourceTextInfo;

pub fn ident(name: String) -> Ident {
Ident {
Expand Down Expand Up @@ -170,44 +160,3 @@ pub fn maybe_lit_to_ts_type(lit: &Lit) -> Option<TsType> {
Lit::JSXText(_) => None,
}
}

pub fn emit(
specifier: &ModuleSpecifier,
comments: &SingleThreadedComments,
text_info: &SourceTextInfo,
module: &deno_ast::swc::ast::Module,
) -> Result<(String, Vec<u8>), anyhow::Error> {
let source_map = Rc::new(SourceMap::default());
let file_name = FileName::Url(specifier.clone());
source_map.new_source_file(file_name, text_info.text_str().to_string());

let mut src_map_buf = vec![];
let mut buf = vec![];
{
let mut writer = Box::new(JsWriter::new(
source_map.clone(),
"\n",
&mut buf,
Some(&mut src_map_buf),
));
writer.set_indent_str(" "); // two spaces

let mut emitter = deno_ast::swc::codegen::Emitter {
cfg: swc_codegen_config(),
comments: Some(comments),
cm: source_map.clone(),
wr: writer,
};
module.emit_with(&mut emitter)?;
}
let src = String::from_utf8(buf)?;
let map = {
let mut buf = Vec::new();
source_map
.build_source_map_from(&src_map_buf, None)
.to_writer(&mut buf)?;
buf
};

Ok((src, map))
}
40 changes: 23 additions & 17 deletions src/fast_check/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
use std::collections::HashSet;
use std::sync::Arc;

use deno_ast::emit;
use deno_ast::swc::ast::*;
use deno_ast::swc::common::comments::CommentKind;
use deno_ast::swc::common::comments::SingleThreadedComments;
use deno_ast::swc::common::comments::SingleThreadedCommentsMapInner;
use deno_ast::swc::common::Spanned;
use deno_ast::swc::common::DUMMY_SP;
use deno_ast::EmitOptions;
use deno_ast::EmittedSource;
use deno_ast::ModuleSpecifier;
use deno_ast::MultiThreadedComments;
use deno_ast::ParsedSource;
use deno_ast::SourceMap;
use deno_ast::SourceRange;
use deno_ast::SourceRangedForSpanned;

Expand All @@ -26,7 +30,6 @@ use crate::WorkspaceMember;

use super::range_finder::ModulePublicRanges;
use super::swc_helpers::any_type_ann;
use super::swc_helpers::emit;
use super::swc_helpers::get_return_stmts_with_arg_from_function_body;
use super::swc_helpers::ident;
use super::swc_helpers::is_void_type;
Expand Down Expand Up @@ -79,7 +82,8 @@ impl CommentsMut {

#[derive(Debug, Clone)]
pub struct FastCheckDtsModule {
pub text: String,
pub program: Program,
pub comments: SingleThreadedComments,

This comment has been minimized.

Copy link
@dsherret

dsherret Apr 11, 2024

Member

We can't store this in the graph because this isn't Send. We'll have to come up with some other solution. I'm going to revert part of this unfortunately (sorry, I mised this change in my initial review).

This comment has been minimized.

Copy link
@dsherret

dsherret Apr 11, 2024

Member

Actually, we can cheaply make this MultiThreadedComments (I think... or it will be good enough)

pub diagnostics: Vec<FastCheckDtsDiagnostic>,
}

Expand Down Expand Up @@ -135,11 +139,20 @@ pub fn transform(
};

// now emit
let (text, source_map) = emit(
specifier,
let source_map = SourceMap::single(
specifier.clone(),
parsed_source.text_info().text_str().to_owned(),
);
let program = Program::Module(module);
let EmittedSource { text, source_map } = emit(
&program,
&fast_check_comments,
parsed_source.text_info(),
&module,
&source_map,
&EmitOptions {
keep_comments: true,
source_map: deno_ast::SourceMapOption::Separate,
inline_sources: false,
},
)
.map_err(|e| {
vec![FastCheckDiagnostic::Emit {
Expand All @@ -152,18 +165,11 @@ pub fn transform(
let mut dts_transformer =
FastCheckDtsTransformer::new(parsed_source.text_info(), specifier);

let module = dts_transformer.transform(module)?;
let (text, _source_map) =
emit(specifier, &dts_comments, parsed_source.text_info(), &module)
.map_err(|e| {
vec![FastCheckDiagnostic::Emit {
specifier: specifier.clone(),
inner: Arc::new(e),
}]
})?;
let module = dts_transformer.transform(program.expect_module())?;

Some(FastCheckDtsModule {
text,
program: Program::Module(module),
comments: dts_comments,
diagnostics: dts_transformer.diagnostics,
})
} else {
Expand All @@ -174,7 +180,7 @@ pub fn transform(
module_info: module_info.into(),
text: text.into(),
dts,
source_map: source_map.into(),
source_map: source_map.unwrap().into_bytes().into(),
})
}

Expand Down
23 changes: 20 additions & 3 deletions src/fast_check/transform_dts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@ impl<'a> FastCheckDtsTransformer<'a> {

#[cfg(test)]
mod tests {
use crate::fast_check::swc_helpers::emit;
use crate::fast_check::transform_dts::FastCheckDtsTransformer;
use crate::source::MemoryLoader;
use crate::source::Source;
Expand All @@ -939,6 +938,10 @@ mod tests {
use crate::CapturingModuleAnalyzer;
use crate::GraphKind;
use crate::ModuleGraph;
use deno_ast::emit;
use deno_ast::EmitOptions;
use deno_ast::EmittedSource;
use deno_ast::SourceMap;
use url::Url;

async fn transform_dts_test(source: &str, expected: &str) {
Expand Down Expand Up @@ -985,8 +988,22 @@ mod tests {

let comments = parsed_source.comments().as_single_threaded();

let (actual, _) =
emit(&specifier, &comments, parsed_source.text_info(), &module).unwrap();
let source_map = SourceMap::single(
specifier,
parsed_source.text_info().text_str().to_owned(),
);

let EmittedSource { text: actual, .. } = emit(
&deno_ast::swc::ast::Program::Module(module),
&comments,
&source_map,
&EmitOptions {
keep_comments: true,
source_map: deno_ast::SourceMapOption::None,
inline_sources: false,
},
)
.unwrap();

assert_eq!(actual.trim(), expected.trim());
}
Expand Down
37 changes: 33 additions & 4 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4589,6 +4589,10 @@ mod tests {
use crate::packages::JsrPackageInfoVersion;
use crate::ParserModuleAnalyzer;
use deno_ast::dep::ImportAttribute;
use deno_ast::emit;
use deno_ast::EmitOptions;
use deno_ast::EmittedSource;
use deno_ast::SourceMap;
use pretty_assertions::assert_eq;
use serde_json::json;

Expand Down Expand Up @@ -5382,8 +5386,21 @@ mod tests {
unreachable!();
};
let dts = fsm.dts.unwrap();
let source_map =
SourceMap::single(module.specifier.clone(), module.source.to_string());
let EmittedSource { text, .. } = emit(
&dts.program,
&dts.comments,
&source_map,
&EmitOptions {
keep_comments: true,
source_map: deno_ast::SourceMapOption::None,
..Default::default()
},
)
.unwrap();
assert_eq!(
dts.text.to_string().trim(),
text.trim(),
"export declare function add(a: number, b: number): number;"
);
assert!(dts.diagnostics.is_empty());
Expand Down Expand Up @@ -5451,10 +5468,22 @@ mod tests {
unreachable!();
};
let dts = fsm.dts.unwrap();
assert_eq!(
dts.text.to_string().trim(),
"export * from 'jsr:@package/foo';"
let source_map = SourceMap::single(
module.specifier().clone(),
module.source().unwrap().to_string(),
);
let EmittedSource { text, .. } = emit(
&dts.program,
&dts.comments,
&source_map,
&EmitOptions {
keep_comments: true,
source_map: deno_ast::SourceMapOption::None,
..Default::default()
},
)
.unwrap();
assert_eq!(text.trim(), "export * from 'jsr:@package/foo';");
assert!(dts.diagnostics.is_empty());
}

Expand Down
23 changes: 21 additions & 2 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use std::path::PathBuf;
use std::rc::Rc;

use anyhow::anyhow;
use deno_ast::emit;
use deno_ast::EmitOptions;
use deno_ast::EmittedSource;
use deno_ast::ModuleSpecifier;
use deno_ast::SourceMap;
use deno_graph::source::CacheSetting;
use deno_graph::source::LoadFuture;
use deno_graph::source::LoadOptions;
Expand Down Expand Up @@ -112,9 +116,24 @@ async fn test_graph_specs() {
));

if let Some(dts) = &fast_check.dts {
if !dts.text.is_empty() {
let source_map = SourceMap::single(
module.specifier.clone(),
module.source.to_string(),
);
let EmittedSource { text, .. } = emit(
&dts.program,
&dts.comments,
&source_map,
&EmitOptions {
keep_comments: true,
source_map: deno_ast::SourceMapOption::None,
..Default::default()
},
)
.unwrap();
if !text.is_empty() {
output_text.push_str(&indent("--- DTS ---\n"));
output_text.push_str(&indent(&dts.text));
output_text.push_str(&indent(&text));
}
if !dts.diagnostics.is_empty() {
output_text.push_str(&indent("--- DTS Diagnostics ---\n"));
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/graph/jsr/FastCheck_ClassMethodsWithTsPrivate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
private "stringOnly"(value: string): string;
private "stringOnly"(): string;
}
export declare namespace Test {
export declare module Test {
export class AmbientClassInModule {
private constructor(value: string);
private constructor(private: Private);
Expand Down Expand Up @@ -187,7 +187,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
private "stringOnly"(value: string): string;
private "stringOnly"(): string;
}
export declare namespace Test {
export declare module Test {
export class AmbientClassInModule {
private constructor(value: string);
private constructor(private: Private);
Expand Down
8 changes: 4 additions & 4 deletions tests/specs/graph/jsr/FastCheck_InitIdentsAndMembers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
}
class Public {
}
namespace Test {
module Test {
export class A {
}
}
Expand All @@ -140,7 +140,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
}
declare class Public {
}
declare namespace Test {
declare module Test {
export class A {
}
}
Expand All @@ -155,15 +155,15 @@ Fast check https://jsr.io/@scope/b/1.0.0/mod.ts:
{}
class BaseHandler {
}
namespace Test {
module Test {
export class A {
}
}
export function test(a = BaseHandler, C = Test.A): void {}
--- DTS ---
declare class BaseHandler {
}
declare namespace Test {
declare module Test {
export class A {
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/graph/jsr/FastCheck_Issue22829.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/functions.ts:
func,
other: other_func
};
export declare namespace functions {
export declare module functions {
type Function<T> = Internal<T>;
}
--- DTS ---
Expand All @@ -123,7 +123,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/functions.ts:
export declare const functions: {
readonly other;
};
export declare namespace functions {
export declare module functions {
type Function<T> = Internal<T>;
}
--- DTS Diagnostics ---
Expand Down
Loading

0 comments on commit 2b1b063

Please sign in to comment.