rubocop-dev_doc 0.5.0 → 0.6.0.beta1
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.
- checksums.yaml +4 -4
- data/config/default.yml +151 -0
- data/lib/rubocop/cop/dev_doc/i18n/avoid_titleize_humanize.rb +59 -0
- data/lib/rubocop/cop/dev_doc/i18n/localizable_props.rb +109 -0
- data/lib/rubocop/cop/dev_doc/i18n/report_text.rb +76 -0
- data/lib/rubocop/cop/dev_doc/i18n/require_translation.rb +95 -0
- data/lib/rubocop/cop/dev_doc/i18n/translation_key_prefix.rb +89 -0
- data/lib/rubocop/dev_doc/version.rb +1 -1
- metadata +8 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dfa7b6778294801c95bb37bb3c15a2c4b30f81e9b915ba3ee548475c4adc9cd4
|
|
4
|
+
data.tar.gz: 6606b6c2b6745b2c54f63a89a7b3ae1b4ce3bfb60dda96a471dc51e33232e802
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: adc4743bfba263e139f6ca4cc5abbc213c427a46081e0aa5bc12be84ae96cc26f5b37eb218707449ca4566e5fca6958534f9581104c0d92c97f83e2ef17003b2
|
|
7
|
+
data.tar.gz: f8aa2b1f19d0f1a628f86ae58b19ed71ba6c5f508fd3247203909f459fb22deeef7777fc5de17509af04a0e4618aebd95c1b19a6fdf78fe53c72f97db063e261
|
data/config/default.yml
CHANGED
|
@@ -7,6 +7,10 @@ DevDoc/Auth:
|
|
|
7
7
|
DocumentationBaseURL: https://github.com/hgani/dev-doc/blob/main/docs/cops
|
|
8
8
|
DocumentationExtension: ".md"
|
|
9
9
|
|
|
10
|
+
DevDoc/I18n:
|
|
11
|
+
DocumentationBaseURL: https://github.com/hgani/dev-doc/blob/main/docs/cops
|
|
12
|
+
DocumentationExtension: ".md"
|
|
13
|
+
|
|
10
14
|
DevDoc/Migration:
|
|
11
15
|
DocumentationBaseURL: https://github.com/hgani/dev-doc/blob/main/docs/cops
|
|
12
16
|
DocumentationExtension: ".md"
|
|
@@ -396,3 +400,150 @@ DevDoc/Auth/CurrentUserBranching:
|
|
|
396
400
|
- "app/helpers/**/*.rb"
|
|
397
401
|
- "app/controllers/concerns/**/*.rb"
|
|
398
402
|
- "app/views/layouts/**/*"
|
|
403
|
+
|
|
404
|
+
DevDoc/I18n/AvoidTitleizeHumanize:
|
|
405
|
+
Description: "Avoid `.titleize`/`.humanize` for display text; it's English-only and bypasses I18n. Use `t(...)`."
|
|
406
|
+
# A review aid, not a clean lint: it can't tell a display string from one
|
|
407
|
+
# used to build a key or a log line. Off by default and `warning` severity;
|
|
408
|
+
# run it during a localization pass. The flagged methods are configurable
|
|
409
|
+
# via `Methods:`.
|
|
410
|
+
Enabled: false
|
|
411
|
+
Severity: warning
|
|
412
|
+
Methods:
|
|
413
|
+
- titleize
|
|
414
|
+
- humanize
|
|
415
|
+
Include:
|
|
416
|
+
- "app/views/**/*.jbuilder"
|
|
417
|
+
- "app/views/**/*.rb"
|
|
418
|
+
|
|
419
|
+
DevDoc/I18n/RequireTranslation:
|
|
420
|
+
Description: "Localize user-facing strings in glib JSON-UI props; pass `t(...)` instead of a hardcoded string."
|
|
421
|
+
Enabled: false
|
|
422
|
+
# WatchedMethods: glib component builder methods whose hash props carry
|
|
423
|
+
# user-facing text. LocalizableKeys: the prop names checked on those calls.
|
|
424
|
+
# Extend both if your project adds custom components or text props.
|
|
425
|
+
WatchedMethods:
|
|
426
|
+
- h1
|
|
427
|
+
- h2
|
|
428
|
+
- h3
|
|
429
|
+
- h4
|
|
430
|
+
- h5
|
|
431
|
+
- h6
|
|
432
|
+
- p
|
|
433
|
+
- label
|
|
434
|
+
- markdown
|
|
435
|
+
- html
|
|
436
|
+
- button
|
|
437
|
+
- switch
|
|
438
|
+
- chip
|
|
439
|
+
- progressCircle
|
|
440
|
+
- shareButton
|
|
441
|
+
- fields_text
|
|
442
|
+
- fields_number
|
|
443
|
+
- fields_select
|
|
444
|
+
- fields_password
|
|
445
|
+
- fields_textarea
|
|
446
|
+
- fields_check
|
|
447
|
+
- fields_checkGroup
|
|
448
|
+
- fields_chipGroup
|
|
449
|
+
- fields_timeZone
|
|
450
|
+
- fields_radioGroup
|
|
451
|
+
- fields_date
|
|
452
|
+
- fields_datetime
|
|
453
|
+
- fields_upload
|
|
454
|
+
- dialogs_alert
|
|
455
|
+
- dialogs_notification
|
|
456
|
+
- snackbars_alert
|
|
457
|
+
- snackbars_select
|
|
458
|
+
- banners_alert
|
|
459
|
+
- banners_select
|
|
460
|
+
LocalizableKeys:
|
|
461
|
+
- title
|
|
462
|
+
- subtitle
|
|
463
|
+
- subsubtitle
|
|
464
|
+
- label
|
|
465
|
+
- placeholder
|
|
466
|
+
- text
|
|
467
|
+
- message
|
|
468
|
+
- description
|
|
469
|
+
- uploadText
|
|
470
|
+
- leftText
|
|
471
|
+
- rightText
|
|
472
|
+
- onLabel
|
|
473
|
+
Include:
|
|
474
|
+
- "app/views/**/*.jbuilder"
|
|
475
|
+
- "app/views/**/*.rb"
|
|
476
|
+
|
|
477
|
+
DevDoc/I18n/TranslationKeyPrefix:
|
|
478
|
+
Description: "Translation keys must start with an allowed namespace prefix (e.g. `hotel.`, `general.`)."
|
|
479
|
+
Enabled: false
|
|
480
|
+
# AllowedPrefixes is project-specific, so it defaults to empty and the cop is
|
|
481
|
+
# a no-op until configured. Matches `t`, `translate`, and `I18n.t`. Only
|
|
482
|
+
# statically-literal keys are checked; dynamic/interpolated keys are skipped.
|
|
483
|
+
AllowedPrefixes: []
|
|
484
|
+
Include:
|
|
485
|
+
- "app/views/**/*.jbuilder"
|
|
486
|
+
- "app/views/**/*.rb"
|
|
487
|
+
- "app/controllers/**/*.rb"
|
|
488
|
+
- "app/mailers/**/*.rb"
|
|
489
|
+
- "app/helpers/**/*.rb"
|
|
490
|
+
|
|
491
|
+
DevDoc/I18n/ReportText:
|
|
492
|
+
Description: "Report every user-facing glib text prop — hardcoded and already-localized — to collect all possible texts."
|
|
493
|
+
# A tooling aid, not a lint: unlike RequireTranslation it fires on *every*
|
|
494
|
+
# text value (including `t(...)` calls) so you can sweep the codebase and
|
|
495
|
+
# collect the full list of user-facing strings. Off by default and `info`
|
|
496
|
+
# severity. Mirrors RequireTranslation's WatchedMethods/LocalizableKeys.
|
|
497
|
+
Enabled: false
|
|
498
|
+
Severity: info
|
|
499
|
+
WatchedMethods:
|
|
500
|
+
- h1
|
|
501
|
+
- h2
|
|
502
|
+
- h3
|
|
503
|
+
- h4
|
|
504
|
+
- h5
|
|
505
|
+
- h6
|
|
506
|
+
- p
|
|
507
|
+
- label
|
|
508
|
+
- markdown
|
|
509
|
+
- html
|
|
510
|
+
- button
|
|
511
|
+
- switch
|
|
512
|
+
- chip
|
|
513
|
+
- progressCircle
|
|
514
|
+
- shareButton
|
|
515
|
+
- fields_text
|
|
516
|
+
- fields_number
|
|
517
|
+
- fields_select
|
|
518
|
+
- fields_password
|
|
519
|
+
- fields_textarea
|
|
520
|
+
- fields_check
|
|
521
|
+
- fields_checkGroup
|
|
522
|
+
- fields_chipGroup
|
|
523
|
+
- fields_timeZone
|
|
524
|
+
- fields_radioGroup
|
|
525
|
+
- fields_date
|
|
526
|
+
- fields_datetime
|
|
527
|
+
- fields_upload
|
|
528
|
+
- dialogs_alert
|
|
529
|
+
- dialogs_notification
|
|
530
|
+
- snackbars_alert
|
|
531
|
+
- snackbars_select
|
|
532
|
+
- banners_alert
|
|
533
|
+
- banners_select
|
|
534
|
+
LocalizableKeys:
|
|
535
|
+
- title
|
|
536
|
+
- subtitle
|
|
537
|
+
- subsubtitle
|
|
538
|
+
- label
|
|
539
|
+
- placeholder
|
|
540
|
+
- text
|
|
541
|
+
- message
|
|
542
|
+
- description
|
|
543
|
+
- uploadText
|
|
544
|
+
- leftText
|
|
545
|
+
- rightText
|
|
546
|
+
- onLabel
|
|
547
|
+
Include:
|
|
548
|
+
- "app/views/**/*.jbuilder"
|
|
549
|
+
- "app/views/**/*.rb"
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
module RuboCop
|
|
2
|
+
module Cop
|
|
3
|
+
module DevDoc
|
|
4
|
+
module I18n
|
|
5
|
+
# Warn when `.titleize` or `.humanize` is used to derive display text.
|
|
6
|
+
#
|
|
7
|
+
# ## Rationale
|
|
8
|
+
# `titleize`/`humanize` turn an identifier into an English phrase
|
|
9
|
+
# (`'sign_up'.titleize # => 'Sign Up'`). When that result is shown to a
|
|
10
|
+
# user it bypasses the I18n catalog and can only ever read as English —
|
|
11
|
+
# it can't be translated. Localize the text with a `t(...)` lookup
|
|
12
|
+
# instead of transforming a symbol/column name at render time.
|
|
13
|
+
#
|
|
14
|
+
# This can't tell a display string from one used to build a key or a
|
|
15
|
+
# log line, so it is a **review aid**: disabled by default, `warning`
|
|
16
|
+
# severity, and scoped to view files. Run it during a localization pass.
|
|
17
|
+
#
|
|
18
|
+
# ⚠️ English-only — can't be translated
|
|
19
|
+
# view.h1 text: status.titleize
|
|
20
|
+
# view.p text: user.role.humanize
|
|
21
|
+
#
|
|
22
|
+
# ✔️ Resolved through I18n
|
|
23
|
+
# view.h1 text: t("status.#{status}")
|
|
24
|
+
#
|
|
25
|
+
# @example
|
|
26
|
+
# # warning
|
|
27
|
+
# view.h1 text: status.titleize
|
|
28
|
+
#
|
|
29
|
+
# # warning
|
|
30
|
+
# view.p text: model_name.humanize
|
|
31
|
+
#
|
|
32
|
+
# # good
|
|
33
|
+
# view.h1 text: t("status.#{status}")
|
|
34
|
+
class AvoidTitleizeHumanize < Base
|
|
35
|
+
MSG = 'Avoid `.%<method>s` for display text: the result is ' \
|
|
36
|
+
'English-only and bypasses I18n. Use `t(...)` instead.'.freeze
|
|
37
|
+
|
|
38
|
+
DEFAULT_METHODS = %w[titleize humanize].freeze
|
|
39
|
+
|
|
40
|
+
def on_send(node)
|
|
41
|
+
return unless forbidden_methods.include?(node.method_name.to_s)
|
|
42
|
+
|
|
43
|
+
add_offense(
|
|
44
|
+
node.loc.selector,
|
|
45
|
+
message: format(MSG, method: node.method_name)
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
alias on_csend on_send
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def forbidden_methods
|
|
53
|
+
cop_config.fetch('Methods', DEFAULT_METHODS)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
module RuboCop
|
|
2
|
+
module Cop
|
|
3
|
+
module DevDoc
|
|
4
|
+
module I18n
|
|
5
|
+
# Shared traversal for the glib JSON-UI localization cops
|
|
6
|
+
# (`RequireTranslation`, `ReportText`).
|
|
7
|
+
#
|
|
8
|
+
# It locates user-facing text values on watched glib component calls and
|
|
9
|
+
# hands each `(key, value_node)` to the including cop's
|
|
10
|
+
# `#inspect_localizable`, which decides whether to report. Three call
|
|
11
|
+
# shapes are covered:
|
|
12
|
+
#
|
|
13
|
+
# * hash prop: `view.p text: '...'`
|
|
14
|
+
# * nested hash prop: `view.icon tooltip: { text: '...' }`
|
|
15
|
+
# * jbuilder positional: `json.title '...'`
|
|
16
|
+
#
|
|
17
|
+
# Nested hashes (and hashes inside arrays, e.g. select options) are
|
|
18
|
+
# walked to any depth, so a localizable key is found wherever it sits.
|
|
19
|
+
#
|
|
20
|
+
# The watched method names and localizable keys are configurable via
|
|
21
|
+
# `WatchedMethods:` and `LocalizableKeys:`.
|
|
22
|
+
module LocalizableProps
|
|
23
|
+
DEFAULT_WATCHED_METHODS = %w[
|
|
24
|
+
h1 h2 h3 h4 h5 h6 p label markdown html
|
|
25
|
+
button switch chip progressCircle shareButton
|
|
26
|
+
fields_text fields_number fields_select fields_password
|
|
27
|
+
fields_textarea fields_check fields_checkGroup fields_chipGroup
|
|
28
|
+
fields_timeZone fields_radioGroup fields_date fields_datetime
|
|
29
|
+
fields_upload
|
|
30
|
+
dialogs_alert dialogs_notification
|
|
31
|
+
snackbars_alert snackbars_select
|
|
32
|
+
banners_alert banners_select
|
|
33
|
+
].freeze
|
|
34
|
+
|
|
35
|
+
DEFAULT_LOCALIZABLE_KEYS = %w[
|
|
36
|
+
title subtitle subsubtitle label placeholder text
|
|
37
|
+
message description uploadText leftText rightText onLabel
|
|
38
|
+
].freeze
|
|
39
|
+
|
|
40
|
+
TRANSLATION_METHODS = %i[t translate].freeze
|
|
41
|
+
|
|
42
|
+
def on_send(node)
|
|
43
|
+
if watched_methods.include?(node.method_name.to_s)
|
|
44
|
+
node.arguments.each { |arg| each_localizable(arg) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
check_jbuilder_positional(node)
|
|
48
|
+
end
|
|
49
|
+
alias on_csend on_send
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# Walk a value, handing every localizable `(key, value)` pair found at
|
|
54
|
+
# any depth to the cop: top-level hash pairs, nested hashes, and
|
|
55
|
+
# hashes nested inside arrays (e.g. select options / menu buttons).
|
|
56
|
+
def each_localizable(node)
|
|
57
|
+
case node.type
|
|
58
|
+
when :hash
|
|
59
|
+
node.pairs.each do |pair|
|
|
60
|
+
key = pair.key
|
|
61
|
+
if key.sym_type? && localizable_keys.include?(key.value.to_s)
|
|
62
|
+
inspect_localizable(key.value, pair.value)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
each_localizable(pair.value)
|
|
66
|
+
end
|
|
67
|
+
when :array
|
|
68
|
+
node.children.each { |child| each_localizable(child) }
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# jbuilder positional form: `json.title 'Forms'`. The method name is
|
|
73
|
+
# the key and the first argument is the value. Restricted to the
|
|
74
|
+
# jbuilder root `json` so ordinary `obj.text('...')` calls aren't
|
|
75
|
+
# mistaken for localizable props.
|
|
76
|
+
def check_jbuilder_positional(node)
|
|
77
|
+
return unless localizable_keys.include?(node.method_name.to_s)
|
|
78
|
+
return unless json_receiver?(node.receiver)
|
|
79
|
+
|
|
80
|
+
value = node.first_argument
|
|
81
|
+
return unless value
|
|
82
|
+
|
|
83
|
+
inspect_localizable(node.method_name, value)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def json_receiver?(node)
|
|
87
|
+
return false unless node
|
|
88
|
+
|
|
89
|
+
(node.send_type? && node.method_name == :json && node.receiver.nil?) ||
|
|
90
|
+
(node.lvar_type? && node.children.first == :json)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def translation_call?(node)
|
|
94
|
+
(node.send_type? || node.csend_type?) &&
|
|
95
|
+
TRANSLATION_METHODS.include?(node.method_name)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def watched_methods
|
|
99
|
+
cop_config.fetch('WatchedMethods', DEFAULT_WATCHED_METHODS)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def localizable_keys
|
|
103
|
+
cop_config.fetch('LocalizableKeys', DEFAULT_LOCALIZABLE_KEYS)
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require_relative 'localizable_props'
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module DevDoc
|
|
6
|
+
module I18n
|
|
7
|
+
# Report every user-facing text in a glib JSON-UI text prop — both
|
|
8
|
+
# hardcoded strings and already-localized `t(...)` calls.
|
|
9
|
+
#
|
|
10
|
+
# ## Rationale
|
|
11
|
+
# `DevDoc/I18n/RequireTranslation` flags only *hardcoded* strings; it
|
|
12
|
+
# stays silent once a value is localized. This cop is the opposite: it
|
|
13
|
+
# fires on **every** text value, localized or not, so you can sweep a
|
|
14
|
+
# codebase and collect the full list of user-facing strings (e.g. to
|
|
15
|
+
# seed a translation catalog or audit coverage).
|
|
16
|
+
#
|
|
17
|
+
# It is a tooling aid, not a lint — **disabled by default** and runs at
|
|
18
|
+
# `info` severity. Run it during a localization pass; it is not meant
|
|
19
|
+
# for every commit.
|
|
20
|
+
#
|
|
21
|
+
# Both the hardcoded form (`view.p text: 'Welcome'`) and the localized
|
|
22
|
+
# form (`view.p text: t('home.welcome')`) are reported. Blank/whitespace
|
|
23
|
+
# strings and pure dynamic values (`user.name`) carry no static text and
|
|
24
|
+
# are skipped.
|
|
25
|
+
#
|
|
26
|
+
# The watched method names and localizable keys are configurable via
|
|
27
|
+
# `WatchedMethods:` and `LocalizableKeys:`.
|
|
28
|
+
#
|
|
29
|
+
# 📋 Reported — hardcoded text
|
|
30
|
+
# view.p text: 'Welcome'
|
|
31
|
+
#
|
|
32
|
+
# 📋 Reported — localized text
|
|
33
|
+
# view.p text: t('home.welcome')
|
|
34
|
+
#
|
|
35
|
+
# @example
|
|
36
|
+
# # info (hardcoded text)
|
|
37
|
+
# view.p text: 'Welcome'
|
|
38
|
+
#
|
|
39
|
+
# # info (localized text — still reported)
|
|
40
|
+
# view.p text: t('home.welcome')
|
|
41
|
+
#
|
|
42
|
+
# # info (interpolated string)
|
|
43
|
+
# view.p text: "Hi #{name}"
|
|
44
|
+
#
|
|
45
|
+
# # ignored (blank — no text)
|
|
46
|
+
# view.fields_text label: ''
|
|
47
|
+
#
|
|
48
|
+
# # ignored (pure dynamic — no static text)
|
|
49
|
+
# view.p text: user.name
|
|
50
|
+
class ReportText < Base
|
|
51
|
+
include LocalizableProps
|
|
52
|
+
|
|
53
|
+
MSG = 'Text for `%<key>s:`: review/collect this for localization.'.freeze
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def inspect_localizable(key, value)
|
|
58
|
+
return unless text?(value)
|
|
59
|
+
|
|
60
|
+
add_offense(value, message: format(MSG, key: key))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Any value that carries static user-facing text: a non-blank string
|
|
64
|
+
# literal, an interpolated string, or a translation call. Blank strings
|
|
65
|
+
# and pure dynamic values (`user.name`) carry no text and are skipped.
|
|
66
|
+
def text?(node)
|
|
67
|
+
return true if node.dstr_type?
|
|
68
|
+
return !node.value.strip.empty? if node.str_type?
|
|
69
|
+
|
|
70
|
+
translation_call?(node)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
require_relative 'localizable_props'
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module DevDoc
|
|
6
|
+
module I18n
|
|
7
|
+
# Flag hardcoded user-facing strings in glib JSON-UI component props.
|
|
8
|
+
#
|
|
9
|
+
# ## Rationale
|
|
10
|
+
# Glib components render text from props like `text:`, `label:`, and
|
|
11
|
+
# `title:`. Passing a string literal ships untranslatable copy — it can
|
|
12
|
+
# never be localized and bypasses the I18n catalog. Pass `t('...')` (or
|
|
13
|
+
# any non-literal) so the text resolves through the locale files.
|
|
14
|
+
#
|
|
15
|
+
# Hash props (`view.p text: '...'`), nested hash props
|
|
16
|
+
# (`view.icon tooltip: { text: '...' }`) and the jbuilder positional
|
|
17
|
+
# form (`json.title '...'`) are all checked. Empty/whitespace strings
|
|
18
|
+
# are ignored — they carry no user-facing text. An interpolated string
|
|
19
|
+
# (`"Hi #{name}"`) is flagged because its literal portions are still
|
|
20
|
+
# hardcoded copy — unless it interpolates a `t(...)` (or
|
|
21
|
+
# `translate`/`I18n.t`) call, which is treated as positive localization
|
|
22
|
+
# and left alone.
|
|
23
|
+
#
|
|
24
|
+
# The watched method names and localizable keys are configurable via
|
|
25
|
+
# `WatchedMethods:` and `LocalizableKeys:`.
|
|
26
|
+
#
|
|
27
|
+
# ❌ Hardcoded — can't be translated
|
|
28
|
+
# view.p text: 'Welcome'
|
|
29
|
+
# view.fields_text label: 'Email'
|
|
30
|
+
# view.icon tooltip: { text: 'Share' }
|
|
31
|
+
# json.title 'Forms'
|
|
32
|
+
#
|
|
33
|
+
# ✔️ Resolved through I18n
|
|
34
|
+
# view.p text: t('home.welcome')
|
|
35
|
+
# view.fields_text label: t('user.email')
|
|
36
|
+
# view.p text: "#{t('home.welcome')}, #{user.name}"
|
|
37
|
+
#
|
|
38
|
+
# @example
|
|
39
|
+
# # bad
|
|
40
|
+
# view.p text: 'Welcome'
|
|
41
|
+
#
|
|
42
|
+
# # bad
|
|
43
|
+
# view.fields_text label: 'Email'
|
|
44
|
+
#
|
|
45
|
+
# # bad (nested hash prop)
|
|
46
|
+
# view.icon tooltip: { text: 'Share' }
|
|
47
|
+
#
|
|
48
|
+
# # bad (jbuilder positional)
|
|
49
|
+
# json.title 'Forms'
|
|
50
|
+
#
|
|
51
|
+
# # bad (interpolation, but no translation call)
|
|
52
|
+
# view.p text: "Hi #{name}"
|
|
53
|
+
#
|
|
54
|
+
# # good
|
|
55
|
+
# view.p text: t('home.welcome')
|
|
56
|
+
#
|
|
57
|
+
# # good (interpolates a translation call)
|
|
58
|
+
# view.p text: "#{t('home.greeting')} #{user.name}"
|
|
59
|
+
#
|
|
60
|
+
# # good (non-literal — not flagged)
|
|
61
|
+
# view.p text: user.name
|
|
62
|
+
class RequireTranslation < Base
|
|
63
|
+
include LocalizableProps
|
|
64
|
+
|
|
65
|
+
MSG = 'Localize this string: pass `t(...)` instead of a hardcoded ' \
|
|
66
|
+
'string for `%<key>s:`.'.freeze
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def inspect_localizable(key, value)
|
|
71
|
+
return unless hardcoded_string?(value)
|
|
72
|
+
|
|
73
|
+
add_offense(value, message: format(MSG, key: key))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# A plain string literal is hardcoded copy unless it's blank. A `dstr`
|
|
77
|
+
# (interpolated string) is hardcoded copy too — unless it interpolates
|
|
78
|
+
# a translation call, which counts as positive localization.
|
|
79
|
+
def hardcoded_string?(node)
|
|
80
|
+
return !localized_interpolation?(node) if node.dstr_type?
|
|
81
|
+
return false unless node.str_type?
|
|
82
|
+
|
|
83
|
+
!node.value.strip.empty?
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def localized_interpolation?(node)
|
|
87
|
+
node.each_descendant(:send, :csend).any? do |call|
|
|
88
|
+
TRANSLATION_METHODS.include?(call.method_name)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
module RuboCop
|
|
2
|
+
module Cop
|
|
3
|
+
module DevDoc
|
|
4
|
+
module I18n
|
|
5
|
+
# Require translation keys to start with an allowed namespace prefix.
|
|
6
|
+
#
|
|
7
|
+
# ## Rationale
|
|
8
|
+
# A flat translation catalog drifts into collisions and dead keys.
|
|
9
|
+
# Requiring every `t(...)` key to start with an agreed namespace (e.g.
|
|
10
|
+
# `hotel.`, `general.`) keeps the locale files organized and makes it
|
|
11
|
+
# obvious which feature owns a key.
|
|
12
|
+
#
|
|
13
|
+
# The allowed prefixes are project-specific, so `AllowedPrefixes:`
|
|
14
|
+
# defaults to empty and the cop does nothing until a project configures
|
|
15
|
+
# it. Matches `t`, `translate`, and `I18n.t` (the receiver is ignored).
|
|
16
|
+
#
|
|
17
|
+
# Only statically-literal keys are checked. A key built from
|
|
18
|
+
# interpolation (`t("#{prefix}.foo")`) or a variable (`t(key)`) can't be
|
|
19
|
+
# verified and is skipped. A key that begins with a literal segment
|
|
20
|
+
# (`t("hotel.#{id}")`) is checked against that leading segment.
|
|
21
|
+
#
|
|
22
|
+
# ❌ No recognized prefix
|
|
23
|
+
# t('welcome.title')
|
|
24
|
+
#
|
|
25
|
+
# ✔️ Namespaced
|
|
26
|
+
# t('general.welcome.title')
|
|
27
|
+
# t('hotel.rooms.heading')
|
|
28
|
+
#
|
|
29
|
+
# @example AllowedPrefixes: ['hotel.', 'general.']
|
|
30
|
+
# # bad
|
|
31
|
+
# t('welcome.title')
|
|
32
|
+
#
|
|
33
|
+
# # bad (lazy key has no namespace)
|
|
34
|
+
# t('.title')
|
|
35
|
+
#
|
|
36
|
+
# # good
|
|
37
|
+
# t('hotel.rooms.heading')
|
|
38
|
+
#
|
|
39
|
+
# # good (variable suffix, literal prefix is checked)
|
|
40
|
+
# t("general.#{key}")
|
|
41
|
+
#
|
|
42
|
+
# # not checked (fully dynamic key)
|
|
43
|
+
# t("#{prefix}.title")
|
|
44
|
+
class TranslationKeyPrefix < Base
|
|
45
|
+
MSG = 'Translation key `%<key>s` must start with an allowed ' \
|
|
46
|
+
'prefix: %<prefixes>s.'.freeze
|
|
47
|
+
|
|
48
|
+
RESTRICT_ON_SEND = %i[t translate].freeze
|
|
49
|
+
|
|
50
|
+
def on_send(node)
|
|
51
|
+
prefixes = allowed_prefixes
|
|
52
|
+
return if prefixes.empty?
|
|
53
|
+
|
|
54
|
+
key_node = node.first_argument
|
|
55
|
+
return unless key_node
|
|
56
|
+
|
|
57
|
+
leading = leading_literal(key_node)
|
|
58
|
+
return if leading.nil?
|
|
59
|
+
return if prefixes.any? { |prefix| leading.start_with?(prefix) }
|
|
60
|
+
|
|
61
|
+
add_offense(key_node, message: format(MSG, key: key_node.source, prefixes: prefixes_display(prefixes)))
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
# The statically-known leading portion of the key, or nil when the key
|
|
67
|
+
# is dynamic (a variable, or a string that begins with interpolation).
|
|
68
|
+
def leading_literal(node)
|
|
69
|
+
case node.type
|
|
70
|
+
when :str then node.value
|
|
71
|
+
when :sym then node.value.to_s
|
|
72
|
+
when :dstr
|
|
73
|
+
first = node.children.first
|
|
74
|
+
first&.str_type? ? first.value : nil
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def allowed_prefixes
|
|
79
|
+
Array(cop_config['AllowedPrefixes'])
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def prefixes_display(prefixes)
|
|
83
|
+
prefixes.map { |prefix| "`#{prefix}`" }.join(', ')
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rubocop-dev_doc
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.6.0.beta1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- dev-doc contributors
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activesupport
|
|
@@ -80,8 +79,6 @@ dependencies:
|
|
|
80
79
|
- - ">="
|
|
81
80
|
- !ruby/object:Gem::Version
|
|
82
81
|
version: '2.0'
|
|
83
|
-
description:
|
|
84
|
-
email:
|
|
85
82
|
executables: []
|
|
86
83
|
extensions: []
|
|
87
84
|
extra_rdoc_files: []
|
|
@@ -94,6 +91,11 @@ files:
|
|
|
94
91
|
- lib/rubocop-dev_doc.rb
|
|
95
92
|
- lib/rubocop/cop/dev_doc/auth/current_user_branching.rb
|
|
96
93
|
- lib/rubocop/cop/dev_doc/auth/load_resource_current_user_guard.rb
|
|
94
|
+
- lib/rubocop/cop/dev_doc/i18n/avoid_titleize_humanize.rb
|
|
95
|
+
- lib/rubocop/cop/dev_doc/i18n/localizable_props.rb
|
|
96
|
+
- lib/rubocop/cop/dev_doc/i18n/report_text.rb
|
|
97
|
+
- lib/rubocop/cop/dev_doc/i18n/require_translation.rb
|
|
98
|
+
- lib/rubocop/cop/dev_doc/i18n/translation_key_prefix.rb
|
|
97
99
|
- lib/rubocop/cop/dev_doc/migration/amount_column_in_cents.rb
|
|
98
100
|
- lib/rubocop/cop/dev_doc/migration/avoid_bypassing_validation.rb
|
|
99
101
|
- lib/rubocop/cop/dev_doc/migration/avoid_column_default.rb
|
|
@@ -137,12 +139,10 @@ files:
|
|
|
137
139
|
- lib/rubocop/dev_doc.rb
|
|
138
140
|
- lib/rubocop/dev_doc/plugin.rb
|
|
139
141
|
- lib/rubocop/dev_doc/version.rb
|
|
140
|
-
homepage:
|
|
141
142
|
licenses: []
|
|
142
143
|
metadata:
|
|
143
144
|
default_lint_roller_plugin: RuboCop::DevDoc::Plugin
|
|
144
145
|
rubygems_mfa_required: 'true'
|
|
145
|
-
post_install_message:
|
|
146
146
|
rdoc_options: []
|
|
147
147
|
require_paths:
|
|
148
148
|
- lib
|
|
@@ -157,8 +157,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
157
157
|
- !ruby/object:Gem::Version
|
|
158
158
|
version: '0'
|
|
159
159
|
requirements: []
|
|
160
|
-
rubygems_version:
|
|
161
|
-
signing_key:
|
|
160
|
+
rubygems_version: 4.0.6
|
|
162
161
|
specification_version: 4
|
|
163
162
|
summary: RuboCop cops enforcing dev-doc best practices
|
|
164
163
|
test_files: []
|