Class: SasLinter::Rules::LineEndings
- Inherits:
-
SasLinter::Rule
- Object
- SasLinter::Rule
- SasLinter::Rules::LineEndings
- Defined in:
- lib/sas_linter/rules/line_endings.rb
Overview
Flag non-standard line endings in SAS sources. Two patterns appear in legacy SAS sources and tend to be hand-fixed when they show up:
1. `\r\r\n` — double CR before LF. Word/Outlook copy-paste
injects an extra CR; SAS Viya tolerates it but downstream
tools and diffs treat the file as if every line had a
trailing literal CR character.
2. Lone `\r` (CR not followed by LF) — old-Mac CR-only
endings. SAS Viya treats the entire file as one logical
line, breaking saspy's shard-based submission flow.
Autofix collapses ‘rrn` to `rn` unconditionally and maps every lone `r` to the file’s dominant ending: ‘rn` if the source has any CRLF (i.e. it’s a Windows file with stragglers), ‘n` otherwise (i.e. pure-CR file → POSIX).
Recognized config options:
autofix: true | false (default: false)
Class Method Summary collapse
Instance Method Summary collapse
-
#autofix(source) ⇒ Object
Collapse ‘rrn` to `rn`; map every remaining lone `r` to the file’s dominant terminator (‘rn` if any CRLF survives, else `n`).
-
#check(_tokens, path:, all_tokens: nil, source: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
Methods inherited from SasLinter::Rule
all, #autofix?, description, fetch, from_config, inherited, #initialize, register, registry, rule_id, severity
Constructor Details
This class inherits a constructor from SasLinter::Rule
Class Method Details
.supports_autofix? ⇒ Boolean
32 33 34 |
# File 'lib/sas_linter/rules/line_endings.rb', line 32 def self.supports_autofix? true end |
Instance Method Details
#autofix(source) ⇒ Object
Collapse ‘rrn` to `rn`; map every remaining lone `r` to the file’s dominant terminator (‘rn` if any CRLF survives, else `n`).
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sas_linter/rules/line_endings.rb', line 87 def autofix(source) # Step 1: remove the duplicate CR in `\r\r\n` sequences. This # leaves at most one `\r` adjacent to `\n` (real CRLF) and # any other `\r` on its own. step1 = source.b.gsub(/\r\r\n/, "\r\n") # Step 2: pick the dominant terminator. `\r\n` wins if there # are any CRLF sequences; otherwise we collapse to LF. dominant_crlf = step1.include?("\r\n") replacement = dominant_crlf ? "\r\n" : "\n" # Step 3: replace every lone `\r` (not followed by `\n`) with # the dominant ending. The negative lookahead leaves real # CRLF intact when CRLF is the dominant style. step1.gsub(/\r(?!\n)/, replacement).force_encoding(source.encoding) end |
#check(_tokens, path:, all_tokens: nil, source: nil) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/sas_linter/rules/line_endings.rb', line 36 def check(_tokens, path:, all_tokens: nil, source: nil) # rubocop:disable Lint/UnusedMethodArgument return [] unless source findings = [] bytes = source.b.bytes line = 1 col = 1 i = 0 n = bytes.length while i < n b = bytes[i] if b == 0x0D && bytes[i + 1] == 0x0D && bytes[i + 2] == 0x0A findings << finding( line: line, column: col, message: "double CR before LF (\\r\\r\\n)#{autofix? ? ' (autofixed)' : ''}", path: path ) line += 1 col = 1 i += 3 elsif b == 0x0D && bytes[i + 1] == 0x0A line += 1 col = 1 i += 2 elsif b == 0x0D findings << finding( line: line, column: col, message: "lone CR (\\r)#{autofix? ? ' (autofixed)' : ''}", path: path ) line += 1 col = 1 i += 1 elsif b == 0x0A line += 1 col = 1 i += 1 else col += 1 i += 1 end end findings end |