Skip to content

Commit

Permalink
Framework,Rules: adapt code to changes in FCS
Browse files Browse the repository at this point in the history
Adapte code to changes in FSharp.Compiler.Service.
  • Loading branch information
webwarrior-ws committed Mar 4, 2024
1 parent aacc621 commit ac20c53
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/FSharpLint.Core/Framework/AbstractSyntaxArray.fs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module AbstractSyntaxArray =
| Expression(_) -> SyntaxNode.Expression
| Pattern(SynPat.Ands(_)) -> SyntaxNode.And
| Pattern(SynPat.Or(_)) -> SyntaxNode.Or
| Pattern(Cons(_)) -> SyntaxNode.Cons
| Pattern(SynPat.ListCons(_)) -> SyntaxNode.Cons
| Pattern(SynPat.Wild(_)) -> SyntaxNode.Wildcard
| Pattern(SynPat.Const(constant, _)) -> constToSyntaxNode constant
| Pattern(SynPat.ArrayOrList(_)) -> SyntaxNode.ArrayOrList
Expand Down
64 changes: 41 additions & 23 deletions src/FSharpLint.Core/Framework/Ast.fs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ module Ast =
match pattern with
| SynPat.LongIdent(SynLongIdent([identifier], _, _),
_, _,
SynArgPats.Pats([SynPat.Tuple(_, [lhs; rhs], _)]), _, _)
SynArgPats.Pats([SynPat.Tuple(_, [lhs; rhs], _, _)]), _, _)
when identifier.idText = "op_ColonColon" ->
Some(lhs, rhs)
| SynPat.ListCons(lhsPat, rhsPat, _, _) ->
Some(lhsPat, rhsPat)
| _ -> None

/// Gets a string literal from the AST.
Expand Down Expand Up @@ -146,13 +148,20 @@ module Ast =
| SynType.App(synType, _, types, _, _, _, _) ->
types |> List.revIter (Type >> add)
add <| Type synType
| SynType.Tuple(_, types, _) ->
types |> List.revIter (snd >> Type >> add)
| SynType.Tuple(_, typeSegments, _) ->
typeSegments
|> List.revIter
(fun segment ->
match segment with
| SynTupleTypeSegment.Type(typ) -> typ |> Type |> add
| _ -> ())
| SynType.Fun(synType, synType1, _, _)
| SynType.StaticConstantNamed(synType, synType1, _)
| SynType.MeasureDivide(synType, synType1, _) ->
| SynType.Or(synType, synType1, _, _)
| SynType.StaticConstantNamed(synType, synType1, _) ->
add <| Type synType1
add <| Type synType
| SynType.MeasurePower(synType, _, _) ->
add <| Type synType
| SynType.Var(_)
| SynType.Anon(_)
| SynType.LongIdent(_)
Expand All @@ -166,20 +175,26 @@ module Ast =
typeNames |> List.revIter (snd >> Type >> add)
| SynType.Paren(innerType, _) ->
add <| Type innerType
| SynType.FromParseError(_) ->
()
| SynType.Intersection(_, types, _, _) ->
types |> List.revIter (Type >> add)
| SynType.SignatureParameter(_, _, _, synType, _) ->
add <| Type synType


/// Concatenates the typed-or-untyped structure of `SynSimplePats` into a `SynSimplePat list` to keep other code
/// mostly unchanged.
let inline extractPatterns (simplePats:SynSimplePats) =
let rec loop pat acc =
let loop pat acc =
match pat with
| SynSimplePats.SimplePats(patterns, _range) -> patterns @ acc
| SynSimplePats.Typed(patterns, _type, _range) -> loop patterns acc
| SynSimplePats.SimplePats(patterns, _, _range) -> patterns @ acc
loop simplePats []

