Module: Docscribe::Types::Yard::Validator
- Defined in:
- lib/docscribe/types/yard/validator.rb
Overview
Validates YARD type strings for syntax errors.
This validator is intentionally lightweight and does not perform semantic type-existence checks (e.g. ‘Symbol2l` vs `Symbol`). Semantic checks are handled by `Docscribe::Validator::TypeMismatchValidator` which compares a YARD tag against the inferred / RBS type.
Class Method Summary collapse
- .artefact_type?(type_str) ⇒ Boolean
-
.balanced_brackets?(type_str) ⇒ Boolean
Check balanced brackets for ‘<>`, `()`, `{}`, `[]`.
- .blank_type?(type_str) ⇒ Boolean
-
.fully_consumed?(type_str) ⇒ Boolean
Whether ‘Yard::Parser` consumes the entire string.
- .process_bracket_char?(char, stack, pairs, opens) ⇒ Boolean
-
.syntax_valid?(type_str) ⇒ Boolean
Strict syntax validation with balanced brackets and full consumption.
-
.valid?(type_str) ⇒ Boolean
Check if a YARD type string is syntactically valid.
Class Method Details
.artefact_type?(type_str) ⇒ Boolean
module_function: defines #artefact_type? (visibility: private)
63 64 65 66 67 |
# File 'lib/docscribe/types/yard/validator.rb', line 63 def artefact_type?(type_str) return false if type_str.nil? type_str.include?(',,') || type_str.include?('<>') || type_str.include?(',]') end |
.balanced_brackets?(type_str) ⇒ Boolean
module_function: defines #balanced_brackets? (visibility: private)
Check balanced brackets for ‘<>`, `()`, `{}`, `[]`.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/docscribe/types/yard/validator.rb', line 74 def balanced_brackets?(type_str) return false if type_str.nil? stack = [] #: Array[String] pairs = { '>' => '<', ')' => '(', '}' => '{', ']' => '[' } opens = pairs.values type_str.each_char do |ch| return false unless process_bracket_char?(ch, stack, pairs, opens) end stack.empty? end |
.blank_type?(type_str) ⇒ Boolean
module_function: defines #blank_type? (visibility: private)
56 57 58 |
# File 'lib/docscribe/types/yard/validator.rb', line 56 def blank_type?(type_str) type_str.nil? || type_str.strip.empty? end |
.fully_consumed?(type_str) ⇒ Boolean
module_function: defines #fully_consumed? (visibility: private)
Whether ‘Yard::Parser` consumes the entire string.
Catches cases like ‘Sym bol` where parser would return `Sym` and leave ` bol` unconsumed.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/docscribe/types/yard/validator.rb', line 113 def fully_consumed?(type_str) return false if type_str.nil? stripped = type_str.strip parser = Parser.new(stripped) node = parser.parse return false unless node idx = parser.instance_variable_get(:@i) # `Parser#parse` calls `skip_space` before and after `parse_union`, # so `idx` should be at the end if fully consumed. idx == stripped.length rescue StandardError false end |
.process_bracket_char?(char, stack, pairs, opens) ⇒ Boolean
module_function: defines #process_bracket_char? (visibility: private)
94 95 96 97 98 99 100 101 |
# File 'lib/docscribe/types/yard/validator.rb', line 94 def process_bracket_char?(char, stack, pairs, opens) if opens.include?(char) stack << char elsif pairs.key?(char) return false if stack.empty? || stack.pop != pairs[char] end true end |
.syntax_valid?(type_str) ⇒ Boolean
module_function: defines #syntax_valid? (visibility: private)
Strict syntax validation with balanced brackets and full consumption.
43 44 45 46 47 48 49 50 51 |
# File 'lib/docscribe/types/yard/validator.rb', line 43 def syntax_valid?(type_str) return false if blank_type?(type_str) return false unless balanced_brackets?(type_str) return false if artefact_type?(type_str) fully_consumed?(type_str) rescue StandardError false end |
.valid?(type_str) ⇒ Boolean
module_function: defines #valid? (visibility: private)
Check if a YARD type string is syntactically valid.
Criteria:
-
non-empty after strip
-
brackets are balanced (‘<>`, `()`, `{}`, `[]`)
-
no ‘,,`, `<>`, `,]` artefacts
-
‘Yard::Parser` consumes the entire string (catches `Sym bol` leftover)
30 31 32 33 34 |
# File 'lib/docscribe/types/yard/validator.rb', line 30 def valid?(type_str) syntax_valid?(type_str) rescue StandardError false end |