From 8ac1d0dd8ce710c61dd454bb3208f9bcf62d97e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=98=A4=EB=B3=91=EC=A7=84?= <64676070+sunrabbit123@users.noreply.github.com> Date: Mon, 18 Sep 2023 21:37:37 +0900 Subject: [PATCH] feat: Resolve `panic!` cause by `import.meta` (#1089) **Description:** ```ts declare global { interface ImportMeta {foo?: () => void} }; if (import.meta.foo) { import.meta.foo(); } ``` --- crates/stc_ts_file_analyzer/src/analyzer/expr/meta_prop.rs | 7 ++++++- crates/stc_ts_type_checker/tests/conformance.pass.txt | 3 +++ .../importMetaNarrowing(module=es2020).stats.rust-debug | 2 +- .../importMetaNarrowing(module=esnext).stats.rust-debug | 2 +- .../importMetaNarrowing(module=system).stats.rust-debug | 2 +- crates/stc_ts_type_checker/tests/tsc-stats.rust-debug | 2 +- 6 files changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/expr/meta_prop.rs b/crates/stc_ts_file_analyzer/src/analyzer/expr/meta_prop.rs index dd9a07adc8..3f2f810162 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/expr/meta_prop.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/expr/meta_prop.rs @@ -19,7 +19,12 @@ impl Analyzer<'_, '_> { Ok(Type::any(e.span, Default::default())) } - _ => { + MetaPropKind::ImportMeta => { + if let Ok(mut ty) = self.env.get_global_type(e.span(), &"ImportMeta".into()) { + ty.reposition(e.span()); + return Ok(ty); + } + todo!("Unsupported meta property {:?}", e) } } diff --git a/crates/stc_ts_type_checker/tests/conformance.pass.txt b/crates/stc_ts_type_checker/tests/conformance.pass.txt index 9927353d12..baf950f3a1 100644 --- a/crates/stc_ts_type_checker/tests/conformance.pass.txt +++ b/crates/stc_ts_type_checker/tests/conformance.pass.txt @@ -475,6 +475,7 @@ es2018/es2018IntlAPIs.ts es2018/usePromiseFinally.ts es2018/useRegexpGroups.ts es2019/globalThisTypeIndexAccess.ts +es2019/importMeta/importMetaNarrowing.ts es2020/bigintMissingES2019.ts es2020/bigintMissingES2020.ts es2020/bigintMissingESNext.ts @@ -1018,6 +1019,7 @@ es6/templates/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01.ts es6/templates/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes01_ES6.ts es6/templates/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes02.ts es6/templates/taggedTemplateStringsPlainCharactersThatArePartsOfEscapes02_ES6.ts +es6/templates/taggedTemplateStringsPldirectives/ts-expect-error-nocheck.ts es6/templates/taggedTemplateStringsWithIncompatibleTypedTags.ts es6/templates/taggedTemplateStringsWithIncompatibleTypedTagsES6.ts es6/templates/taggedTemplateStringsWithManyCallAndMemberExpressions.ts @@ -1148,6 +1150,7 @@ es6/templates/templateStringWithEmbeddedObjectLiteral.ts es6/templates/templateStringWithEmbeddedObjectLiteralES6.ts es6/templates/templateStringWithEmbeddedTemplateString.ts es6/templates/templateStringWithEmbeddedTemplateStringES6.ts +es6/templates/templateStringWithEmbeddedTypdirectives/ts-expect-error-nocheck.ts es6/templates/templateStringWithEmbeddedTypeAssertionOnAddition.ts es6/templates/templateStringWithEmbeddedTypeAssertionOnAdditionES6.ts es6/templates/templateStringWithEmbeddedTypeOfOperator.ts diff --git a/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=es2020).stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=es2020).stats.rust-debug index 27ba984132..c086b5ab15 100644 --- a/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=es2020).stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=es2020).stats.rust-debug @@ -2,5 +2,5 @@ Stats { required_error: 0, matched_error: 0, extra_error: 0, - panic: 1, + panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=esnext).stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=esnext).stats.rust-debug index 27ba984132..c086b5ab15 100644 --- a/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=esnext).stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=esnext).stats.rust-debug @@ -2,5 +2,5 @@ Stats { required_error: 0, matched_error: 0, extra_error: 0, - panic: 1, + panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=system).stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=system).stats.rust-debug index 27ba984132..c086b5ab15 100644 --- a/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=system).stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/es2019/importMeta/importMetaNarrowing(module=system).stats.rust-debug @@ -2,5 +2,5 @@ Stats { required_error: 0, matched_error: 0, extra_error: 0, - panic: 1, + 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 a8892c675c..436c94ebb2 100644 --- a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug @@ -2,5 +2,5 @@ Stats { required_error: 3487, matched_error: 6548, extra_error: 768, - panic: 63, + panic: 60, } \ No newline at end of file