let inline private memberDefinitionChildren node add =
match node with
| SynMemberDefn.Member(binding, _) -> add <| Binding binding
| SynMemberDefn.ImplicitCtor(_, _, patterns, _, _, _) ->
| SynMemberDefn.ImplicitCtor(_, _, patterns, _, _, _, _) ->
let combinedPatterns = extractPatterns patterns
combinedPatterns |> List.revIter (SimplePattern >> add)
| SynMemberDefn.ImplicitInherit(synType, expression, _, _) ->
Expand All @@ -195,10 +210,10 @@ module Ast =
| SynMemberDefn.AbstractSlot(_) -> ()
| SynMemberDefn.ValField(field, _) -> add <| Field field
| SynMemberDefn.NestedType(typeDefinition, _, _) -> add <| TypeDefinition typeDefinition
| SynMemberDefn.AutoProperty(_, _, _, Some(synType), _, _, _, _, _, expression, _, _, _) ->
| SynMemberDefn.AutoProperty(_, _, _, Some(synType), _, _, _, _, _, expression, _, _) ->
add <| Expression expression
add <| Type synType
| SynMemberDefn.AutoProperty(_, _, _, None, _, _, _, _, _, expression, _, _, _) ->
| SynMemberDefn.AutoProperty(_, _, _, None, _, _, _, _, _, expression, _, _) ->
add <| Expression expression
| SynMemberDefn.GetSetMember(memberDefnForGet, memberDefnForSet, _, _) ->
memberDefnForGet |> Option.iter (Binding >> add)
Expand All @@ -215,7 +230,7 @@ module Ast =
add <| Pattern pattern1
add <| Pattern pattern
| SynPat.ArrayOrList(_, patterns, _)
| SynPat.Tuple(_, patterns, _)
| SynPat.Tuple(_, patterns, _, _)
| SynPat.Ands(patterns, _) -> patterns |> List.revIter (Pattern >> add)
| SynPat.Attrib(pattern, _, _)
| SynPat.Paren(pattern, _) -> add <| Pattern pattern
Expand All @@ -225,10 +240,9 @@ module Ast =
| SynPat.Wild(_)
| SynPat.FromParseError(_)
| SynPat.InstanceMember(_)
| SynPat.DeprecatedCharRange(_)
| SynPat.Null(_)
| SynPat.OptionalVal(_) -> ()
| Cons(lhs, rhs) ->
| SynPat.ListCons(lhs, rhs, _, _) ->
add <| Pattern rhs
add <| Pattern lhs
| SynPat.LongIdent(_, _, _, constructorArguments, _, _) ->
Expand Down Expand Up @@ -277,9 +291,9 @@ module Ast =
| SynExpr.ArrayOrList(_, expressions, _) -> expressions |> List.revIter (Expression >> add)
| SynExpr.Record(_, Some(expr, _), _, _) -> add <| Expression expr
| SynExpr.Record(_, None, _, _) -> ()
| SynExpr.AnonRecd(_, Some (expr,_), _, _) ->
| SynExpr.AnonRecd(_, Some (expr,_), _, _, _) ->
add <| Expression expr
| SynExpr.AnonRecd(_, None, _, _) -> ()
| SynExpr.AnonRecd(_, None, _, _, _) -> ()
| SynExpr.ObjExpr(synType, _, _, bindings, _, _, _, _) ->
bindings |> List.revIter (Binding >> add)
add <| Type synType
Expand Down Expand Up @@ -362,6 +376,13 @@ module Ast =
| SynExpr.IndexRange(expr1, _, expr2, _, _, _) ->
expr1 |> Option.iter (Expression >> add)
expr2 |> Option.iter (Expression >> add)
| SynExpr.DotLambda(expr, _, _) ->
expr |> Expression |> add
| SynExpr.Typar(typar, _) ->
()
| SynExpr.WhileBang(_, whileExpr, doExpr, _) ->
add <| Expression whileExpr
add <| Expression doExpr

let inline private typeSimpleRepresentationChildren node add =
match node with
Expand All @@ -376,11 +397,8 @@ module Ast =

let inline private simplePatternsChildren node add =
match node with
| SynSimplePats.SimplePats(simplePatterns, _) ->
| SynSimplePats.SimplePats(simplePatterns, _, _) ->
simplePatterns |> List.revIter (SimplePattern >> add)
| SynSimplePats.Typed(simplePatterns, synType, _) ->
add <| Type synType
add <| SimplePatterns simplePatterns

let inline private simplePatternChildren node add =
match node with
Expand All @@ -404,7 +422,7 @@ module Ast =
match node with
| SynArgPats.Pats(patterns) ->
patterns |> List.revIter (Pattern >> add)
| SynArgPats.NamePatPairs(namePatterns, _) ->
| SynArgPats.NamePatPairs(namePatterns, _, _) ->
namePatterns |> List.revIter (fun (_, _, pattern) -> pattern |> Pattern |> add)

let inline private typeRepresentationChildren node add =
Expand Down Expand Up @@ -443,7 +461,7 @@ module Ast =
| Type(x) -> typeChildren x add
| Match(x) -> matchChildren x add
| MemberDefinition(x) -> memberDefinitionChildren x add
| Field(SynField(_, _, _, synType, _, _, _, _)) -> add <| Type synType
| Field(SynField(_, _, _, synType, _, _, _, _, _)) -> add <| Type synType
| Pattern(x) -> patternChildren x add
| ConstructorArguments(x) -> constructorArgumentsChildren x add
| SimplePattern(x) -> simplePatternChildren x add
Expand All @@ -462,7 +480,7 @@ module Ast =
| Else(x)
| Expression(x) -> expressionChildren x add

