Class: Shirobai::Cop::Naming::AsciiIdentifiers
- Inherits:
-
RuboCop::Cop::Base
- Object
- RuboCop::Cop::Base
- Shirobai::Cop::Naming::AsciiIdentifiers
- Includes:
- RuboCop::Cop::RangeHelp, BundleEligible
- Defined in:
- lib/shirobai/cop/naming/ascii_identifiers.rb
Overview
Drop-in Rust reimplementation of Naming/AsciiIdentifiers.
Stock iterates processed_source.tokens (the parser-gem token stream,
materialized on every file — the "toucher" cost) and flags every
tIDENTIFIER (always) and tCONSTANT (when AsciiConstants) whose text
has a non-ASCII byte, at the first maximal run of non-ASCII chars in the
token. No autocorrect.
Only a file that HAS a non-ASCII byte can ever offend, so the Rust rule
fast-paths every all-ASCII file with a single is_ascii scan and builds
no token stream at all. On the rare non-ASCII file it reads prism's own
lex tokens and maps them to the parser-gem tIDENTIFIER / tCONSTANT
distinction stock tests (validated against stock over every non-ASCII
file in the five corpora). The wrapper converts the byte range to char
offsets and builds stock's exact offense with range_between.
Constant Summary collapse
- IDENTIFIER_MSG =
"Use only ascii symbols in identifiers."- CONSTANT_MSG =
"Use only ascii symbols in constants."
Class Method Summary collapse
- .badge ⇒ Object
-
.bundle_args(config) ⇒ Object
Packed as a single 3-state num: 0 disabled (bundle skips the lex), 1 enabled with AsciiConstants off, 2 enabled with AsciiConstants on.
- .cop_name ⇒ Object
Instance Method Summary collapse
Class Method Details
.badge ⇒ Object
29 |
# File 'lib/shirobai/cop/naming/ascii_identifiers.rb', line 29 def self.badge = RuboCop::Cop::Badge.parse(cop_name) |
.bundle_args(config) ⇒ Object
Packed as a single 3-state num: 0 disabled (bundle skips the lex), 1 enabled with AsciiConstants off, 2 enabled with AsciiConstants on.
33 34 35 36 37 38 |
# File 'lib/shirobai/cop/naming/ascii_identifiers.rb', line 33 def self.bundle_args(config) cop_config = config.for_badge(badge) return [[0]] if cop_config["Enabled"] == false [[cop_config.fetch("AsciiConstants", true) ? 2 : 1]] end |
.cop_name ⇒ Object
28 |
# File 'lib/shirobai/cop/naming/ascii_identifiers.rb', line 28 def self.cop_name = "Naming/AsciiIdentifiers" |
Instance Method Details
#on_new_investigation ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/shirobai/cop/naming/ascii_identifiers.rb', line 40 def on_new_investigation buffer = processed_source.buffer source = bundle_eligible? ? processed_source.raw_source : buffer.source off = SourceOffsets.for(source) resolved_result(source).each do |is_constant, start, fin| range = range_between(off[start], off[fin]) add_offense(range, message: is_constant ? CONSTANT_MSG : IDENTIFIER_MSG) end end |