Skip to content

Commit 5b20717

Browse files
committed
feat: don't crash on unsupported syntax
[converter][web]
1 parent ed37502 commit 5b20717

20 files changed

+68
-105
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
261261
interface end
262262
```
263263

264+
* Don't crash on unsupported syntax, instead we log the warning and continue the process. If needed, we default to `obj`
265+
264266
## 0.4.0 - 2024-01-08
265267

266268
### Changed

src/Glutinum.Converter.CLI/Program.fs

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ EXAMPLES
2727
glutinum ./node_modules/my-lib/index.d.ts
2828
"""
2929

30-
printfn $"%s{helpText}"
30+
Log.log $"%s{helpText}"
3131

3232
let private getVersion () =
3333
emitJsStatement
@@ -55,12 +55,12 @@ let main (argv: string array) =
5555
| "--version" :: [] ->
5656
let version = getVersion ()
5757

58-
printfn $"%s{version}"
58+
Log.log $"%s{version}"
5959
0
6060

6161
| input :: "--out-file" :: outFile :: [] ->
6262

63-
printfn "Generating binding file for %s" input
63+
Log.info $"Generating binding file for %s{input}"
6464
let res = generateBindingFile input
6565

6666
let outFileDir = path.dirname (outFile)
@@ -75,15 +75,15 @@ let main (argv: string array) =
7575
0
7676

7777
| input :: [] ->
78-
printfn "Generating binding file for %s" input
78+
Log.info $"Generating binding file for %s{input}"
7979
let res = generateBindingFile input
8080

81-
printfn "Generation result:\n%s" res
81+
Log.log $"Generation result:\n%s{res}"
8282

8383
Log.success "Success!"
8484

8585
0
8686
| _ ->
87-
printfn "Invalid arguments"
87+
Log.error "Invalid arguments"
8888
printHelp ()
8989
1

src/Glutinum.Converter/Generate.fs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ let generateBindingFile (filePath: string) =
3030

3131
let readerResult = Read.readSourceFile checker sourceFile
3232

33+
for warning in readerResult.Warnings do
34+
Log.warn warning
35+
3336
let res = Transform.transform true readerResult.GlueAST
3437

3538
let outFile =

src/Glutinum.Converter/Log.fs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ open Fable.Core
66
let success (text: string) =
77
JS.console.log (chalk.greenBright.Invoke text)
88

9+
let log (text: string) = JS.console.log text
10+
911
let info (text: string) =
10-
JS.console.log (chalk.blueBright.Invoke text)
12+
JS.console.info (chalk.blueBright.Invoke text)
1113

1214
let warn (text: string) =
13-
JS.console.log (chalk.yellowBright.Invoke text)
15+
JS.console.warn (chalk.yellowBright.Invoke text)
1416

1517
let error (text: string) =
16-
JS.console.log (chalk.redBright.Invoke text)
17-
18-
let debug (text: string) = JS.console.log text
18+
JS.console.error (chalk.redBright.Invoke text)

src/Glutinum.Converter/Reader/Declaration.fs

+1-2
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,4 @@ let readDeclaration
164164
"declaration"
165165
$"Unsupported kind %A{declaration.kind}"
166166
declaration
167-
|> TypeScriptReaderException
168-
|> raise
167+
|> failwith

src/Glutinum.Converter/Reader/EnumDeclaration.fs

+11-11
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ let private readEnumMembers
3838
| GlueLiteral.Float _ as value -> value
3939
| GlueLiteral.Null
4040
| GlueLiteral.Bool _ ->
41+
failwith (
42+
generateReaderError
43+
"enum member"
44+
"Boolean and null literals are not supported in enums"
45+
initializer
46+
)
47+
48+
| None ->
49+
failwith (
4150
generateReaderError
4251
"enum member"
43-
"Boolean literals are not supported in enums"
52+
"readEnumCases: Unsupported enum initializer"
4453
initializer
45-
|> TypeScriptReaderException
46-
|> raise
47-
48-
| None ->
49-
generateReaderError
50-
"enum member"
51-
"readEnumCases: Unsupported enum initializer"
52-
initializer
53-
|> TypeScriptReaderException
54-
|> raise
54+
)
5555

5656
let name = unbox<Ts.Identifier> enumMember.name
5757

src/Glutinum.Converter/Reader/FunctionDeclaration.fs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ let readFunctionDeclaration
2828
"function declaration"
2929
"Missing name"
3030
declaration
31-
|> TypeScriptReaderException
32-
|> raise
31+
|> failwith
3332

3433
{
3534
IsDeclared = isDeclared

src/Glutinum.Converter/Reader/IndexedAccessType.fs

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ let readIndexedAccessType
2727

2828
reader.Warnings.Add warning
2929

30-
printfn "%s" warning
31-
3230
GlueType.Discard
3331

3432
GlueType.IndexedAccessType typ

src/Glutinum.Converter/Reader/Node.fs

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ let readNode (reader: ITypeScriptReader) (node: Ts.Node) : GlueType =
3939
$"Unsupported node kind %A{unsupported}"
4040
node
4141

42-
printfn "Warning: %s" warning
43-
4442
reader.Warnings.Add warning
4543

4644
GlueType.Discard

src/Glutinum.Converter/Reader/TypeNode.fs

+16-16
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ let readTypeNode
4242
"type node"
4343
"Missing symbol"
4444
typeReferenceNode
45-
|> TypeScriptReaderException
46-
|> raise
45+
|> failwith
4746
| Some symbol -> checker.getFullyQualifiedName symbol
4847

4948
// Could this detect false positive, if the library defined
@@ -214,12 +213,12 @@ let readTypeNode
214213
match tryReadLiteral literalExpression with
215214
| Some literal -> GlueType.Literal literal
216215
| None ->
217-
generateReaderError
218-
"type node - literal type"
219-
$"Could not read literal type"
220-
typeNode
221-
|> TypeScriptReaderException
222-
|> raise
216+
failwith (
217+
generateReaderError
218+
"type node - literal type"
219+
$"Could not read literal type"
220+
typeNode
221+
)
223222

224223
| Ts.SyntaxKind.ThisType ->
225224
let thisTypeNode = typeNode :?> Ts.ThisTypeNode
@@ -261,12 +260,12 @@ let readTypeNode
261260
else
262261
Some ForceAny
263262
| None ->
264-
generateReaderError
265-
"type node"
266-
"Missing declarations"
267-
typeNode
268-
|> TypeScriptReaderException
269-
|> raise
263+
failwith (
264+
generateReaderError
265+
"type node"
266+
"Missing declarations"
267+
typeNode
268+
)
270269
)
271270

272271
// We can't create a contract for some of the properties
@@ -324,5 +323,6 @@ let readTypeNode
324323
"type node"
325324
$"Unsupported kind %A{typeNode.kind}"
326325
typeNode
327-
|> TypeScriptReaderException
328-
|> raise
326+
|> reader.Warnings.Add
327+
328+
GlueType.Primitive GluePrimitive.Any

src/Glutinum.Converter/Reader/TypeOperatorNode.fs

+6-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ let readTypeOperatorNode
2525
"type operator (keyof)"
2626
"Missing symbol"
2727
node
28-
|> TypeScriptReaderException
29-
|> raise
28+
|> failwith
3029

3130
| Some symbol ->
3231
match symbol.declarations with
@@ -42,16 +41,14 @@ let readTypeOperatorNode
4241
"type operator (keyof)"
4342
"Missing declarations"
4443
node
45-
|> TypeScriptReaderException
46-
|> raise
44+
|> failwith
4745

4846
else
4947
Utils.generateReaderError
5048
"type operator (keyof)"
5149
$"Was expecting a type reference instead got a Node of type %A{node.``type``.kind}"
5250
node
53-
|> TypeScriptReaderException
54-
|> raise
51+
|> failwith
5552

5653
| Ts.SyntaxKind.ReadonlyKeyword -> reader.ReadTypeNode node.``type``
5754

@@ -60,5 +57,6 @@ let readTypeOperatorNode
6057
"type operator"
6158
$"Unsupported operator %A{node.operator}"
6259
node
63-
|> TypeScriptReaderException
64-
|> raise
60+
|> reader.Warnings.Add
61+
62+
GlueType.Primitive GluePrimitive.Any

src/Glutinum.Converter/Reader/Types.fs

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ open TypeScript
55
open Glutinum.Converter.GlueAST
66
open System.Collections.Generic
77

8-
exception TypeScriptReaderException of message: string
9-
108
[<Mangle>]
119
type ITypeScriptReader =
1210
abstract checker: Ts.TypeChecker with get

src/Glutinum.Converter/Reader/UnionTypeNode.fs

+12-12
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ let rec private readUnionTypeCases
6666
let symbol =
6767
Option.defaultWith
6868
(fun () ->
69-
generateReaderError
70-
"union type cases"
71-
"Missing symbol"
72-
typeReferenceNode
73-
|> TypeScriptReaderException
74-
|> raise
69+
failwith (
70+
generateReaderError
71+
"union type cases"
72+
"Missing symbol"
73+
typeReferenceNode
74+
)
7575
)
7676
symbolOpt
7777

@@ -121,12 +121,12 @@ let rec private readUnionTypeCases
121121
|> List.singleton
122122
|> Some
123123
| _ ->
124-
generateReaderError
125-
"union type cases"
126-
"Unsupported type reference reach a point where it was expected to have flags like Any"
127-
typeReferenceNode
128-
|> TypeScriptReaderException
129-
|> raise
124+
failwith (
125+
generateReaderError
126+
"union type cases"
127+
"Unsupported type reference reach a point where it was expected to have flags like Any"
128+
typeReferenceNode
129+
)
130130

131131
// else
132132
// symbol.declarations

src/Glutinum.Converter/Reader/VariableStatement.fs

+1-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ let readVariableStatement
3636
"variable statement"
3737
"Unable to read variable name"
3838
declaration
39-
|> TypeScriptReaderException
40-
|> raise
39+
|> failwith
4140

4241
({
4342
Name = name

src/Glutinum.Converter/Transform.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ let rec private transformType
286286
| GlueType.Partial _
287287
| GlueType.IntersectionType _
288288
| GlueType.FunctionDeclaration _ ->
289-
printfn "Could not transform type: %A" glueType
289+
Log.error $"Could not transform type: %A{glueType}"
290290
FSharpType.Discard
291291

292292
/// <summary></summary>

src/Glutinum.Web/Global/Types.fs

-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ module Glutinum.Web.Global.Types
33
[<RequireQualifiedAccess>]
44
type CompilationResult =
55
| Success of fsharpCode: string * warnings: string list
6-
| TypeScriptReaderException of string
76
| Error of string

src/Glutinum.Web/IssueGenerator.fs

+1-12
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@ let private compilationResultToText (result: CompilationResult) =
2626
else
2727
let warningsText =
2828
warnings
29-
|> List.map (fun warning ->
30-
printfn "%A" (warning.Split('\n'))
31-
32-
warning.Replace("\n", "\n> ")
33-
)
29+
|> List.map _.Replace("\n", "\n> ")
3430
|> String.concat "\n> ```\n> ```"
3531

3632
$"""**FSharp (with warnings)**
@@ -44,13 +40,6 @@ let private compilationResultToText (result: CompilationResult) =
4440
> %s{warningsText}
4541
> ```"""
4642

47-
| CompilationResult.TypeScriptReaderException error ->
48-
$"""**Reader failed**
49-
50-
```
51-
%s{error}
52-
```"""
53-
5443
| CompilationResult.Error error ->
5544
$"""**Error**
5645

