vinter 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e35b8436cd29035d2d4f78482e343da5cbb88f3972dc47487abdf47b28f4f5ee
4
- data.tar.gz: e6ce76acaf998dc56ea09712c90dea53f348445179dd435f9186c98ab8a3f4f7
3
+ metadata.gz: e9fbaf8c5cf8f88e2454d0402da0eb39ebbd1bfe740c8fcb96edd4252c1d549d
4
+ data.tar.gz: '0699b40b5f811f28b6692c4fbea634255faa6314d6626ad3649948aea4948c02'
5
5
  SHA512:
6
- metadata.gz: 01061b7d58353485bc083c77ca992dc14e8c0395d499d4f9aa704f7efb15fd71c2061464248283b46cda8533a68aca3c8a1d7cf091b029de02a673b86aa7d134
7
- data.tar.gz: e2862a606a016a4ea64a1cd38956dacd816c4e96520ffab058529774106e4484e8905cb72350a83bf1f1870bc8a9d56f54f62899ce9be0167dc22b609b9bb410
6
+ metadata.gz: 895bd9ac2880f80566a08726a5715e0897afc2524e285b97648ffac58cfda6d89c7fc678238f69bc6e79bb2fefcfefa86e3ee051d3f9f9fa3a0a3ba398076f65
7
+ data.tar.gz: fc22105c7d49b896841a82042db736bb9c2c44006ae309ea4193bbdf29747a8d05b04d8e3563be5c5d139e9cc536c301dd0fbcce37a3842a0b4dcdb8fbcd4efc
data/README.md CHANGED
@@ -93,3 +93,27 @@ issues = linter.lint(content)
93
93
  3. Commit your changes: `git commit -am 'Add some feature'`
94
94
  4. Push to the branch: `git push origin my-new-feature`
95
95
  5. Submit a pull request
96
+
97
+ ## Real-world plugin benchmark
98
+
99
+ See the [plugin benchmark report](reports/plugin-benchmark.md) for source revisions,
100
+ physical LOC, lint time, throughput, and diagnostic counts. To refresh it from
101
+ clean plugin checkouts, run these commands from the Vinter repository.
102
+
103
+ ```sh
104
+ git clone --depth 1 https://github.com/monkoose/vim9-stargate /tmp/vim9-stargate
105
+ git clone --depth 1 https://github.com/ShayHill/vim9-limelight /tmp/vim9-limelight
106
+ ruby script/plugin-benchmark.rb /tmp/vim9-stargate /tmp/vim9-limelight > reports/plugin-benchmark.md
107
+ ```
108
+
109
+ For later runs, update the checkouts first. Pass additional checkout paths to
110
+ expand coverage. Use the same machine and revisions for speed comparisons.
111
+
112
+ Exclude a plugin revision from the published corpus when Vim rejects a scanned
113
+ file or fails to compile a function in it, including test fixtures. Recheck the
114
+ failure before adding a newer revision. Two revisions excluded using Vim 9.2:
115
+
116
+ - `Bakudankun/qline.vim@e51daab` — compiling the palette code reaches
117
+ `GetLightlinePalette()` and raises E1027 (missing return statement).
118
+ - `ubaldot/vim9-conversion-aid@a2a49e6` — Vim rejects eight scoped variable
119
+ declarations in `test/expected_script.vim` (E1268 and E1069).
data/lib/vinter/lexer.rb CHANGED
@@ -583,7 +583,7 @@ module Vinter
583
583
  #builtin_funcs: /\b(highlight||normal!|normal|filter|match|extend|redraw!|setbufline)\b/,
584
584
  type: /\b(number|list|dict|void|string)\b/,
585
585
  mode_command: /\b(?:normal|norm)(?:!)?(?!\w).*$/,
