Skip to content

Commit

Permalink
Merge pull request #169 from NickDarvey/upgrade-fantomas
Browse files Browse the repository at this point in the history
Upgrades Fantomas from v5 to v6
  • Loading branch information
7sharp9 authored Sep 1, 2023
2 parents fb627b4 + 57ea834 commit c6ea173
Show file tree
Hide file tree
Showing 15 changed files with 1,298 additions and 1,256 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
Nuget package now has correct link to changelog
## Added
Readme added to Nuget package
## Changed
- Upgraded Fantomas.Core to 6.1.1.
- Changed namespace of Fantomas.FCS. (Open `Fantomas.FCS` instead of `open FSharp.Compiler`.)
- Changed type signatures in Fantomas.FCS. (Update code where affected.)
- Fantomas.FCS is more sensitive to missing ranges. (Include a `Some range0` where one is needed, instead of `None`.)

## [0.8.2]
### Changed
Expand Down
7 changes: 7 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,11 @@
<EnableSourceLink Condition=" 'Configuration' == 'Release' ">true</EnableSourceLink>
<NoWarn>$(NoWarn);FS2003;NU5128</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\README.md">
<Pack>true</Pack>
<PackagePath></PackagePath>
<Visible>true</Visible>
</None>
</ItemGroup>
</Project>
1,145 changes: 573 additions & 572 deletions paket.lock

Large diffs are not rendered by default.

36 changes: 24 additions & 12 deletions src/Myriad.Core/Ast.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ namespace Myriad.Core

open System
open System.IO
open FSharp.Compiler.Syntax
open Fantomas.FCS.Syntax
open Fantomas.Core
open FSharp.Compiler.Text.Range
open FSharp.Compiler.Xml
open FSharp.Compiler.SyntaxTrivia
open Fantomas.FCS.Text.Range
open Fantomas.FCS.Xml
open Fantomas.FCS.SyntaxTrivia

module DynamicReflection =
open System.Reflection
Expand Down Expand Up @@ -170,7 +170,7 @@ module Ast =
]

[ match ast with
| ParsedInput.ImplFile(ParsedImplFileInput(_name, _isScript, _qualifiedNameOfFile, _scopedPragmas, _hashDirectives, modules, _g, _)) ->
| ParsedInput.ImplFile(ParsedImplFileInput(_name, _isScript, _qualifiedNameOfFile, _scopedPragmas, _hashDirectives, modules, _g, _, _)) ->
for SynModuleOrNamespace(namespaceId, _isRec, _isModule, moduleDecls, _preXmlDoc, _attributes, _access, _, _) as ns in modules do
yield! extractTypes moduleDecls namespaceId
| _ -> () ]
Expand Down Expand Up @@ -209,7 +209,7 @@ module Ast =

