From dca8229de6c4401ac42aa55e54034f4ef4cadf3a Mon Sep 17 00:00:00 2001 From: Dunqing Date: Wed, 19 Jun 2024 21:25:09 +0800 Subject: [PATCH] feat(isolated-declarations): support inferring ParenthesizedExpression --- crates/oxc_isolated_declarations/src/inferrer.rs | 7 +++---- .../tests/fixtures/infer-expression.ts | 5 +++++ .../tests/snapshots/infer-expression.snap | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 crates/oxc_isolated_declarations/tests/fixtures/infer-expression.ts create mode 100644 crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap diff --git a/crates/oxc_isolated_declarations/src/inferrer.rs b/crates/oxc_isolated_declarations/src/inferrer.rs index 7d7cba7e39ae3e..39245637ec9a23 100644 --- a/crates/oxc_isolated_declarations/src/inferrer.rs +++ b/crates/oxc_isolated_declarations/src/inferrer.rs @@ -52,16 +52,15 @@ impl<'a> IsolatedDeclarations<'a> { self.error(inferred_type_of_class_expression(expr.span)); Some(self.ast.ts_unknown_keyword(SPAN)) } + Expression::ParenthesizedExpression(expr) => { + self.infer_type_from_expression(&expr.expression) + } Expression::TSNonNullExpression(expr) => { self.infer_type_from_expression(&expr.expression) } Expression::TSSatisfiesExpression(expr) => { self.infer_type_from_expression(&expr.expression) } - Expression::TSInstantiationExpression(_expr) => { - unreachable!(); - // infer_type_from_expression(ctx, &expr.expression) - } Expression::TSTypeAssertion(expr) => Some(self.ast.copy(&expr.type_annotation)), _ => None, } diff --git a/crates/oxc_isolated_declarations/tests/fixtures/infer-expression.ts b/crates/oxc_isolated_declarations/tests/fixtures/infer-expression.ts new file mode 100644 index 00000000000000..06222db62bcc97 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/infer-expression.ts @@ -0,0 +1,5 @@ +// ParenthesizedExpression +const n = (0); +const s = (""); +const t = (``); +const b = (true); \ No newline at end of file diff --git a/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap b/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap new file mode 100644 index 00000000000000..40ca55fc06e7b2 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/infer-expression.snap @@ -0,0 +1,10 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/infer-expression.ts +--- +==================== .D.TS ==================== + +declare const n: number; +declare const s: string; +declare const t: string; +declare const b: boolean;