From 07fb8534c428bed98d87af60cbc1df791bfc4369 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 20 Jun 2024 16:29:32 +0800 Subject: [PATCH] fix(isolated-declarations): should stripe async and generator keyword after transformed --- .../oxc_isolated_declarations/src/function.rs | 4 +-- .../oxc_isolated_declarations/src/inferrer.rs | 4 --- .../tests/fixtures/async-function.ts | 16 ++++++++++ .../tests/fixtures/generator.ts | 10 ++++++ .../tests/snapshots/async-function.snap | 31 +++++++++++++++++++ .../tests/snapshots/generator.snap | 20 ++++++++++++ 6 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 crates/oxc_isolated_declarations/tests/fixtures/async-function.ts create mode 100644 crates/oxc_isolated_declarations/tests/fixtures/generator.ts create mode 100644 crates/oxc_isolated_declarations/tests/snapshots/async-function.snap create mode 100644 crates/oxc_isolated_declarations/tests/snapshots/generator.snap diff --git a/crates/oxc_isolated_declarations/src/function.rs b/crates/oxc_isolated_declarations/src/function.rs index b92b9e0b1fb3cd..ca5c6d8a500c7d 100644 --- a/crates/oxc_isolated_declarations/src/function.rs +++ b/crates/oxc_isolated_declarations/src/function.rs @@ -26,8 +26,8 @@ impl<'a> IsolatedDeclarations<'a> { func.r#type, func.span, self.ast.copy(&func.id), - func.generator, - func.r#async, + false, + false, self.ast.copy(&func.this_param), params, None, diff --git a/crates/oxc_isolated_declarations/src/inferrer.rs b/crates/oxc_isolated_declarations/src/inferrer.rs index 39245637ec9a23..874c8bfb9c8781 100644 --- a/crates/oxc_isolated_declarations/src/inferrer.rs +++ b/crates/oxc_isolated_declarations/src/inferrer.rs @@ -120,10 +120,6 @@ impl<'a> IsolatedDeclarations<'a> { return None; } - if function.r#async { - return None; - } - if function.expression { if let Some(Statement::ExpressionStatement(stmt)) = function.body.statements.first() { return self diff --git a/crates/oxc_isolated_declarations/tests/fixtures/async-function.ts b/crates/oxc_isolated_declarations/tests/fixtures/async-function.ts new file mode 100644 index 00000000000000..3927f10d1e10ed --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/async-function.ts @@ -0,0 +1,16 @@ +// Correct +async function asyncFunctionGood(): Promise {} +const asyncFunctionGoo2 = async (): Promise => { + return Promise.resolve(0); +} + + +// Need to explicit return type for async functions +// Incorrect +async function asyncFunction() { + return 42; +} + +const asyncFunction2 = async () => { + return "Hello, World!"; +} \ No newline at end of file diff --git a/crates/oxc_isolated_declarations/tests/fixtures/generator.ts b/crates/oxc_isolated_declarations/tests/fixtures/generator.ts new file mode 100644 index 00000000000000..c09739a260a9ea --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/fixtures/generator.ts @@ -0,0 +1,10 @@ +// Correct +function *generatorGood(): Generator {} + + +// Need to explicit return type for async functions +// Incorrect +function *generatorGoodBad() { + yield 50; + return 42; +} \ No newline at end of file diff --git a/crates/oxc_isolated_declarations/tests/snapshots/async-function.snap b/crates/oxc_isolated_declarations/tests/snapshots/async-function.snap new file mode 100644 index 00000000000000..3af230361db6d8 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/async-function.snap @@ -0,0 +1,31 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/async-function.ts +--- +==================== .D.TS ==================== + +declare function asyncFunctionGood(): Promise; +declare const asyncFunctionGoo2: () => Promise; +declare function asyncFunction(); +declare const asyncFunction2: unknown; + + +==================== Errors ==================== + + x TS9007: Function must have an explicit return type annotation with + | --isolatedDeclarations. + ,-[10:16] + 9 | // Incorrect + 10 | async function asyncFunction() { + : ^^^^^^^^^^^^^ + 11 | return 42; + `---- + + x TS9007: Function must have an explicit return type annotation with + | --isolatedDeclarations. + ,-[14:30] + 13 | + 14 | const asyncFunction2 = async () => { + : ^^^^^^^ + 15 | return "Hello, World!"; + `---- diff --git a/crates/oxc_isolated_declarations/tests/snapshots/generator.snap b/crates/oxc_isolated_declarations/tests/snapshots/generator.snap new file mode 100644 index 00000000000000..b72acbc6c276c2 --- /dev/null +++ b/crates/oxc_isolated_declarations/tests/snapshots/generator.snap @@ -0,0 +1,20 @@ +--- +source: crates/oxc_isolated_declarations/tests/mod.rs +input_file: crates/oxc_isolated_declarations/tests/fixtures/generator.ts +--- +==================== .D.TS ==================== + +declare function generatorGood(): Generator; +declare function generatorGoodBad(); + + +==================== Errors ==================== + + x TS9007: Function must have an explicit return type annotation with + | --isolatedDeclarations. + ,-[7:11] + 6 | // Incorrect + 7 | function *generatorGoodBad() { + : ^^^^^^^^^^^^^^^^ + 8 | yield 50; + `----