Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Compiler/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -1561,18 +1561,23 @@ attributeListElements:
attribute:
/* A custom attribute */
| path opt_HIGH_PRECEDENCE_APP opt_atomicExprAfterType
{ let arg = match $3 with None -> mkSynUnit $1.Range | Some e -> e
({ TypeName=$1; ArgExpr=arg; Target=None; AppliesToGetterAndSetter=false; Range=$1.Range } : SynAttribute) }
{ let arg = match $3 with None -> mkSynUnit $1.Range | Some e -> e
let m = mkFileIndexRange $1.Range.FileIndex $1.Range.Start arg.Range.End
({ TypeName=$1; ArgExpr=arg; Target=None; AppliesToGetterAndSetter=false; Range=m } : SynAttribute) }

/* A custom attribute with an attribute target */
| attributeTarget path opt_HIGH_PRECEDENCE_APP opt_atomicExprAfterType
{ let arg = match $4 with None -> mkSynUnit $2.Range | Some e -> e
({ TypeName=$2; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=$2.Range } : SynAttribute) }
{ let arg = match $4 with None -> mkSynUnit $2.Range | Some e -> e
let startPos = match $1 with Some (ident:Ident) -> ident.idRange.Start | None -> $2.Range.Start
let m = mkFileIndexRange arg.Range.FileIndex startPos arg.Range.End
({ TypeName=$2; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=m } : SynAttribute) }

/* A custom attribute with an attribute target */
| attributeTarget OBLOCKBEGIN path oblockend opt_HIGH_PRECEDENCE_APP opt_atomicExprAfterType
{ let arg = match $6 with None -> mkSynUnit $3.Range | Some e -> e
({ TypeName=$3; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=$3.Range } : SynAttribute) }
let startPos = match $1 with Some ident -> ident.idRange.Start | None -> $3.Range.Start
let m = mkFileIndexRange arg.Range.FileIndex startPos arg.Range.End
({ TypeName=$3; ArgExpr=arg; Target=$1; AppliesToGetterAndSetter=false; Range=m } : SynAttribute) }


/* The target of a custom attribute */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
<Compile Include="..\service\SyntaxTreeTests\SynIdentTests.fs">
<Link>SyntaxTree\SynIdentTests.fs</Link>
</Compile>
<Compile Include="..\service\SyntaxTreeTests\AttributeTests.fs">
<Link>SyntaxTree\AttributeTests.fs</Link>
</Compile>
<Compile Include="..\service\FileSystemTests.fs">
<Link>FileSystemTests.fs</Link>
</Compile>
Expand Down
51 changes: 51 additions & 0 deletions tests/service/SyntaxTreeTests/AttributeTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module FSharp.Compiler.Service.Tests.SyntaxTreeTests.AttributeTests

open FSharp.Compiler.Service.Tests.Common
open FSharp.Compiler.Syntax
open NUnit.Framework
open Tests.Service.Symbols

[<Test>]
let ``range of attribute`` () =
let ast =
"""
[<MyAttribute(foo ="bar")>]
do ()
"""
|> getParseResults

match ast with
| ParsedInput.ImplFile (ParsedImplFileInput(contents = [ SynModuleOrNamespace.SynModuleOrNamespace(decls =
[ SynModuleDecl.Attributes(attributes = [ { Attributes = [ { Range = mAttribute } ] } ]) ; SynModuleDecl.Expr _ ] ) ])) ->
assertRange (2, 2) (2, 25) mAttribute
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"

[<Test>]
let ``range of attribute with path`` () =
let ast =
"""
[<Prefix.MyAttribute(foo ="bar")>]
do ()
"""
|> getParseResults

match ast with
| ParsedInput.ImplFile (ParsedImplFileInput(contents = [ SynModuleOrNamespace.SynModuleOrNamespace(decls =
[ SynModuleDecl.Attributes(attributes = [ { Attributes = [ { Range = mAttribute } ] } ]) ; SynModuleDecl.Expr _ ] ) ])) ->
assertRange (2, 2) (2, 32) mAttribute
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"

[<Test>]
let ``range of attribute with target`` () =
let ast =
"""
[<assembly: MyAttribute(foo ="bar")>]
do ()
"""
|> getParseResults

match ast with
| ParsedInput.ImplFile (ParsedImplFileInput(contents = [ SynModuleOrNamespace.SynModuleOrNamespace(decls =
[ SynModuleDecl.Attributes(attributes = [ { Attributes = [ { Range = mAttribute } ] } ]) ; SynModuleDecl.Expr _ ] ) ])) ->
assertRange (2, 2) (2, 35) mAttribute
| _ -> Assert.Fail $"Could not get valid AST, got {ast}"