Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/jsx.py: 100%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2 pygments.lexers.jsx
3 ~~~~~~~~~~~~~~~~~~~
5 Lexers for JSX (React) and TSX (TypeScript flavor).
7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11import re
13from pygments.lexer import bygroups, default, include, inherit
14from pygments.lexers.javascript import JavascriptLexer, TypeScriptLexer
15from pygments.token import Name, Operator, Punctuation, String, Text, \
16 Whitespace
18__all__ = ['JsxLexer', 'TsxLexer']
20_JSX_RULES = {
21 "jsx": [
22 (r"</?>", Punctuation), # JSXFragment <>|</>
23 (r"(<)(\w+)(\.?)", bygroups(Punctuation, Name.Tag, Punctuation), "tag"),
24 (
25 r"(</)(\w+)(>)",
26 bygroups(Punctuation, Name.Tag, Punctuation),
27 ),
28 (
29 r"(</)(\w+)",
30 bygroups(Punctuation, Name.Tag),
31 "fragment",
32 ), # Same for React.Context
33 ],
34 "tag": [
35 (r"\s+", Whitespace),
36 (r"([\w-]+)(\s*)(=)(\s*)", bygroups(Name.Attribute, Whitespace, Operator, Whitespace), "attr"),
37 (r"[{}]+", Punctuation),
38 (r"[\w\.]+", Name.Attribute),
39 (r"(/?)(\s*)(>)", bygroups(Punctuation, Text, Punctuation), "#pop"),
40 ],
41 "fragment": [
42 (r"(.)(\w+)", bygroups(Punctuation, Name.Attribute)),
43 (r"(>)", bygroups(Punctuation), "#pop"),
44 ],
45 "attr": [
46 (r"\{", Punctuation, "expression"),
47 (r'".*?"', String, "#pop"),
48 (r"'.*?'", String, "#pop"),
49 default("#pop"),
50 ],
51 "expression": [
52 (r"\{", Punctuation, "#push"),
53 (r"\}", Punctuation, "#pop"),
54 include("root"),
55 ],
56}
59class JsxLexer(JavascriptLexer):
60 """For JavaScript Syntax Extension (JSX).
61 """
63 name = "JSX"
64 aliases = ["jsx", "react"]
65 filenames = ["*.jsx", "*.react"]
66 mimetypes = ["text/jsx", "text/typescript-jsx"]
67 url = "https://facebook.github.io/jsx/"
68 version_added = '2.17'
70 flags = re.MULTILINE | re.DOTALL
72 # Use same tokens as `JavascriptLexer`, but with tags and attributes support
73 tokens = {
74 "root": [
75 include("jsx"),
76 inherit,
77 (r"'", Text),
78 ],
79 **_JSX_RULES}
82class TsxLexer(TypeScriptLexer):
83 """For TypeScript with embedded JSX
84 """
86 name = "TSX"
87 aliases = ["tsx"]
88 filenames = ["*.tsx"]
89 mimetypes = ["text/typescript-tsx"]
90 url = "https://www.typescriptlang.org/docs/handbook/jsx.html"
91 version_added = '2.19'
93 flags = re.MULTILINE | re.DOTALL
95 # Use same tokens as `TypescriptLexer`, but with tags and attributes support
96 tokens = {
97 "root": [
98 include("jsx"),
99 inherit,
100 (r"'", Text),
101 ],
102 **_JSX_RULES}