1
1
# frozen_string_literal: true
2
2
3
- require "ripper"
3
+ begin
4
+ require "prism"
5
+ rescue LoadError
6
+ # If Prism isn't available (because of using an older Ruby version) then we'll
7
+ # define a fallback for the ParserExtractor using ripper.
8
+ require "ripper"
9
+ end
4
10
5
11
module Rails
6
12
# Implements the logic behind +Rails::Command::NotesCommand+. See <tt>rails notes --help</tt> for usage information.
@@ -16,24 +22,35 @@ class SourceAnnotationExtractor
16
22
# Wraps a regular expression that will be tested against each of the source
17
23
# file's comments.
18
24
class ParserExtractor < Struct . new ( :pattern )
19
- class Parser < Ripper
20
- attr_reader :comments , :pattern
25
+ if defined? ( Prism )
26
+ def annotations ( file )
27
+ result = Prism . parse_file ( file )
28
+ return [ ] unless result . success?
21
29
22
- def initialize ( source , pattern :)
23
- super ( source )
24
- @pattern = pattern
25
- @comments = [ ]
30
+ result . comments . filter_map do |comment |
31
+ Annotation . new ( comment . location . start_line , $1, $2) if comment . location . slice =~ pattern
32
+ end
26
33
end
34
+ else
35
+ class Parser < Ripper
36
+ attr_reader :comments , :pattern
37
+
38
+ def initialize ( source , pattern :)
39
+ super ( source )
40
+ @pattern = pattern
41
+ @comments = [ ]
42
+ end
27
43
28
- def on_comment ( value )
29
- @comments << Annotation . new ( lineno , $1, $2) if value =~ pattern
44
+ def on_comment ( value )
45
+ @comments << Annotation . new ( lineno , $1, $2) if value =~ pattern
46
+ end
30
47
end
31
- end
32
48
33
- def annotations ( file )
34
- contents = File . read ( file , encoding : Encoding ::BINARY )
35
- parser = Parser . new ( contents , pattern : pattern ) . tap ( &:parse )
36
- parser . error? ? [ ] : parser . comments
49
+ def annotations ( file )
50
+ contents = File . read ( file , encoding : Encoding ::BINARY )
51
+ parser = Parser . new ( contents , pattern : pattern ) . tap ( &:parse )
52
+ parser . error? ? [ ] : parser . comments
53
+ end
37
54
end
38
55
end
39
56
0 commit comments