You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/developers/tutorial.md
+20-3Lines changed: 20 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ In this tutorial, you will learn how to
10
10
- use `SymbolInformation` to look up method signatures
11
11
- use `Diagnostic` to report linter errors
12
12
- use `withConfiguration` to make a rule configurable
13
+
- use `atomic` to enable rule suppression
13
14
- publish the rule so others can try it on their own codebase
14
15
15
16
We are going to implement two different rules. The first rule is a semantic
@@ -135,6 +136,7 @@ This solution is simple but it is incomplete
135
136
- the rewrite triggers for any `true` literal even if it is not a function
136
137
argument. For example, `val done = true` becomes
137
138
`val done = isSuccess = true`.
139
+
- the rewrite triggers even if rule suppression is active, i.e. `// scalafix:ok` is present.
138
140
139
141
The first improvement we make is to handle both `true` and `false` literals.
140
142
@@ -248,14 +250,26 @@ a non-empty parameter list.
248
250
+ }
249
251
```
250
252
251
-
The final step is to extract the parameter at the index of the argument
253
+
The next step is to extract the parameter at the index of the argument
252
254
253
255
```scala
254
256
valparameter= method.parameterLists.head(i)
255
257
valparameterName= parameter.displayName
256
258
Patch.addLeft(t, s"$parameterName = ")
257
259
```
258
260
261
+
We're not done quite yet, because the rule does not respect rule suppression comments like `// scalafix:ok`. Let's add a test case to reproduce this bug
262
+
263
+
```scala
264
+
complete(false) // scalafix:ok; rule suppression
265
+
```
266
+
267
+
We can fix this bug by specifying that `Patch.addLeft` should be `atomic`
268
+
269
+
```scala
270
+
Patch.addLeft(t, s"$parameterName = ").atomic
271
+
```
272
+
259
273
That completes the `NamedLiteralArguments` rule! Run all tests and we see they
260
274
pass. Putting it together, the final code for the rule looks like this
261
275
@@ -280,7 +294,7 @@ class NamedLiteralArguments
280
294
if method.parameterLists.nonEmpty =>
281
295
valparameter= method.parameterLists.head(i)
282
296
valparameterName= parameter.displayName
283
-
Patch.addLeft(t, s"$parameterName = ")
297
+
Patch.addLeft(t, s"$parameterName = ").atomic
284
298
case _ =>
285
299
// Do nothing, the symbol is not a method with matching signature
286
300
Patch.empty
@@ -358,6 +372,7 @@ package test
358
372
classNoLiteralArguments {
359
373
defcomplete(isSuccess: Boolean):Unit= ()
360
374
complete(true) // assert: NoLiteralArguments
375
+
complete(true) // scalafix:ok; should not assert
361
376
}
362
377
```
363
378
@@ -377,7 +392,7 @@ doc.tree.collect {
377
392
}.flatten.asPatch
378
393
```
379
394
380
-
Finally, to report a diagnostic we use `Patch.lint`
395
+
Finally, to report a diagnostic we use `Patch.lint` (which supports rule suppression comments out of the box)
381
396
382
397
```scala
383
398
Patch.lint(LiteralArgument(t))
@@ -388,6 +403,7 @@ to make sure the position and message of the diagnostic make sense
388
403
389
404
```diff
390
405
complete(true) // assert: NoLiteralArguments
406
+
complete(true) // scalafix:ok; should not assert
391
407
+ complete(false) /* assert: NoLiteralArguments
392
408
+ ^^^^^
393
409
+ Use named arguments for literals such as 'parameterName = false'
@@ -434,6 +450,7 @@ class NoLiteralArgumentsConfig {
0 commit comments