From e95d8e3990580b80bc1f19aab076cfa684dc24b0 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:33:23 +0000 Subject: [PATCH] feat(isolated-declarations): shrink span for arrow function that needs an explicit return type (#3752) Current `span` we can't be consistent with typescript. We report the following ```ts (a, b) => ^^^^^^^^^ { } ^ ``` TypeScript reports the following ```ts (a, b) => ^^^^^^^^^ {} ``` The TypeScript only reports the first line, if we want it to be exactly the same we need to find the first newline position --- crates/oxc_isolated_declarations/src/types.rs | 7 +++- .../fixtures/arrow-function-return-type.ts | 9 +++++ .../snapshots/arrow-function-return-type.snap | 38 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 crates/oxc_isolated_declarations/tests/fixtures/arrow-function-return-type.ts create mode 100644 crates/oxc_isolated_declarations/tests/snapshots/arrow-function-return-type.snap diff --git a/crates/oxc_isolated_declarations/src/types.rs b/crates/oxc_isolated_declarations/src/types.rs index 513fb28fb9561..f57630ac53e9f 100644 --- a/crates/oxc_isolated_declarations/src/types.rs +++ b/crates/oxc_isolated_declarations/src/types.rs @@ -4,7 +4,7 @@ use oxc_ast::ast::{ TSTypeOperatorOperator, }; use oxc_diagnostics::OxcDiagnostic; -use oxc_span::{GetSpan, SPAN}; +use oxc_span::{GetSpan, Span, SPAN}; use crate::{ diagnostics::{ @@ -41,7 +41,10 @@ impl<'a> IsolatedDeclarations<'a> { let return_type = self.infer_arrow_function_return_type(func); if return_type.is_none() { - self.error(function_must_have_explicit_return_type(func.span)); + self.error(function_must_have_explicit_return_type(Span::new( + func.params.span.start, + func.body.span.start + 1, + ))); } let params = self.transform_formal_parameters(&func.params); diff --git a/crates/oxc_isolated_declarations/tests/fixtures/arrow-function-return-type.ts b/crates/oxc_isolated_declarations/tests/fixtures/arrow-function-return-type.ts new file mode 100644 index 0000000000000..34e91d499ce7d --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/arrow-function-return-type.ts @@ -0,0 +1,9 @@ +function A() { + return () => { + return C; + } +} + +const B = () => { return B }; + +const C = function () {} \ No newline at end of file diff --git a/crates/oxc_isolated_declarations/tests/snapshots/arrow-function-return-type.snap b/crates/oxc_isolated_declarations/tests/snapshots/arrow-function-return-type.snap new file mode 100644 index 0000000000000..4d2e4433f8e39 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/arrow-function-return-type.snap @@ -0,0 +1,38 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/arrow-function-return-type.ts +--- +==================== .D.TS ==================== + +declare function A(): unknown; +declare const B: unknown; +declare const C: unknown; + + +==================== Errors ==================== + + x Function must have an explicit return type annotation with + | --isolatedDeclarations. + ,-[2:10] + 1 | function A() { + 2 | return () => { + : ^^^^^^^ + 3 | return C; + `---- + + x Function must have an explicit return type annotation with + | --isolatedDeclarations. + ,-[7:11] + 6 | + 7 | const B = () => { return B }; + : ^^^^^^^ + 8 | + `---- + + x Function must have an explicit return type annotation with + | --isolatedDeclarations. + ,-[9:20] + 8 | + 9 | const C = function () {} + : ^ + `----