diff --git a/.changeset/flat-poets-beam.md b/.changeset/flat-poets-beam.md new file mode 100644 index 0000000..a37ada5 --- /dev/null +++ b/.changeset/flat-poets-beam.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/acorn-typescript': patch +--- + +fix: correctly parse async arrow functions with generics diff --git a/src/index.ts b/src/index.ts index 2fb8d40..c3a2e61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -430,7 +430,7 @@ export function tsPlugin(options?: { return ( base.type === 'Identifier' && base.name === 'async' && - this.lastTokEndLoc.column === base.end && + this.lastTokEnd === base.end && !this.canInsertSemicolon() && base.end - base.start === 5 && base.start === this.potentialArrowAt @@ -3228,7 +3228,9 @@ export function tsPlugin(options?: { return node.expression; } - toAssignableList(exprList: any[], isBinding: boolean): any { + toAssignableList(exprList: any[] | null, isBinding: boolean): any { + if (!exprList) exprList = []; + for (let i = 0; i < exprList.length; i++) { const expr = exprList[i]; @@ -4163,7 +4165,7 @@ export function tsPlugin(options?: { override = modified.override; readonly = modified.readonly; if (allowModifiers === false && (accessibility || readonly || override)) { - this.raise(startLoc.start, TypeScriptError.UnexpectedParameterModifier); + this.raise(startLoc.column, TypeScriptError.UnexpectedParameterModifier); } } diff --git a/src/middleware.ts b/src/middleware.ts index 6c7be08..0b4bc8a 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -47,11 +47,11 @@ export declare class AcornParseClass extends Parser { scopeStack: any[]; inModule: any; undefinedExports: any; - lastTokEndLoc: any; - lastTokStartLoc: any; + lastTokEndLoc: Position; + lastTokStartLoc: Position; context: any[]; - endLoc: any; - startLoc: any; + endLoc: Position; + startLoc: Position; potentialArrowInForAwait: boolean; type: TokenType & Record; start: number; diff --git a/test/arrow-function_type_test_async_generic_after_import/expected.json b/test/arrow-function_type_test_async_generic_after_import/expected.json new file mode 100644 index 0000000..48e4f9c --- /dev/null +++ b/test/arrow-function_type_test_async_generic_after_import/expected.json @@ -0,0 +1,170 @@ +{ + "type": "Program", + "start": 0, + "end": 73, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "body": [ + { + "type": "ImportDeclaration", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "importKind": "value", + "specifiers": [], + "source": { + "type": "Literal", + "start": 15, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "value": "./config", + "raw": "'./config'" + } + }, + { + "type": "VariableDeclaration", + "start": 27, + "end": 73, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 33, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 45 + } + }, + "id": { + "type": "Identifier", + "start": 33, + "end": 52, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "name": "loadDataWithGeneric" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 55, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 45 + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start": 61, + "end": 64, + "loc": { + "start": { + "line": 3, + "column": 34 + }, + "end": { + "line": 3, + "column": 37 + } + }, + "params": [ + { + "type": "TSTypeParameter", + "start": 62, + "end": 63, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "name": "T" + } + ] + }, + "params": [], + "id": null, + "expression": false, + "generator": false, + "async": true, + "body": { + "type": "BlockStatement", + "start": 70, + "end": 72, + "loc": { + "start": { + "line": 3, + "column": 43 + }, + "end": { + "line": 3, + "column": 45 + } + }, + "body": [] + } + } + } + ], + "kind": "const" + } + ], + "sourceType": "module" +} diff --git a/test/arrow-function_type_test_async_generic_after_import/input.ts b/test/arrow-function_type_test_async_generic_after_import/input.ts new file mode 100644 index 0000000..e3d493a --- /dev/null +++ b/test/arrow-function_type_test_async_generic_after_import/input.ts @@ -0,0 +1,3 @@ +import {} from './config' + +const loadDataWithGeneric = async () => {}; \ No newline at end of file diff --git a/test/arrow-function_type_test_async_generic_empty_params/expected.json b/test/arrow-function_type_test_async_generic_empty_params/expected.json new file mode 100644 index 0000000..09a27c8 --- /dev/null +++ b/test/arrow-function_type_test_async_generic_empty_params/expected.json @@ -0,0 +1,136 @@ +{ + "type": "Program", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 46, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 46 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "name": "loadDataWithGeneric" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 28, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 28 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start": 34, + "end": 37, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 37 + } + }, + "params": [ + { + "type": "TSTypeParameter", + "start": 35, + "end": 36, + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 36 + } + }, + "name": "T" + } + ] + }, + "params": [], + "id": null, + "expression": false, + "generator": false, + "async": true, + "body": { + "type": "BlockStatement", + "start": 43, + "end": 45, + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "body": [] + } + } + } + ], + "kind": "const" + } + ], + "sourceType": "module" +} diff --git a/test/arrow-function_type_test_async_generic_empty_params/input.ts b/test/arrow-function_type_test_async_generic_empty_params/input.ts new file mode 100644 index 0000000..a2182f8 --- /dev/null +++ b/test/arrow-function_type_test_async_generic_empty_params/input.ts @@ -0,0 +1 @@ +const loadDataWithGeneric = async () => {}; \ No newline at end of file diff --git a/test/arrow-function_type_test_async_generic_with_commented_import/expected.json b/test/arrow-function_type_test_async_generic_with_commented_import/expected.json new file mode 100644 index 0000000..601b002 --- /dev/null +++ b/test/arrow-function_type_test_async_generic_with_commented_import/expected.json @@ -0,0 +1,136 @@ +{ + "type": "Program", + "start": 0, + "end": 76, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 30, + "end": 76, + "loc": { + "start": { + "line": 3, + "column": 0 + }, + "end": { + "line": 3, + "column": 46 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 36, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 45 + } + }, + "id": { + "type": "Identifier", + "start": 36, + "end": 55, + "loc": { + "start": { + "line": 3, + "column": 6 + }, + "end": { + "line": 3, + "column": 25 + } + }, + "name": "loadDataWithGeneric" + }, + "init": { + "type": "ArrowFunctionExpression", + "start": 58, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 28 + }, + "end": { + "line": 3, + "column": 45 + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "start": 64, + "end": 67, + "loc": { + "start": { + "line": 3, + "column": 34 + }, + "end": { + "line": 3, + "column": 37 + } + }, + "params": [ + { + "type": "TSTypeParameter", + "start": 65, + "end": 66, + "loc": { + "start": { + "line": 3, + "column": 35 + }, + "end": { + "line": 3, + "column": 36 + } + }, + "name": "T" + } + ] + }, + "params": [], + "id": null, + "expression": false, + "generator": false, + "async": true, + "body": { + "type": "BlockStatement", + "start": 73, + "end": 75, + "loc": { + "start": { + "line": 3, + "column": 43 + }, + "end": { + "line": 3, + "column": 45 + } + }, + "body": [] + } + } + } + ], + "kind": "const" + } + ], + "sourceType": "module" +} diff --git a/test/arrow-function_type_test_async_generic_with_commented_import/input.ts b/test/arrow-function_type_test_async_generic_with_commented_import/input.ts new file mode 100644 index 0000000..f24af49 --- /dev/null +++ b/test/arrow-function_type_test_async_generic_with_commented_import/input.ts @@ -0,0 +1,3 @@ +// import {} from './config' + +const loadDataWithGeneric = async () => {}; \ No newline at end of file