let modulesWithAttribute<'a> (ast: ParsedInput) =
[ match ast with
| ParsedInput.ImplFile (ParsedImplFileInput (_name, _isScript, _qualifiedNameOfFile, _scopedPragmas, _hashDirectives, modules, _g, _)) ->
| ParsedInput.ImplFile (ParsedImplFileInput (_name, _isScript, _qualifiedNameOfFile, _scopedPragmas, _hashDirectives, modules, _g, _, _)) ->
for SynModuleOrNamespace (_namespaceId, _isRec, moduleOrNs, _moduleDecls, _preXmlDoc, _attributes, _access, _range, _) as ns in modules do
if moduleOrNs.IsModule && hasAttribute<'a> ns then
yield ns
Expand Down Expand Up @@ -271,7 +271,7 @@ module Ast =
let modules = defaultArg modules []
let isLastCompiland = defaultArg isLastCompiland true
let isExe = defaultArg isExe false
ParsedImplFileInput(file, isScript, qualName, scopedPragmas, hashDirectives, modules, (isLastCompiland, isExe), ParsedImplFileInputTrivia.Zero)
ParsedImplFileInput(file, isScript, qualName, scopedPragmas, hashDirectives, modules, (isLastCompiland, isExe), ParsedImplFileInputTrivia.Zero, Set.empty)

type SynModuleOrNamespace with

Expand All @@ -281,7 +281,17 @@ module Ast =
let decls = defaultArg decls []
let docs = defaultArg docs PreXmlDoc.Empty
let attribs = defaultArg attribs SynAttributes.Empty
SynModuleOrNamespace(ident, isRecursive, kind, decls, docs, attribs, access, range, SynModuleOrNamespaceTrivia.Zero)
let keyword =
match kind with
| SynModuleOrNamespaceKind.NamedModule ->
SynModuleOrNamespaceLeadingKeyword.Module range0
| SynModuleOrNamespaceKind.DeclaredNamespace
| SynModuleOrNamespaceKind.GlobalNamespace ->
SynModuleOrNamespaceLeadingKeyword.Namespace range0
| SynModuleOrNamespaceKind.AnonModule ->
SynModuleOrNamespaceLeadingKeyword.None
let trivia = { SynModuleOrNamespaceTrivia.LeadingKeyword = keyword }
SynModuleOrNamespace(ident, isRecursive, kind, decls, docs, attribs, access, range, trivia)

static member CreateNamespace(ident: LongIdent, ?isRecursive: bool, ?decls: SynModuleDecl list, ?docs: PreXmlDoc, ?attribs: SynAttributeList list, ?access: SynAccess) =
SynModuleOrNamespace.Create(ident, SynModuleOrNamespaceKind.DeclaredNamespace, ?isRecursive = isRecursive, ?decls = decls, ?docs = docs, ?attribs = attribs, ?access =access)
Expand Down Expand Up @@ -331,7 +341,9 @@ module Ast =
let headPat = defaultArg pattern SynPat.CreateNull
let expr = defaultArg expr (SynExpr.CreateTyped(SynExpr.CreateNull, SynType.CreateUnit))
let bind = DebugPointAtBinding.NoneAtLet
let trivia = { LetKeyword = Some range0
// Do not use SynBindingTrivia.Zero because ASTTransformer expects values.
let trivia = { SynBindingTrivia.LeadingKeyword = SynLeadingKeyword.Let range0
InlineKeyword = if isInline then Some range0 else None
EqualsRange = Some range0 }
SynBinding.SynBinding(access, SynBindingKind.Normal, isInline, isMutable, attributes, xmldoc, valData, headPat, returnInfo, expr, range0, bind, trivia)

Expand All @@ -350,7 +362,7 @@ module Ast =
type SynBindingReturnInfo with
static member Create(typeName, ?attributes) =
let attributes = defaultArg attributes SynAttributes.Empty
SynBindingReturnInfo.SynBindingReturnInfo(typeName, range0, attributes)
SynBindingReturnInfo.SynBindingReturnInfo(typeName, range0, attributes, SynBindingReturnInfoTrivia.Zero)

type SynUnionCase with
member x.HasFields =
Expand All @@ -369,8 +381,8 @@ module Ast =
static member CreateLambda(pats: SynPat list, body: SynExpr, ?isMember: bool) =
let isMember = defaultArg isMember false
let compiler = System.Reflection.Assembly.Load("Fantomas.FCS")
let syntaxTreeOps = compiler.GetType("FSharp.Compiler.SyntaxTreeOps")
let synArgNameGenerator = compiler.GetType("FSharp.Compiler.SyntaxTreeOps+SynArgNameGenerator")
let syntaxTreeOps = compiler.GetType("Fantomas.FCS.SyntaxTreeOps")
let synArgNameGenerator = compiler.GetType("Fantomas.FCS.SyntaxTreeOps+SynArgNameGenerator")
let nameGen = synArgNameGenerator?``.ctor``()
syntaxTreeOps?mkSynFunMatchLambdas(nameGen, isMember, range0, pats, Some range0, body)

Expand Down
43 changes: 27 additions & 16 deletions src/Myriad.Core/AstExtensions.fs
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
namespace Myriad.Core

open System
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text.Range
open FSharp.Compiler.Text.Position
open FSharp.Compiler.Xml
open FSharp.Compiler.SyntaxTrivia
open Fantomas.FCS.Syntax
open Fantomas.FCS.Text.Range
open Fantomas.FCS.Text.Position
open Fantomas.FCS.Xml
open Fantomas.FCS.SyntaxTrivia

[<AutoOpen>]
module AstExtensions =
let private dotsOrCommas ls =
match ls with
| [] -> []
| xs -> [ for _ in 1..xs.Length - 1 do range0 ]

type ParsedImplFileInputTrivia with
static member Zero =
{ ParsedImplFileInputTrivia.ConditionalDirectives = []
CodeComments = [] }

type SynModuleOrNamespaceTrivia with
static member Zero =
{ SynModuleOrNamespaceTrivia.ModuleKeyword = Some range0
NamespaceKeyword = Some range0 }
{ SynModuleOrNamespaceTrivia.LeadingKeyword = SynModuleOrNamespaceLeadingKeyword.None }

type SynMemberDefnImplicitCtorTrivia with
static member Zero =
{ SynMemberDefnImplicitCtorTrivia.AsKeyword = Some range0 }

type SynBindingReturnInfoTrivia with
static member Zero =
{ SynBindingReturnInfoTrivia.ColonRange = Some range0 }

type Ident with
static member Create text =
Ident(text, range0)
Expand All @@ -27,7 +39,7 @@ module AstExtensions =

type SynLongIdent with
static member CreateFromLongIdent (longIdent: LongIdent) =
SynLongIdent(longIdent, [], List.replicate longIdent.Length None)
SynLongIdent(longIdent, dotsOrCommas longIdent, List.replicate longIdent.Length None)

static member Create (texts) =
SynLongIdent.CreateFromLongIdent (texts |> List.map Ident.Create)
Expand Down Expand Up @@ -69,8 +81,7 @@ module AstExtensions =
IsDispatchSlot = false
IsOverrideOrExplicitImpl = false
IsFinal = false
GetterOrSetterIsCompilerGenerated = false
Trivia = SynMemberFlagsTrivia.Zero }
GetterOrSetterIsCompilerGenerated = false }
static member StaticMember =
{ SynMemberFlags.InstanceMember with IsInstance = false }

