Skip to content

Commit

Permalink
Don’t break upstream TypeScriptLexer by mutating JavaScriptLexer.tokens
Browse files Browse the repository at this point in the history
This is not our dictionary to mutate.  Fixes #19.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Loading branch information
andersk committed May 15, 2023
1 parent 04d486d commit 2ec00bf
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions jsx/lexer.py
Expand Up @@ -5,50 +5,51 @@
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"))
TOKENS = {
**JavascriptLexer.tokens,
"root": [
include("jsx"),
*JavascriptLexer.tokens["root"],
],
"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"),
],
}


class JsxLexer(JavascriptLexer):
Expand Down

0 comments on commit 2ec00bf

Please sign in to comment.