Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tuple and custom array declared as interface compatible #413

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Changes from 11 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
370071b
feat: implement interface array case
devgony Jan 14, 2023
ce4cf98
fix: filter array by `type_of_ts_entity_name`
devgony Jan 14, 2023
fc48981
temp-1
devgony Feb 1, 2023
71cefdd
fix: implement handling interface at `assign/mod.rs`
devgony Feb 1, 2023
0399886
Merge branch 'arityAndOrderCompatibility-n' into arityAndOrderCompati…
devgony Feb 1, 2023
96f4567
fix: remove previous implement
devgony Feb 1, 2023
f27ad7e
fix: remove imports
devgony Feb 1, 2023
43f4aec
chore: remove redundant diff
devgony Feb 1, 2023
3194496
Merge remote-tracking branch 'upstream/main' into arityAndOrderCompat…
devgony Feb 1, 2023
20f4513
fix: compare types of lhs and rhs
devgony Feb 6, 2023
bd11c05
fix: zip and iterate each lkind and rkind
devgony Feb 7, 2023
259c374
fix: early return `assign_with_opts`
devgony Feb 7, 2023
5928792
chore: remove unused import
devgony Feb 7, 2023
2e880cb
fix: remove unused params
devgony Feb 7, 2023
135e2b3
fix: remove wrong spell
devgony Feb 7, 2023
e40402d
Merge remote-tracking branch 'upstream/main' into arityAndOrderCompat…
devgony Mar 23, 2023
bba28d3
fix: thanks clippy
devgony Mar 23, 2023
939a9c0
Merge remote-tracking branch 'upstream/main' into arityAndOrderCompat…
devgony May 19, 2023
7e87f8e
style: cargo fmt
devgony May 19, 2023
78011c1
style: thanks clippy
devgony May 19, 2023
b794a61
Merge remote-tracking branch 'upstream/main' into arityAndOrderCompat…
devgony Jun 3, 2023
9d463e3
Merge branch 'main' into arityAndOrderCompatibility
devgony Jun 3, 2023
4976748
test: add a case that should pass
devgony Jun 3, 2023
3d3fcca
fix: replace the order of lhs and rhs
devgony Jun 3, 2023
e85466f
Merge remote-tracking branch 'origin/arityAndOrderCompatibility' into…
devgony Jun 3, 2023
e4a786b
feat: correct errors.json
devgony Jun 3, 2023
fb8d765
refactor: remove redundant line, reduce clone
devgony Jun 3, 2023
0a08ed7
fix: renamt lhss to lsh_types
devgony Jun 4, 2023
bdf5da0
fix: remove unresolved test cases
devgony Jun 6, 2023
5759dc2
chore: run check.sh
devgony Jun 6, 2023
0da1d65
fix: correct error line number
devgony Jun 6, 2023
594c1e1
Merge remote-tracking branch 'upstream/main' into arityAndOrderCompat…
devgony Dec 29, 2023
4a839ca
fix: recover official tsc conformance test suite
devgony Dec 29, 2023
6ba341b
fix: remove error on line 39
devgony Dec 29, 2023
3df5f8b
fix: amend matched_error to 17
devgony Dec 29, 2023
b8551db
fix: remove formatting
devgony Dec 29, 2023
3ea7e93
fix: recover solved issue
devgony Jan 25, 2024
5f8c1df
feat: handle line16
devgony Jan 25, 2024
97256d8
feat: handle line17, 19
devgony Jan 25, 2024
66ecbd5
test: add arityAndOrderCompatibility02 case
devgony Jan 25, 2024
ed10347
chore: add test log
devgony Jan 25, 2024
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
115 changes: 110 additions & 5 deletions crates/stc_ts_file_analyzer/src/analyzer/assign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use std::{borrow::Cow, collections::HashMap};

