Skip to content

Commit 6ddbbed

Browse files
authored
[TS] Added support for erasableSyntaxOnly in TypeScript (#4235)
1 parent 563b681 commit 6ddbbed

File tree

22 files changed

+486
-297
lines changed

22 files changed

+486
-297
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@
263263
"--outDir", "${workspaceRoot}/../fable-test",
264264
"--fableLib", "${workspaceRoot}/temp/fable-library-rust",
265265
"--exclude", "Fable.Core",
266-
"--lang", "Rust",
266+
"--lang", "TypeScript",
267267
"--noCache",
268268
"--noParallelTypeCheck"
269269
],

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* [TS] Added support for `erasableSyntaxOnly` in TypeScript (by @ncave)
1213
* [All] Added some default `System` exception implementations (by @ncave)
1314
* [All] Added `ofOption`/`toOption`/`ofValueOption`/`toValueOption` (by @ncave)
1415
* [Python] Added `Decorate` attribute to add Python decorators to classes (by @dbrattli)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* [TS] Added support for `erasableSyntaxOnly` in TypeScript (by @ncave)
1213
* [All] Added some default `System` exception implementations (by @ncave)
1314
* [All] Added `ofOption`/`toOption`/`ofValueOption`/`toValueOption` (by @ncave)
1415
* [Python] Added `Decorate` attribute to add Python decorators to classes (by @dbrattli)

src/Fable.Transforms/Fable2Babel.fs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,8 @@ module Annotation =
609609
| Fable.String -> StringTypeAnnotation
610610
| Fable.Regex -> makeAliasTypeAnnotation com ctx "RegExp"
611611
| Fable.Number(BigInt, _) -> makeAliasTypeAnnotation com ctx "bigint"
612+
| Fable.Number(Int32, Fable.NumberInfo.IsEnum ent) when ent.FullName = "System.DateTimeKind" ->
613+
makeFableLibImportTypeAnnotation com ctx [] "Util" "DateTimeKind"
612614
| Fable.Number(kind, _) -> makeNumericTypeAnnotation com ctx kind
613615
| Fable.Nullable(genArg, isStruct) -> makeNullableTypeAnnotation com ctx isStruct genArg
614616
| Fable.Option(genArg, isStruct) -> makeOptionTypeAnnotation com ctx isStruct genArg
@@ -1462,7 +1464,7 @@ module Util =
14621464
match e, typ with
14631465
| Literal(NumericLiteral(_)), _ -> e
14641466
// TODO: Unsigned ints seem to cause problems, should we check only Int32 here?
1465-
| _, Fable.Number((Int8 | Int16 | Int32), _) ->
1467+
| _, Fable.Number((Int8 | Int16 | Int32), Fable.NumberInfo.Empty) ->
14661468
Expression.binaryExpression (BinaryOrBitwise, e, Expression.numericLiteral (0.))
14671469
| _ -> e
14681470

@@ -3402,10 +3404,10 @@ but thanks to the optimisation done below we get
34023404
|> Seq.choose (fun ifc ->
34033405
match ifc.Entity.FullName with
34043406
// Discard non-generic versions of IEquatable & IComparable
3405-
| "System.IEquatable"
3407+
| Types.iequatable
34063408
| Types.iStructuralEquatable
34073409
| Types.iequalityComparer
3408-
| "System.IComparable"
3410+
| Types.icomparable
34093411
| Types.iStructuralComparable
34103412
| Types.ienumerable
34113413
| Types.ienumerator -> None
@@ -3812,10 +3814,37 @@ but thanks to the optimisation done below we get
38123814
Parameter.parameter ("fields", typeAnnotation = fieldsArgTa)
38133815
|]
38143816

3815-
let consArgsModifiers = [| Readonly; Readonly |]
3817+
let consArgsModifiers = [||]
38163818

3817-
let consBody = BlockStatement [| callSuperAsStatement [] |]
3818-
let classMembers = Array.append [| cases |] classMembers
3819+
let consBody =
3820+
BlockStatement
3821+
[|
3822+
callSuperAsStatement []
3823+
yield!
3824+
[ "tag"; "fields" ]
3825+
|> List.map (fun name ->
3826+
let left = get None thisExpr name
3827+
let right = Expression.identifier (name)
3828+
assign None left right |> ExpressionStatement
3829+
)
3830+
|]
3831+
3832+
let classMembers =
3833+
[|
3834+
ClassMember.classProperty (
3835+
Expression.identifier "tag",
3836+
typeAnnotation = tagArgTa,
3837+
accessModifier = Readonly
3838+
)
3839+
3840+
ClassMember.classProperty (
3841+
Expression.identifier "fields",
3842+
typeAnnotation = fieldsArgTa,
3843+
accessModifier = Readonly
3844+
)
3845+
cases
3846+
yield! classMembers
3847+
|]
38193848