Expand Down Expand Up @@ -106,7 +117,7 @@ module AstExtensions =
static member CreateParen expr =
SynExpr.Paren(expr, range0, Some range0, range0)
static member CreateTuple list =
SynExpr.Tuple(false, list, [], range0)
SynExpr.Tuple(false, list, dotsOrCommas list, range0)
static member CreateParenedTuple list =
SynExpr.CreateTuple list
|> SynExpr.CreateParen
Expand Down Expand Up @@ -154,7 +165,7 @@ module AstExtensions =
/// Creates : `instanceAndMethod<type1, type2,... type}>(args)`
static member CreateInstanceMethodCall(instanceAndMethod : SynLongIdent, instanceMethodsGenericTypes, args) =
let valueExpr = SynExpr.CreateLongIdent instanceAndMethod
let valueExprWithType = SynExpr.TypeApp(valueExpr, range0, instanceMethodsGenericTypes, [], None, range0, range0)
let valueExprWithType = SynExpr.TypeApp(valueExpr, range0, instanceMethodsGenericTypes, dotsOrCommas instanceMethodsGenericTypes, None, range0, range0)
SynExpr.CreateApp(valueExprWithType, args)
/// Creates: expr1; expr2; ... exprN
static member CreateSequential exprs =
Expand Down Expand Up @@ -182,7 +193,7 @@ module AstExtensions =

type SynType with
static member CreateApp (typ, args, ?isPostfix) =
SynType.App(typ, None, args, [], None, (defaultArg isPostfix false), range0)
SynType.App(typ, None, args, dotsOrCommas args, None, (defaultArg isPostfix false), range0)
static member CreateLongIdent id =
SynType.LongIdent(id)
static member CreateLongIdent s =
Expand Down Expand Up @@ -385,7 +396,7 @@ module AstExtensions =

type SynMemberDefn with
static member CreateImplicitCtor (ctorArgs : SynSimplePat list) =
SynMemberDefn.ImplicitCtor(None, SynAttributes.Empty, SynSimplePats.SimplePats(ctorArgs, range0), None, PreXmlDoc.Empty, range0 )
SynMemberDefn.ImplicitCtor(None, SynAttributes.Empty, SynSimplePats.SimplePats(ctorArgs, dotsOrCommas ctorArgs, range0), None, PreXmlDoc.Empty, range0, SynMemberDefnImplicitCtorTrivia.Zero)
static member CreateImplicitCtor() =
SynMemberDefn.CreateImplicitCtor []

