Class: SasLinter::Rules::CommentedOutGuard

Inherits:
SasLinter::Rule show all
Defined in:
lib/sas_linter/rules/commented_out_guard.rb

Overview

Flag SAS line-comments (‘* … ;`) whose body looks like a disabled validity guard — specifically, the body contains both an `IF` and a `THEN DO` (case-insensitive).

Motivating shape: a source’s outer ‘if … then do;` validity guard is commented out by a leading `*`, leaving an orphan `end;` further down. The body then runs unguarded for inputs the guard would have rejected. Worth a human review on each finding — either the guard should be live, or the orphan `end;` should be removed.

Constant Summary collapse

TT =
SasLexer::Lexer::TokenType
TC =
SasLexer::Lexer::TokenChannel
GUARD_PATTERN =

Match ‘if … then do` anywhere in the comment body, case-insensitive. Look for `then` followed (after whitespace and possibly more tokens) by `do` — the SAS authoring style where the guard expression is spread across multiple lines.

/\bif\b.*\bthen\b\s+do\b/im

Instance Attribute Summary

Attributes inherited from SasLinter::Rule

#autofix

Instance Method Summary collapse

Methods inherited from SasLinter::Rule

all, #autofix?, description, fetch, from_config, inherited, #initialize, register, registry, rule_id, severity, supports_autofix?

Constructor Details

This class inherits a constructor from SasLinter::Rule

Instance Method Details

#check(_tokens, path:, all_tokens: nil, source: nil) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sas_linter/rules/commented_out_guard.rb', line 33

def check(_tokens, path:, all_tokens: nil, source: nil) # rubocop:disable Lint/UnusedMethodArgument
  return [] unless all_tokens

  all_tokens.filter_map do |tok|
    next unless tok[:channel] == TC::COMMENT
    next unless tok[:type] == TT::COMMENT_STAT

    body = tok[:text]
    # Only flag SAS statement-comments that start with `*` (not `**`),
    # since `** ... **;` is a header comment style and `* ...;` is
    # the disable-this-statement style.
    next unless body =~ /\A\s*\*(?!\*)/
    next unless body =~ GUARD_PATTERN

    finding(
      line: tok[:start_line],
      column: tok[:start_column] + 1,
      message: "looks like a disabled validity guard (`* if ... then do; ...`); " \
               "review whether the guard should be live or whether the matching " \
               "`end;` is now orphaned.",
      path: path
    )
  end
end