Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 38 additions & 71 deletions crates/biome_js_type_info/src/local_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::str::FromStr;

use biome_js_syntax::{
AnyJsArrayBindingPatternElement, AnyJsArrayElement, AnyJsArrowFunctionParameters,
AnyJsArrayBindingPatternElement, AnyJsArrayElement, AnyJsArrowFunctionParameters, AnyJsBinding,
AnyJsBindingPattern, AnyJsCallArgument, AnyJsClassMember, AnyJsDeclaration,
AnyJsDeclarationClause, AnyJsExportDefaultDeclaration, AnyJsExpression, AnyJsFormalParameter,
AnyJsFunction, AnyJsFunctionBody, AnyJsLiteralExpression, AnyJsName,
Expand Down Expand Up @@ -1479,26 +1479,7 @@ impl FunctionParameter {
is_rest: false,
}),
AnyJsParameter::JsRestParameter(param) => {
let ty = param
.type_annotation()
.and_then(|annotation| annotation.ty().ok())
.map(|ty| TypeData::from_any_ts_type(resolver, scope_id, &ty))
.unwrap_or_default();
let bindings = param
.binding()
.ok()
.and_then(|binding| {
FunctionParameterBinding::bindings_from_any_js_binding_pattern_of_type(
resolver, scope_id, &binding, &ty,
)
})
.unwrap_or_default();
Self::Pattern(PatternFunctionParameter {
ty: resolver.reference_to_owned_data(ty),
bindings,
is_optional: false,
is_rest: true,
})
Self::from_js_rest_parameter(resolver, scope_id, param)
}
AnyJsParameter::TsThisParameter(param) => Self::Named(NamedFunctionParameter {
name: Text::new_static("this"),
Expand All @@ -1518,73 +1499,59 @@ impl FunctionParameter {
scope_id: ScopeId,
param: &JsFormalParameter,
) -> Self {
let name = param
.binding()
.ok()
.as_ref()
.and_then(|binding| binding.as_any_js_binding())
.and_then(|binding| binding.as_js_identifier_binding())
.and_then(|identifier| identifier.name_token().ok())
.map(|token| token.token_text_trimmed().into());
let ty = param
.type_annotation()
.and_then(|annotation| annotation.ty().ok())
.map(|ty| TypeData::from_any_ts_type(resolver, scope_id, &ty))
.unwrap_or_default();
if let Some(name) = name {
Self::Named(NamedFunctionParameter {
name,
ty: resolver.reference_to_owned_data(ty),
is_optional: param.question_mark_token().is_some(),
is_rest: false,
})
} else {
let bindings = param
.binding()
.ok()
.and_then(|binding| {
FunctionParameterBinding::bindings_from_any_js_binding_pattern_of_type(
resolver, scope_id, &binding, &ty,
)
})
.unwrap_or_default();
Self::Pattern(PatternFunctionParameter {
bindings,
ty: resolver.reference_to_owned_data(ty),
is_optional: param.question_mark_token().is_some(),
is_rest: false,
})
}
Self::from_binding_with_annotation(
resolver,
scope_id,
param.binding(),
param.type_annotation(),
param.question_mark_token().is_some(),
false,
)
}

pub fn from_js_rest_parameter(
resolver: &mut dyn TypeResolver,
scope_id: ScopeId,
param: &JsRestParameter,
) -> Self {
let name = param
.binding()
.ok()
Self::from_binding_with_annotation(
resolver,
scope_id,
param.binding(),
param.type_annotation(),
false,
true,
)
}

fn from_binding_with_annotation(
resolver: &mut dyn TypeResolver,
scope_id: ScopeId,
binding: SyntaxResult<AnyJsBindingPattern>,
annotation: Option<TsTypeAnnotation>,
is_optional: bool,
is_rest: bool,
) -> Self {
let name = binding
.as_ref()
.and_then(|binding| binding.as_any_js_binding())
.and_then(|binding| binding.as_js_identifier_binding())
.ok()
.and_then(AnyJsBindingPattern::as_any_js_binding)
.and_then(AnyJsBinding::as_js_identifier_binding)
.and_then(|identifier| identifier.name_token().ok())
.map(|token| token.token_text_trimmed().into());
let ty = param
.type_annotation()
let ty = annotation
.and_then(|annotation| annotation.ty().ok())
.map(|ty| TypeData::from_any_ts_type(resolver, scope_id, &ty))
.unwrap_or_default();
if let Some(name) = name {
Self::Named(NamedFunctionParameter {
name,
ty: resolver.reference_to_owned_data(ty),
is_optional: false,
is_rest: true,
is_optional,
is_rest,
})
} else {
let bindings = param
.binding()
let bindings = binding
.ok()
.and_then(|binding| {
FunctionParameterBinding::bindings_from_any_js_binding_pattern_of_type(
Expand All @@ -1595,8 +1562,8 @@ impl FunctionParameter {
Self::Pattern(PatternFunctionParameter {
bindings,
ty: resolver.reference_to_owned_data(ty),
is_optional: false,
is_rest: true,
is_optional,
is_rest,
})
}
}
Expand Down
Loading