| File(ParsedInput.ImplFile(ParsedImplFileInput(_, _, _, _, _, moduleOrNamespaces, _, _))) ->
| File(ParsedInput.ImplFile(ParsedImplFileInput(_, _, _, _, _, moduleOrNamespaces, _, _, _))) ->
moduleOrNamespaces |> List.revIter (ModuleOrNamespace >> add)

| UnionCase(x) -> unionCaseChildren x add
Expand Down
4 changes: 2 additions & 2 deletions src/FSharpLint.Core/Framework/Utilities.fs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ module ExpressionUtilities =

/// Converts an operator name e.g. op_Add to the operator symbol e.g. +
let identAsDecompiledOpName (ident:Ident) =
if ident.idText.StartsWith("op_") then
PrettyNaming.DecompileOpName ident.idText
if PrettyNaming.IsLogicalOpName ident.idText then
PrettyNaming.ConvertValLogicalNameToDisplayNameCore ident.idText
else ident.idText

let identAsCompiledOpName (identName: string) =
Expand Down
6 changes: 3 additions & 3 deletions src/FSharpLint.Core/Rules/Conventions/AvoidTooShortNames.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let private getParameterWithBelowMinimumLength (pats: SynPat list): (Ident * str
match pat with
| SynPat.Typed(typedPat, _, _) ->
loop (typedPat::tail) acc
| SynPat.Tuple(_, elementPats, _) ->
| SynPat.Tuple(_, elementPats, _, _) ->
loop elementPats acc
| _ -> loop (pat::tail) acc
| SynPat.LongIdent(_, _, _, argPats, _, _)::tail ->
Expand Down Expand Up @@ -77,12 +77,12 @@ let private getIdentifiers (args:AstNodeRuleParams) =
| SynPat.Named(SynIdent(identifier, _), _, _, _) when isIdentifierTooShort identifier.idText ->
(identifier, identifier.idText, None) |> Array.singleton
| _ -> Array.empty
| AstNode.Field(SynField(_, _, Some identifier, _, _, _, _, _)) when isIdentifierTooShort identifier.idText ->
| AstNode.Field(SynField(_, _, Some identifier, _, _, _, _, _, _)) when isIdentifierTooShort identifier.idText ->
(identifier, identifier.idText, None) |> Array.singleton
| AstNode.TypeDefinition(SynTypeDefn(componentInfo, _typeDef, _, _, _, _)) ->
let checkTypes types =
seq {
for SynTyparDecl(_attr, SynTypar(id, _, _)) in types do
for SynTyparDecl(_attr, SynTypar(id, _, _), _, _) in types do
if isIdentifierTooShort id.idText then
yield (id, id.idText, None)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let private checkTupleOfWildcards pattern identifier =
constructorName + "(" + arguments + ")"

match pattern with
| SynPat.Tuple(_isStruct, patterns, range) when List.length patterns > 1 && patterns |> List.forall isWildcard ->
| SynPat.Tuple(_isStruct, patterns, _commaRanges, range) when List.length patterns > 1 && patterns |> List.forall isWildcard ->
let errorFormat = Resources.GetString("RulesTupleOfWildcardsError")
let refactorFrom = constructorString (List.length patterns)
let refactorTo = (constructorString 1)
Expand All @@ -37,14 +37,14 @@ let private isTupleMemberArgs breadcrumbs tupleRange =
| _ -> None

match breadcrumbs with
| AstNode.Binding(MemberBindingArgs(SynPat.Tuple(_, _, range)))::AstNode.Expression(SynExpr.ObjExpr(_))::_
| AstNode.Binding(MemberBindingArgs(SynPat.Tuple(_, _, range)))::AstNode.MemberDefinition(_)::_ ->
| AstNode.Binding(MemberBindingArgs(SynPat.Tuple(_, _, _, range)))::AstNode.Expression(SynExpr.ObjExpr(_))::_
| AstNode.Binding(MemberBindingArgs(SynPat.Tuple(_, _, _, range)))::AstNode.MemberDefinition(_)::_ ->
tupleRange = range
| _ -> false

let private runner (args:AstNodeRuleParams) =
match args.AstNode with
| AstNode.Pattern(SynPat.LongIdent(identifier, _, _, SynArgPats.Pats([SynPat.Paren(SynPat.Tuple(_, _, range) as pattern, _)]), _, _)) ->
| AstNode.Pattern(SynPat.LongIdent(identifier, _, _, SynArgPats.Pats([SynPat.Paren(SynPat.Tuple(_, _, _, range) as pattern, _)]), _, _)) ->
let breadcrumbs = args.GetParents 2
if (not << isTupleMemberArgs breadcrumbs) range then
let identifier = identifier.LongIdent |> List.map (fun x -> x.idText)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ open FSharpLint.Framework.Ast
open FSharpLint.Framework.Rules

let rec getLambdaParamIdent = function
| SynSimplePats.SimplePats([pattern], _) ->
| SynSimplePats.SimplePats([pattern], _, _) ->
let rec getIdent = function
| SynSimplePat.Id(ident, _, _, _, _, _) -> ident
| SynSimplePat.Typed(simplePattern, _, _)
Expand All @@ -14,8 +14,6 @@ let rec getLambdaParamIdent = function

getIdent pattern |> Some
| SynSimplePats.SimplePats(_) -> None
| SynSimplePats.Typed(simplePatterns, _, _) ->
getLambdaParamIdent simplePatterns

let checkLambda (args:AstNodeRuleParams) checker =
match args.AstNode with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ let private validateLambdaIsNotPointless (text:string) lambda range =
let identifier =
identifier
|> List.map (fun x ->
if PrettyNaming.IsMangledOpName x.idText then
PrettyNaming.DecompileOpName x.idText |> sprintf "( %s )"
if PrettyNaming.IsLogicalOpName x.idText then
PrettyNaming.ConvertValLogicalNameToDisplayNameCore x.idText |> sprintf "( %s )"
else
x.idText)
|> String.concat "."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open FSharpLint.Rules.Helper.Naming

let private getIdentifiers (args:AstNodeRuleParams) =
match args.AstNode with
| AstNode.EnumCase(SynEnumCase(_, SynIdent(identifier, _), _, _, _, _, _)) ->
| AstNode.EnumCase(SynEnumCase(_, SynIdent(identifier, _), _, _, _, _)) ->
(identifier, identifier.idText, None) |> Array.singleton
| _ -> Array.empty

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let private getIdentifiers (args: AstNodeRuleParams) =
| AstNode.TypeDefinition(SynTypeDefn(componentInfo, _typeDef, _, _, _, _)) ->
let checkTypes types =
seq {
for SynTyparDecl(_attr, SynTypar(id, _, _)) in types do
for SynTyparDecl(_attr, SynTypar(id, _, _), _, _) in types do
yield (id, id.idText, None)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let private getIdentifiers (args:AstNodeRuleParams) =
Array.empty
| AstNode.MemberDefinition(memberDef) ->
match memberDef with
| SynMemberDefn.AbstractSlot(SynValSig(_, SynIdent(identifier, _), _, _, _, _, _, _, _, _, _, _), _, _) ->
| SynMemberDefn.AbstractSlot(SynValSig(_, SynIdent(identifier, _), _, _, _, _, _, _, _, _, _, _), _, _, _) ->
(identifier, identifier.idText, None) |> Array.singleton
| _ -> Array.empty
| _ -> Array.empty
Expand Down
13 changes: 8 additions & 5 deletions src/FSharpLint.Core/Rules/Conventions/Naming/NamingHelper.fs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ let getAccessControlLevel (syntaxArray:AbstractSyntaxArray.Node []) i =
| TypeSimpleRepresentation(SynTypeDefnSimpleRepr.Record(access, _, _))
| TypeSimpleRepresentation(SynTypeDefnSimpleRepr.Union(access, _, _))
| UnionCase(SynUnionCase(_, _, _, _, access, _, _))
| Field(SynField(_, _, _, _, _, _, access, _))
| Field(SynField(_, _, _, _, _, _, access, _, _))
| ComponentInfo(SynComponentInfo(_, _, _, _, _, _, access, _))
| ModuleOrNamespace (SynModuleOrNamespace.SynModuleOrNamespace(_, _, _, _, _, _, access, _, _))
| ExceptionRepresentation(SynExceptionDefnRepr.SynExceptionDefnRepr(_, _, _, _, access, _))
Expand Down Expand Up @@ -318,12 +318,12 @@ let rec getPatternIdents<'T> (accessibility:AccessControlLevel) (getIdents:GetId

let hasNoArgs =
match args with
| SynArgPats.NamePatPairs(pats, _) -> pats.IsEmpty
| SynArgPats.NamePatPairs(pats, _, _) -> pats.IsEmpty
| SynArgPats.Pats(pats) -> pats.IsEmpty

let argSuggestions =
match args with
| SynArgPats.NamePatPairs(pats, _) ->
| SynArgPats.NamePatPairs(pats, _, _) ->
pats
|> List.toArray
|> Array.collect (fun(_, _, synPat) -> getPatternIdents AccessControlLevel.Private getIdents false synPat)
Expand All @@ -347,7 +347,7 @@ let rec getPatternIdents<'T> (accessibility:AccessControlLevel) (getIdents:GetId
| SynPat.Paren(p, _) ->
getPatternIdents accessibility getIdents false p
| SynPat.Ands(pats, _)
| SynPat.Tuple(_, pats, _)
| SynPat.Tuple(_, pats, _, _)
| SynPat.ArrayOrList(_, pats, _) ->
pats
|> List.toArray
Expand All @@ -361,11 +361,14 @@ let rec getPatternIdents<'T> (accessibility:AccessControlLevel) (getIdents:GetId
| SynPat.Const(_)
| SynPat.Wild(_)
| SynPat.OptionalVal(_)
| SynPat.DeprecatedCharRange(_) | SynPat.InstanceMember(_) | SynPat.FromParseError(_) -> Array.empty
| SynPat.InstanceMember(_) | SynPat.FromParseError(_) -> Array.empty
| SynPat.As(lhsPat, rhsPat, _) ->
Array.append
(getPatternIdents accessibility getIdents false lhsPat)
(getPatternIdents accessibility getIdents false rhsPat)
| SynPat.ListCons(lhsPat, rhsPat, _, _) ->
[|lhsPat; rhsPat|]
|> Array.collect (getPatternIdents accessibility getIdents false)

let rec identFromSimplePat = function
| SynSimplePat.Id(ident, _, _, _, _, _) -> Some ident
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let private getIdentifiers (args:AstNodeRuleParams) =
match args.AstNode with
| AstNode.MemberDefinition(memberDef) ->
match memberDef with
| SynMemberDefn.ImplicitCtor(_, _, ctorArgs, _, _, _) ->
| SynMemberDefn.ImplicitCtor(_, _, ctorArgs, _, _, _, _) ->
ctorArgs
|> extractPatterns
|> List.toArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ let private getMembers (members:SynMemberDefn list) =
let isPublicMember = function
| SynMemberDefn.AbstractSlot(_) -> true
| SynMemberDefn.Member(SynBinding(access, _, _, _, _, _, _, _, _, _, _, _, _), _)
| SynMemberDefn.AutoProperty(_, _, _, _, _, _, _, access, _, _, _, _, _) -> isPublic access
| SynMemberDefn.AutoProperty(_, _, _, _, _, _, _, _, access, _, _, _) -> isPublic access
| _ -> false

members
Expand Down
17 changes: 4 additions & 13 deletions src/FSharpLint.Core/Rules/Hints/HintMatcher.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ open FSharpLint.Framework.Rules
type Config =
{ HintTrie:MergeSyntaxTrees.Edges }

let rec private extractSimplePatterns = function
| SynSimplePats.SimplePats(simplePatterns, _) ->
let private extractSimplePatterns = function
| SynSimplePats.SimplePats(simplePatterns, _, _) ->
simplePatterns
| SynSimplePats.Typed(simplePatterns, _, _) ->
extractSimplePatterns simplePatterns

let rec private extractIdent = function
| SynSimplePat.Id(ident, _, isCompilerGenerated, _, _, _) -> (ident, isCompilerGenerated)
Expand Down Expand Up @@ -443,20 +441,13 @@ module private MatchPattern =

and private matchTuple (pattern, hint) =
match (pattern, hint) with
| SynPat.Tuple(_, patterns, _), Pattern.Tuple(hintExpressions) ->
| SynPat.Tuple(_, patterns, _, _), Pattern.Tuple(hintExpressions) ->
doPatternsMatch patterns hintExpressions
| _ -> false

and private matchConsPattern (pattern, hint) =
match (pattern, hint) with
| SynPat.LongIdent(
SynLongIdent([ident], _, _),
_,
_,
SynArgPats.Pats([SynPat.Tuple(_, [leftPattern;rightPattern], _)]),
_,
_), Pattern.Cons(left, right)
when ident.idText = "op_ColonColon" ->
| SynPat.ListCons(leftPattern, rightPattern, _, _), Pattern.Cons(left, right) ->
matchHintPattern (leftPattern, left) && matchHintPattern (rightPattern, right)
| _ -> false

Expand Down
Loading

0 comments on commit ac20c53

Please sign in to comment.