From 97668f4e87535ce6e170792d6e3773155601880f Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sat, 17 Oct 2020 11:40:47 +0200 Subject: [PATCH] TSX: Temporary fix for the collisions of JSX tags and TS generics --- components/prism-jsx.js | 4 +- components/prism-jsx.min.js | 2 +- components/prism-tsx.js | 13 +- components/prism-tsx.min.js | 2 +- tests/languages/tsx/issue2594.test | 252 +++++++++++++++++++++++++++++ 5 files changed, 267 insertions(+), 6 deletions(-) create mode 100644 tests/languages/tsx/issue2594.test diff --git a/components/prism-jsx.js b/components/prism-jsx.js index 30e6e3f4c6..8c5a868e46 100644 --- a/components/prism-jsx.js +++ b/components/prism-jsx.js @@ -3,10 +3,10 @@ var javascript = Prism.util.clone(Prism.languages.javascript); Prism.languages.jsx = Prism.languages.extend('markup', javascript); -Prism.languages.jsx.tag.pattern= /<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:$-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s{'">=]+|\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}))?|\{\s*\.{3}\s*[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\s*\}))*\s*\/?)?>/i; +Prism.languages.jsx.tag.pattern = /<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:$-]+(?:=(?:"(?:\\[^]|[^\\"])*"|'(?:\\[^]|[^\\'])*'|[^\s{'">=]+|\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}))?|\{\s*\.{3}\s*[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\s*\}))*\s*\/?)?>/i; Prism.languages.jsx.tag.inside['tag'].pattern = /^<\/?[^\s>\/]*/i; -Prism.languages.jsx.tag.inside['attr-value'].pattern = /=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i; +Prism.languages.jsx.tag.inside['attr-value'].pattern = /=(?!\{)(?:"(?:\\[^]|[^\\"])*"|'(?:\\[^]|[^\\'])*'|[^\s'">]+)/i; Prism.languages.jsx.tag.inside['tag'].inside['class-name'] = /^[A-Z]\w*(?:\.[A-Z]\w*)*$/; Prism.languages.insertBefore('inside', 'attr-name', { diff --git a/components/prism-jsx.min.js b/components/prism-jsx.min.js index 81390211bc..a5a35662df 100644 --- a/components/prism-jsx.min.js +++ b/components/prism-jsx.min.js @@ -1 +1 @@ -!function(i){var t=i.util.clone(i.languages.javascript);i.languages.jsx=i.languages.extend("markup",t),i.languages.jsx.tag.pattern=/<\/?(?:[\w.:-]+\s*(?:\s+(?:[\w.:$-]+(?:=(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s{'">=]+|\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}))?|\{\s*\.{3}\s*[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\s*\}))*\s*\/?)?>/i,i.languages.jsx.tag.inside.tag.pattern=/^<\/?[^\s>\/]*/i,i.languages.jsx.tag.inside["attr-value"].pattern=/=(?!\{)(?:("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|[^\s'">]+)/i,i.languages.jsx.tag.inside.tag.inside["class-name"]=/^[A-Z]\w*(?:\.[A-Z]\w*)*$/,i.languages.insertBefore("inside","attr-name",{spread:{pattern:/\{\s*\.{3}\s*[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\s*\}/,inside:{punctuation:/\.{3}|[{}.]/,"attr-value":/\w+/}}},i.languages.jsx.tag),i.languages.insertBefore("inside","attr-value",{script:{pattern:/=(?:\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\})/i,inside:{"script-punctuation":{pattern:/^=(?={)/,alias:"punctuation"},rest:i.languages.jsx},alias:"language-javascript"}},i.languages.jsx.tag);var o=function(t){return t?"string"==typeof t?t:"string"==typeof t.content?t.content:t.content.map(o).join(""):""},p=function(t){for(var n=[],e=0;e"===a.content[a.content.length-1].content||n.push({tagName:o(a.content[0].content[1]),openedBraces:0}):0=]+|\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\}))?|\{\s*\.{3}\s*[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\s*\}))*\s*\/?)?>/i,i.languages.jsx.tag.inside.tag.pattern=/^<\/?[^\s>\/]*/i,i.languages.jsx.tag.inside["attr-value"].pattern=/=(?!\{)(?:"(?:\\[^]|[^\\"])*"|'(?:\\[^]|[^\\'])*'|[^\s'">]+)/i,i.languages.jsx.tag.inside.tag.inside["class-name"]=/^[A-Z]\w*(?:\.[A-Z]\w*)*$/,i.languages.insertBefore("inside","attr-name",{spread:{pattern:/\{\s*\.{3}\s*[a-z_$][\w$]*(?:\.[a-z_$][\w$]*)*\s*\}/,inside:{punctuation:/\.{3}|[{}.]/,"attr-value":/\w+/}}},i.languages.jsx.tag),i.languages.insertBefore("inside","attr-value",{script:{pattern:/=(?:\{(?:\{(?:\{[^{}]*\}|[^{}])*\}|[^{}])+\})/i,inside:{"script-punctuation":{pattern:/^=(?={)/,alias:"punctuation"},rest:i.languages.jsx},alias:"language-javascript"}},i.languages.jsx.tag);var o=function(t){return t?"string"==typeof t?t:"string"==typeof t.content?t.content:t.content.map(o).join(""):""},p=function(t){for(var n=[],e=0;e"===a.content[a.content.length-1].content||n.push({tagName:o(a.content[0].content[1]),openedBraces:0}):0a + b } + +type Bar = Foo; + +function Add2(a, b) { return
a + b
} + +function handleSubmit(event: FormEvent) { + event.preventDefault(); +} + +function handleChange(event: ChangeEvent) { + console.log(event.target.value); +} + +function handleClick(event: MouseEvent) { + console.log(event.button); +} + +export default function Form() { + return ( +
+ + +
+ ); +} + +---------------------------------------------------- + +[ + ["keyword", "function"], + ["function", "Add1"], + ["punctuation", "("], + ["parameter", [ + "a", + ["punctuation", ","], + " b" + ]], + ["punctuation", ")"], + ["punctuation", "{"], + ["keyword", "return"], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "div" + ]], + ["punctuation", ">"] + ]], + ["plain-text", "a + b"], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], + ["punctuation", "}"], + + ["keyword", "type"], + ["class-name", [ + "Bar" + ]], + ["operator", "="], + " Foo", + ["operator", "<"], + ["builtin", "string"], + ["operator", ">"], + ["punctuation", ";"], + + ["keyword", "function"], + ["function", "Add2"], + ["punctuation", "("], + ["parameter", [ + "a", + ["punctuation", ","], + " b" + ]], + ["punctuation", ")"], + ["punctuation", "{"], + ["keyword", "return"], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "div" + ]], + ["punctuation", ">"] + ]], + ["plain-text", "a + b"], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], + ["punctuation", "}"], + + ["keyword", "function"], + ["function", "handleSubmit"], + ["punctuation", "("], + ["parameter", [ + "event", + ["operator", ":"], + " FormEvent", + ["operator", "<"], + "HTMLFormElement", + ["operator", ">"] + ]], + ["punctuation", ")"], + ["punctuation", "{"], + "\r\n event", + ["punctuation", "."], + ["function", "preventDefault"], + ["punctuation", "("], + ["punctuation", ")"], + ["punctuation", ";"], + ["punctuation", "}"], + + ["keyword", "function"], + ["function", "handleChange"], + ["punctuation", "("], + ["parameter", [ + "event", + ["operator", ":"], + " ChangeEvent", + ["operator", "<"], + "HTMLInputElement", + ["operator", ">"] + ]], + ["punctuation", ")"], + ["punctuation", "{"], + ["builtin", "console"], + ["punctuation", "."], + ["function", "log"], + ["punctuation", "("], + "event", + ["punctuation", "."], + "target", + ["punctuation", "."], + "value", + ["punctuation", ")"], + ["punctuation", ";"], + ["punctuation", "}"], + + ["keyword", "function"], + ["function", "handleClick"], + ["punctuation", "("], + ["parameter", [ + "event", + ["operator", ":"], + " MouseEvent" + ]], + ["punctuation", ")"], + ["punctuation", "{"], + ["builtin", "console"], + ["punctuation", "."], + ["function", "log"], + ["punctuation", "("], + "event", + ["punctuation", "."], + "button", + ["punctuation", ")"], + ["punctuation", ";"], + ["punctuation", "}"], + + ["keyword", "export"], + ["keyword", "default"], + ["keyword", "function"], + ["function", "Form"], + ["punctuation", "("], + ["punctuation", ")"], + ["punctuation", "{"], + ["keyword", "return"], + ["punctuation", "("], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "form" + ]], + ["attr-name", [ + "onSubmit" + ]], + ["script", [ + ["script-punctuation", "="], + ["punctuation", "{"], + "handleSubmit", + ["punctuation", "}"] + ]], + ["punctuation", ">"] + ]], + ["plain-text", "\r\n "], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "input" + ]], + ["attr-name", [ + "onChange" + ]], + ["script", [ + ["script-punctuation", "="], + ["punctuation", "{"], + "handleChange", + ["punctuation", "}"] + ]], + ["attr-name", [ + "placeholder" + ]], + ["attr-value", [ + ["punctuation", "="], + ["punctuation", "\""], + "Name", + ["punctuation", "\""] + ]], + ["punctuation", "/>"] + ]], + ["plain-text", "\r\n "], + ["tag", [ + ["tag", [ + ["punctuation", "<"], + "button" + ]], + ["attr-name", [ + "onClick" + ]], + ["script", [ + ["script-punctuation", "="], + ["punctuation", "{"], + "handleClick", + ["punctuation", "}"] + ]], + ["punctuation", ">"] + ]], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], + ["plain-text", "\r\n "], + ["tag", [ + ["tag", [ + ["punctuation", ""] + ]], + ["punctuation", ")"], + ["punctuation", ";"], + ["punctuation", "}"] +] \ No newline at end of file