Expand Down Expand Up @@ -536,7 +547,7 @@ module AstExtensions =

type SynSimplePats with
static member Create(patterns) =
SynSimplePats.SimplePats(patterns, range0)
SynSimplePats.SimplePats(patterns, dotsOrCommas patterns, range0)

type SynTypeDefn with
static member CreateFromRepr(name : Ident, repr : SynTypeDefnRepr, ?members : SynMemberDefns, ?xmldoc : PreXmlDoc) =
Expand All @@ -560,7 +571,7 @@ module AstExtensions =
static member Create(fieldType : SynType, ?name : Ident, ?attributes : SynAttributes, ?access : SynAccess, ?xmldoc : PreXmlDoc) =
let xmldoc = defaultArg xmldoc PreXmlDoc.Empty
let attributes = defaultArg attributes SynAttributes.Empty
SynField(attributes, false, name, fieldType, false, xmldoc, access, range0)
SynField(attributes, false, name, fieldType, false, xmldoc, access, range0, SynFieldTrivia.Zero)

type SynUnionCase with
static member Create(name : Ident, fields : SynField list, ?attributes : SynAttributes, ?access : SynAccess, ?xmldoc : PreXmlDoc) =
Expand Down
2 changes: 1 addition & 1 deletion src/Myriad.Core/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Myriad.Core

open System
open System.Collections.Generic
open FSharp.Compiler.Syntax
open Fantomas.FCS.Syntax

type MyriadGeneratorAttribute(name: string) =
inherit Attribute()
Expand Down
6 changes: 3 additions & 3 deletions src/Myriad.Plugins/DUCasesGenerator.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Myriad.Plugins

open FSharp.Compiler.Syntax
open FSharp.Compiler.SyntaxTrivia
open Fantomas.FCS.Syntax
open Fantomas.FCS.SyntaxTrivia
open Myriad.Core
open Myriad.Core.Ast

module internal CreateDUModule =
open FSharp.Compiler.Text.Range
open Fantomas.FCS.Text.Range

let createToString (requiresQualifiedAccess: bool) (parent: LongIdent) (cases: SynUnionCase list) =
let varIdent = SynLongIdent.CreateString "toString"
Expand Down
18 changes: 9 additions & 9 deletions src/Myriad.Plugins/FieldsGenerator.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
namespace Myriad.Plugins

open FSharp.Compiler.Syntax
open Fantomas.FCS.Syntax
open Myriad.Core
open Myriad.Core.Ast

module internal Create =
open FSharp.Compiler.Text.Range
open Fantomas.FCS.Text.Range

let createFieldMap (parent: LongIdent) (field: SynField) =
let (SynField.SynField(_,_,id,_,_,_,_,_)) = field
let (SynField.SynField(_,_,id,_,_,_,_,_,_)) = field
let fieldName = match id with None -> failwith "no field name" | Some f -> f