586
- exec_command: /\b(?:execute|exec).*$/,
586
+ exec_command: /\b(?:execute|exec|exe)\b(?![:(.]).*$/,
587
587
  builtin_funcs: /\b(#{Regexp.union(BUILTINS).source})\b/,
588
588
  # Identifiers can include # and special characters
589
589
  heredoc: /=<</,
@@ -593,6 +593,7 @@ module Vinter
593
593
  number: /\b(0[xX][0-9A-Fa-f]+|0[oO][0-7]+|0[bB][01]+|\d+(\.\d+)?([eE][+-]?\d+)?[smh]?)\b/,
594
594
  # Handle both single and double quoted strings
595
595
  # string: /"(\\"|[^"])*"|'(\\'|[^'])*'/,
596
+ environment_variable: /\$[A-Za-z_][A-Za-z0-9_]*/,
596
597
  register_access: /@[a-zA-Z0-9":.%#=*+~_\/\-]/,
597
598
  identifier: /\b[a-zA-Z_][a-zA-Z0-9_#]*/,
598
599
  # Vim9 comments use #
@@ -612,8 +613,9 @@ module Vinter
612
613
  command_separator: /\|/,
613
614
  }
614
615
 
615
- CONTINUATION_OPERATORS = %w(. .. + - * / = == ==# ==? != > < >= <= && || ? : -> =>)
616
+ CONTINUATION_OPERATORS = %w(. .. + - * / = == ==# ==? != > < >= <= && || ?? ? : -> =>)
616
617
  def initialize(input)
618
+ @vim9script = false
617
619
  @input = input
618
620
  @tokens = []
619
621
  @variables = []
@@ -628,7 +630,7 @@ module Vinter
628
630
 
629
631
  # Check for contexts where regex is expected
630
632
  return true if recent_tokens.any? { |t|
631
- t && t[:type] == :keyword && ['syntax'].include?(t[:value])
633
+ t[:line] == @line_num && %w[syntax syn].include?(t[:value]) && [:identifier, :keyword].include?(t[:type])
632
634
  }
633
635
 
634
636
  return true if recent_tokens.any? { |t|
@@ -654,6 +656,49 @@ module Vinter
654
656
  nil # Return nil if no unescaped newline is found
655
657
  end
656
658
 
659
+ # Legacy assignment and option commands accept comments after a complete
660
+ # value. Expression commands such as echo can take adjacent strings.
661
+ def trailing_legacy_comment?
662
+ previous = @tokens.last
663
+ return false unless previous && previous[:line] == @line_num
664
+ return false unless @input[@position - 1].match?(/[ \t]/)
665
+ return true if previous[:type] == :keyword && %w[else endif endfor endwhile endfunction].include?(previous[:value])
666
+ return false unless [:number, :string, :identifier, :type, :paren_close,
667
+ :bracket_close, :brace_close, :option_variable,
668
+ :global_variable, :local_variable, :arg_variable].include?(previous[:type])
669
+
670
+ line_tokens = @tokens.reverse_each.take_while { |token| token[:line] == @line_num }
671
+ %w[let const set setlocal for if elseif while].include?(line_tokens.last[:value])
672
+ end
673
+
674
+ def tokenize_syntax_arguments
675
+ loop do
676
+ rest = @input[@position..]
677
+ if (continuation = rest.match(/\A[ \t]*\r?\n([ \t]*)\\/))
678
+ @position += continuation.end(0)
679
+ @line_num += 1
680
+ @column = continuation[1].length + 2
681
+ @tokens << { type: :line_continuation, value: "\\", line: @line_num, column: @column - 1 }
682
+ next
683
+ end
684
+ break if rest.empty? || rest.start_with?("\r", "\n", '|')
685
+
686
+ if (space = rest.match(/\A[ \t]+/))
687
+ @position += space.end(0)
688
+ @column += space.end(0)
689
+ next
690
+ end
691
+
692
+ # Match a delimited pattern, including region start=/.../ arguments.
693
+ pattern = rest.match(/\A(?:(?:start|end|skip)=)?([^\w\s\\|])(?:\\[^\r\n]|(?!\1)[^\r\n])*?\1/)
694
+ word = pattern ? pattern[0] : rest[/\A(?:\\[^\r\n]|[^\s|])+/]
695
+ break unless word
696
+ @tokens << { type: pattern ? :regex : :identifier, value: word, line: @line_num, column: @column }
697
+ @position += word.length
698
+ @column += word.length
699
+ end
700
+ end
701
+
657
702
  def tokenize
658
703
  until @position >= @input.length
659
704
  chunk = @input[@position..-1]
@@ -670,23 +715,48 @@ module Vinter
670
715
  line_start = temp_pos < 0 || @input[temp_pos] == "\n"
671
716
  end
672
717
 
673
- # If we're at the start of a line and it begins with a quote
674
- #if line_start && chunk.start_with?('"')
675
- ## Find the end of the line
676
- #line_end = find_unescaped_newline(chunk) || chunk.length
677
- #comment_text = chunk[0...line_end]
718
+ if !@vim9script && chunk.start_with?('"') && (line_start || trailing_legacy_comment?)
719
+ comment = chunk[/[^\n]*/]
720
+ @tokens << { type: :comment, value: comment, line: @line_num, column: @column }
721
+ @position += comment.length
722
+ @column += comment.length
723
+ next
724
+ end
725
+
726
+ # Patterns in syntax definitions use Ex quoting and can contain
727
+ # characters that would start comments or strings in Vimscript.
728
+ if (line_start || @tokens.last&.dig(:value) == '|') &&
729
+ (header = chunk.match(/\A(syn(?:tax)?)[ \t]+(match|region|keyword|cluster)\b[ \t]+(\w+)/))
730
+ command, subcommand, group = header.captures
731
+ @tokens << { type: :keyword, value: command == 'syn' ? 'syntax' : command, line: @line_num, column: @column }
732
+ @tokens << { type: :identifier, value: subcommand, line: @line_num, column: @column + header.begin(2) }
733
+ @tokens << { type: :identifier, value: group, line: @line_num, column: @column + header.begin(3) }
734
+ @position += header.end(0)
735
+ @column += header.end(0)
736
+ tokenize_syntax_arguments
737
+ next
738
+ end
678
739
 
679
- #@tokens << {
680
- #type: :comment,
681
- #value: comment_text,
682
- #line: @line_num,
683
- #column: @column
684
- #}
740
+ # Ex range commands have their own quoting (including marks such as
741
+ # '[ and search addresses). Keep the address out of expression lexing.
742
+ if line_start && (ex = chunk.match(/\A:(?:(?:[0-9$'])[^|\r\n]*?[ \t]+(?:substitute|sort)\b|substitute\b)(?:\\[^\r\n]|[^|\r\n])*/))
743
+ @tokens << { type: :ex_command_line, value: ex[0], line: @line_num, column: @column }
744
+ @position += ex[0].length
745
+ @column += ex[0].length
746
+ next
747
+ end
685
748
 
686
- #@position += comment_text.length
687
- #@column += comment_text.length
688
- #next
689
- #end
749
+ # Terminal mappings contain literal key sequences and Ex command text.
750
+ # Tokenizing their RHS as Vim expressions changes its meaning.
751
+ if (line_start || @tokens.last&.dig(:value) == '|') &&
752
+ (mapping = chunk.match(/\A(tnoremap|tmap)(?=[ \t]|\r?$)((?:\\[^\r\n]|\x16[^\r\n]|[^|\r\n])*)/))
753
+ command, arguments = mapping.captures
754
+ @tokens << { type: :terminal_mapping, value: command, arguments: arguments,
755
+ line: @line_num, column: @column }
756
+ @position += mapping[0].length
757
+ @column += mapping[0].length
758
+ next
759
+ end
690
760
 
691
761
  # --- Interpolated String Handling ---
692
762
  if chunk.start_with?("$'")
@@ -777,12 +847,15 @@ module Vinter
777
847
  # Keep going until we find an unescaped closing quote
778
848
  while i < chunk.length
779
849
  char = chunk[i]
780
- next_char = chunk[i + 1] if i + 1 < chunk.length
850
+ next_char = chunk[i + 1]
781
851
 
782
852
  string_value += char
783
853
 
784
- if char == quote && next_char == quote
785
- # Handle escaped single quote ('') or double quote ("")
854
+ if quote == '"' && char == "\\" && next_char
855
+ string_value += next_char
856
+ i += 1
857
+ elsif quote == "'" && char == quote && next_char == quote
858
+ # A doubled single quote represents a literal apostrophe.
786
859
  string_value += next_char
787
860
  i += 1
788
861
  elsif char == quote
@@ -809,8 +882,8 @@ module Vinter
809
882
  end
810
883
 
811
884
  # Add special handling for command options in the tokenize method
812
- if chunk.start_with?('<q-args>', '<f-args>', '<args>')
813
- arg_token = chunk.match(/\A(<q-args>|<f-args>|<args>)/)[0]
885
+ if chunk.start_with?('<q-args>', '<f-args>', '<args>', '<q-mods>')
886
+ arg_token = chunk.match(/\A(<q-args>|<f-args>|<args>|<q-mods>)/)[0]
814
887
  @tokens << {
815
888
  type: :command_arg_placeholder,
816
889
  value: arg_token,
@@ -982,7 +1055,8 @@ module Vinter
982
1055
  end
983
1056
 
984
1057
  # Add support for standalone namespace prefixes (like g:)
985
- if match = chunk.match(/\A([gbwt]):/)
1058
+ if (match = chunk.match(/\A([gbwt]):/)) &&
1059
+ !(@tokens.last&.dig(:line) == @line_num && %w[var const final].include?(@tokens.last[:value]))
986
1060
  @tokens << {
987
1061
  type: :namespace_prefix,
988
1062
  value: match[0],
@@ -1024,7 +1098,7 @@ module Vinter
1024
1098
  end
1025
1099
 
1026
1100
  # Handle multi-character operators explicitly
1027
- if match = chunk.match(/\A(=~#|=~\?|=~|!~#|!~\?|!~|==#|==\?|==|!=#|!=\?|!=|=>\?|=>|>=#|>=\?|>=|<=#|<=\?|<=|->#|->\?|->|\.\.|\|\||&&)/)
1101
+ if match = chunk.match(/\A(=~#|=~\?|=~|!~#|!~\?|!~|==#|==\?|==|!=#|!=\?|!=|=>\?|=>|>=#|>=\?|>=|<=#|<=\?|<=|[<>][#?]|->#|->\?|->|\.\.|\|\||&&|\?\?)/)
1028
1102
  @tokens << {
1029
1103
  type: :operator,
1030
1104
  value: match[0],
@@ -1038,31 +1112,9 @@ module Vinter
1038
1112
 
1039
1113
  # Handle regex patterns /pattern/ - only in specific contexts
1040
1114
  if chunk.start_with?('/') && should_parse_as_regex
1041
- i = 1
1042
- regex_value = '/'
1043
-
1044
- # Keep going until we find the closing slash
1045
- while i < chunk.length
1046
- char = chunk[i]
1047
- regex_value += char
1048
-
1049
- if char == '/' && (i == 1 || chunk[i-1] != '\\')
1050
- # Found closing slash
1051
- i += 1
1052
- break
1053
- end
1054
-
1055
- i += 1
1056
- end
1057
-
1058
- # Add the regex token if we found a closing slash
1059
- if regex_value.end_with?('/')
1060
- @tokens << {
1061
- type: :regex,
1062
- value: regex_value,
1063
- line: @line_num,
1064
- column: @column
1065
- }
1115
+ # A slash escaped by an odd number of backslashes is pattern text.
1116
+ if (regex_value = chunk[/\A\/(?:\\[^\r\n]|[^\\\/\r\n])*\//])
1117
+ @tokens << { type: :regex, value: regex_value, line: @line_num, column: @column }
1066
1118
  @column += regex_value.length
1067
1119
  @position += regex_value.length
1068
1120
  next
@@ -1132,6 +1184,9 @@ module Vinter
1132
1184
  match_found = false
1133
1185
 
1134
1186
  TOKEN_TYPES.each do |type, pattern|
1187
+ # echohl takes a highlight group name, including Normal.
1188
+ next if type == :mode_command && @tokens.last&.values_at(:value, :line) == ['echohl', @line_num]
1189
+
1135
1190
  if match = chunk.match(/\A(#{pattern})/)
1136
1191
  value = match[0]
1137
1192
  token = {
@@ -1142,7 +1197,7 @@ module Vinter
1142
1197
  }
1143
1198
 
1144
1199
  # handling variable names that are also keywords
1145
- if !@tokens.empty? && @tokens.last[:value] == 'var'
1200
+ if @tokens.last && @tokens.last[:type] == :keyword && %w[var const final].include?(@tokens.last[:value]) && [:identifier, :keyword, :type].include?(type)
1146
1201
  @variables << value
1147
1202
  token[:type] = :identifier
1148
1203
  end
@@ -1150,6 +1205,7 @@ module Vinter
1150
1205
  #if type != :whitespace
1151
1206
  #@tokens << token
1152
1207
  #end
1208
+ @vim9script = true if line_start && token[:type] == :keyword && value == 'vim9script'
1153
1209
  @tokens << token unless type == :whitespace
1154
1210
 
1155
1211
  # Update position
data/lib/vinter/linter.rb CHANGED
@@ -37,6 +37,16 @@ module Vinter
37
37
  issues
38
38
  end)
39
39
 
40
+ register_rule(Rule.new("prefer-def-over-function", "Prefer def to legacy function declarations") do |ast|
41
+ issues = []
42
+ traverse_ast(ast) do |node|
43
+ if node[:type] == :legacy_function
44
+ issues << { message: "Use def for #{node[:name]}", line: node[:line], column: node[:column] }
45
+ end
46
+ end
47
+ issues
48
+ end)
49
+
40
50
  # Rule: Functions should have return type annotations
41
51
  register_rule(Rule.new("missing-return-type", "Function is missing return type annotation") do |ast|
42
52
  issues = []