Skip to content

Commit

Permalink
refactor: pass EsModuleInfo instead of ParsedSource around (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
crowlKats authored Jan 28, 2025
1 parent 8f80891 commit 90c8f00
Show file tree
Hide file tree
Showing 12 changed files with 388 additions and 420 deletions.
65 changes: 30 additions & 35 deletions src/class.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use deno_ast::ParsedSource;
use deno_ast::SourceRangedForSpanned;
use deno_graph::symbols::EsModuleInfo;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -235,7 +235,7 @@ pub struct ClassDef {
}

pub fn class_to_class_def(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
class: &deno_ast::swc::ast::Class,
def_name: Option<Box<str>>,
) -> (ClassDef, JsDoc) {
Expand Down Expand Up @@ -273,18 +273,17 @@ pub fn class_to_class_def(
let implements = class
.implements
.iter()
.map(|expr| TsTypeDef::ts_expr_with_type_args(parsed_source, expr))
.map(|expr| TsTypeDef::ts_expr_with_type_args(module_info, expr))
.collect::<Box<[_]>>();

for member in &class.body {
use deno_ast::swc::ast::ClassMember::*;

match member {
Constructor(ctor) => {
if let Some(ctor_js_doc) =
js_doc_for_range(parsed_source, &ctor.range())
if let Some(ctor_js_doc) = js_doc_for_range(module_info, &ctor.range())
{
let constructor_name = prop_name_to_string(parsed_source, &ctor.key);
let constructor_name = prop_name_to_string(module_info, &ctor.key);

let mut params = vec![];

Expand All @@ -295,18 +294,18 @@ pub fn class_to_class_def(
Param(param) => ClassConstructorParamDef {
accessibility: None,
is_override: false,
param: param_to_param_def(parsed_source, param),
param: param_to_param_def(module_info, param),
readonly: false,
},
TsParamProp(ts_param_prop) => {
use deno_ast::swc::ast::TsParamPropParam;

let param = match &ts_param_prop.param {
TsParamPropParam::Ident(ident) => {
ident_to_param_def(parsed_source, ident)
ident_to_param_def(module_info, ident)
}
TsParamPropParam::Assign(assign_pat) => {
assign_pat_to_param_def(parsed_source, assign_pat)
assign_pat_to_param_def(module_info, assign_pat)
}
};

Expand All @@ -328,22 +327,18 @@ pub fn class_to_class_def(
has_body: ctor.body.is_some(),
name: constructor_name,
params,
location: get_location(parsed_source, ctor.start()),
location: get_location(module_info, ctor.start()),
};
constructors.push(constructor_def);
}
}
Method(class_method) => {
if let Some(method_js_doc) =
js_doc_for_range(parsed_source, &class_method.range())
js_doc_for_range(module_info, &class_method.range())
{
let method_name =
prop_name_to_string(parsed_source, &class_method.key);
let fn_def = function_to_function_def(
parsed_source,
&class_method.function,
None,
);
let method_name = prop_name_to_string(module_info, &class_method.key);
let fn_def =
function_to_function_def(module_info, &class_method.function, None);
let method_def = ClassMethodDef {
js_doc: method_js_doc,
accessibility: class_method.accessibility,
Expand All @@ -354,30 +349,30 @@ pub fn class_to_class_def(
name: method_name.into_boxed_str(),
kind: class_method.kind,
function_def: fn_def,
location: get_location(parsed_source, class_method.start()),
location: get_location(module_info, class_method.start()),
};
methods.push(method_def);
}
}
ClassProp(class_prop) => {
if let Some(prop_js_doc) =
js_doc_for_range(parsed_source, &class_prop.range())
js_doc_for_range(module_info, &class_prop.range())
{
let ts_type = if let Some(type_ann) = &class_prop.type_ann {
// if the property has a type annotation, use it
Some(TsTypeDef::new(parsed_source, &type_ann.type_ann))
Some(TsTypeDef::new(module_info, &type_ann.type_ann))
} else if let Some(value) = &class_prop.value {
// else, if it has an initializer, try to infer the type
infer_ts_type_from_expr(parsed_source, value, false)
infer_ts_type_from_expr(module_info, value, false)
} else {
// else, none
None
};

let prop_name = prop_name_to_string(parsed_source, &class_prop.key);
let prop_name = prop_name_to_string(module_info, &class_prop.key);

let decorators =
decorators_to_defs(parsed_source, &class_prop.decorators);
decorators_to_defs(module_info, &class_prop.decorators);

let prop_def = ClassPropertyDef {
js_doc: prop_js_doc,
Expand All @@ -390,28 +385,28 @@ pub fn class_to_class_def(
accessibility: class_prop.accessibility,
name: prop_name.into_boxed_str(),
decorators,
location: get_location(parsed_source, class_prop.start()),
location: get_location(module_info, class_prop.start()),
};
properties.push(prop_def);
}
}
TsIndexSignature(ts_index_sig) => {
if let Some(js_doc) =
js_doc_for_range(parsed_source, &ts_index_sig.range())
js_doc_for_range(module_info, &ts_index_sig.range())
{
let mut params = vec![];
for param in &ts_index_sig.params {
let param_def = ts_fn_param_to_param_def(parsed_source, param);
let param_def = ts_fn_param_to_param_def(module_info, param);
params.push(param_def);
}

let ts_type = ts_index_sig
.type_ann
.as_ref()
.map(|rt| TsTypeDef::new(parsed_source, &rt.type_ann));
.map(|rt| TsTypeDef::new(module_info, &rt.type_ann));

let index_sig_def = IndexSignatureDef {
location: get_location(parsed_source, ts_index_sig.start()),
location: get_location(module_info, ts_index_sig.start()),
js_doc,
readonly: ts_index_sig.readonly,
params,
Expand All @@ -428,21 +423,21 @@ pub fn class_to_class_def(
}

let type_params = maybe_type_param_decl_to_type_param_defs(
parsed_source,
module_info,
class.type_params.as_deref(),
);

let super_type_params = maybe_type_param_instantiation_to_type_defs(
parsed_source,
module_info,
class.super_type_params.as_deref(),
);

let decorators = decorators_to_defs(parsed_source, &class.decorators);
let decorators = decorators_to_defs(module_info, &class.decorators);

// JSDoc associated with the class may actually be a leading comment on a
// decorator, and so we should parse out the JSDoc for the first decorator
let js_doc = if !class.decorators.is_empty() {
js_doc_for_range(parsed_source, &class.decorators[0].range()).unwrap()
js_doc_for_range(module_info, &class.decorators[0].range()).unwrap()
} else {
JsDoc::default()
};
Expand All @@ -466,12 +461,12 @@ pub fn class_to_class_def(
}

pub fn get_doc_for_class_decl(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
class_decl: &deno_ast::swc::ast::ClassDecl,
) -> (String, ClassDef, JsDoc) {
let class_name = class_decl.ident.sym.to_string();
let (class_def, js_doc) =
class_to_class_def(parsed_source, &class_decl.class, None);
class_to_class_def(module_info, &class_decl.class, None);

(class_name, class_def, js_doc)
}
21 changes: 12 additions & 9 deletions src/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::util::swc::get_location;

use deno_ast::swc::ast::Decorator;
use deno_ast::swc::ast::Expr;
use deno_ast::ParsedSource;
use deno_ast::SourceRangedForSpanned;
use deno_graph::symbols::EsModuleInfo;
use deno_terminal::colors;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -41,7 +41,7 @@ impl Display for DecoratorDef {

impl DecoratorDef {
fn from_ast_decorator(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
decorator: &Decorator,
) -> Self {
match decorator.expr.as_ref() {
Expand All @@ -51,41 +51,44 @@ impl DecoratorDef {
let args = call_expr
.args
.iter()
.map(|a| a.text_fast(parsed_source.text_info_lazy()).to_string())
.map(|a| {
a.text_fast(module_info.source().text_info_lazy())
.to_string()
})
.collect();
return Self {
name: ident.sym.to_string(),
args,
location: get_location(parsed_source, ident.start()),
location: get_location(module_info, ident.start()),
};
}
}
Self {
name: "[UNSUPPORTED]".to_string(),
args: vec![],
location: get_location(parsed_source, call_expr.start()),
location: get_location(module_info, call_expr.start()),
}
}
Expr::Ident(ident) => Self {
name: ident.sym.to_string(),
args: vec![],
location: get_location(parsed_source, ident.start()),
location: get_location(module_info, ident.start()),
},
_ => Self {
name: "[UNSUPPORTED]".to_string(),
args: vec![],
location: get_location(parsed_source, decorator.start()),
location: get_location(module_info, decorator.start()),
},
}
}
}

pub fn decorators_to_defs(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
decorators: &[Decorator],
) -> Box<[DecoratorDef]> {
decorators
.iter()
.map(|d| DecoratorDef::from_ast_decorator(parsed_source, d))
.map(|d| DecoratorDef::from_ast_decorator(module_info, d))
.collect()
}
11 changes: 5 additions & 6 deletions src/enum.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use deno_ast::ParsedSource;
use deno_ast::SourceRangedForSpanned;
use deno_graph::symbols::EsModuleInfo;
use serde::Deserialize;
use serde::Serialize;

Expand Down Expand Up @@ -30,7 +30,7 @@ pub struct EnumDef {
}

pub fn get_doc_for_ts_enum_decl(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
enum_decl: &deno_ast::swc::ast::TsEnumDecl,
) -> (String, EnumDef) {
let enum_name = enum_decl.id.sym.to_string();
Expand All @@ -39,14 +39,13 @@ pub fn get_doc_for_ts_enum_decl(
for enum_member in &enum_decl.members {
use deno_ast::swc::ast::TsEnumMemberId::*;

if let Some(js_doc) = js_doc_for_range(parsed_source, &enum_member.range())
{
if let Some(js_doc) = js_doc_for_range(module_info, &enum_member.range()) {
let name = match &enum_member.id {
Ident(ident) => ident.sym.to_string(),
Str(str_) => str_.value.to_string(),
};
let init = if let Some(expr) = &enum_member.init {
infer_ts_type_from_expr(parsed_source, expr, true)
infer_ts_type_from_expr(module_info, expr, true)
} else {
None
};
Expand All @@ -55,7 +54,7 @@ pub fn get_doc_for_ts_enum_decl(
name,
init,
js_doc,
location: get_location(parsed_source, enum_member.start()),
location: get_location(module_info, enum_member.start()),
};
members.push(member_def);
}
Expand Down
16 changes: 8 additions & 8 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::util::swc::is_false;
use crate::ParamDef;
use deno_ast::swc::ast::ReturnStmt;
use deno_ast::swc::ast::Stmt;
use deno_ast::ParsedSource;
use deno_graph::symbols::EsModuleInfo;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -32,20 +32,20 @@ pub struct FunctionDef {
}

pub fn function_to_function_def(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
function: &deno_ast::swc::ast::Function,
def_name: Option<String>,
) -> FunctionDef {
let params = function
.params
.iter()
.map(|param| param_to_param_def(parsed_source, param))
.map(|param| param_to_param_def(module_info, param))
.collect();

let maybe_return_type = match function
.return_type
.as_deref()
.map(|return_type| TsTypeDef::new(parsed_source, &return_type.type_ann))
.map(|return_type| TsTypeDef::new(module_info, &return_type.type_ann))
{
Some(return_type) => Some(return_type),
None
Expand All @@ -71,13 +71,13 @@ pub fn function_to_function_def(
};

let type_params = maybe_type_param_decl_to_type_param_defs(
parsed_source,
module_info,
function.type_params.as_deref(),
);

let has_body = function.body.is_some();

let decorators = decorators_to_defs(parsed_source, &function.decorators);
let decorators = decorators_to_defs(module_info, &function.decorators);

FunctionDef {
def_name,
Expand All @@ -92,11 +92,11 @@ pub fn function_to_function_def(
}

pub fn get_doc_for_fn_decl(
parsed_source: &ParsedSource,
module_info: &EsModuleInfo,
fn_decl: &deno_ast::swc::ast::FnDecl,
) -> (String, FunctionDef) {
let name = fn_decl.ident.sym.to_string();
let fn_def = function_to_function_def(parsed_source, &fn_decl.function, None);
let fn_def = function_to_function_def(module_info, &fn_decl.function, None);
(name, fn_def)
}

Expand Down
Loading

0 comments on commit 90c8f00

Please sign in to comment.