From 606691b2962348a5d39fcc92d8df124b50c93eb6 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Thu, 16 Aug 2018 11:32:37 +0200 Subject: [PATCH 1/3] JS: Updated catch and finally --- components/prism-javascript.js | 10 +++++- components/prism-javascript.min.js | 2 +- prism.js | 10 +++++- tests/languages/javascript/issue1526.test | 35 +++++++++++++++++++ .../languages/javascript/keyword_feature.test | 19 +++++----- 5 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 tests/languages/javascript/issue1526.test diff --git a/components/prism-javascript.js b/components/prism-javascript.js index 7800a7d424..15969af285 100644 --- a/components/prism-javascript.js +++ b/components/prism-javascript.js @@ -1,11 +1,19 @@ Prism.languages.javascript = Prism.languages.extend('clike', { - 'keyword': /\b(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/, + 'keyword': [ + { + pattern: /(^|}\s*)(?:catch|finally)\b/, + lookbehind: true + }, + /\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/ + ], 'number': /\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/, // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444) 'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i, 'operator': /-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/ }); +Prism.languages.javascript['class-name'].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/ + Prism.languages.insertBefore('javascript', 'keyword', { 'regex': { pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/, diff --git a/components/prism-javascript.min.js b/components/prism-javascript.min.js index 50a4d54f80..b20e2964c7 100644 --- a/components/prism-javascript.min.js +++ b/components/prism-javascript.min.js @@ -1 +1 @@ -Prism.languages.javascript=Prism.languages.extend("clike",{keyword:/\b(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/,number:/\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,"function":/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,operator:/-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/}),Prism.languages.insertBefore("javascript","keyword",{regex:{pattern:/((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^\/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,lookbehind:!0,greedy:!0},"function-variable":{pattern:/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,alias:"function"},constant:/\b[A-Z][A-Z\d_]*\b/}),Prism.languages.insertBefore("javascript","string",{"template-string":{pattern:/`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,greedy:!0,inside:{interpolation:{pattern:/\${[^}]+}/,inside:{"interpolation-punctuation":{pattern:/^\${|}$/,alias:"punctuation"},rest:null}},string:/[\s\S]+/}}}),Prism.languages.javascript["template-string"].inside.interpolation.inside.rest=Prism.languages.javascript,Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/()[\s\S]*?(?=<\/script>)/i,lookbehind:!0,inside:Prism.languages.javascript,alias:"language-javascript",greedy:!0}}),Prism.languages.js=Prism.languages.javascript; \ No newline at end of file +Prism.languages.javascript=Prism.languages.extend("clike",{keyword:[{pattern:/(^|}\s*)(?:catch|finally)\b/,lookbehind:!0},/\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/],number:/\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,"function":/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,operator:/-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/}),Prism.languages.javascript["class-name"].pattern=/(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/,Prism.languages.insertBefore("javascript","keyword",{regex:{pattern:/((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^\/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,lookbehind:!0,greedy:!0},"function-variable":{pattern:/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,alias:"function"},constant:/\b[A-Z][A-Z\d_]*\b/}),Prism.languages.insertBefore("javascript","string",{"template-string":{pattern:/`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,greedy:!0,inside:{interpolation:{pattern:/\${[^}]+}/,inside:{"interpolation-punctuation":{pattern:/^\${|}$/,alias:"punctuation"},rest:null}},string:/[\s\S]+/}}}),Prism.languages.javascript["template-string"].inside.interpolation.inside.rest=Prism.languages.javascript,Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/()[\s\S]*?(?=<\/script>)/i,lookbehind:!0,inside:Prism.languages.javascript,alias:"language-javascript",greedy:!0}}),Prism.languages.js=Prism.languages.javascript; \ No newline at end of file diff --git a/prism.js b/prism.js index 2baadce878..05462d73a8 100644 --- a/prism.js +++ b/prism.js @@ -722,13 +722,21 @@ Prism.languages.clike = { ********************************************** */ Prism.languages.javascript = Prism.languages.extend('clike', { - 'keyword': /\b(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/, + 'keyword': [ + { + pattern: /(^|}\s*)(?:catch|finally)\b/, + lookbehind: true + }, + /\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/ + ], 'number': /\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/, // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444) 'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i, 'operator': /-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/ }); +Prism.languages.javascript['class-name'].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/ + Prism.languages.insertBefore('javascript', 'keyword', { 'regex': { pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/, diff --git a/tests/languages/javascript/issue1526.test b/tests/languages/javascript/issue1526.test new file mode 100644 index 0000000000..d361155e41 --- /dev/null +++ b/tests/languages/javascript/issue1526.test @@ -0,0 +1,35 @@ +fetch('some-resource.json') + .then(response => response.json()) + .catch(console.error); + +---------------------------------------------------- + +[ + ["function", "fetch"], + ["punctuation", "("], + ["string", "'some-resource.json'"], + ["punctuation", ")"], + ["punctuation", "."], + ["function", "then"], + ["punctuation", "("], + "response ", + ["operator", "=>"], + " response", + ["punctuation", "."], + ["function", "json"], + ["punctuation", "("], + ["punctuation", ")"], + ["punctuation", ")"], + ["punctuation", "."], + ["function", "catch"], + ["punctuation", "("], + "console", + ["punctuation", "."], + "error", + ["punctuation", ")"], + ["punctuation", ";"] +] + +---------------------------------------------------- + +Checks for catch function which is not a keyword. See #1526 diff --git a/tests/languages/javascript/keyword_feature.test b/tests/languages/javascript/keyword_feature.test index 13b88875d0..bcce8ce8dc 100644 --- a/tests/languages/javascript/keyword_feature.test +++ b/tests/languages/javascript/keyword_feature.test @@ -1,15 +1,17 @@ as; async; await; break; case; -catch; class; const; continue; debugger; +class; const; continue; debugger; default; delete; do; else; enum; -export; extends; finally; for; +export; extends; for; from; function; get; if; implements; import; in; instanceof; interface; let; new; null; of; package; private; protected; public; return; set; static; super; switch; this; throw; -try; typeof; var; void; while; +typeof; var; void; while; with; yield; +try {} catch (e) {} finally {} + ---------------------------------------------------- [ @@ -18,7 +20,6 @@ with; yield; ["keyword", "await"], ["punctuation", ";"], ["keyword", "break"], ["punctuation", ";"], ["keyword", "case"], ["punctuation", ";"], - ["keyword", "catch"], ["punctuation", ";"], ["keyword", "class"], ["punctuation", ";"], ["keyword", "const"], ["punctuation", ";"], ["keyword", "continue"], ["punctuation", ";"], @@ -30,7 +31,6 @@ with; yield; ["keyword", "enum"], ["punctuation", ";"], ["keyword", "export"], ["punctuation", ";"], ["keyword", "extends"], ["punctuation", ";"], - ["keyword", "finally"], ["punctuation", ";"], ["keyword", "for"], ["punctuation", ";"], ["keyword", "from"], ["punctuation", ";"], ["keyword", "function"], ["punctuation", ";"], @@ -56,15 +56,18 @@ with; yield; ["keyword", "switch"], ["punctuation", ";"], ["keyword", "this"], ["punctuation", ";"], ["keyword", "throw"], ["punctuation", ";"], - ["keyword", "try"], ["punctuation", ";"], ["keyword", "typeof"], ["punctuation", ";"], ["keyword", "var"], ["punctuation", ";"], ["keyword", "void"], ["punctuation", ";"], ["keyword", "while"], ["punctuation", ";"], ["keyword", "with"], ["punctuation", ";"], - ["keyword", "yield"], ["punctuation", ";"] + ["keyword", "yield"], ["punctuation", ";"], + + ["keyword", "try"], ["punctuation", "{"], ["punctuation", "}"], + ["keyword", "catch"], ["punctuation", "("], "e", ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"], + ["keyword", "finally"], ["punctuation", "{"], ["punctuation", "}"] ] ---------------------------------------------------- -Checks for all keywords. \ No newline at end of file +Checks for all keywords. From 6156bf4925939899c0bb1f5c10ba8bcc611ca95f Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 19 Aug 2018 10:43:17 +0200 Subject: [PATCH 2/3] JS: Add test for try-catch --- .../javascript/try-catch_feature.test | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 tests/languages/javascript/try-catch_feature.test diff --git a/tests/languages/javascript/try-catch_feature.test b/tests/languages/javascript/try-catch_feature.test new file mode 100644 index 0000000000..c4bbd8b2b8 --- /dev/null +++ b/tests/languages/javascript/try-catch_feature.test @@ -0,0 +1,22 @@ +try { } catch (e) { } finally { } + +---------------------------------------------------- + +[ + ["keyword", "try"], + ["punctuation", "{"], + ["punctuation", "}"], + ["keyword", "catch"], + ["punctuation", "("], + "e", + ["punctuation", ")"], + ["punctuation", "{"], + ["punctuation", "}"], + ["keyword", "finally"], + ["punctuation", "{"], + ["punctuation", "}"] +] + +---------------------------------------------------- + +Checks for try statements. \ No newline at end of file From 3f8eedb6f3f0b59a636edbb496b26f82cd7cfd34 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sun, 19 Aug 2018 11:01:17 +0200 Subject: [PATCH 3/3] Simplified test. --- components/prism-javascript.js | 2 +- components/prism-javascript.min.js | 2 +- prism.js | 2 +- tests/languages/javascript/keyword_feature.test | 16 ++++++++-------- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/components/prism-javascript.js b/components/prism-javascript.js index ca4cd8b225..47853d3e0b 100644 --- a/components/prism-javascript.js +++ b/components/prism-javascript.js @@ -1,7 +1,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', { 'keyword': [ { - pattern: /(^|}\s*)(?:catch|finally)\b/, + pattern: /((?:^|})\s*)(?:catch|finally)\b/, lookbehind: true }, /\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/ diff --git a/components/prism-javascript.min.js b/components/prism-javascript.min.js index fb91b08a38..29cfee7d0a 100644 --- a/components/prism-javascript.min.js +++ b/components/prism-javascript.min.js @@ -1 +1 @@ -Prism.languages.javascript=Prism.languages.extend("clike",{keyword:[{pattern:/(^|}\s*)(?:catch|finally)\b/,lookbehind:!0},/\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/],number:/\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,"function":/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,operator:/-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/}),Prism.languages.javascript["class-name"].pattern=/(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/,Prism.languages.insertBefore("javascript","keyword",{regex:{pattern:/((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^\/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,lookbehind:!0,greedy:!0},"function-variable":{pattern:/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,alias:"function"},constant:/\b[A-Z][A-Z\d_]*\b/}),Prism.languages.insertBefore("javascript","string",{"template-string":{pattern:/`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,greedy:!0,inside:{interpolation:{pattern:/\${[^}]+}/,inside:{"interpolation-punctuation":{pattern:/^\${|}$/,alias:"punctuation"},rest:Prism.languages.javascript}},string:/[\s\S]+/}}}),Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/()[\s\S]*?(?=<\/script>)/i,lookbehind:!0,inside:Prism.languages.javascript,alias:"language-javascript",greedy:!0}}),Prism.languages.js=Prism.languages.javascript; \ No newline at end of file +Prism.languages.javascript=Prism.languages.extend("clike",{keyword:[{pattern:/((?:^|})\s*)(?:catch|finally)\b/,lookbehind:!0},/\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/],number:/\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,"function":/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,operator:/-[-=]?|\+[+=]?|!=?=?|<>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/}),Prism.languages.javascript["class-name"].pattern=/(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/,Prism.languages.insertBefore("javascript","keyword",{regex:{pattern:/((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^\/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,lookbehind:!0,greedy:!0},"function-variable":{pattern:/[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)|[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/i,alias:"function"},constant:/\b[A-Z][A-Z\d_]*\b/}),Prism.languages.insertBefore("javascript","string",{"template-string":{pattern:/`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,greedy:!0,inside:{interpolation:{pattern:/\${[^}]+}/,inside:{"interpolation-punctuation":{pattern:/^\${|}$/,alias:"punctuation"},rest:Prism.languages.javascript}},string:/[\s\S]+/}}}),Prism.languages.markup&&Prism.languages.insertBefore("markup","tag",{script:{pattern:/()[\s\S]*?(?=<\/script>)/i,lookbehind:!0,inside:Prism.languages.javascript,alias:"language-javascript",greedy:!0}}),Prism.languages.js=Prism.languages.javascript; \ No newline at end of file diff --git a/prism.js b/prism.js index a81d86dd00..b48a825af2 100644 --- a/prism.js +++ b/prism.js @@ -727,7 +727,7 @@ Prism.languages.clike = { Prism.languages.javascript = Prism.languages.extend('clike', { 'keyword': [ { - pattern: /(^|}\s*)(?:catch|finally)\b/, + pattern: /((?:^|})\s*)(?:catch|finally)\b/, lookbehind: true }, /\b(?:as|async|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|var|void|while|with|yield)\b/ diff --git a/tests/languages/javascript/keyword_feature.test b/tests/languages/javascript/keyword_feature.test index bcce8ce8dc..8053f2b1b3 100644 --- a/tests/languages/javascript/keyword_feature.test +++ b/tests/languages/javascript/keyword_feature.test @@ -1,3 +1,5 @@ +catch finally; + as; async; await; break; case; class; const; continue; debugger; default; delete; do; else; enum; @@ -6,15 +8,16 @@ from; function; get; if; implements; import; in; instanceof; interface; let; new; null; of; package; private; protected; public; return; set; static; -super; switch; this; throw; +super; switch; this; throw; try; typeof; var; void; while; with; yield; -try {} catch (e) {} finally {} - ---------------------------------------------------- [ + ["keyword", "catch"], + ["keyword", "finally"], ["punctuation", ";"], + ["keyword", "as"], ["punctuation", ";"], ["keyword", "async"], ["punctuation", ";"], ["keyword", "await"], ["punctuation", ";"], @@ -56,16 +59,13 @@ try {} catch (e) {} finally {} ["keyword", "switch"], ["punctuation", ";"], ["keyword", "this"], ["punctuation", ";"], ["keyword", "throw"], ["punctuation", ";"], + ["keyword", "try"], ["punctuation", ";"], ["keyword", "typeof"], ["punctuation", ";"], ["keyword", "var"], ["punctuation", ";"], ["keyword", "void"], ["punctuation", ";"], ["keyword", "while"], ["punctuation", ";"], ["keyword", "with"], ["punctuation", ";"], - ["keyword", "yield"], ["punctuation", ";"], - - ["keyword", "try"], ["punctuation", "{"], ["punctuation", "}"], - ["keyword", "catch"], ["punctuation", "("], "e", ["punctuation", ")"], ["punctuation", "{"], ["punctuation", "}"], - ["keyword", "finally"], ["punctuation", "{"], ["punctuation", "}"] + ["keyword", "yield"], ["punctuation", ";"] ] ----------------------------------------------------