Skip to content

Commit

Permalink
perf: deno_ast 0.41 (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jul 31, 2024
1 parent d3e42b3 commit 7922922
Show file tree
Hide file tree
Showing 11 changed files with 576 additions and 350 deletions.
744 changes: 425 additions & 319 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 @@ -41,7 +41,7 @@ harness = false
anyhow = "1.0.43"
async-trait = "0.1.68"
data-url = "0.3.0"
deno_ast = { version = "0.40.0", features = ["dep_analysis", "emit"] }
deno_ast = { version = "0.41.2", features = ["dep_analysis", "emit"] }
deno_unsync.workspace = true
deno_semver = "0.5.4"
encoding_rs = "0.8.33"
Expand Down
4 changes: 2 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.78.0"
components = [ "clippy", "rustfmt" ]
channel = "1.80.0"
components = [ "clippy", "rustfmt" ]
23 changes: 15 additions & 8 deletions src/fast_check/swc_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
use std::ops::ControlFlow;

use deno_ast::swc::ast::*;
use deno_ast::swc::atoms::Atom;
use deno_ast::swc::common::SyntaxContext;
use deno_ast::swc::common::DUMMY_SP;

pub fn ident(name: String) -> Ident {
pub fn new_ident(name: Atom) -> Ident {
Ident {
span: DUMMY_SP,
sym: name.clone().into(),
ctxt: Default::default(),
sym: name,
optional: false,
}
}
Expand All @@ -29,7 +32,7 @@ pub enum ReturnStatementAnalysis {
Void,
/// There is only a single return statement in the function body, and it has
/// an argument.
Single(ReturnStmt),
Single,
/// There are multiple return statements in the function body, and at least
/// one of them has an argument.
Multiple,
Expand Down Expand Up @@ -72,9 +75,9 @@ fn analyze_return_stmts_from_stmt(
(None, ReturnStatementAnalysis::Void) => {}
(Some(_), ReturnStatementAnalysis::None)
| (Some(_), ReturnStatementAnalysis::Void) => {
*analysis = ReturnStatementAnalysis::Single(n.clone());
*analysis = ReturnStatementAnalysis::Single;
}
(_, ReturnStatementAnalysis::Single(_)) => {
(_, ReturnStatementAnalysis::Single { .. }) => {
*analysis = ReturnStatementAnalysis::Multiple;
return ControlFlow::Break(());
}
Expand Down Expand Up @@ -145,10 +148,14 @@ pub fn type_ann(ts_type: TsType) -> Box<TsTypeAnn> {
})
}

pub fn type_ref(name: String) -> TsTypeRef {
pub fn type_ref(name: Atom) -> TsTypeRef {
TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(Ident::new(name.into(), DUMMY_SP)),
type_name: TsEntityName::Ident(Ident::new(
name,
DUMMY_SP,
SyntaxContext::default(),
)),
type_params: None,
}
}
Expand All @@ -161,7 +168,7 @@ pub fn ts_lit_type(lit: TsLit) -> TsType {
}

pub fn regex_type() -> TsType {
TsType::TsTypeRef(type_ref("RegExp".to_string()))
TsType::TsTypeRef(type_ref("RegExp".into()))
}

pub fn ts_tuple_element(ts_type: TsType) -> TsTupleElement {
Expand Down
47 changes: 35 additions & 12 deletions src/fast_check/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ use crate::WorkspaceMember;
use super::range_finder::ModulePublicRanges;
use super::swc_helpers::analyze_return_stmts_in_function_body;
use super::swc_helpers::any_type_ann;
use super::swc_helpers::ident;
use super::swc_helpers::is_void_type;
use super::swc_helpers::maybe_lit_to_ts_type;
use super::swc_helpers::new_ident;
use super::swc_helpers::ts_keyword_type;
use super::swc_helpers::ReturnStatementAnalysis;
use super::transform_dts::FastCheckDtsDiagnostic;
Expand Down Expand Up @@ -153,6 +153,7 @@ pub fn transform(
&EmitOptions {
remove_comments: false,
source_map: deno_ast::SourceMapOption::Separate,
source_map_base: None,
source_map_file: None,
inline_sources: false,
},
Expand Down Expand Up @@ -303,14 +304,19 @@ impl<'a> FastCheckTransformer<'a> {
span: DUMMY_SP,
declare: false,
global: false,
id: TsModuleName::Ident(Ident::new(swc_id.0, DUMMY_SP)),
id: TsModuleName::Ident(Ident::new(
swc_id.0,
DUMMY_SP,
SyntaxContext::default(),
)),
body: Some(TsNamespaceBody::TsModuleBlock(TsModuleBlock {
span: DUMMY_SP,
body: vec![ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(
ExportDecl {
span: DUMMY_SP,
decl: Decl::Var(Box::new(VarDecl {
span: DUMMY_SP,
ctxt: SyntaxContext::empty(),
kind: VarDeclKind::Var,
declare: false,
decls: var_decls,
Expand Down Expand Up @@ -732,8 +738,9 @@ impl<'a> FastCheckTransformer<'a> {
span: DUMMY_SP,
key: PrivateName {
span: DUMMY_SP,
id: ident("private".to_string()),
name: "private".into(),
},
ctxt: SyntaxContext::default(),
value: None,
type_ann: Some(Box::new(TsTypeAnn {
span: DUMMY_SP,
Expand Down Expand Up @@ -800,11 +807,17 @@ impl<'a> FastCheckTransformer<'a> {
insert_members.push(ClassMember::ClassProp(ClassProp {
span: DUMMY_SP,
key: match &prop.param {
TsParamPropParam::Ident(ident) => {
PropName::Ident(ident.id.clone())
TsParamPropParam::Ident(binding_ident) => {
PropName::Ident(IdentName {
span: binding_ident.span,
sym: binding_ident.sym.clone(),
})
}
TsParamPropParam::Assign(assign) => match &*assign.left {
Pat::Ident(ident) => PropName::Ident(ident.id.clone()),
Pat::Ident(binding_ident) => PropName::Ident(IdentName {
span: binding_ident.span,
sym: binding_ident.sym.clone(),
}),
Pat::Array(_)
| Pat::Rest(_)
| Pat::Object(_)
Expand Down Expand Up @@ -892,6 +905,7 @@ impl<'a> FastCheckTransformer<'a> {
arg: Box::new(Pat::Ident(BindingIdent {
id: Ident {
span: DUMMY_SP,
ctxt: SyntaxContext::default(),
sym: format!("param{}", i).into(),
optional: false,
},
Expand All @@ -906,6 +920,7 @@ impl<'a> FastCheckTransformer<'a> {
pat: Pat::Ident(BindingIdent {
id: Ident {
span: DUMMY_SP,
ctxt: SyntaxContext::default(),
sym: format!("param{}", i).into(),
optional: true,
},
Expand Down Expand Up @@ -1107,6 +1122,7 @@ impl<'a> FastCheckTransformer<'a> {
arg: Box::new(Pat::Ident(BindingIdent {
id: Ident {
span: DUMMY_SP,
ctxt: SyntaxContext::default(),
sym: format!("param{}", i).into(),
optional: false,
},
Expand All @@ -1121,6 +1137,7 @@ impl<'a> FastCheckTransformer<'a> {
pat: Pat::Ident(BindingIdent {
id: Ident {
span: DUMMY_SP,
ctxt: SyntaxContext::default(),
sym: format!("param{}", i).into(),
optional: true,
},
Expand Down Expand Up @@ -1258,6 +1275,7 @@ impl<'a> FastCheckTransformer<'a> {
.unwrap_or_else(|| {
BlockStmtOrExpr::BlockStmt(BlockStmt {
span: DUMMY_SP,
ctxt: SyntaxContext::default(),
stmts: vec![],
})
});
Expand Down Expand Up @@ -1303,7 +1321,7 @@ impl<'a> FastCheckTransformer<'a> {
},
)?;
}
(ReturnStatementAnalysis::Single(_), _) => {
(ReturnStatementAnalysis::Single { .. }, _) => {
// TODO: infer return type based on return type
self.mark_diagnostic(
FastCheckDiagnostic::MissingExplicitReturnType {
Expand Down Expand Up @@ -1367,7 +1385,7 @@ impl<'a> FastCheckTransformer<'a> {
if !is_expr_leavable {
self.mark_diagnostic(
FastCheckDiagnostic::MissingExplicitType {
range: self.source_range_to_range(ident.range()),
range: self.source_range_to_range(ident.id.range()),
},
)?;
}
Expand Down Expand Up @@ -1503,7 +1521,7 @@ impl<'a> FastCheckTransformer<'a> {
if !is_init_leavable {
self.mark_diagnostic(
FastCheckDiagnostic::MissingExplicitType {
range: self.source_range_to_range(ident.range()),
range: self.source_range_to_range(ident.id.range()),
},
)?;
}
Expand Down Expand Up @@ -1638,6 +1656,7 @@ impl<'a> FastCheckTransformer<'a> {
id: Ident::new(
expando_prop.prop_name().clone(),
expando_prop.prop_name_range().into(),
SyntaxContext::default(),
),
type_ann: None,
}),
Expand Down Expand Up @@ -1803,7 +1822,7 @@ impl<'a> FastCheckTransformer<'a> {
op: TsTypeOperatorOp::Unique,
type_ann: Box::new(TsType::TsTypeRef(TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(ident("symbol".to_string())),
type_name: TsEntityName::Ident(new_ident("symbol".into())),
type_params: None,
})),
}))
Expand Down Expand Up @@ -1896,7 +1915,7 @@ impl<'a> FastCheckTransformer<'a> {
}
_ => Box::new(TsType::TsTypeRef(TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(ident("Promise".into())),
type_name: TsEntityName::Ident(new_ident("Promise".into())),
type_params: Some(Box::new(TsTypeParamInstantiation {
span: DUMMY_SP,
params: vec![ty],
Expand Down Expand Up @@ -1962,7 +1981,11 @@ fn void_or_promise_void(is_async: bool) -> Box<TsType> {
if is_async {
Box::new(TsType::TsTypeRef(TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(Ident::new("Promise".into(), DUMMY_SP)),
type_name: TsEntityName::Ident(Ident::new(
"Promise".into(),
DUMMY_SP,
SyntaxContext::default(),
)),
type_params: Some(Box::new(TsTypeParamInstantiation {
span: DUMMY_SP,
params: vec![void_type],
Expand Down
22 changes: 17 additions & 5 deletions src/fast_check/transform_dts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use deno_ast::swc::ast::*;
use deno_ast::swc::common::SyntaxContext;
use deno_ast::swc::common::DUMMY_SP;
use deno_ast::ModuleSpecifier;
use deno_ast::SourceRange;
Expand Down Expand Up @@ -207,7 +208,8 @@ impl<'a> FastCheckDtsTransformer<'a> {
}
ModuleDecl::ExportDefaultExpr(export_default_expr) => {
let name = self.gen_unique_name();
let name_ident = Ident::new(name.into(), DUMMY_SP);
let name_ident =
Ident::new(name.into(), DUMMY_SP, SyntaxContext::default());
let type_ann = self
.expr_to_ts_type(*export_default_expr.expr.clone(), false, true)
.map(type_ann);
Expand All @@ -216,6 +218,7 @@ impl<'a> FastCheckDtsTransformer<'a> {
new_items.push(ModuleItem::Stmt(Stmt::Decl(Decl::Var(
Box::new(VarDecl {
span: DUMMY_SP,
ctxt: Default::default(),
kind: VarDeclKind::Const,
declare: true,
decls: vec![VarDeclarator {
Expand Down Expand Up @@ -343,7 +346,15 @@ impl<'a> FastCheckDtsTransformer<'a> {
match prop {
Prop::KeyValue(key_value) => {
let (key, computed) = match key_value.key {
PropName::Ident(ident) => (Expr::Ident(ident), false),
PropName::Ident(ident_name) => (
Expr::Ident(Ident {
span: ident_name.span,
ctxt: SyntaxContext::default(),
sym: ident_name.sym,
optional: false,
}),
false,
),
PropName::Str(str_prop) => {
(Expr::Lit(Lit::Str(str_prop)), false)
}
Expand Down Expand Up @@ -867,13 +878,13 @@ impl<'a> FastCheckDtsTransformer<'a> {
.expr_to_ts_type(*assign_pat.right, false, false)
.map(|param| {
let name = if let Pat::Ident(ident) = *assign_pat.left {
ident.id.sym.as_str().to_string()
ident.id.sym.clone()
} else {
self.gen_unique_name()
self.gen_unique_name().into()
};

TsFnParam::Ident(BindingIdent {
id: Ident::new(name.into(), assign_pat.span),
id: Ident::new(name, assign_pat.span, Default::default()),
type_ann: Some(type_ann(param)),
})
}),
Expand Down Expand Up @@ -986,6 +997,7 @@ mod tests {
&EmitOptions {
remove_comments: false,
source_map: deno_ast::SourceMapOption::None,
source_map_base: None,
source_map_file: None,
inline_sources: false,
},
Expand Down
2 changes: 1 addition & 1 deletion src/symbols/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ impl<'a> SymbolNodeRef<'a> {

fn maybe_key_name(key: &Key) -> Option<Cow<str>> {
match key {
Key::Private(n) => Some(Cow::Owned(format!("#{}", n.id.sym))),
Key::Private(n) => Some(Cow::Owned(format!("#{}", n.name))),
Key::Public(n) => maybe_prop_name(n),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/symbols/dep_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ fn member_expr_into_id_and_parts(
fn member_prop_to_str(member_prop: &MemberProp) -> Option<String> {
match member_prop {
MemberProp::Ident(ident) => Some(ident.sym.to_string()),
MemberProp::PrivateName(n) => Some(format!("#{}", n.id.sym)),
MemberProp::PrivateName(n) => Some(format!("#{}", n.name)),
MemberProp::Computed(n) => match &*n.expr {
Expr::Lit(Lit::Str(str)) => Some(str.value.to_string()),
_ => None,
Expand Down
4 changes: 4 additions & 0 deletions tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ impl Loader for TestLoader {
#[cfg(feature = "symbols")]
pub mod symbols {
pub struct SymbolsResult {
#[allow(unused)]
pub output: String,
}
}

pub struct BuildResult {
#[allow(unused)]
pub locker: Option<HashMapLocker>,
pub graph: ModuleGraph,
#[allow(unused)]
pub diagnostics: Vec<BuildDiagnostic>,
pub analyzer: deno_graph::CapturingModuleAnalyzer,
#[allow(unused)]
pub fast_check_cache: Option<TestFastCheckCache>,
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/ecosystem/iz7n/std/0_1_0.test
Original file line number Diff line number Diff line change
Expand Up @@ -3704,7 +3704,7 @@ error[missing-explicit-type]: missing explicit type in the public API
--> /src/math/constants.ts:6:14
|
6 | export const Φ = (1 + Math.sqrt(5)) / 2;
| ^^ this symbol is missing an explicit type
| ^ this symbol is missing an explicit type
|
= hint: add an explicit type annotation to the symbol

Expand Down
Loading

0 comments on commit 7922922

Please sign in to comment.