Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks upstream TypeScriptLexer by mutating pygments.lexers.javascript.JavascriptLexer.tokens #19

Closed
andersk opened this issue May 15, 2023 · 0 comments · Fixed by #20
Closed

Comments

@andersk
Copy link
Contributor

andersk commented May 15, 2023

Here’s the upstream Pygments TypeScriptLexer working as normal:

$ python
Python 3.8.10 (default, Mar 13 2023, 10:26:41) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments, pygments.lexers
>>> print(*pygments.lex("let m: Map<K, V>;", pygments.lexers.TypeScriptLexer()), sep='\n')
(Token.Keyword.Declaration, 'let')
(Token.Text.Whitespace, ' ')
(Token.Name.Other, 'm')
(Token.Operator, ':')
(Token.Text.Whitespace, ' ')
(Token.Keyword.Type, 'Map')
(Token.Operator, '<')
(Token.Name.Other, 'K')
(Token.Punctuation, ',')
(Token.Text.Whitespace, ' ')
(Token.Name.Other, 'V')
(Token.Operator, '>')
(Token.Punctuation, ';')
(Token.Text.Whitespace, '\n')

But if I start a new Python process and instead import jsx before the first time I use TypeScriptLexer, TypeScriptLexer incorrectly produces Token.Error tokens:

$ python
Python 3.8.10 (default, Mar 13 2023, 10:26:41) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygments, pygments.lexers, jsx
>>> print(*pygments.lex("let m: Map<K, V>;", pygments.lexers.TypeScriptLexer()), sep='\n')
(Token.Keyword.Declaration, 'let')
(Token.Text.Whitespace, ' ')
(Token.Name.Other, 'm')
(Token.Operator, ':')
(Token.Text.Whitespace, ' ')
(Token.Keyword.Type, 'Map')
(Token.Punctuation, '<')
(Token.Name.Tag, 'K')
(Token.Error, ',')
(Token.Text, ' ')
(Token.Name.Attribute, 'V')
(Token.Punctuation, '>')
(Token.Punctuation, ';')
(Token.Text.Whitespace, '\n')

This is because jsx-lexer destructively mutates pygments.lexers.javascript.JavascriptLexer.tokens which doesn’t belong to it:

TOKENS = JavascriptLexer.tokens
TOKENS.update(

andersk added a commit to andersk/jsx-lexer that referenced this issue May 15, 2023
This is not our dictionary to mutate.  Fixes fcurella#19.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
andersk added a commit to andersk/jsx-lexer that referenced this issue May 15, 2023
This is not our dictionary to mutate.  Fixes fcurella#19.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant