From 9618112850d4f63a51b24dd8b3e2c6b0b819e6db Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Mon, 22 Apr 2019 19:38:15 +1000 Subject: [PATCH] Import JSX lexer from https://github.com/fcurella/jsx-lexer Fixes #218. --- lexers/j/jsx.go | 106 ++++++++++++++++++++++++++--------- lexers/testdata/jsx.expected | 10 ++-- 2 files changed, 84 insertions(+), 32 deletions(-) diff --git a/lexers/j/jsx.go b/lexers/j/jsx.go index 82879f7..71642d6 100644 --- a/lexers/j/jsx.go +++ b/lexers/j/jsx.go @@ -5,38 +5,90 @@ import ( "github.com/alecthomas/chroma/lexers/internal" ) -var JSXRules = func() Rules { - rules := JavascriptRules.Clone() - rules["jsx"] = []Rule{ - {`(<)([\w\._\-]+)`, ByGroups(Punctuation, NameTag), Push("tag")}, - {`(<)(/)(\s*)([\w\._\-]+)(\s*)(>)`, ByGroups(Punctuation, Punctuation, Text, NameTag, Text, Punctuation), nil}, - } - rules["tag"] = []Rule{ - {`\s+`, Text, nil}, - {`([\w]+\s*)(=)(\s*)`, ByGroups(NameAttribute, Operator, Text), Push("attr")}, - {`[{}]+`, Punctuation, nil}, - {`[\w\.]+`, NameAttribute, nil}, - {`(/?)(\s*)(>)`, ByGroups(Punctuation, Text, Punctuation), Pop(1)}, - } - rules["attr"] = []Rule{ - {`\s+`, Text, nil}, - {`".*?"`, String, Pop(1)}, - {`'.*?'`, String, Pop(1)}, - {`[^\s>]+`, String, Pop(1)}, - } - - rules["root"] = append([]Rule{Include("jsx")}, rules["root"]...) - return rules -}() - // JSX lexer. +// +// This was generated from https://github.com/fcurella/jsx-lexer var JSX = internal.Register(MustNewLexer( &Config{ - Name: "JSX", - Aliases: []string{"react"}, + Name: "react", + Aliases: []string{"jsx", "react"}, Filenames: []string{"*.jsx", "*.react"}, MimeTypes: []string{"text/jsx", "text/typescript-jsx"}, DotAll: true, }, - JSXRules, + Rules{ + "commentsandwhitespace": { + {`\s+`, Text, nil}, + {`