Class: Isort::Parser
- Inherits:
-
Object
- Object
- Isort::Parser
- Defined in:
- lib/isort/parser.rb
Overview
Parses Ruby source lines and classifies them by type This is the core component for understanding file structure
Constant Summary collapse
- LINE_TYPES =
Line types that can be returned
%i[ shebang magic_comment require require_relative include extend autoload using comment blank code ].freeze
- IMPORT_TYPES =
%i[require require_relative include extend autoload using].freeze
- MAGIC_COMMENT_PATTERN =
Magic comment patterns (encoding, frozen_string_literal, etc.)
/^#\s*(?:encoding|coding|frozen_string_literal|warn_indent|shareable_constant_value):/i- SKIP_LINE_PATTERN =
Skip directive patterns
/#\s*isort:\s*skip\b/i.freeze
- SKIP_FILE_PATTERN =
/^#\s*isort:\s*skip_file\b/i.freeze
Instance Method Summary collapse
-
#classify_line(line, line_number: nil) ⇒ Object
Classify a single line and return its type.
-
#extract_indentation(line) ⇒ Object
Extract the indentation from a line.
-
#has_skip_directive?(line) ⇒ Boolean
Check if line has an isort:skip directive.
-
#has_skip_file_directive?(line) ⇒ Boolean
Check if line has an isort:skip_file directive.
-
#import_type?(type) ⇒ Boolean
Check if a line type is an import.
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
-
#shebang?(line, line_number) ⇒ Boolean
Check if line is a shebang (must be line 1).
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
31 32 33 |
# File 'lib/isort/parser.rb', line 31 def initialize @line_number = 0 end |
Instance Method Details
#classify_line(line, line_number: nil) ⇒ Object
Classify a single line and return its type
36 37 38 39 40 41 42 43 44 |
# File 'lib/isort/parser.rb', line 36 def classify_line(line, line_number: nil) @line_number = line_number if line_number stripped = line.to_s.strip return :blank if stripped.empty? return classify_comment(line, stripped) if stripped.start_with?("#") classify_code(stripped) end |
#extract_indentation(line) ⇒ Object
Extract the indentation from a line
52 53 54 55 |
# File 'lib/isort/parser.rb', line 52 def extract_indentation(line) match = line.to_s.match(/^(\s*)/) match ? match[1] : "" end |
#has_skip_directive?(line) ⇒ Boolean
Check if line has an isort:skip directive
63 64 65 |
# File 'lib/isort/parser.rb', line 63 def has_skip_directive?(line) line.to_s.match?(SKIP_LINE_PATTERN) && !line.to_s.match?(SKIP_FILE_PATTERN) end |
#has_skip_file_directive?(line) ⇒ Boolean
Check if line has an isort:skip_file directive
68 69 70 |
# File 'lib/isort/parser.rb', line 68 def has_skip_file_directive?(line) line.to_s.match?(SKIP_FILE_PATTERN) end |
#import_type?(type) ⇒ Boolean
Check if a line type is an import
47 48 49 |
# File 'lib/isort/parser.rb', line 47 def import_type?(type) IMPORT_TYPES.include?(type) end |
#shebang?(line, line_number) ⇒ Boolean
Check if line is a shebang (must be line 1)
58 59 60 |
# File 'lib/isort/parser.rb', line 58 def shebang?(line, line_number) line_number == 1 && line.to_s.strip.start_with?("#!") end |