Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Feb 12, 2024
1 parent 5b0a85d commit af5aae6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 70 deletions.
10 changes: 0 additions & 10 deletions src/fast_check/swc_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,16 +291,6 @@ pub fn expr_as_keyword_expr(
}))
}

pub fn replacement_return_value(ty: &TsType) -> Option<Box<Expr>> {
if is_void_type(ty) {
None
} else if is_never_type(ty) {
Some(obj_as_never_expr())
} else {
Some(obj_as_any_expr())
}
}

pub fn regex_type() -> TsType {
TsType::TsTypeRef(type_ref("RegExp".to_string()))
}
Expand Down
130 changes: 70 additions & 60 deletions src/fast_check/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ use super::swc_helpers::is_never_type;
use super::swc_helpers::is_void_type;
use super::swc_helpers::maybe_lit_to_ts_type;
use super::swc_helpers::obj_as_any_expr;
use super::swc_helpers::obj_as_never_expr;
use super::swc_helpers::paren_expr;
use super::swc_helpers::regex_type;
use super::swc_helpers::replacement_return_value;
use super::swc_helpers::ts_keyword_type;
use super::swc_helpers::unknown_type_ann;
use super::FastCheckDiagnostic;
Expand Down Expand Up @@ -1524,6 +1524,75 @@ impl<'a> FastCheckTransformer<'a> {
}
}

fn void_or_promise_void(is_async: bool) -> Box<TsType> {
let void_type = Box::new(ts_keyword_type(TsKeywordTypeKind::TsVoidKeyword));
if is_async {
Box::new(TsType::TsTypeRef(TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(Ident::new("Promise".into(), DUMMY_SP)),
type_params: Some(Box::new(TsTypeParamInstantiation {
span: DUMMY_SP,
params: vec![void_type],
})),
}))
} else {
void_type
}
}

fn replacement_return_value(ty: &TsType) -> Option<Box<Expr>> {
if is_void_type(ty) {
None
} else if is_never_type(ty) {
Some(obj_as_never_expr())
} else {
Some(obj_as_any_expr())
}
}

fn prefix_ident(ident: &mut Ident, prefix: &str) {
ident.sym = format!("{}{}", prefix, ident.sym).into();
}

fn prefix_idents_in_pat(pat: &mut Pat, prefix: &str) {
match pat {
Pat::Ident(ident) => {
prefix_ident(&mut ident.id, prefix);
}
Pat::Array(array) => {
for pat in array.elems.iter_mut().flatten() {
prefix_idents_in_pat(pat, prefix);
}
}
Pat::Rest(rest) => {
prefix_idents_in_pat(&mut rest.arg, prefix);
}
Pat::Object(o) => {
for prop in &mut o.props {
match prop {
ObjectPatProp::KeyValue(n) => match &mut n.key {
PropName::Ident(ident) => {
prefix_ident(ident, prefix);
}
PropName::Str(str) => {
str.value = format!("{}{}", prefix, str.value).into();
}
PropName::Num(_) | PropName::Computed(_) | PropName::BigInt(_) => {
// ignore
}
},
ObjectPatProp::Assign(n) => prefix_ident(&mut n.key, prefix),
ObjectPatProp::Rest(n) => prefix_idents_in_pat(&mut n.arg, prefix),
}
}
}
Pat::Assign(a) => prefix_idents_in_pat(&mut a.left, prefix),
Pat::Expr(_) | Pat::Invalid(_) => {
// ignore
}
}
}

pub fn emit(
specifier: &ModuleSpecifier,
comments: &SingleThreadedComments,
Expand Down Expand Up @@ -1700,62 +1769,3 @@ fn is_expr_ident_or_member_idents(expr: &Expr) -> bool {
_ => false,
}
}

fn void_or_promise_void(is_async: bool) -> Box<TsType> {
let void_type = Box::new(ts_keyword_type(TsKeywordTypeKind::TsVoidKeyword));
if is_async {
Box::new(TsType::TsTypeRef(TsTypeRef {
span: DUMMY_SP,
type_name: TsEntityName::Ident(Ident::new("Promise".into(), DUMMY_SP)),
type_params: Some(Box::new(TsTypeParamInstantiation {
span: DUMMY_SP,
params: vec![void_type],
})),
}))
} else {
void_type
}
}

fn prefix_ident(ident: &mut Ident, prefix: &str) {
ident.sym = format!("{}{}", prefix, ident.sym).into();
}

fn prefix_idents_in_pat(pat: &mut Pat, prefix: &str) {
match pat {
Pat::Ident(ident) => {
prefix_ident(&mut ident.id, prefix);
}
Pat::Array(array) => {
for pat in array.elems.iter_mut().flatten() {
prefix_idents_in_pat(pat, prefix);
}
}
Pat::Rest(rest) => {
prefix_idents_in_pat(&mut rest.arg, prefix);
}
Pat::Object(o) => {
for prop in &mut o.props {
match prop {
ObjectPatProp::KeyValue(n) => match &mut n.key {
PropName::Ident(ident) => {
prefix_ident(ident, prefix);
}
PropName::Str(str) => {
str.value = format!("{}{}", prefix, str.value).into();
}
PropName::Num(_) | PropName::Computed(_) | PropName::BigInt(_) => {
// ignore
}
},
ObjectPatProp::Assign(n) => prefix_ident(&mut n.key, prefix),
ObjectPatProp::Rest(n) => prefix_idents_in_pat(&mut n.arg, prefix),
}
}
}
Pat::Assign(a) => prefix_idents_in_pat(&mut a.left, prefix),
Pat::Expr(_) | Pat::Invalid(_) => {
// ignore
}
}
}

0 comments on commit af5aae6

Please sign in to comment.