use stc_ts_ast_rnode::{RBool, RExpr, RIdent, RLit, RNumber, RStr, RTsEntityName, RTsEnumMemberId, RTsLit};
use stc_ts_errors::{
debug::{dump_type_as_string, force_dump_type_as_string},
debug::{dump_type_as_string, force_dump_type_as_string, print_backtrace},
DebugExt, ErrorKind,
};
use stc_ts_file_analyzer_macros::context;
use stc_ts_types::{
Array, Conditional, EnumVariant, IdCtx, Instance, Interface, Intersection, IntrinsicKind, Key, KeywordType, KeywordTypeMetadata,
LitType, Mapped, Operator, PropertySignature, QueryExpr, QueryType, Ref, RestType, StringMapping, ThisType, Tuple, Type, TypeElement,
TypeLit, TypeParam,
Array, Conditional, EnumVariant, Freezed, IdCtx, Instance, Interface, InterfaceMetadata, Intersection, IntrinsicKind, Key, KeywordType,
KeywordTypeMetadata, LitType, Mapped, Operator, PropertySignature, QueryExpr, QueryType, Ref, RestType, StringMapping, ThisType,
TsExpr, Tuple, TupleElement, Type, TypeElement, TypeLit, TypeParam, Union,
};
use stc_utils::{cache::Freeze, stack};
use swc_atoms::js_word;
Expand Down Expand Up @@ -1031,6 +1031,56 @@ impl Analyzer<'_, '_> {
return Ok(());
}

(
Type::Tuple(Tuple {
span: tspan,
elems,
metadata,
tracker,
}),
Type::Interface(Interface {
span: ispan,
name,
type_params,
extends,
body,
metadata: imetadata,
tracker: itracker,
}),
) => {
let lhs = elems.iter().map(|TupleElement { span, label, ty, tracker }| ty);
for parent in extends {
let params = parent.type_args.clone().unwrap().params;
let rhs = self
.type_of_ts_entity_name(span, &parent.expr, parent.type_args.as_deref())?
.freezed();
if let Some(Array {
span,
elem_type,
metadata,
tracker,
}) = rhs.array()
{
if let Some(Union {
span,
types,
metadata,
tracker,
}) = elem_type.union_type()
{
for (lhs, ty) in lhs.clone().zip(types) {
if let (Some(KeywordType { kind: lkind, .. }), Some(KeywordType { kind: rkind, .. })) =
(lhs.to_owned().keyword(), ty.keyword())
{
println!("@@@\n{lkind:#?}:{rkind:#?}");
let diff = lkind == rkind;
println!("@@@\n{diff}");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Is there any existing function to compare two KeywordType and return reasonable error?
  2. Do you think I'm going correct way?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. You can use assign_with_opts(...)? to do it
  2. I think you should not reimplement whole logic here, but delegate to a recursive call to assign_with_opts

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we should keep logic here.
Otherwise, it breaks other tests
https://github.com/dudykr/stc/actions/runs/4110781244/jobs/7093929846

}
}
}
};
}
}
_ => {}
}

Expand Down Expand Up @@ -1410,7 +1460,6 @@ impl Analyzer<'_, '_> {
.assign_with_opts(data, ty, rhs, opts)
.context("tried to assign a type to an operand of readonly type")
}

_ => {}
}

Expand All @@ -1421,6 +1470,62 @@ impl Analyzer<'_, '_> {
}

match rhs {
Type::Interface(Interface {
// span,
name,
type_params,
extends,
body,
metadata: InterfaceMetadata { common },
tracker,
..
}) => {
// for parent in extends {
// let ty = self.type_of_ts_entity_name(parent.span(),
// &parent.expr, parent.type_args.as_deref())?;
// if let Type::Array(_) | Type::Tpl(_) = &ty {
// return self.assign_with_opts(data, &ty, rhs,
// opts); } }

// let mut errors = vec![];
// for parent in extends {
// let parent = self
// .type_of_ts_entity_name(span, &parent.expr,
// parent.type_args.as_deref())? .freezed();

// // An interface can extend a class.
// let parent = self.instantiate_class(span, &parent)?;

// let res = self.assign_with_opts(
// data,
// &parent,
// rhs,
// AssignOpts {
// allow_unknown_rhs: Some(true),
// ..opts
// },
// );

// errors.extend(res.err());
// }

// if !extends.is_empty() && errors.is_empty() {
// return Ok(());
// }

// if !errors.is_empty() {
// return Err(ErrorKind::AssignFailed {
// span,
// left: box to.clone(),
// right: box rhs.clone(),
// right_ident: opts.right_ident_span,
// cause: errors,
// }
// .into());
// }

// return Ok(());
}
Type::Ref(..) => {
let mut new_rhs = self.expand_top_ref(span, Cow::Borrowed(rhs), Default::default())?;
new_rhs.freeze();
Expand Down