Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion jsx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .lexer import JsxLexer # noqa
from .lexer import TsxLexer

__all__ = ["JsxLexer"]

__all__ = ["JsxLexer", "TsxLexer"]
128 changes: 80 additions & 48 deletions jsx/lexer.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,94 @@
import re

from pygments.lexer import bygroups, default, include
from pygments.lexers.javascript import JavascriptLexer
from pygments.lexers.javascript import JavascriptLexer, TypeScriptLexer
from pygments.token import Name, Operator, Punctuation, String, Text

# Use same tokens as `JavascriptLexer`, but with tags and attributes support
TOKENS = JavascriptLexer.tokens
TOKENS.update(
{
"jsx": [
(
r"(<)(/?)(>)",
bygroups(Punctuation, Punctuation, Punctuation),
), # JSXFragment <>|</>
(r"(<)([\w]+)(\.?)", bygroups(Punctuation, Name.Tag, Punctuation), "tag"),
(
r"(<)(/)([\w]+)(>)",
bygroups(Punctuation, Punctuation, Name.Tag, Punctuation),
),
(
r"(<)(/)([\w]+)",
bygroups(Punctuation, Punctuation, Name.Tag),
"fragment",
), # Same for React.Context
],
"tag": [
(r"\s+", Text),
(r"([\w-]+\s*)(=)(\s*)", bygroups(Name.Attribute, Operator, Text), "attr"),
(r"[{}]+", Punctuation),
(r"[\w\.]+", Name.Attribute),
(r"(/?)(\s*)(>)", bygroups(Punctuation, Text, Punctuation), "#pop"),
],
"fragment": [
(r"(.)([\w]+)", bygroups(Punctuation, Name.Attribute)),
(r"(>)", bygroups(Punctuation), "#pop"),
],
"attr": [
("{", Punctuation, "expression"),
('".*?"', String, "#pop"),
("'.*?'", String, "#pop"),
default("#pop"),
],
"expression": [
("{", Punctuation, "#push"),
("}", Punctuation, "#pop"),
include("root"),
],
}
)
TOKENS["root"].insert(0, include("jsx"))
# Add tags and attributes support to existing tokens


def build_react_tokens(tokens):
tokens.update(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be fixed to avoid mutating dictionaries that don’t belong to us (see #20).

{
"jsx": [
(
r"(<)(/?)(>)",
bygroups(Punctuation, Punctuation, Punctuation),
), # JSXFragment <>|</>
(r"(<)([\w]+)(\.?)", bygroups(Punctuation,
Name.Tag, Punctuation), "tag"),
(
r"(<)(/)([\w]+)(>)",
bygroups(Punctuation, Punctuation, Name.Tag, Punctuation),
),
(
r"(<)(/)([\w]+)",
bygroups(Punctuation, Punctuation, Name.Tag),
"fragment",
), # Same for React.Context
],
"tag": [
(r"\s+", Text),
(r"([\w-]+\s*)(=)(\s*)",
bygroups(Name.Attribute, Operator, Text), "attr"),
(r"[{}]+", Punctuation),
(r"[\w\.]+", Name.Attribute),
(r"(/?)(\s*)(>)", bygroups(Punctuation, Text, Punctuation), "#pop"),
],
"fragment": [
(r"(.)([\w]+)", bygroups(Punctuation, Name.Attribute)),
(r"(>)", bygroups(Punctuation), "#pop"),
],
"attr": [
("{", Punctuation, "expression"),
('".*?"', String, "#pop"),
("'.*?'", String, "#pop"),
default("#pop"),
],
"expression": [
("{", Punctuation, "#push"),
("}", Punctuation, "#pop"),
include("root"),
],
}
)
tokens["root"].insert(0, include("jsx"))
return tokens


JSX_TOKENS = build_react_tokens(JavascriptLexer.tokens)
TSX_TOKENS = build_react_tokens(TypeScriptLexer.tokens)


class JsxLexer(JavascriptLexer):
name = "react"
aliases = ["jsx", "react"]
filenames = ["*.jsx", "*.react"]
mimetypes = ["text/jsx", "text/typescript-jsx"]
mimetypes = ["text/jsx"]

flags = re.MULTILINE | re.DOTALL | re.UNICODE

tokens = JSX_TOKENS


class TsxLexer(TypeScriptLexer):
name = "react-typescript"
aliases = ["tsx", "react-typescript"]
filenames = ["*.tsx"]
mimetypes = ["text/tsx", "text/typescript-jsx"]
mimetypes = ["text/jsx"]

flags = re.MULTILINE | re.DOTALL | re.UNICODE

tokens = JSX_TOKENS


class TsxLexer(TypeScriptLexer):
name = "react-typescript"
aliases = ["tsx", "react-typescript"]
filenames = ["*.tsx"]
mimetypes = ["text/tsx", "text/typescript-jsx"]

flags = re.MULTILINE | re.DOTALL | re.UNICODE

tokens = TOKENS
tokens = TSX_TOKENS
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
entry_points="""
[pygments.lexers]
jsx=jsx:JsxLexer
tsx=jsx:TsxLexer
""",
)