summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/rouge-4.7.0/lib/rouge/lexers/jsx.rb
blob: 7b2ad339a5a9745c1330e0e3898b233e2171f313 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Rouge
  module Lexers
    load_lexer 'javascript.rb'

    class JSX < Javascript
      title 'JSX'
      desc 'An XML-like syntax extension to JavaScript (facebook.github.io/jsx/)'
      tag 'jsx'
      aliases 'jsx', 'react'
      filenames '*.jsx'

      mimetypes 'text/x-jsx', 'application/x-jsx'

      start { @html = HTML.new(options); push :expr_start }

      prepend :expr_start do
        mixin :tag
      end

      state :tag do
        rule %r/</ do
          token Punctuation
          push :tag_opening
          push :element
          push :element_name
        end
      end

      state :tag_opening do
        rule %r/<\// do
          token Punctuation
          goto :element
          push :element_name
        end
        mixin :tag
        rule %r/{/ do
          token Str::Interpol
          push :interpol
          push :expr_start
        end
        rule %r/[^<{]+/ do
          delegate @html
        end
      end

      state :element do
        mixin :comments_and_whitespace
        rule %r/\/>/ do
          token Punctuation
          pop! 2
        end
        rule %r/>/, Punctuation, :pop!
        rule %r/{/ do
          token Str::Interpol
          push :interpol
          push :expr_start
        end
        rule %r/\w[\w-]*/, Name::Attribute
        rule %r/=/, Punctuation
        rule %r/(["']).*?(\1)/, Str
      end

      state :element_name do
        rule %r/[A-Z]\w*/, Name::Class
        rule %r/\w+/, Name::Tag
        rule %r/\./, Punctuation
        rule(//) { pop! }
      end

      state :interpol do
        rule %r/}/, Str::Interpol, :pop!
        rule %r/{/ do
          token Punctuation
          push :interpol_inner
          push :statement
        end
        mixin :root
      end

      state :interpol_inner do
        rule %r/}/ do
          token Punctuation
          goto :statement
        end
        mixin :root
      end
    end
  end
end