Skip to content

Commit

Permalink
feat: don't crash on unsupported syntax
Browse files Browse the repository at this point in the history
[converter][web]
  • Loading branch information
MangelMaxime committed Mar 30, 2024
1 parent ed37502 commit 5b20717
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 105 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
interface end
```

* Don't crash on unsupported syntax, instead we log the warning and continue the process. If needed, we default to `obj`

## 0.4.0 - 2024-01-08

### Changed
Expand Down
12 changes: 6 additions & 6 deletions src/Glutinum.Converter.CLI/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ EXAMPLES
glutinum ./node_modules/my-lib/index.d.ts
"""

printfn $"%s{helpText}"
Log.log $"%s{helpText}"

let private getVersion () =
emitJsStatement
Expand Down Expand Up @@ -55,12 +55,12 @@ let main (argv: string array) =
| "--version" :: [] ->
let version = getVersion ()

printfn $"%s{version}"
Log.log $"%s{version}"
0

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

printfn "Generating binding file for %s" input
Log.info $"Generating binding file for %s{input}"
let res = generateBindingFile input

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

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

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

Log.success "Success!"

0
| _ ->
printfn "Invalid arguments"
Log.error "Invalid arguments"
printHelp ()
1
3 changes: 3 additions & 0 deletions src/Glutinum.Converter/Generate.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ let generateBindingFile (filePath: string) =

let readerResult = Read.readSourceFile checker sourceFile

for warning in readerResult.Warnings do
Log.warn warning

let res = Transform.transform true readerResult.GlueAST

let outFile =
Expand Down
10 changes: 5 additions & 5 deletions src/Glutinum.Converter/Log.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ open Fable.Core
let success (text: string) =
JS.console.log (chalk.greenBright.Invoke text)

let log (text: string) = JS.console.log text

let info (text: string) =
JS.console.log (chalk.blueBright.Invoke text)
JS.console.info (chalk.blueBright.Invoke text)

let warn (text: string) =
JS.console.log (chalk.yellowBright.Invoke text)
JS.console.warn (chalk.yellowBright.Invoke text)

let error (text: string) =
JS.console.log (chalk.redBright.Invoke text)

let debug (text: string) = JS.console.log text
JS.console.error (chalk.redBright.Invoke text)
3 changes: 1 addition & 2 deletions src/Glutinum.Converter/Reader/Declaration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,4 @@ let readDeclaration
"declaration"
$"Unsupported kind %A{declaration.kind}"
declaration
|> TypeScriptReaderException
|> raise
|> failwith
22 changes: 11 additions & 11 deletions src/Glutinum.Converter/Reader/EnumDeclaration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ let private readEnumMembers
| GlueLiteral.Float _ as value -> value
| GlueLiteral.Null
| GlueLiteral.Bool _ ->
failwith (
generateReaderError
"enum member"
"Boolean and null literals are not supported in enums"
initializer
)

| None ->
failwith (
generateReaderError
"enum member"
"Boolean literals are not supported in enums"
"readEnumCases: Unsupported enum initializer"
initializer
|> TypeScriptReaderException
|> raise

| None ->
generateReaderError
"enum member"
"readEnumCases: Unsupported enum initializer"
initializer
|> TypeScriptReaderException
|> raise
)

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

Expand Down
3 changes: 1 addition & 2 deletions src/Glutinum.Converter/Reader/FunctionDeclaration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ let readFunctionDeclaration
"function declaration"
"Missing name"
declaration
|> TypeScriptReaderException
|> raise
|> failwith

{
IsDeclared = isDeclared
Expand Down
2 changes: 0 additions & 2 deletions src/Glutinum.Converter/Reader/IndexedAccessType.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ let readIndexedAccessType

reader.Warnings.Add warning

printfn "%s" warning

GlueType.Discard

GlueType.IndexedAccessType typ
2 changes: 0 additions & 2 deletions src/Glutinum.Converter/Reader/Node.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ let readNode (reader: ITypeScriptReader) (node: Ts.Node) : GlueType =
$"Unsupported node kind %A{unsupported}"
node

printfn "Warning: %s" warning

reader.Warnings.Add warning

GlueType.Discard
32 changes: 16 additions & 16 deletions src/Glutinum.Converter/Reader/TypeNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ let readTypeNode
"type node"
"Missing symbol"
typeReferenceNode
|> TypeScriptReaderException
|> raise
|> failwith
| Some symbol -> checker.getFullyQualifiedName symbol

// Could this detect false positive, if the library defined
Expand Down Expand Up @@ -214,12 +213,12 @@ let readTypeNode
match tryReadLiteral literalExpression with
| Some literal -> GlueType.Literal literal
| None ->
generateReaderError
"type node - literal type"
$"Could not read literal type"
typeNode
|> TypeScriptReaderException
|> raise
failwith (
generateReaderError
"type node - literal type"
$"Could not read literal type"
typeNode
)

| Ts.SyntaxKind.ThisType ->
let thisTypeNode = typeNode :?> Ts.ThisTypeNode
Expand Down Expand Up @@ -261,12 +260,12 @@ let readTypeNode
else
Some ForceAny
| None ->
generateReaderError
"type node"
"Missing declarations"
typeNode
|> TypeScriptReaderException
|> raise
failwith (
generateReaderError
"type node"
"Missing declarations"
typeNode
)
)

// We can't create a contract for some of the properties
Expand Down Expand Up @@ -324,5 +323,6 @@ let readTypeNode
"type node"
$"Unsupported kind %A{typeNode.kind}"
typeNode
|> TypeScriptReaderException
|> raise
|> reader.Warnings.Add

GlueType.Primitive GluePrimitive.Any
14 changes: 6 additions & 8 deletions src/Glutinum.Converter/Reader/TypeOperatorNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ let readTypeOperatorNode
"type operator (keyof)"
"Missing symbol"
node
|> TypeScriptReaderException
|> raise
|> failwith

| Some symbol ->
match symbol.declarations with
Expand All @@ -42,16 +41,14 @@ let readTypeOperatorNode
"type operator (keyof)"
"Missing declarations"
node
|> TypeScriptReaderException
|> raise
|> failwith

else
Utils.generateReaderError
"type operator (keyof)"
$"Was expecting a type reference instead got a Node of type %A{node.``type``.kind}"
node
|> TypeScriptReaderException
|> raise
|> failwith

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

Expand All @@ -60,5 +57,6 @@ let readTypeOperatorNode
"type operator"
$"Unsupported operator %A{node.operator}"
node
|> TypeScriptReaderException
|> raise
|> reader.Warnings.Add

GlueType.Primitive GluePrimitive.Any
2 changes: 0 additions & 2 deletions src/Glutinum.Converter/Reader/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ open TypeScript
open Glutinum.Converter.GlueAST
open System.Collections.Generic

exception TypeScriptReaderException of message: string

[<Mangle>]
type ITypeScriptReader =
abstract checker: Ts.TypeChecker with get
Expand Down
24 changes: 12 additions & 12 deletions src/Glutinum.Converter/Reader/UnionTypeNode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ let rec private readUnionTypeCases
let symbol =
Option.defaultWith
(fun () ->
generateReaderError
"union type cases"
"Missing symbol"
typeReferenceNode
|> TypeScriptReaderException
|> raise
failwith (
generateReaderError
"union type cases"
"Missing symbol"
typeReferenceNode
)
)
symbolOpt

Expand Down Expand Up @@ -121,12 +121,12 @@ let rec private readUnionTypeCases
|> List.singleton
|> Some
| _ ->
generateReaderError
"union type cases"
"Unsupported type reference reach a point where it was expected to have flags like Any"
typeReferenceNode
|> TypeScriptReaderException
|> raise
failwith (
generateReaderError
"union type cases"
"Unsupported type reference reach a point where it was expected to have flags like Any"
typeReferenceNode
)

// else
// symbol.declarations
Expand Down
3 changes: 1 addition & 2 deletions src/Glutinum.Converter/Reader/VariableStatement.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ let readVariableStatement
"variable statement"
"Unable to read variable name"
declaration
|> TypeScriptReaderException
|> raise
|> failwith

({
Name = name
Expand Down
2 changes: 1 addition & 1 deletion src/Glutinum.Converter/Transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ let rec private transformType
| GlueType.Partial _
| GlueType.IntersectionType _
| GlueType.FunctionDeclaration _ ->
printfn "Could not transform type: %A" glueType
Log.error $"Could not transform type: %A{glueType}"
FSharpType.Discard

/// <summary></summary>
Expand Down
1 change: 0 additions & 1 deletion src/Glutinum.Web/Global/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ module Glutinum.Web.Global.Types
[<RequireQualifiedAccess>]
type CompilationResult =
| Success of fsharpCode: string * warnings: string list
| TypeScriptReaderException of string
| Error of string
13 changes: 1 addition & 12 deletions src/Glutinum.Web/IssueGenerator.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ let private compilationResultToText (result: CompilationResult) =
else
let warningsText =
warnings
|> List.map (fun warning ->
printfn "%A" (warning.Split('\n'))

warning.Replace("\n", "\n> ")
)
|> List.map _.Replace("\n", "\n> ")
|> String.concat "\n> ```\n> ```"

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

| CompilationResult.TypeScriptReaderException error ->
$"""**Reader failed**
```
%s{error}
```"""

| CompilationResult.Error error ->
$"""**Error**
Expand Down
6 changes: 0 additions & 6 deletions src/Glutinum.Web/Pages/Editors.FSharpAST.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ open FSharpASTViewer
[<RequireQualifiedAccess>]
type CompilationResult =
| Success of ast: FSharpType list * warnings: string list
| TypeScriptReaderException of string
| Error of string

type SuccessModel =
Expand Down Expand Up @@ -58,8 +57,6 @@ let private generateAST (typeScriptCode: string) =
)

with
| :? TypeScriptReaderException as error ->
CompilationResult.TypeScriptReaderException error.message

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

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

| CompilationResult.TypeScriptReaderException msg ->
Errored msg, Cmd.none

| Expand path ->
match model with
| Success data ->
Expand Down
7 changes: 1 addition & 6 deletions src/Glutinum.Web/Pages/Editors.FSharpCode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ let private generateFile
readerResult.Warnings |> Seq.toList
)

with
| :? TypeScriptReaderException as error ->
CompilationResult.TypeScriptReaderException error.message

| error ->
with error ->
console.log error

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

updatedModel, cmd

| CompilationResult.TypeScriptReaderException message
| CompilationResult.Error message ->
let cmd =
match result.Source with
Expand Down
Loading

0 comments on commit 5b20717

Please sign in to comment.