diff --git a/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs index 4c30da014b..448af7913f 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs @@ -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()); @@ -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() != *sym { + continue; + } + + if let Some(ty) = tys.iter().find(|ty| 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() { diff --git a/crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs index 0b62af2660..b1c36e9bbe 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/scope/mod.rs @@ -1220,6 +1220,12 @@ impl Analyzer<'_, '_> { } } + self.find_local_type_without_this(name) + } + + fn find_local_type_without_this(&self, name: &Id) -> Option> { + 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); } @@ -2064,6 +2070,7 @@ impl Expander<'_, '_, '_> { type_args: Option<&TypeParamInstantiation>, was_top_level: bool, trying_primitive_expansion: bool, + ignore_this: bool, ) -> VResult> { macro_rules! verify { ($ty:expr) => {{ @@ -2080,26 +2087,20 @@ 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 { + self.analyzer.find_local_type_without_this(&i.into()) + } else { + None + } + .map_or_else(|| self.analyzer.find_type(&i.into()), |res| Ok(Some(res)))?; + + if let Some(types) = tys { info!("expand: expanding `{}` using analyzer: {}", Id::from(i), types.clone().count()); let mut stored_ref = None; @@ -2247,6 +2248,11 @@ impl Expander<'_, '_, '_> { Type::Mapped(m) => {} + Type::Namespace(..) => { + return Ok(Some(t.into_owned())); + } + + Type::StaticThis(..) => stored_ref = Some(t), _ => stored_ref = Some(t), } } @@ -2258,7 +2264,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()))); } @@ -2269,26 +2275,29 @@ 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 prop = Key::Normal { + span, + sym: right.sym.clone(), + }; + if let Some(ty) = self.expand_ts_entity_name(span, left, None, was_top_level, trying_primitive_expansion, false)? { + match self .analyzer - .access_property( - span, - left, - &Key::Normal { - span, - sym: right.sym.clone(), - }, - TypeOfMode::RValue, - IdCtx::Type, - Default::default(), - ) + .access_property(span, &ty, &prop, TypeOfMode::RValue, IdCtx::Type, 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) => return Ok(Some(t)), + err => { + if let Some(left) = + self.expand_ts_entity_name(span, left, None, was_top_level, trying_primitive_expansion, true)? + { + return Ok(self + .analyzer + .access_property(span, &left, &prop, TypeOfMode::RValue, IdCtx::Type, Default::default()) + .context("tried to access property as a part of type expansion(retry without this type)") + .report(&mut self.analyzer.storage)); + } + } + }; } } } @@ -2313,7 +2322,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); diff --git a/crates/stc_ts_type_checker/tests/conformance.pass.txt b/crates/stc_ts_type_checker/tests/conformance.pass.txt index 3b62640fe9..e13517914a 100644 --- a/crates/stc_ts_type_checker/tests/conformance.pass.txt +++ b/crates/stc_ts_type_checker/tests/conformance.pass.txt @@ -2341,7 +2341,10 @@ types/contextualTypes/partiallyAnnotatedFunction/partiallyAnnotatedFunctionWitou types/forAwait/types.forAwait.es2018.1.ts types/forAwait/types.forAwait.es2018.2.ts types/forAwait/types.forAwait.es2018.3.ts +types/import/importTypeAmbient.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 diff --git a/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedAndNonExportedInterfacesOfTheSameName.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedAndNonExportedInterfacesOfTheSameName.error-diff.json index fc72f54093..1830759d9e 100644 --- a/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedAndNonExportedInterfacesOfTheSameName.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedAndNonExportedInterfacesOfTheSameName.error-diff.json @@ -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 ] } diff --git a/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedModulesOfTheSameName.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedModulesOfTheSameName.error-diff.json index 47483375cd..519155a71f 100644 --- a/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedModulesOfTheSameName.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/internalModules/DeclarationMerging/TwoInternalModulesThatMergeEachWithExportedModulesOfTheSameName.error-diff.json @@ -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 ] } diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.error-diff.json deleted file mode 100644 index d8db441db2..0000000000 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.error-diff.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "required_errors": {}, - "required_error_lines": {}, - "extra_errors": { - "TS0": 1 - }, - "extra_error_lines": { - "TS0": [ - 28 - ] - } -} \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.stats.rust-debug index 0498397634..c086b5ab15 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeAmbient.stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 0, matched_error: 0, - extra_error: 1, + extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.error-diff.json deleted file mode 100644 index 27fcd6e0ee..0000000000 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.error-diff.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "required_errors": {}, - "required_error_lines": {}, - "extra_errors": { - "TS0": 1 - }, - "extra_error_lines": { - "TS0": [ - 18 - ] - } -} \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.stats.rust-debug index 0498397634..c086b5ab15 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeGenericTypes.stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 0, matched_error: 0, - extra_error: 1, + extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.error-diff.json deleted file mode 100644 index 1dc2bc9b4c..0000000000 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.error-diff.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "required_errors": {}, - "required_error_lines": {}, - "extra_errors": { - "TS0": 1 - }, - "extra_error_lines": { - "TS0": [ - 16 - ] - } -} \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.stats.rust-debug index 0498397634..c086b5ab15 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocal.stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 0, matched_error: 0, - extra_error: 1, + extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.error-diff.json index 77be6774be..e8ffa5f5f8 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.error-diff.json @@ -7,12 +7,6 @@ 3 ] }, - "extra_errors": { - "TS0": 1 - }, - "extra_error_lines": { - "TS0": [ - 16 - ] - } + "extra_errors": {}, + "extra_error_lines": {} } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.stats.rust-debug index c28efdd8e5..8a4b298eee 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/import/importTypeLocalMissing.stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 1, matched_error: 3, - extra_error: 1, + extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug index 3bad9ce15d..d972a81e03 100644 --- a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug @@ -1,6 +1,6 @@ Stats { required_error: 2861, matched_error: 7174, - extra_error: 1090, + extra_error: 1086, panic: 3, } \ No newline at end of file