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

fix: Import local type from module and namespace #1099

Merged
merged 9 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 14 additions & 8 deletions crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3035,9 +3035,9 @@ impl Analyzer<'_, '_> {
}

Type::Module(ty::Module { name, ref exports, .. }) => {
match id_ctx {
IdCtx::Type => {
if let Key::Normal { sym, .. } = prop {
if let Key::Normal { sym, .. } = prop {
match id_ctx {
IdCtx::Type => {
if let Some(types) = exports.types.get(sym).cloned() {
if types.len() == 1 {
return Ok(types.into_iter().next().unwrap());
Expand All @@ -3053,16 +3053,22 @@ impl Analyzer<'_, '_> {
if let Some(vars) = exports.vars.get(sym).cloned() {
return Ok(vars);
}

for (id, tys) in exports.private_types.iter() {
if id.sym().eq(sym) {
Copy link
Member

Choose a reason for hiding this comment

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

Please use == instead

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is there a particular reason other than readability?

sunrabbit123 marked this conversation as resolved.
Show resolved Hide resolved
for ty in tys.iter() {
if ty.is_module() || ty.is_interface() {
return Ok(ty.clone());
}
}
}
}
}
}
IdCtx::Var => {
if let Key::Normal { sym, .. } = prop {
IdCtx::Var => {
if let Some(item) = exports.vars.get(sym) {
return Ok(item.clone());
}
}

if let Key::Normal { sym, .. } = prop {
if let Some(types) = exports.types.get(sym) {
for ty in types.iter() {
if ty.is_module() || ty.is_interface() {
Expand Down
93 changes: 69 additions & 24 deletions crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,12 @@ impl Analyzer<'_, '_> {
}
}

self.find_local_type_without_this(name)
}

fn find_local_type_without_this(&self, name: &Id) -> Option<ItemRef<Type>> {
let _tracing = dev_span!("find_local_type_without_this", name = tracing::field::debug(name));

if cfg!(debug_assertions) {
debug!("({}) Scope.find_type(`{}`)", self.scope.depth(), name);
}
Expand Down Expand Up @@ -2064,6 +2070,7 @@ impl Expander<'_, '_, '_> {
type_args: Option<&TypeParamInstantiation>,
was_top_level: bool,
trying_primitive_expansion: bool,
ignore_this: bool,
) -> VResult<Option<Type>> {
macro_rules! verify {
($ty:expr) => {{
Expand All @@ -2080,26 +2087,24 @@ impl Expander<'_, '_, '_> {

match type_name {
RTsEntityName::Ident(ref i) => {
if let Some(class) = &self.analyzer.scope.get_this_class_name() {
if *class == *i {
return Ok(Some(Type::This(ThisType {
span,
metadata: Default::default(),
tracker: Default::default(),
})));
}
}
if i.sym == js_word!("void") {
return Ok(Some(Type::any(span, Default::default())));
}

info!("Info: {}{:?}", i.sym, i.span.ctxt);
if !trying_primitive_expansion && self.dejavu.contains(&i.into()) {
error!("Dejavu: {}{:?}", &i.sym, i.span.ctxt);
return Ok(None);
}

if let Some(types) = self.analyzer.find_type(&i.into())? {
let tys = if ignore_this {
let res = self.analyzer.find_local_type_without_this(&i.into());
if res.is_some() {
res
} else {
self.analyzer.find_type(&i.into())?
}
} else {
self.analyzer.find_type(&i.into())?
};

if let Some(types) = tys {
info!("expand: expanding `{}` using analyzer: {}", Id::from(i), types.clone().count());

let mut stored_ref = None;
Expand Down Expand Up @@ -2247,6 +2252,16 @@ impl Expander<'_, '_, '_> {

Type::Mapped(m) => {}

Type::Namespace(..) => {
dbg!(&t);
sunrabbit123 marked this conversation as resolved.
Show resolved Hide resolved
return Ok(Some(t.into_owned()));
}

Type::StaticThis(..) => {
dbg!(&t);

stored_ref = Some(t)
}
_ => stored_ref = Some(t),
}
}
Expand All @@ -2258,7 +2273,7 @@ impl Expander<'_, '_, '_> {
}
}

if i.sym == *"undefined" || i.sym == *"null" {
if i.sym == *"undefined" || i.sym == *"null" || i.sym == *"void" {
return Ok(Some(Type::any(span, Default::default())));
}

Expand All @@ -2269,14 +2284,13 @@ impl Expander<'_, '_, '_> {
//
// let a: StringEnum.Foo = x;
RTsEntityName::TsQualifiedName(box RTsQualifiedName { left, ref right, .. }) => {
let left = self.expand_ts_entity_name(span, left, None, was_top_level, trying_primitive_expansion)?;

if let Some(left) = &left {
let ty = self
let left_ty = self.expand_ts_entity_name(span, left, None, was_top_level, trying_primitive_expansion, false)?;
if let Some(ty) = &left_ty {
let res = match self
.analyzer
.access_property(
span,
left,
ty,
&Key::Normal {
span,
sym: right.sym.clone(),
Expand All @@ -2286,9 +2300,33 @@ impl Expander<'_, '_, '_> {
Default::default(),
)
.context("tried to access property as a part of type expansion")
.report(&mut self.analyzer.storage)
.unwrap_or_else(|| Type::any(span, Default::default()));
return Ok(Some(ty));
{
Ok(t) => Some(t),
err => {
if let Some(left) =
self.expand_ts_entity_name(span, left, None, was_top_level, trying_primitive_expansion, true)?
{
self.analyzer
.access_property(
span,
&left,
&Key::Normal {
span,
sym: right.sym.clone(),
},
TypeOfMode::RValue,
IdCtx::Type,
Default::default(),
)
.context("tried to access property as a part of type expansion")
.report(&mut self.analyzer.storage)
} else {
None
}
}
};

return Ok(res.or_else(|| Some(Type::any(span, Default::default()))));
}
}
}
Expand All @@ -2313,7 +2351,14 @@ impl Expander<'_, '_, '_> {
return Ok(None);
}

let mut ty = self.expand_ts_entity_name(span, &type_name, type_args.as_deref(), was_top_level, trying_primitive_expansion)?;
let mut ty = self.expand_ts_entity_name(
span,
&type_name,
type_args.as_deref(),
was_top_level,
trying_primitive_expansion,
false,
)?;

if let Some(ty) = &mut ty {
ty.reposition(r_span);
Expand Down
2 changes: 2 additions & 0 deletions crates/stc_ts_type_checker/tests/conformance.pass.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2329,6 +2329,8 @@ types/forAwait/types.forAwait.es2018.2.ts
types/forAwait/types.forAwait.es2018.3.ts
types/import/importTypeAmbientMissing.ts
types/import/importTypeAmdBundleRewrite.ts
types/import/importTypeGenericTypes.ts
types/import/importTypeLocal.ts
types/intersection/contextualIntersectionType.ts
types/intersection/intersectionMemberOfUnionNarrowsCorrectly.ts
types/intersection/intersectionNarrowing.ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"required_errors": {},
"required_error_lines": {},
"extra_errors": {
"TS2339": 2
"TS2403": 1,
"TS2339": 1
},
"extra_error_lines": {
"TS2403": [
17
],
"TS2339": [
17,
36
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
"required_errors": {},
"required_error_lines": {},
"extra_errors": {
"TS2339": 2
"TS2339": 1,
"TS2403": 1
},
"extra_error_lines": {
"TS2339": [
13,
13
],
"TS2403": [
33
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
"required_errors": {},
"required_error_lines": {},
"extra_errors": {
"TS2307": 3,
"TS0": 1
"TS2307": 3
},
"extra_error_lines": {
"TS2307": [
10,
33,
40
],
"TS0": [
28
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 4,
extra_error: 3,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 0,
matched_error: 0,
extra_error: 1,
extra_error: 0,
panic: 0,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
3
]
},
"extra_errors": {
"TS0": 1
},
"extra_error_lines": {
"TS0": [
16
]
}
"extra_errors": {},
"extra_error_lines": {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 1,
matched_error: 3,
extra_error: 1,
extra_error: 0,
panic: 0,
}
2 changes: 1 addition & 1 deletion crates/stc_ts_type_checker/tests/tsc-stats.rust-debug
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Stats {
required_error: 2860,
matched_error: 7175,
extra_error: 1107,
extra_error: 1103,
panic: 3,
}
Loading