Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/assign/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
impl Analyzer<'_, '_> {
/// Returns `Ok(Some())` if the assignment is handled.
pub(super) fn assign_to_tuple(
&mut self,
&self,
data: &mut AssignData,
l: &Tuple,
l_type: &Type,
Expand Down
2 changes: 1 addition & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/assign/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Analyzer<'_, '_> {
/// - Handles assignment of `Function` types.
/// - Handles assignment of various array types.
/// - Handles assignment of promise types.
pub(super) fn assign_to_builtin(&mut self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
pub(super) fn assign_to_builtin(&self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
let span = opts.span;
let l = l.normalize();
let r = r.normalize();
Expand Down
2 changes: 1 addition & 1 deletion crates/stc_ts_file_analyzer/src/analyzer/assign/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::analyzer::{types::NormalizeTypeOpts, Analyzer};
impl Analyzer<'_, '_> {
/// Returns true if the type can be casted to number if it's in the rvalue
/// position.
pub(crate) fn can_be_casted_to_number_in_rhs(&mut self, span: Span, ty: &Type) -> bool {
pub(crate) fn can_be_casted_to_number_in_rhs(&self, span: Span, ty: &Type) -> bool {
let ty = match self.normalize(
Some(span),
Cow::Borrowed(ty),
Expand Down
6 changes: 3 additions & 3 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
};

impl Analyzer<'_, '_> {
pub(super) fn assign_to_class_def(&mut self, data: &mut AssignData, l: &ClassDef, r: &Type, opts: AssignOpts) -> VResult<()> {
pub(super) fn assign_to_class_def(&self, data: &mut AssignData, l: &ClassDef, r: &Type, opts: AssignOpts) -> VResult<()> {
let r = self.normalize(Some(opts.span), Cow::Borrowed(r), Default::default())?;

match r.normalize() {
Expand Down Expand Up @@ -127,7 +127,7 @@ impl Analyzer<'_, '_> {
.into())
}

pub(super) fn assign_to_class(&mut self, data: &mut AssignData, l: &Class, r: &Type, opts: AssignOpts) -> VResult<()> {
pub(super) fn assign_to_class(&self, data: &mut AssignData, l: &Class, r: &Type, opts: AssignOpts) -> VResult<()> {
// debug_assert!(!span.is_dummy());

let r = self.normalize(Some(opts.span), Cow::Borrowed(r), Default::default())?;
Expand Down Expand Up @@ -279,7 +279,7 @@ impl Analyzer<'_, '_> {
}

fn assign_class_members_to_class_member(
&mut self,
&self,
data: &mut AssignData,
l: &ClassMember,
r: &[ClassMember],
Expand Down
12 changes: 6 additions & 6 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
/// Methods to handle assignment to function types and constructor types.
impl Analyzer<'_, '_> {
pub(crate) fn assign_to_fn_like(
&mut self,
&self,
data: &mut AssignData,
is_call: bool,
l_type_params: Option<&TypeParamDecl>,
Expand Down Expand Up @@ -423,7 +423,7 @@ impl Analyzer<'_, '_> {
/// a = b;
/// b = a; // error
/// ```
pub(super) fn assign_to_function(&mut self, data: &mut AssignData, lt: &Type, l: &Function, r: &Type, opts: AssignOpts) -> VResult<()> {
pub(super) fn assign_to_function(&self, data: &mut AssignData, lt: &Type, l: &Function, r: &Type, opts: AssignOpts) -> VResult<()> {
let _tracing = dev_span!("assign_to_function");

let span = opts.span;
Expand Down Expand Up @@ -530,7 +530,7 @@ impl Analyzer<'_, '_> {
/// b18 = a18; // ok
/// ```
pub(super) fn assign_to_constructor(
&mut self,
&self,
data: &mut AssignData,
lt: &Type,
l: &Constructor,
Expand Down Expand Up @@ -668,7 +668,7 @@ impl Analyzer<'_, '_> {
/// # Notes
///
/// - `string` is assignable to `...args: any[]`.
fn assign_param(&mut self, data: &mut AssignData, l: &FnParam, r: &FnParam, opts: AssignOpts) -> VResult<()> {
fn assign_param(&self, data: &mut AssignData, l: &FnParam, r: &FnParam, opts: AssignOpts) -> VResult<()> {
let _tracing = dev_span!("assign_param");

let span = opts.span;
Expand All @@ -692,7 +692,7 @@ impl Analyzer<'_, '_> {
}

/// Implementation of `assign_param`.
fn assign_param_type(&mut self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> VResult<()> {
fn assign_param_type(&self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> VResult<()> {
let _tracing = dev_span!("assign_param_type");

let span = opts.span;
Expand Down Expand Up @@ -832,7 +832,7 @@ impl Analyzer<'_, '_> {
/// ```
///
/// So, it's an error if `l.params.len() < r.params.len()`.
pub(crate) fn assign_params(&mut self, data: &mut AssignData, l: &[FnParam], r: &[FnParam], opts: AssignOpts) -> VResult<()> {
pub(crate) fn assign_params(&self, data: &mut AssignData, l: &[FnParam], r: &[FnParam], opts: AssignOpts) -> VResult<()> {
let _tracing = dev_span!("assign_params");

let span = opts.span;
Expand Down
30 changes: 15 additions & 15 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub struct AssignData {
impl Analyzer<'_, '_> {
/// Denies `null` and `undefined`. This method does not check for elements
/// of union.
pub(crate) fn deny_null_or_undefined(&mut self, span: Span, ty: &Type) -> VResult<()> {
pub(crate) fn deny_null_or_undefined(&self, span: Span, ty: &Type) -> VResult<()> {
if ty.is_kwd(TsKeywordTypeKind::TsUndefinedKeyword) {
return Err(ErrorKind::ObjectIsPossiblyUndefined { span }.into());
}
Expand All @@ -217,7 +217,7 @@ impl Analyzer<'_, '_> {
}

/// Used to validate assignments like `a += b`.
pub(crate) fn assign_with_operator(&mut self, span: Span, op: AssignOp, lhs: &Type, rhs: &Type) -> VResult<()> {
pub(crate) fn assign_with_operator(&self, span: Span, op: AssignOp, lhs: &Type, rhs: &Type) -> VResult<()> {
debug_assert_ne!(op, op!("="));

let l = self.normalize(
Expand Down Expand Up @@ -446,7 +446,7 @@ impl Analyzer<'_, '_> {
}

/// Assign `right` to `left`. You can just use default for [AssignData].
pub(crate) fn assign(&mut self, span: Span, data: &mut AssignData, left: &Type, right: &Type) -> VResult<()> {
pub(crate) fn assign(&self, span: Span, data: &mut AssignData, left: &Type, right: &Type) -> VResult<()> {
self.assign_with_opts(
data,
left,
Expand All @@ -459,7 +459,7 @@ impl Analyzer<'_, '_> {
}

/// Assign `right` to `left`. You can just use default for [AssignData].
pub(crate) fn assign_with_opts(&mut self, data: &mut AssignData, left: &Type, right: &Type, opts: AssignOpts) -> VResult<()> {
pub(crate) fn assign_with_opts(&self, data: &mut AssignData, left: &Type, right: &Type, opts: AssignOpts) -> VResult<()> {
if self.config.is_builtin {
return Ok(());
}
Expand Down Expand Up @@ -499,7 +499,7 @@ impl Analyzer<'_, '_> {
})
}

fn normalize_for_assign<'a>(&mut self, span: Span, ty: &'a Type, opts: AssignOpts) -> VResult<Cow<'a, Type>> {
fn normalize_for_assign<'a>(&self, span: Span, ty: &'a Type, opts: AssignOpts) -> VResult<Cow<'a, Type>> {
ty.assert_valid();

let ty = ty.normalize();
Expand Down Expand Up @@ -560,7 +560,7 @@ impl Analyzer<'_, '_> {
Ok(Cow::Borrowed(ty))
}

fn assign_inner(&mut self, data: &mut AssignData, left: &Type, right: &Type, opts: AssignOpts) -> VResult<()> {
fn assign_inner(&self, data: &mut AssignData, left: &Type, right: &Type, opts: AssignOpts) -> VResult<()> {
left.assert_valid();
right.assert_valid();

Expand Down Expand Up @@ -611,7 +611,7 @@ impl Analyzer<'_, '_> {
}

/// Assigns, but does not wrap error with [Error::AssignFailed].
fn assign_without_wrapping(&mut self, data: &mut AssignData, to: &Type, rhs: &Type, opts: AssignOpts) -> VResult<()> {
fn assign_without_wrapping(&self, data: &mut AssignData, to: &Type, rhs: &Type, opts: AssignOpts) -> VResult<()> {
let span = opts.span;

if !self.config.is_builtin && span.is_dummy() {
Expand Down Expand Up @@ -2703,7 +2703,7 @@ impl Analyzer<'_, '_> {
}

/// Should be called only if `to` is not expandable.
pub(super) fn assign_to_intrinsic(&mut self, data: &mut AssignData, to: &StringMapping, r: &Type, opts: AssignOpts) -> VResult<()> {
pub(super) fn assign_to_intrinsic(&self, data: &mut AssignData, to: &StringMapping, r: &Type, opts: AssignOpts) -> VResult<()> {
match r.normalize() {
Type::Keyword(KeywordType {
kind: TsKeywordTypeKind::TsAnyKeyword,
Expand Down Expand Up @@ -3110,7 +3110,7 @@ impl Analyzer<'_, '_> {
Ok(())
}

fn extract_keys(&mut self, span: Span, ty: &Type) -> VResult<Type> {
fn extract_keys(&self, span: Span, ty: &Type) -> VResult<Type> {
(|| -> VResult<_> {
let ty = self.normalize(
Some(span),
Expand Down Expand Up @@ -3170,7 +3170,7 @@ impl Analyzer<'_, '_> {
///
///
/// Currently only literals and unions are supported for `keys`.
fn assign_keys(&mut self, data: &mut AssignData, keys: &Type, rhs: &Type, opts: AssignOpts) -> VResult<()> {
fn assign_keys(&self, data: &mut AssignData, keys: &Type, rhs: &Type, opts: AssignOpts) -> VResult<()> {
let keys = keys.normalize();
let rhs = rhs.normalize();

Expand All @@ -3189,7 +3189,7 @@ impl Analyzer<'_, '_> {
.context("tried to assign keys")
}

fn assign_to_mapped(&mut self, data: &mut AssignData, l: &Mapped, r: &Type, opts: AssignOpts) -> VResult<()> {
fn assign_to_mapped(&self, data: &mut AssignData, l: &Mapped, r: &Type, opts: AssignOpts) -> VResult<()> {
let span = opts.span;
let mut r = self
.normalize(Some(span), Cow::Borrowed(r), NormalizeTypeOpts { ..Default::default() })
Expand Down Expand Up @@ -3320,7 +3320,7 @@ impl Analyzer<'_, '_> {
/// Returns true for `A | B | | C = A | B` and similar cases.
///
/// Should be called iff lhs is a union type.
fn should_use_special_union_assignment(&mut self, span: Span, r: &Type) -> VResult<bool> {
fn should_use_special_union_assignment(&self, span: Span, r: &Type) -> VResult<bool> {
match r.normalize() {
Type::Union(..) => return Ok(true),
Type::TypeLit(r) => {
Expand All @@ -3339,7 +3339,7 @@ impl Analyzer<'_, '_> {
}

/// TODO(kdy1): I'm not sure about this.
fn variance(&mut self, ty: &Conditional) -> VResult<Variance> {
fn variance(&self, ty: &Conditional) -> VResult<Variance> {
let can_be_covariant = self.is_covariant(&ty.check_type, &ty.true_type)? || self.is_covariant(&ty.check_type, &ty.false_type)?;

let can_be_contravariant =
Expand All @@ -3352,11 +3352,11 @@ impl Analyzer<'_, '_> {
}
}

fn is_covariant(&mut self, check_type: &Type, output_type: &Type) -> VResult<bool> {
fn is_covariant(&self, check_type: &Type, output_type: &Type) -> VResult<bool> {
Ok(check_type.type_eq(output_type))
}

fn is_contravariant(&mut self, check_type: &Type, output_type: &Type) -> VResult<bool> {
fn is_contravariant(&self, check_type: &Type, output_type: &Type) -> VResult<bool> {
if let Type::Index(Index { ty, .. }) = output_type.normalize() {
if output_type.type_eq(&**ty) {
return Ok(true);
Expand Down
4 changes: 2 additions & 2 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/tpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Analyzer<'_, '_> {
/// orders.
///
/// After splitting, we can check if each element is assignable.
pub(crate) fn assign_to_tpl(&mut self, data: &mut AssignData, l: &TplType, r_ty: &Type, opts: AssignOpts) -> VResult<()> {
pub(crate) fn assign_to_tpl(&self, data: &mut AssignData, l: &TplType, r_ty: &Type, opts: AssignOpts) -> VResult<()> {
let span = opts.span;
let r_ty = r_ty.normalize();

Expand All @@ -52,7 +52,7 @@ impl Analyzer<'_, '_> {
}

/// Ported from `isValidTypeForTemplateLiteralPlaceholder` of `tsc`
pub(crate) fn is_valid_type_for_tpl_lit_placeholder(&mut self, span: Span, source: &Type, target: &Type) -> VResult<bool> {
pub(crate) fn is_valid_type_for_tpl_lit_placeholder(&self, span: Span, source: &Type, target: &Type) -> VResult<bool> {
let _tracing = dev_span!(
"is_valid_type_for_tpl_lit_placeholder",
source = tracing::field::display(&force_dump_type_as_string(source)),
Expand Down
12 changes: 6 additions & 6 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/type_el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Analyzer<'_, '_> {
/// let b: { key: string } = foo;
/// ```
pub(crate) fn assign_to_type_elements(
&mut self,
&self,
data: &mut AssignData,
lhs_span: Span,
lhs: &[TypeElement],
Expand Down Expand Up @@ -951,7 +951,7 @@ impl Analyzer<'_, '_> {
Ok(())
}

fn should_report_properties(&mut self, span: Span, lhs: &[TypeElement], rhs: &Type) -> bool {
fn should_report_properties(&self, span: Span, lhs: &[TypeElement], rhs: &Type) -> bool {
let type_call_signatures = lhs
.iter()
.filter_map(|m| match m {
Expand Down Expand Up @@ -1004,7 +1004,7 @@ impl Analyzer<'_, '_> {
true
}

pub(super) fn try_assign_using_parent(&mut self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
pub(super) fn try_assign_using_parent(&self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
let span = opts.span;

match r.normalize() {
Expand Down Expand Up @@ -1040,7 +1040,7 @@ impl Analyzer<'_, '_> {
}

fn handle_assignment_of_type_elements_to_type_elements(
&mut self,
&self,
data: &mut AssignData,
missing_fields: &mut Vec<TypeElement>,
unhandled_rhs: &mut Vec<Span>,
Expand Down Expand Up @@ -1174,7 +1174,7 @@ impl Analyzer<'_, '_> {
/// b = a // ok
/// ```
fn assign_type_elements_to_type_element(
&mut self,
&self,
data: &mut AssignData,
missing_fields: &mut Vec<TypeElement>,
unhandled_rhs: &mut Vec<Span>,
Expand Down Expand Up @@ -1627,7 +1627,7 @@ impl Analyzer<'_, '_> {
Ok(())
}

pub(crate) fn index_signature_matches(&mut self, span: Span, index_ty: &Type, prop_ty: &Type) -> VResult<bool> {
pub(crate) fn index_signature_matches(&self, span: Span, index_ty: &Type, prop_ty: &Type) -> VResult<bool> {
if (prop_ty.is_num() && index_ty.is_kwd(TsKeywordTypeKind::TsNumberKeyword))
|| (prop_ty.is_str() && index_ty.is_kwd(TsKeywordTypeKind::TsStringKeyword))
{
Expand Down
10 changes: 5 additions & 5 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/unions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Analyzer<'_, '_> {
///
/// - lhs = `(["a", number] | ["b", number] | ["c", string]);`
/// - rhs = `[("b" | "a"), 1];`
pub(super) fn assign_to_union(&mut self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
pub(super) fn assign_to_union(&self, data: &mut AssignData, l: &Type, r: &Type, opts: AssignOpts) -> Option<VResult<()>> {
let r_res = self.flatten_unions_for_assignment(opts.span, Cow::Borrowed(r));

match r_res {
Expand All @@ -46,7 +46,7 @@ impl Analyzer<'_, '_> {
}
}

fn flatten_unions_for_assignment(&mut self, span: Span, ty: Cow<Type>) -> VResult<Type> {
fn flatten_unions_for_assignment(&self, span: Span, ty: Cow<Type>) -> VResult<Type> {
let ty = self.normalize(Some(span), ty, Default::default())?;

match ty.normalize() {
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Analyzer<'_, '_> {
}

/// TODO(kdy1): Use Cow<TupleElement>
fn append_type_element_to_type(&mut self, span: Span, to: &mut Type, el: &TypeElement) -> VResult<()> {
fn append_type_element_to_type(&self, span: Span, to: &mut Type, el: &TypeElement) -> VResult<()> {
if let TypeElement::Property(el) = el {
if let Some(el_ty) = &el.type_ann {
if let Some(ty) = self.expand_union_for_assignment(span, el_ty) {
Expand Down Expand Up @@ -129,7 +129,7 @@ impl Analyzer<'_, '_> {
}

/// TODO(kdy1): Use Cow<TupleElement>
fn append_tuple_element_to_type(&mut self, span: Span, to: &mut Type, el: &TupleElement) -> VResult<()> {
fn append_tuple_element_to_type(&self, span: Span, to: &mut Type, el: &TupleElement) -> VResult<()> {
if let Some(el_ty) = self.expand_union_for_assignment(span, &el.ty) {
let mut to_types = (0..el_ty.types.len()).map(|_| to.clone()).collect_vec();

Expand Down Expand Up @@ -174,7 +174,7 @@ impl Analyzer<'_, '_> {
}

/// Expands `boolean` to `true | false`.
fn expand_union_for_assignment(&mut self, span: Span, t: &Type) -> Option<Union> {
fn expand_union_for_assignment(&self, span: Span, t: &Type) -> Option<Union> {
let t = self.normalize(Some(span), Cow::Borrowed(t), Default::default()).ok()?;

match t.normalize() {
Expand Down
Loading