Skip to content

Commit

Permalink
fix(parser): handle import types before function types
Browse files Browse the repository at this point in the history
This fixes the 'Expected =>, got :' error when parsing function parameters
with import type annotations. The issue occurred because the parser was
trying to parse import types as function types. By checking for import
types first in parse_ts_non_conditional_type, we ensure they are parsed
correctly.

Fixes #9802

Co-Authored-By: 강동윤 <kdy.1997.dev@gmail.com>
  • Loading branch information
devin-ai-integration[bot] and 강동윤 committed Dec 19, 2024
1 parent 3397257 commit 078c305
Show file tree
Hide file tree
Showing 3 changed files with 215 additions and 0 deletions.
5 changes: 5 additions & 0 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,11 @@ impl<I: Tokens> Parser<I> {

debug_assert!(self.input.syntax().typescript());

// Check for import types first
if is!(self, "import") {
return self.parse_ts_import_type().map(TsType::from).map(Box::new);
}

if self.is_ts_start_of_fn_type()? {
return self
.parse_ts_fn_or_constructor_type(true)
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_parser/tests/tsc/import-type-param/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function validate(input: string): import("typia").IValidation<import("typia").Primitive<number>> {
return null as any;
}
207 changes: 207 additions & 0 deletions crates/swc_ecma_parser/tests/tsc/import-type-param/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
{
"type": "Module",
"span": {
"start": 0,
"end": 102,
"ctxt": 0
},
"body": [
{
"type": "FunctionDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 9,
"end": 17,
"ctxt": 0
},
"value": "validate",
"optional": false
},
"declare": false,
"params": [
{
"type": "Parameter",
"span": {
"start": 18,
"end": 31,
"ctxt": 0
},
"decorators": [],
"pat": {
"type": "Identifier",
"span": {
"start": 18,
"end": 23,
"ctxt": 0
},
"value": "input",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 23,
"end": 31,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 25,
"end": 31,
"ctxt": 0
},
"kind": "string"
}
}
}
}
],
"span": {
"start": 0,
"end": 102,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 84,
"end": 102,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 90,
"end": 100,
"ctxt": 0
},
"argument": {
"type": "TsAsExpression",
"span": {
"start": 90,
"end": 100,
"ctxt": 0
},
"expression": {
"type": "NullLiteral",
"span": {
"start": 90,
"end": 94,
"ctxt": 0
}
},
"typeAnnotation": {
"type": "TsKeywordType",
"span": {
"start": 96,
"end": 100,
"ctxt": 0
},
"kind": "any"
}
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": {
"type": "TsTypeAnnotation",
"span": {
"start": 31,
"end": 83,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsImportType",
"span": {
"start": 33,
"end": 83,
"ctxt": 0
},
"argument": {
"type": "StringLiteral",
"span": {
"start": 40,
"end": 47,
"ctxt": 0
},
"value": "typia",
"raw": "\"typia\""
},
"qualifier": {
"type": "Identifier",
"span": {
"start": 48,
"end": 59,
"ctxt": 0
},
"value": "IValidation",
"optional": false
},
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 59,
"end": 83,
"ctxt": 0
},
"params": [
{
"type": "TsImportType",
"span": {
"start": 60,
"end": 82,
"ctxt": 0
},
"argument": {
"type": "StringLiteral",
"span": {
"start": 67,
"end": 74,
"ctxt": 0
},
"value": "typia",
"raw": "\"typia\""
},
"qualifier": {
"type": "Identifier",
"span": {
"start": 75,
"end": 84,
"ctxt": 0
},
"value": "Primitive",
"optional": false
},
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 75,
"end": 82,
"ctxt": 0
},
"params": [
{
"type": "TsKeywordType",
"span": {
"start": 76,
"end": 82,
"ctxt": 0
},
"kind": "number"
}
]
}
}
]
}
}
}
}
],
"interpreter": null
}

0 comments on commit 078c305

Please sign in to comment.