38203849
let unionConsTypeParams =
38213850
Some(

src/Fable.Transforms/Replacements.fs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,13 +3148,9 @@ let dateTime (com: ICompiler) (ctx: Context) r t (i: CallInfo) (thisArg: Expr op
31483148
| "ToString" ->
31493149
Helper.LibCall(com, "Date", "toString", t, args, i.SignatureArgTypes, ?thisArg = thisArg, ?loc = r)
31503150
|> Some
3151-
// | "get_Kind" ->
3152-
// Helper.LibCall(com, moduleName, "kind", t, [ thisArg.Value ], [ thisArg.Value.Type ], ?loc = r)
3153-
// |> Some
3154-
// let y = DateTime.Now.UtcTicks
3155-
// let x = DateTimeOffset.Now.UtcTicks
3156-
// failwith "Not implemented"
3157-
3151+
| "get_Kind" ->
3152+
Helper.LibCall(com, "Date", "getKind", t, [ thisArg.Value ], [ thisArg.Value.Type ], ?loc = r)
3153+
|> Some
31583154
| "get_Ticks" ->
31593155
Helper.LibCall(com, "Date", "getTicks", t, [ thisArg.Value ], [ thisArg.Value.Type ], ?loc = r)
31603156
|> Some

src/Fable.Transforms/Transforms.Util.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ module Types =
420420
[<Literal>]
421421
let icomparableGeneric = "System.IComparable`1"
422422

423+
[<Literal>]
424+
let iequatable = "System.IEquatable"
425+
423426
[<Literal>]
424427
let icomparable = "System.IComparable"
425428

src/fable-library-ts/Char.ts

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,40 @@ function getCategoryFunc() {
3333
};
3434
}
3535

36-
export const enum UnicodeCategory {
37-
UppercaseLetter,
38-
LowercaseLetter,
39-
TitlecaseLetter,
40-
ModifierLetter,
41-
OtherLetter,
42-
NonSpacingMark,
43-
SpacingCombiningMark,
44-
EnclosingMark,
45-
DecimalDigitNumber,
46-
LetterNumber,
47-
OtherNumber,
48-
SpaceSeparator,
49-
LineSeparator,
50-
ParagraphSeparator,
51-
Control,
52-
Format,
53-
Surrogate,
54-
PrivateUse,
55-
ConnectorPunctuation,
56-
DashPunctuation,
57-
OpenPunctuation,
58-
ClosePunctuation,
59-
InitialQuotePunctuation,
60-
FinalQuotePunctuation,
61-
OtherPunctuation,
62-
MathSymbol,
63-
CurrencySymbol,
64-
ModifierSymbol,
65-
OtherSymbol,
66-
OtherNotAssigned,
67-
}
36+
export const UnicodeCategory = {
37+
UppercaseLetter: 0,
38+
LowercaseLetter: 1,
39+
TitlecaseLetter: 2,
40+
ModifierLetter: 3,
41+
OtherLetter: 4,
42+
NonSpacingMark: 5,
43+
SpacingCombiningMark: 6,
44+
EnclosingMark: 7,
45+
DecimalDigitNumber: 8,
46+
LetterNumber: 9,
47+
OtherNumber: 10,
48+
SpaceSeparator: 11,
49+
LineSeparator: 12,
50+
ParagraphSeparator: 13,
51+
Control: 14,
52+
Format: 15,
53+
Surrogate: 16,
54+
PrivateUse: 17,
55+
ConnectorPunctuation: 18,
56+
DashPunctuation: 19,
57+
OpenPunctuation: 20,
58+
ClosePunctuation: 21,
59+
InitialQuotePunctuation: 22,
60+
FinalQuotePunctuation: 23,
61+
OtherPunctuation: 24,
62+
MathSymbol: 25,
63+
CurrencySymbol: 26,
64+
ModifierSymbol: 27,
65+
OtherSymbol: 28,
66+
OtherNotAssigned: 29,
67+
} as const;
68+
69+
export type UnicodeCategory = typeof UnicodeCategory[keyof typeof UnicodeCategory];
6870

6971
const isControlMask = 1 << UnicodeCategory.Control;
7072
const isDigitMask = 1 << UnicodeCategory.DecimalDigitNumber;

0 commit comments

Comments
 (0)