src/Glutinum.Web/Pages/Editors.FSharpAST.fs

-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ open FSharpASTViewer
1616
[<RequireQualifiedAccess>]
1717
type CompilationResult =
1818
| Success of ast: FSharpType list * warnings: string list
19-
| TypeScriptReaderException of string
2019
| Error of string
2120

2221
type SuccessModel =
@@ -58,8 +57,6 @@ let private generateAST (typeScriptCode: string) =
5857
)
5958

6059
with
61-
| :? TypeScriptReaderException as error ->
62-
CompilationResult.TypeScriptReaderException error.message
6360

6461
| error ->
6562
Fable.Core.JS.console.log error
@@ -98,9 +95,6 @@ let update (msg: Msg) (model: Model) (currentTsCode: string) =
9895

9996
| CompilationResult.Error msg -> Errored msg, Cmd.none
10097

101-
| CompilationResult.TypeScriptReaderException msg ->
102-
Errored msg, Cmd.none
103-
10498
| Expand path ->
10599
match model with
106100
| Success data ->

src/Glutinum.Web/Pages/Editors.FSharpCode.fs

+1-6
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,7 @@ let private generateFile
9999
readerResult.Warnings |> Seq.toList
100100
)
101101

102-
with
103-
| :? TypeScriptReaderException as error ->
104-
CompilationResult.TypeScriptReaderException error.message
105-
106-
| error ->
102+
with error ->
107103
console.log error
108104

109105
CompilationResult.Error
@@ -175,7 +171,6 @@ let update (msg: Msg) (model: Model) (currentTsCode: string) =
175171

176172
updatedModel, cmd
177173

178-
| CompilationResult.TypeScriptReaderException message
179174
| CompilationResult.Error message ->
180175
let cmd =
181176
match result.Source with

0 commit comments

Comments
 (0)