1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

Adds support for jsx blocks in typescript lexer

Adds support for jsx tags with a period in their name
Adds support for jsx fragments (<></>)
This commit is contained in:
Tristan Menzel 2020-04-20 12:24:48 +10:00 committed by Alec Thomas
parent 28041a86ba
commit 0da4bd1471
6 changed files with 124 additions and 5 deletions

View File

@ -69,8 +69,9 @@ var JSX = internal.Register(MustNewLexer(
Include("root"),
},
"jsx": {
{`(<)([\w]+)`, ByGroups(Punctuation, NameTag), Push("tag")},
{`(<)(/)([\w]+)(>)`, ByGroups(Punctuation, Punctuation, NameTag, Punctuation), nil},
{`(<)(/?)(>)`, ByGroups(Punctuation, Punctuation, Punctuation), nil},
{`(<)([\w\.]+)`, ByGroups(Punctuation, NameTag), Push("tag")},
{`(<)(/)([\w\.]+)(>)`, ByGroups(Punctuation, Punctuation, NameTag, Punctuation), nil},
},
"tag": {
{`\s+`, Text, nil},

View File

@ -9,7 +9,7 @@ import (
var TypeScript = internal.Register(MustNewLexer(
&Config{
Name: "TypeScript",
Aliases: []string{"ts", "typescript"},
Aliases: []string{"ts", "tsx", "typescript"},
Filenames: []string{"*.ts", "*.tsx"},
MimeTypes: []string{"text/x-typescript"},
DotAll: true,
@ -32,6 +32,7 @@ var TypeScript = internal.Register(MustNewLexer(
{`\n`, Text, Pop(1)},
},
"root": {
Include("jsx"),
{`^(?=\s|/|<!--)`, Text, Push("slashstartsregex")},
Include("commentsandwhitespace"),
{`\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|(<<|>>>?|==?|!=?|[-<>+*%&|^/])=?`, Operator, Push("slashstartsregex")},
@ -69,5 +70,28 @@ var TypeScript = internal.Register(MustNewLexer(
{`\}`, LiteralStringInterpol, Pop(1)},
Include("root"),
},
"jsx": {
{`(<)(/?)(>)`, ByGroups(Punctuation, Punctuation, Punctuation), nil},
{`(<)([\w\.]+)`, ByGroups(Punctuation, NameTag), Push("tag")},
{`(<)(/)([\w\.]*)(>)`, ByGroups(Punctuation, Punctuation, NameTag, Punctuation), nil},
},
"tag": {
{`\s+`, Text, nil},
{`([\w]+\s*)(=)(\s*)`, ByGroups(NameAttribute, Operator, Text), Push("attr")},
{`[{}]+`, Punctuation, nil},
{`[\w\.]+`, NameAttribute, nil},
{`(/?)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation), Pop(1)},
},
"attr": {
{`{`, Punctuation, Push("expression")},
{`".*?"`, LiteralString, Pop(1)},
{`'.*?'`, LiteralString, Pop(1)},
Default(Pop(1)),
},
"expression": {
{`{`, Punctuation, Push()},
{`}`, Punctuation, Pop(1)},
Include("root"),
},
},
))

View File

@ -5,6 +5,10 @@ import './index.css';
import 'github-fork-ribbon-css/gh-fork-ribbon.css';
ReactDOM.render(
<App />,
<>
<React.StrictMode>
<App />
</React.StrictMode>
</>,
document.getElementById('root')
);

View File

@ -41,10 +41,22 @@
{"type":"NameOther","value":"render"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"React.StrictMode"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"App"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"/\u003e,"},
{"type":"Punctuation","value":"/\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"React.StrictMode"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c/\u003e,"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltin","value":"document"},
{"type":"Punctuation","value":"."},

13
lexers/testdata/tsx.actual vendored Normal file
View File

@ -0,0 +1,13 @@
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import { App } from './App'
ReactDOM.render(
<>
<React.StrictMode>
<App />
</React.StrictMode>
</>,
document.getElementById('root'),
)

65
lexers/testdata/tsx.expected vendored Normal file
View File

@ -0,0 +1,65 @@
[
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"React"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"from"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'react'"},
{"type":"Text","value":"\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"ReactDOM"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"from"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'react-dom'"},
{"type":"Text","value":"\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'./index.css'"},
{"type":"Text","value":"\n"},
{"type":"KeywordReserved","value":"import"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"App"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":" "},
{"type":"NameOther","value":"from"},
{"type":"Text","value":" "},
{"type":"LiteralStringSingle","value":"'./App'"},
{"type":"Text","value":"\n\n"},
{"type":"NameOther","value":"ReactDOM"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"render"},
{"type":"Punctuation","value":"("},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"React.StrictMode"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c"},
{"type":"NameTag","value":"App"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"/\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c/"},
{"type":"NameTag","value":"React.StrictMode"},
{"type":"Punctuation","value":"\u003e"},
{"type":"Text","value":"\n "},
{"type":"Punctuation","value":"\u003c/\u003e,"},
{"type":"Text","value":"\n "},
{"type":"NameBuiltin","value":"document"},
{"type":"Punctuation","value":"."},
{"type":"NameOther","value":"getElementById"},
{"type":"Punctuation","value":"("},
{"type":"LiteralStringSingle","value":"'root'"},
{"type":"Punctuation","value":"),"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":")"},
{"type":"Text","value":"\n"}
]