Skip to content

Commit

Permalink
Fix regex for catch and finally (#1527)
Browse files Browse the repository at this point in the history
When called as methods on a promise chain, the current regex
would mark `catch` and `finally` as keywords. This regex improvement
ensures they're only caught as part of a `try / catch` block, and are
marked as functions as part of the promise chain.
  • Loading branch information
RunDevelopment authored and mAAdhaTTah committed Aug 19, 2018
1 parent 9dfec34 commit ebd1b9a
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 10 deletions.
10 changes: 9 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
@@ -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,.;})\]]))/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,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,.;})\]]))/,
Expand Down
35 changes: 35 additions & 0 deletions tests/languages/javascript/issue1526.test
Original file line number Diff line number Diff line change
@@ -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
17 changes: 10 additions & 7 deletions tests/languages/javascript/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
catch finally;

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;
super; switch; this; throw; try;
typeof; var; void; while;
with; yield;

----------------------------------------------------

[
["keyword", "catch"],
["keyword", "finally"], ["punctuation", ";"],

["keyword", "as"], ["punctuation", ";"],
["keyword", "async"], ["punctuation", ";"],
["keyword", "await"], ["punctuation", ";"],
["keyword", "break"], ["punctuation", ";"],
["keyword", "case"], ["punctuation", ";"],
["keyword", "catch"], ["punctuation", ";"],
["keyword", "class"], ["punctuation", ";"],
["keyword", "const"], ["punctuation", ";"],
["keyword", "continue"], ["punctuation", ";"],
Expand All @@ -30,7 +34,6 @@ with; yield;
["keyword", "enum"], ["punctuation", ";"],
["keyword", "export"], ["punctuation", ";"],
["keyword", "extends"], ["punctuation", ";"],
["keyword", "finally"], ["punctuation", ";"],
["keyword", "for"], ["punctuation", ";"],
["keyword", "from"], ["punctuation", ";"],
["keyword", "function"], ["punctuation", ";"],
Expand Down Expand Up @@ -67,4 +70,4 @@ with; yield;

----------------------------------------------------

Checks for all keywords.
Checks for all keywords.
22 changes: 22 additions & 0 deletions tests/languages/javascript/try-catch_feature.test
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit ebd1b9a

Please sign in to comment.