Skip to content

Commit 58f548a

Browse files
zhiyuuuuEx10si0n
andauthored
Add support to type alias (#205)
*Issue #, if available:* *Description of changes:* Please review the PR #175 , #178 , #192 first because of the dependency. This feature supports the semantics `type ID = string | number` By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice. --------- Co-authored-by: ex10si0n <[email protected]>
1 parent 6297366 commit 58f548a

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ Strata.code-workspace
99
conformance_testing/__pycache__
1010
conformance_testing/failures
1111

12-
test_single_file.sh
12+
test_single_file.sh
13+
14+
node_modules
15+
package-lock.json
16+
package.json

Strata/Languages/TypeScript/js_ast.lean

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ structure TS_TSTypeAnnotation extends BaseNode where
5757
typeAnnotation : Option TS_TSTypeKeyword
5858
deriving Repr, Lean.FromJson, Lean.ToJson
5959

60+
structure TS_TSUnionType extends BaseNode where
61+
types : Array TS_TSTypeKeyword
62+
deriving Repr, Lean.FromJson, Lean.ToJson
63+
6064
structure TS_Identifier extends BaseNode where
6165
name : String
6266
typeAnnotation : Option TS_TSTypeAnnotation
@@ -125,6 +129,7 @@ mutual
125129
| TS_BreakStatement : TS_BreakStatement → TS_Statement
126130
| TS_SwitchStatement : TS_SwitchStatement → TS_Statement
127131
| TS_ContinueStatement: TS_ContinueStatement → TS_Statement
132+
| TS_TypeAliasDeclaration : TS_TypeAliasDeclaration → TS_Statement
128133
deriving Repr, Lean.FromJson, Lean.ToJson
129134

130135
inductive TS_AssignmentIdentifier where
@@ -289,6 +294,12 @@ mutual
289294
discriminant : TS_Expression
290295
cases : Array TS_SwitchCase
291296
deriving Repr, Lean.FromJson, Lean.ToJson
297+
298+
structure TS_TypeAliasDeclaration extends BaseNode where
299+
id : TS_Identifier
300+
typeAnnotation : TS_TSUnionType
301+
deriving Repr, Lean.FromJson, Lean.ToJson
302+
292303
end
293304

294305
instance : Inhabited TS_Expression where
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type ID = string | number;
2+
3+
// Variable declarations using the alias
4+
let userId: ID = 101;
5+
let orderId: ID = "ORD_2025";

conformance_testing/babel_to_lean.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def parse_ts_type(j):
3838
return {"TS_TSArrayType": inner}
3939
elif t == "TSAnyKeyword" or t is None:
4040
return None
41+
elif t == "TSUnionType":
42+
return None
4143
else:
4244
print("Unsupported type annotation type: " + str(t), file=sys.stderr)
4345
return None
@@ -334,6 +336,24 @@ def parse_break_statement(j):
334336
add_missing_node_info(j, target_j)
335337
return target_j
336338

339+
def parse_type_alias_declaration(j):
340+
union_node = j["typeAnnotation"]
341+
342+
inner_types = union_node.get("types", [])
343+
tagged_types = [parse_ts_type(tnode) for tnode in inner_types]
344+
345+
union_struct = {
346+
"types": tagged_types
347+
}
348+
add_missing_node_info(union_node, union_struct)
349+
350+
target_j = {
351+
"id": parse_identifier(j["id"]),
352+
"typeAnnotation": union_struct,
353+
}
354+
add_missing_node_info(j, target_j)
355+
return target_j
356+
337357
def parse_for_statement(j):
338358
target_body = parse_statement(j['body'])
339359
target_j = {
@@ -380,6 +400,8 @@ def parse_statement(j):
380400
# case "ForInStatement":
381401
# case "ForOfStatement":
382402
# case "ClassDeclaration":
403+
case "TSTypeAliasDeclaration":
404+
return {"TS_TypeAliasDeclaration": parse_type_alias_declaration(j)}
383405
case _:
384406
print("Unsupported statement type: " + j['type'], file=sys.stderr)
385407
return j

0 commit comments

Comments
 (0)