Skip to content

Commit ebd1b9a

Browse files
RunDevelopmentmAAdhaTTah
authored andcommitted
Fix regex for catch and finally (#1527)
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.
1 parent 9dfec34 commit ebd1b9a

File tree

6 files changed

+86
-10
lines changed

6 files changed

+86
-10
lines changed

components/prism-javascript.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
Prism.languages.javascript = Prism.languages.extend('clike', {
2-
'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/,
2+
'keyword': [
3+
{
4+
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
5+
lookbehind: true
6+
},
7+
/\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/
8+
],
39
'number': /\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,
410
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
511
'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,
612
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
713
});
814

15+
Prism.languages.javascript['class-name'].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/
16+
917
Prism.languages.insertBefore('javascript', 'keyword', {
1018
'regex': {
1119
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,

components/prism-javascript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,13 +725,21 @@ Prism.languages.clike = {
725725
********************************************** */
726726

727727
Prism.languages.javascript = Prism.languages.extend('clike', {
728-
'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/,
728+
'keyword': [
729+
{
730+
pattern: /((?:^|})\s*)(?:catch|finally)\b/,
731+
lookbehind: true
732+
},
733+
/\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/
734+
],
729735
'number': /\b(?:0[xX][\dA-Fa-f]+|0[bB][01]+|0[oO][0-7]+|NaN|Infinity)\b|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][+-]?\d+)?/,
730736
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
731737
'function': /[_$a-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*\()/i,
732738
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
733739
});
734740

741+
Prism.languages.javascript['class-name'].pattern = /(\b(?:class|interface|extends|implements|instanceof|new)\s+)[\w.\\]+/
742+
735743
Prism.languages.insertBefore('javascript', 'keyword', {
736744
'regex': {
737745
pattern: /((?:^|[^$\w\xA0-\uFFFF."'\])\s])\s*)\/(\[[^\]\r\n]+]|\\.|[^/\\\[\r\n])+\/[gimyu]{0,5}(?=\s*($|[\r\n,.;})\]]))/,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
fetch('some-resource.json')
2+
.then(response => response.json())
3+
.catch(console.error);
4+
5+
----------------------------------------------------
6+
7+
[
8+
["function", "fetch"],
9+
["punctuation", "("],
10+
["string", "'some-resource.json'"],
11+
["punctuation", ")"],
12+
["punctuation", "."],
13+
["function", "then"],
14+
["punctuation", "("],
15+
"response ",
16+
["operator", "=>"],
17+
" response",
18+
["punctuation", "."],
19+
["function", "json"],
20+
["punctuation", "("],
21+
["punctuation", ")"],
22+
["punctuation", ")"],
23+
["punctuation", "."],
24+
["function", "catch"],
25+
["punctuation", "("],
26+
"console",
27+
["punctuation", "."],
28+
"error",
29+
["punctuation", ")"],
30+
["punctuation", ";"]
31+
]
32+
33+
----------------------------------------------------
34+
35+
Checks for catch function which is not a keyword. See #1526

tests/languages/javascript/keyword_feature.test

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1+
catch finally;
2+
13
as; async; await; break; case;
2-
catch; class; const; continue; debugger;
4+
class; const; continue; debugger;
35
default; delete; do; else; enum;
4-
export; extends; finally; for;
6+
export; extends; for;
57
from; function; get; if; implements;
68
import; in; instanceof; interface; let;
79
new; null; of; package; private;
810
protected; public; return; set; static;
9-
super; switch; this; throw;
10-
try; typeof; var; void; while;
11+
super; switch; this; throw; try;
12+
typeof; var; void; while;
1113
with; yield;
1214

1315
----------------------------------------------------
1416

1517
[
18+
["keyword", "catch"],
19+
["keyword", "finally"], ["punctuation", ";"],
20+
1621
["keyword", "as"], ["punctuation", ";"],
1722
["keyword", "async"], ["punctuation", ";"],
1823
["keyword", "await"], ["punctuation", ";"],
1924
["keyword", "break"], ["punctuation", ";"],
2025
["keyword", "case"], ["punctuation", ";"],
21-
["keyword", "catch"], ["punctuation", ";"],
2226
["keyword", "class"], ["punctuation", ";"],
2327
["keyword", "const"], ["punctuation", ";"],
2428
["keyword", "continue"], ["punctuation", ";"],
@@ -30,7 +34,6 @@ with; yield;
3034
["keyword", "enum"], ["punctuation", ";"],
3135
["keyword", "export"], ["punctuation", ";"],
3236
["keyword", "extends"], ["punctuation", ";"],
33-
["keyword", "finally"], ["punctuation", ";"],
3437
["keyword", "for"], ["punctuation", ";"],
3538
["keyword", "from"], ["punctuation", ";"],
3639
["keyword", "function"], ["punctuation", ";"],
@@ -67,4 +70,4 @@ with; yield;
6770

6871
----------------------------------------------------
6972

70-
Checks for all keywords.
73+
Checks for all keywords.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
try { } catch (e) { } finally { }
2+
3+
----------------------------------------------------
4+
5+
[
6+
["keyword", "try"],
7+
["punctuation", "{"],
8+
["punctuation", "}"],
9+
["keyword", "catch"],
10+
["punctuation", "("],
11+
"e",
12+
["punctuation", ")"],
13+
["punctuation", "{"],
14+
["punctuation", "}"],
15+
["keyword", "finally"],
16+
["punctuation", "{"],
17+
["punctuation", "}"]
18+
]
19+
20+
----------------------------------------------------
21+
22+
Checks for try statements.

0 commit comments

Comments
 (0)