Skip to content

Commit 3befee8

Browse files
committed
Updated pattern to be prefix: command [codes]
1 parent a9714a8 commit 3befee8

File tree

4 files changed

+101
-65
lines changed

4 files changed

+101
-65
lines changed

samples/OptionAnalyzer.Test/UnitTests.fs

Lines changed: 69 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -88,36 +88,59 @@ let tryCompareRanges code expected (results: Map<string, AnalyzerIgnoreRange lis
8888
Assert.That(ranges, Is.EquivalentTo(expected))
8989

9090
[<Test>]
91-
let ``get single line scoped ignore with one code`` () =
91+
let ``get next line scoped ignore with one code`` () =
9292
async {
9393
let source = """
9494
module M
95-
// IGNORE: IONIDE-001
95+
// fsharpanalyzer: ignore-line-next IONIDE-001
9696
let x = 1
9797
"""
9898
let ctx = getContext projectOptions source
99-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [SingleLine 3]
99+
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [NextLine 3]
100100
}
101101

102102
[<Test>]
103-
let ``get single line scoped ignore with multiple codes`` () =
103+
let ``get next line scoped ignore with multiple codes`` () =
104104
async {
105105
let source = """
106106
module M
107-
// IGNORE: IONIDE-001, IONIDE-002
107+
// fsharpanalyzer: ignore-line-next IONIDE-001, IONIDE-002
108108
let x = 1
109109
"""
110110
let ctx = getContext projectOptions source
111-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [SingleLine 3]
112-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-002" [SingleLine 3]
111+
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [NextLine 3]
112+
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-002" [NextLine 3]
113+
}
114+
115+
[<Test>]
116+
let ``get current line scoped ignore with one code`` () =
117+
async {
118+
let source = """
119+
module M
120+
let x = 1 // fsharpanalyzer: ignore-line IONIDE-001
121+
"""
122+
let ctx = getContext projectOptions source
123+
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [CurrentLine 3]
124+
}
125+
126+
[<Test>]
127+
let ``get current line scoped ignore with multiple codes`` () =
128+
async {
129+
let source = """
130+
module M
131+
let x = 1 // fsharpanalyzer: ignore-line IONIDE-001, IONIDE-002
132+
"""
133+
let ctx = getContext projectOptions source
134+
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [CurrentLine 3]
135+
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-002" [CurrentLine 3]
113136
}
114137

115138
[<Test>]
116139
let ``get file scoped ignore`` () =
117140
async {
118141
let source = """
119142
module M
120-
// IGNORE FILE: IONIDE-001
143+
// fsharpanalyzer: ignore-file IONIDE-001
121144
let x = 1
122145
"""
123146
let ctx = getContext projectOptions source
@@ -129,7 +152,7 @@ let ``get file scoped ignore with multiple codes`` () =
129152
async {
130153
let source = """
131154
module M
132-
// IGNORE FILE: IONIDE-001, IONIDE-002, IONIDE-003
155+
// fsharpanalyzer: ignore-file IONIDE-001, IONIDE-002, IONIDE-003
133156
let x = 1
134157
"""
135158
let ctx = getContext projectOptions source
@@ -143,9 +166,9 @@ let ``get range scoped ignore`` () =
143166
async {
144167
let source = """
145168
module M
146-
// IGNORE START: IONIDE-001
169+
// fsharpanalyzer: ignore-region-start IONIDE-001
147170
let x = 1
148-
// IGNORE END
171+
// fsharpanalyzer: ignore-region-end
149172
"""
150173
let ctx = getContext projectOptions source
151174
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [Range (3, 5)]
@@ -156,9 +179,9 @@ let ``get range scoped ignore with multiple codes`` () =
156179
async {
157180
let source = """
158181
module M
159-
// IGNORE START: IONIDE-001, IONIDE-002
182+
// fsharpanalyzer: ignore-region-start IONIDE-001, IONIDE-002
160183
let x = 1
161-
// IGNORE END
184+
// fsharpanalyzer: ignore-region-end
162185
"""
163186
let ctx = getContext projectOptions source
164187
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [Range (3, 5)]
@@ -170,11 +193,11 @@ let ``get range scoped ignore handles nested ignores`` () =
170193
async {
171194
let source = """
172195
module M
173-
// IGNORE START: IONIDE-001
174-
// IGNORE START: IONIDE-002
196+
// fsharpanalyzer: ignore-region-start IONIDE-001
197+
// fsharpanalyzer: ignore-region-start IONIDE-002
175198
let x = 1
176-
// IGNORE END
177-
// IGNORE END
199+
// fsharpanalyzer: ignore-region-end
200+
// fsharpanalyzer: ignore-region-end
178201
"""
179202
let ctx = getContext projectOptions source
180203
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [Range (3, 7)]
@@ -186,7 +209,7 @@ let ``ignores unclosed range scoped ignore`` () =
186209
async {
187210
let source = """
188211
module M
189-
// IGNORE START: IONIDE-001
212+
// fsharpanalyzer: ignore-region-start IONIDE-001
190213
let x = 1
191214
"""
192215
let ctx = getContext projectOptions source
@@ -199,7 +222,7 @@ let ``ignores unopened range scoped ignore`` () =
199222
let source = """
200223
module M
201224
let x = 1
202-
// IGNORE END
225+
// fsharpanalyzer: ignore-region-end
203226
"""
204227
let ctx = getContext projectOptions source
205228
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
@@ -209,94 +232,92 @@ let x = 1
209232
let ``code can have multiple ranges for one code`` () =
210233
async {
211234
let source = """
212-
// IGNORE FILE: IONIDE-001
235+
// fsharpanalyzer: ignore-file IONIDE-001
213236
module M
214-
// IGNORE START: IONIDE-001
215-
// IGNORE: IONIDE-001
237+
// fsharpanalyzer: ignore-region-start IONIDE-001
238+
// fsharpanalyzer: ignore-line-next IONIDE-001
216239
let x = 1
217-
// IGNORE END
240+
// fsharpanalyzer: ignore-region-end
218241
"""
219242
let ctx = getContext projectOptions source
220243
ctx.AnalyzerIgnoreRanges
221244
|> tryCompareRanges "IONIDE-001" [
222245
File
223-
SingleLine 5
246+
NextLine 5
224247
Range (4, 7)
225248
]
226249
}
227250

228251
[<Test>]
229-
let ``single line ignore handles tight spacing`` () =
252+
let ``next line ignore handles tight spacing`` () =
230253
async {
231254
let source = """
232255
module M
233-
// IGNORE:IONIDE-001
256+
// fsharpanalyzer:ignore-line-next IONIDE-001
234257
let x = 1
235258
"""
236259
let ctx = getContext projectOptions source
237-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [SingleLine 3]
260+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
238261
}
239262

240263
[<Test>]
241-
let ``single line ignore handles loose spacing`` () =
264+
let ``next line ignore handles loose spacing`` () =
242265
async {
243266
let source = """
244267
module M
245-
// IGNORE : IONIDE-001
268+
// fsharpanalyzer : ignore-line-next IONIDE-001
246269
let x = 1
247270
"""
248271
let ctx = getContext projectOptions source
249-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [SingleLine 3]
272+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
250273
}
251274

252275
[<Test>]
253-
let ``single line, multi-code ignore handles tight spacing`` () =
276+
let ``next line, multi-code ignore handles tight spacing`` () =
254277
async {
255278
let source = """
256279
module M
257-
// IGNORE:IONIDE-001,IONIDE-002
280+
// fsharpanalyzer:ignore-line-next IONIDE-001,IONIDE-002
258281
let x = 1
259282
"""
260283
let ctx = getContext projectOptions source
261-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [SingleLine 3]
262-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-002" [SingleLine 3]
284+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
263285
}
264286

265287
[<Test>]
266-
let ``single line, multi-code ignore handles loose spacing`` () =
288+
let ``next line, multi-code ignore handles loose spacing`` () =
267289
async {
268290
let source = """
269291
module M
270-
// IGNORE : IONIDE-001 , IONIDE-002
292+
// fsharpanalyzer : ignore-line-next IONIDE-001 , IONIDE-002
271293
let x = 1
272294
"""
273295
let ctx = getContext projectOptions source
274-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [SingleLine 3]
275-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-002" [SingleLine 3]
296+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
276297
}
277298

278299
[<Test>]
279300
let ``file ignore handles tight spacing`` () =
280301
async {
281302
let source = """
282303
module M
283-
// IGNORE FILE:IONIDE-001
304+
// fsharpanalyzer:ignore-file IONIDE-001
284305
let x = 1
285306
"""
286307
let ctx = getContext projectOptions source
287-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [File]
308+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
288309
}
289310

290311
[<Test>]
291312
let ``file ignore handles loose spacing`` () =
292313
async {
293314
let source = """
294315
module M
295-
// IGNORE FILE : IONIDE-001
316+
// fsharpanalyzer : ignore-file IONIDE-001
296317
let x = 1
297318
"""
298319
let ctx = getContext projectOptions source
299-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [File]
320+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
300321
}
301322

302323

@@ -306,23 +327,23 @@ let ``range ignore handles tight spacing`` () =
306327
async {
307328
let source = """
308329
module M
309-
// IGNORE START:IONIDE-001
330+
// fsharpanalyzer:ignore-region-start IONIDE-001
310331
let x = 1
311-
// IGNORE END
332+
// fsharpanalyzer:ignore-region-end
312333
"""
313334
let ctx = getContext projectOptions source
314-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [Range (3, 5)]
335+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
315336
}
316337

317338
[<Test>]
318339
let ``range ignore handles loose spacing`` () =
319340
async {
320341
let source = """
321342
module M
322-
// IGNORE START : IONIDE-001
343+
// fsharpanalyzer : ignore-region-start IONIDE-001
323344
let x = 1
324-
// IGNORE END
345+
// fsharpanalyzer : ignore-region-start
325346
"""
326347
let ctx = getContext projectOptions source
327-
ctx.AnalyzerIgnoreRanges |> tryCompareRanges "IONIDE-001" [Range (3, 5)]
348+
Assert.That(ctx.AnalyzerIgnoreRanges, Is.Empty)
328349
}

src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.Client.fs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,16 @@ module Client =
117117
true
118118
else
119119
false
120-
| SingleLine line ->
120+
| NextLine line ->
121121
if message.Range.StartLine - 1 = line then
122122
true
123123
else
124124
false
125+
| CurrentLine line ->
126+
if message.Range.StartLine = line then
127+
true
128+
else
129+
false
125130
)
126131
| None -> false
127132

src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@ open FSharp.Compiler.SyntaxTrivia
1414

1515
[<RequireQualifiedAccess>]
1616
type IgnoreComment =
17-
| SingleLine of line: int * codes: string list
17+
| CurrentLine of line: int * codes: string list
18+
| NextLine of line: int * codes: string list
1819
| File of codes: string list
19-
| RangeStart of startLine: int * codes: string list
20-
| RangeEnd of endLine: int
20+
| RegionStart of startLine: int * codes: string list
21+
| RegionEnd of endLine: int
2122

2223
type AnalyzerIgnoreRange =
2324
| File
2425
| Range of commentStart: int * commentEnd: int
25-
| SingleLine of commentLine: int
26+
| NextLine of commentLine: int
27+
| CurrentLine of commentLine: int
2628

2729
module Ignore =
2830

@@ -54,14 +56,18 @@ module Ignore =
5456
match ct with
5557
| CommentTrivia.BlockComment r
5658
| CommentTrivia.LineComment r ->
59+
// pattern to match is:
60+
// prefix: command [codes]
5761
match sourceText.GetLineString(r.StartLine - 1) with
58-
| ParseRegexCompiled @"IGNORE\s*:\s*(.*)$" [ SplitBy splitBy codes ] ->
59-
Some <| IgnoreComment.SingleLine(r.StartLine, trimCodes codes)
60-
| ParseRegexCompiled @"IGNORE\s+FILE\s*:\s*(.*)$" [ SplitBy splitBy codes ] ->
62+
| ParseRegexCompiled @"fsharpanalyzer:\signore-line-next\s(.*)$" [ SplitBy splitBy codes ] ->
63+
Some <| IgnoreComment.NextLine(r.StartLine, trimCodes codes)
64+
| ParseRegexCompiled @"fsharpanalyzer:\signore-line\s(.*)$" [ SplitBy splitBy codes ] ->
65+
Some <| IgnoreComment.CurrentLine(r.StartLine, trimCodes codes)
66+
| ParseRegexCompiled @"fsharpanalyzer:\signore-file\s(.*)$" [ SplitBy splitBy codes ] ->
6167
Some <| IgnoreComment.File (trimCodes codes)
62-
| ParseRegexCompiled @"IGNORE\s+START\s*:\s*(.*)$" [ SplitBy splitBy codes ] ->
63-
Some <| IgnoreComment.RangeStart(r.StartLine, trimCodes codes)
64-
| ParseRegexCompiled @"IGNORE\s+END(.*)$" _ -> Some <| IgnoreComment.RangeEnd r.StartLine
68+
| ParseRegexCompiled @"fsharpanalyzer:\signore-region-start\s(.*)$" [ SplitBy splitBy codes ] ->
69+
Some <| IgnoreComment.RegionStart(r.StartLine, trimCodes codes)
70+
| ParseRegexCompiled @"fsharpanalyzer:\signore-region-end.*$" _ -> Some <| IgnoreComment.RegionEnd r.StartLine
6571
| _ -> None
6672

6773
let getIgnoreComments (sourceText: ISourceText) (comments: CommentTrivia list) =
@@ -83,13 +89,16 @@ module Ignore =
8389
| IgnoreComment.File codes ->
8490
addRangeForCodes codes File
8591

86-
| IgnoreComment.SingleLine (line, codes) ->
87-
addRangeForCodes codes (SingleLine line)
92+
| IgnoreComment.NextLine (line, codes) ->
93+
addRangeForCodes codes (NextLine line)
94+
95+
| IgnoreComment.CurrentLine (line, codes) ->
96+
addRangeForCodes codes (CurrentLine line)
8897

89-
| IgnoreComment.RangeStart (startLine, codes) ->
98+
| IgnoreComment.RegionStart (startLine, codes) ->
9099
rangeStack <- (startLine, codes) :: rangeStack
91100

92-
| IgnoreComment.RangeEnd endLine ->
101+
| IgnoreComment.RegionEnd endLine ->
93102
match rangeStack with
94103
| [] ->
95104
// Ignore END without matching START - do nothing

src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fsi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ open FSharp.Compiler.Symbols
88
open FSharp.Compiler.EditorServices
99
open FSharp.Compiler.Text
1010

11-
type AnalyzerIgnoreRange =
11+
type AnalyzerIgnoreRange =
1212
| File
1313
| Range of commentStart: int * commentEnd: int
14-
| SingleLine of commentLine: int
14+
| NextLine of commentLine: int
15+
| CurrentLine of commentLine: int
1516

1617
[<AbstractClass>]
1718
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Property ||| AttributeTargets.Field)>]

0 commit comments

Comments
 (0)