rubocop-yast 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,6 +30,8 @@ class VariableScope < Hash
30
30
  def []=(varname, state)
31
31
  super
32
32
  end
33
+
34
+ alias_method :variable?, :key?
33
35
  end
34
36
 
35
37
  # A stack of VariableScope
@@ -3,6 +3,6 @@
3
3
  module RuboCop
4
4
  # Yast plugin settings
5
5
  module Yast
6
- VERSION = "0.0.4"
6
+ VERSION = "0.0.5"
7
7
  end
8
8
  end
data/rubocop-yast.gemspec CHANGED
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_runtime_dependency("rubocop", "~> 0.27")
32
32
 
33
33
  spec.add_development_dependency("rake")
34
+ spec.add_development_dependency("redcarpet", "~> 3")
34
35
  spec.add_development_dependency("rspec", "~> 3.1.0")
35
36
  spec.add_development_dependency("simplecov")
36
37
  end
@@ -0,0 +1,614 @@
1
+ Builtins Cop
2
+ ============
3
+
4
+ Table Of Contents
5
+ -----------------
6
+
7
+ 1. Description
8
+ 1. Builtins.time()
9
+ 1. Builtins.getenv()
10
+ 1. Logging
11
+
12
+ Description
13
+ -----------
14
+
15
+ This Cop is designed to check for `Builtins` call presence. These calls were
16
+ added by YCP Killer to ensure 100% compatibily of the translated Ruby code
17
+ with the old YCP.
18
+
19
+ But these calls should not be used in the new code, native Ruby methods should
20
+ be used instead of these wrappers.
21
+
22
+ The Cop can autocorrect some trivial or easy translatable builtins.
23
+
24
+ Generic Tests
25
+ -------------
26
+
27
+ It reports y2milestone builtin as offense
28
+
29
+ **Offense**
30
+
31
+ ```ruby
32
+ Builtins.y2milestone("foo")
33
+ ```
34
+
35
+ It finds builtin in explicit Yast namespace
36
+
37
+ **Offense**
38
+
39
+ ```ruby
40
+ Yast::Builtins.y2milestone("foo")
41
+ ```
42
+
43
+ It finds builtin in explicit ::Yast namespace
44
+
45
+ **Offense**
46
+
47
+ ```ruby
48
+ ::Yast::Builtins.y2milestone("foo")
49
+ ```
50
+
51
+ Builtins in the ::Builtins name space are ignored
52
+
53
+ **Accepted**
54
+
55
+ ```ruby
56
+ ::Builtins.y2milestone("foo")
57
+ ```
58
+
59
+ Builtins in non Yast name space are ignored
60
+
61
+ **Accepted**
62
+
63
+ ```ruby
64
+ Foo::Builtins.y2milestone("foo")
65
+ ```
66
+
67
+ lsort(), crypt and gettext builtins are allowed
68
+
69
+ **Accepted**
70
+
71
+ ```ruby
72
+ Builtins.lsort(["foo"])
73
+ Builtins.crypt("foo")
74
+ Builtins.dgettext("domain", "foo")
75
+ ```
76
+
77
+ It does not change unknown builtins
78
+
79
+ **Unchanged**
80
+
81
+ ```ruby
82
+ Builtins.foo()
83
+ ```
84
+
85
+
86
+ Builtins.time()
87
+ ---------------
88
+
89
+ Is trivially converted to `::Time.now.to_i`
90
+
91
+ **Original**
92
+
93
+ ```ruby
94
+ Builtins.time()
95
+ ```
96
+
97
+ **Translated**
98
+
99
+ ```ruby
100
+ ::Time.now.to_i
101
+ ```
102
+
103
+ Builtins.getenv()
104
+ -----------------
105
+
106
+ With string literal parameter is translated to ENV equivalent
107
+
108
+ **Original**
109
+
110
+ ```ruby
111
+ Builtins.getenv("foo")
112
+ ```
113
+
114
+ **Translated**
115
+
116
+ ```ruby
117
+ ENV["foo"]
118
+ ```
119
+
120
+ Variable as parameter is preserved.
121
+
122
+ **Original**
123
+
124
+ ```ruby
125
+ foo = bar
126
+ Builtins.getenv(foo)
127
+ ```
128
+
129
+ **Translated**
130
+
131
+ ```ruby
132
+ foo = bar
133
+ ENV[foo]
134
+ ```
135
+
136
+ Any other statement is preserved.
137
+
138
+ **Original**
139
+
140
+ ```ruby
141
+ Builtins.getenv(Ops.add(foo, bar))
142
+ ```
143
+
144
+ **Translated**
145
+
146
+ ```ruby
147
+ ENV[Ops.add(foo, bar)]
148
+ ```
149
+
150
+ Logging
151
+ --------
152
+
153
+ It translates `y2debug` to `log.debug`
154
+
155
+ **Original**
156
+
157
+ ```ruby
158
+ Builtins.y2debug("foo")
159
+ ```
160
+
161
+ **Translated**
162
+
163
+ ```ruby
164
+ include Yast::Logger
165
+ log.debug "foo"
166
+ ```
167
+
168
+ It translates `y2milestone` to `log.info`
169
+
170
+ **Original**
171
+
172
+ ```ruby
173
+ Builtins.y2milestone("foo")
174
+ ```
175
+
176
+ **Translated**
177
+
178
+ ```ruby
179
+ include Yast::Logger
180
+ log.info "foo"
181
+ ```
182
+
183
+ It translates `y2warning` to `log.warn`
184
+
185
+ **Original**
186
+
187
+ ```ruby
188
+ Builtins.y2warning("foo")
189
+ ```
190
+
191
+ **Translated**
192
+
193
+ ```ruby
194
+ include Yast::Logger
195
+ log.warn "foo"
196
+ ```
197
+
198
+ It translates `y2error` to `log.error`
199
+
200
+ **Original**
201
+
202
+ ```ruby
203
+ Builtins.y2error("foo")
204
+ ```
205
+
206
+ **Translated**
207
+
208
+ ```ruby
209
+ include Yast::Logger
210
+ log.error "foo"
211
+ ```
212
+
213
+ It translates `y2security` to `log.error`
214
+
215
+ **Original**
216
+
217
+ ```ruby
218
+ Builtins.y2security("foo")
219
+ ```
220
+
221
+ **Translated**
222
+
223
+ ```ruby
224
+ include Yast::Logger
225
+ log.error "foo"
226
+ ```
227
+
228
+ It translates `y2internal` to `log.fatal`
229
+
230
+ **Original**
231
+
232
+ ```ruby
233
+ Builtins.y2internal("foo")
234
+ ```
235
+
236
+ **Translated**
237
+
238
+ ```ruby
239
+ include Yast::Logger
240
+ log.fatal "foo"
241
+ ```
242
+
243
+ It adds the include statement only once
244
+
245
+ **Original**
246
+
247
+ ```ruby
248
+ Builtins.y2milestone("foo")
249
+ Builtins.y2milestone("foo")
250
+ ```
251
+
252
+ **Translated**
253
+
254
+ ```ruby
255
+ include Yast::Logger
256
+ log.info \"foo\"
257
+ log.info \"foo\"
258
+ ```
259
+
260
+ It converts YCP sformat to Ruby interpolation
261
+
262
+ **Original**
263
+
264
+ ```ruby
265
+ Builtins.y2milestone("foo: %1", foo)
266
+ ```
267
+
268
+ **Translated**
269
+
270
+ ```ruby
271
+ include Yast::Logger
272
+ log.info "foo: #{foo}"
273
+ ```
274
+
275
+ It converts %% in the format string to simple %.
276
+
277
+ **Original**
278
+
279
+ ```ruby
280
+ Builtins.y2milestone("foo: %1%%", foo)
281
+ ```
282
+
283
+ **Translated**
284
+
285
+ ```ruby
286
+ include Yast::Logger
287
+ log.info "foo: #{foo}%"
288
+ ```
289
+
290
+ It replaces repeated % placeholders in the format string
291
+
292
+ **Original**
293
+
294
+ ```ruby
295
+ Builtins.y2warning("%1 %1", foo)
296
+ ```
297
+
298
+ **Translated**
299
+
300
+ ```ruby
301
+ include Yast::Logger
302
+ log.warn "#{foo} #{foo}"
303
+ ```
304
+
305
+ The % placeholders do not need to start from 1
306
+
307
+ **Original**
308
+
309
+ ```ruby
310
+ Builtins.y2warning("%2 %2", foo, bar)
311
+ ```
312
+
313
+ **Translated**
314
+
315
+ ```ruby
316
+ include Yast::Logger
317
+ log.warn "#{bar} #{bar}"
318
+ ```
319
+
320
+ The % placeholders do not need to be in ascending order
321
+
322
+ **Original**
323
+
324
+ ```ruby
325
+ Builtins.y2warning("%2 %1", foo, bar)
326
+ ```
327
+
328
+ **Translated**
329
+
330
+ ```ruby
331
+ include Yast::Logger
332
+ log.warn "#{bar} #{foo}"
333
+ ```
334
+
335
+ It keeps the original statements in interpolated string
336
+
337
+ **Original**
338
+
339
+ ```ruby
340
+ Builtins.y2warning("%1", foo + bar)
341
+ ```
342
+
343
+ **Translated**
344
+
345
+ ```ruby
346
+ include Yast::Logger
347
+ log.warn "#{foo + bar}"
348
+ ```
349
+
350
+ It converts a log with string interpolation
351
+
352
+ **Original**
353
+
354
+ ```ruby
355
+ Builtins.y2warning("foo: #{foo}")
356
+ ```
357
+
358
+ **Translated**
359
+
360
+ ```ruby
361
+ include Yast::Logger
362
+ log.warn "foo: #{foo}"
363
+ ```
364
+
365
+ It converts a log with a message variable
366
+
367
+ **Original**
368
+
369
+ ```ruby
370
+ msg = "message"
371
+ Builtins.y2warning(msg)
372
+ ```
373
+
374
+ **Translated**
375
+
376
+ ```ruby
377
+ include Yast::Logger
378
+ msg = "message"
379
+ log.warn msg
380
+ ```
381
+
382
+ It converts a log with function call
383
+
384
+ **Original**
385
+
386
+ ```ruby
387
+ Builtins.y2warning(msg)
388
+ ```
389
+
390
+ **Translated**
391
+
392
+ ```ruby
393
+ include Yast::Logger
394
+ log.warn msg
395
+ ```
396
+
397
+ It doesn't convert a log with extra parameters
398
+
399
+ **Unchanged**
400
+
401
+ ```ruby
402
+ Builtins.y2warning(msg, arg1, arg2)
403
+ ```
404
+
405
+ It converts log with operator call
406
+
407
+ **Original**
408
+
409
+ ```ruby
410
+ Builtins.y2warning(msg1 + msg2)
411
+ ```
412
+
413
+ **Translated**
414
+
415
+ ```ruby
416
+ include Yast::Logger
417
+ log.warn msg1 + msg2
418
+ ```
419
+
420
+ It adds logger include to the class definition
421
+
422
+ **Original**
423
+
424
+ ```ruby
425
+ class Foo
426
+ Builtins.y2error('foo')
427
+ end
428
+ ```
429
+
430
+ **Translated**
431
+
432
+ ```ruby
433
+ class Foo
434
+ include Yast::Logger
435
+
436
+ log.error "foo"
437
+ end
438
+ ```
439
+
440
+ It adds logger include with correct indentation
441
+
442
+ **Original**
443
+
444
+ ```ruby
445
+ class Foo
446
+ Builtins.y2error('foo')
447
+ end
448
+ ```
449
+
450
+ **Translated**
451
+
452
+ ```ruby
453
+ class Foo
454
+ include Yast::Logger
455
+
456
+ log.error "foo"
457
+ end
458
+ ```
459
+
460
+ It does not add the logger include if already present
461
+
462
+ **Original**
463
+
464
+ ```ruby
465
+ class Foo
466
+ include Yast::Logger
467
+ Builtins.y2error('foo')
468
+ end
469
+ ```
470
+
471
+ **Translated**
472
+
473
+ ```ruby
474
+ class Foo
475
+ include Yast::Logger
476
+ log.error "foo"
477
+ end
478
+ ```
479
+
480
+ It adds the logger include after the parent class name if present
481
+
482
+ **Original**
483
+
484
+ ```ruby
485
+ class Foo < Bar
486
+ Builtins.y2error('foo')
487
+ end
488
+ ```
489
+
490
+ **Translated**
491
+
492
+ ```ruby
493
+ class Foo < Bar
494
+ include Yast::Logger
495
+
496
+ log.error "foo"
497
+ end
498
+ ```
499
+
500
+ It adds logger include once to a derived class in a module
501
+
502
+ **Original**
503
+
504
+ ```ruby
505
+ module Yast
506
+ class TestClass < Module
507
+ def test
508
+ Builtins.y2error("foo")
509
+ Builtins.y2debug("foo")
510
+ end
511
+ end
512
+ end
513
+ ```
514
+
515
+ **Translated**
516
+
517
+ ```ruby
518
+ module Yast
519
+ class TestClass < Module
520
+ include Yast::Logger
521
+
522
+ def test
523
+ log.error "foo"
524
+ log.debug "foo"
525
+ end
526
+ end
527
+ end
528
+ ```
529
+
530
+ It converts the single quoted format string to double quoted
531
+
532
+ **Original**
533
+
534
+ ```ruby
535
+ Builtins.y2milestone('foo: %1', foo)
536
+ ```
537
+
538
+ **Translated**
539
+
540
+ ```ruby
541
+ include Yast::Logger
542
+ log.info "foo: #{foo}"
543
+ ```
544
+
545
+ It escapes double quotes and interpolations
546
+
547
+ **Original**
548
+
549
+ ```ruby
550
+ Builtins.y2milestone('"#{foo}"')
551
+ ```
552
+
553
+ **Translated**
554
+
555
+ ```ruby
556
+ include Yast::Logger
557
+ log.info "\\"\\#{foo}\\""
558
+ ```
559
+
560
+ It does not convert logging with backtrace
561
+
562
+ **Unchanged**
563
+
564
+ ```ruby
565
+ Builtins.y2milestone(-1, "foo")
566
+ ```
567
+
568
+ It does not convert code with a local variable 'log'
569
+
570
+ **Unchanged**
571
+
572
+ ```ruby
573
+ log = 1
574
+ Builtins.y2milestone("foo")
575
+ ```
576
+
577
+ <!--
578
+
579
+ Template
580
+ --------
581
+
582
+ It finds an offense
583
+
584
+ **Offense**
585
+
586
+ ```ruby
587
+ ```
588
+
589
+ This code is OK
590
+
591
+ **Accepted**
592
+
593
+ ```ruby
594
+ ```
595
+
596
+ It translates.
597
+
598
+ **Original**
599
+
600
+ ```ruby
601
+ ```
602
+
603
+ **Translated**
604
+
605
+ ```ruby
606
+ ```
607
+
608
+ It does not translate.
609
+
610
+ **Unchanged**
611
+
612
+ ```ruby
613
+ ```
614
+ -->