fix(isolated-declarations): should stripe async and generator keyword after transformed (#3790)

This commit is contained in:
Dunqing 2024-06-20 10:13:43 +00:00
parent dc8e2b8dc0
commit 7d47fc3fcc
6 changed files with 79 additions and 6 deletions

View file

@ -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,

View file

@ -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

View file

@ -0,0 +1,16 @@
// Correct
async function asyncFunctionGood(): Promise<number> {}
const asyncFunctionGoo2 = async (): Promise<number> => {
return Promise.resolve(0);
}
// Need to explicit return type for async functions
// Incorrect
async function asyncFunction() {
return 42;
}
const asyncFunction2 = async () => {
return "Hello, World!";
}

View file

@ -0,0 +1,10 @@
// Correct
function *generatorGood(): Generator<number> {}
// Need to explicit return type for async functions
// Incorrect
function *generatorGoodBad() {
yield 50;
return 42;
}

View file

@ -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<number>;
declare const asyncFunctionGoo2: () => Promise<number>;
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!";
`----

View file

@ -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<number>;
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;
`----