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..9fc1e2ed2a 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().eq(sym) { + 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() { 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..c32c1f4f7e 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,19 @@ 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 { + 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; @@ -2247,6 +2247,16 @@ impl Expander<'_, '_, '_> { Type::Mapped(m) => {} + Type::Namespace(..) => { + dbg!(&t); + return Ok(Some(t.into_owned())); + } + + Type::StaticThis(..) => { + dbg!(&t); + + stored_ref = Some(t) + } _ => stored_ref = Some(t), } } @@ -2258,7 +2268,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,14 +2279,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(), @@ -2286,9 +2295,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())))); } } } @@ -2313,7 +2346,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 e438f4eba6..826bb9a4b3 100644 --- a/crates/stc_ts_type_checker/tests/conformance.pass.txt +++ b/crates/stc_ts_type_checker/tests/conformance.pass.txt @@ -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 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 index 03d5257c1e..20eacd5b75 100644 --- 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 @@ -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 ] } } \ 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 f8c5b3550b..7d12ad5c64 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: 4, + extra_error: 3, 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 83979399ee..c3fbeb1ada 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: 2860, matched_error: 7175, - extra_error: 1107, + extra_error: 1103, panic: 3, } \ No newline at end of file