let recordType =
Expand Down Expand Up @@ -46,7 +46,7 @@ module internal Create =
let pattern =
let arguments =
fields
|> List.map (fun (SynField.SynField(_,_,id,typ,_,_,_,_)) ->
|> List.map (fun (SynField.SynField(_,_,id,typ,_,_,_,_,_)) ->
let name = SynPat.CreateNamed(Ast.Ident.asCamelCase id.Value)
SynPat.CreateTyped(name, typ) |> SynPat.CreateParen)

Expand All @@ -55,11 +55,11 @@ module internal Create =
let expr =
let fields =
fields
|> List.map (fun (SynField.SynField(_,_,id,_,_,_,_,_)) ->
|> List.map (fun (SynField.SynField(_,_,id,_,_,_,_,_,_)) ->
let fieldIdent = match id with None -> failwith "no field name" | Some f -> f
let name = SynLongIdent.Create([fieldIdent.idText])
let ident = SynExpr.CreateIdent(Ast.Ident.asCamelCase fieldIdent)
SynExprRecordField.SynExprRecordField(RecordFieldName(name, true), None, Some ident, None))
SynExprRecordField.SynExprRecordField(RecordFieldName(name, true), Some range0, Some ident, None))

let newRecord = SynExpr.Record(None, None, fields, range0 )
SynExpr.CreateTyped(newRecord, recordType)
Expand All @@ -77,7 +77,7 @@ module internal Create =
let pattern =
let arguments =
recordFields
|> List.map (fun (SynField.SynField(_,_,id, fieldType,_,_,_,_)) ->
|> List.map (fun (SynField.SynField(_,_,id, fieldType,_,_,_,_,_)) ->
let funType = SynType.CreateFun(fieldType, fieldType)
let ident = createFieldMapNameIdent id
let name = SynPat.CreateNamed(ident)
Expand All @@ -103,7 +103,7 @@ module internal Create =
Some (SynExpr.CreateIdent recordPrimeIdent, blockSep)

let fieldUpdates =
let mapField (SynField(_,_,id,_,_,_,_,_)) =
let mapField (SynField(_,_,id,_,_,_,_,_,_)) =
let lid = SynLongIdent.Create [id.Value.idText]
let rfn = RecordFieldName(lid, true)

Expand All @@ -114,7 +114,7 @@ module internal Create =
SynExpr.CreateLongIdent(false, longIdentWithDots, None)
SynExpr.CreateApp(funcExpr, argExpr)

SynExprRecordField.SynExprRecordField(rfn, None, Some update, None)
SynExprRecordField.SynExprRecordField(rfn, Some range0, Some update, None)

let arguments =
recordFields
Expand Down
17 changes: 10 additions & 7 deletions src/Myriad.Plugins/LensesGenerator.fs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Myriad.Plugins

open System
open FSharp.Compiler.Syntax
open FSharp.Compiler.Xml
open Fantomas.FCS.Syntax
open Fantomas.FCS.Xml
open Myriad.Core
open Myriad.Core.Ast
open FSharp.Compiler.Text.Range
open FSharp.Compiler.SyntaxTrivia
open Fantomas.FCS.Text.Range
open Fantomas.FCS.SyntaxTrivia

module internal CreateLenses =
let private wrap (wrapperName : Option<string>) lens =
Expand All @@ -17,7 +17,7 @@ module internal CreateLenses =
| _ -> lens

let private createLensForRecordField (parent: LongIdent) (wrapperName : Option<string>) (aetherStyle: bool) (field: SynField) =
let (SynField.SynField(_,_,id,fieldType,_,_,_,_)) = field
let (SynField.SynField(_,_,id,fieldType,_,_,_,_,_)) = field
let fieldName = match id with None -> failwith "no field name" | Some f -> f

let recordType =
Expand Down Expand Up @@ -55,7 +55,7 @@ module internal CreateLenses =

let private createLensForDU (requiresQualifiedAccess : bool) (parent: LongIdent) (wrapperName : Option<string>) (du : SynUnionCase) =
let (SynUnionCase.SynUnionCase(_,(SynIdent(id, _)),duType,_,_,_,_)) = du
let (SynField.SynField(_,_,_,fieldType,_,_,_,_)) =
let (SynField.SynField(_,_,_,fieldType,_,_,_,_,_)) =
match duType with
| SynUnionCaseKind.Fields [singleCase] -> singleCase
| SynUnionCaseKind.Fields (_ :: _) -> failwith "It is not possible to create a lens for a DU with several cases"
Expand Down Expand Up @@ -120,7 +120,10 @@ module internal CreateLenses =

let synPat = SynPat.LongIdent (SynLongIdent.CreateString "getter", None, None, SynArgPats.Pats [synPat], None, range0)

let trivia = {EqualsRange = Some range0; LetKeyword = Some range0}
let trivia =
{ SynBindingTrivia.LeadingKeyword = SynLeadingKeyword.Let range0
InlineKeyword = None
EqualsRange = Some range0 }
SynBinding.SynBinding (None, SynBindingKind.Normal, false, false, [], PreXmlDoc.Empty, valData, synPat, None, matchExpression, range0, DebugPointAtBinding.NoneAtDo, trivia)

let lens = SynExpr.LetOrUse (false, false, [getterLet], tuple, range0, { InKeyword = None })
Expand Down
Loading

0 comments on commit c6ea173

Please sign in to comment.