Skip to content

Commit

Permalink
JS: Improved get/set and parameter detection (#2387)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed May 29, 2020
1 parent 6352213 commit ed71515
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
4 changes: 2 additions & 2 deletions components/prism-javascript.js
Expand Up @@ -12,7 +12,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
lookbehind: true
},
{
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|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|undefined|var|void|while|with|yield)\b/,
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|(?:get|set)(?=\s*[\[$\w\xA0-\uFFFF])|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
lookbehind: true
},
],
Expand Down Expand Up @@ -51,7 +51,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
inside: Prism.languages.javascript
},
{
pattern: /((?:\b|\s|^)(?!(?: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|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
pattern: /((?:\b|\s|^)(?!(?: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|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
lookbehind: true,
inside: Prism.languages.javascript
}
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.

4 changes: 2 additions & 2 deletions prism.js
Expand Up @@ -989,7 +989,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
lookbehind: true
},
{
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|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|undefined|var|void|while|with|yield)\b/,
pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|for|from|function|(?:get|set)(?=\s*[\[$\w\xA0-\uFFFF])|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,
lookbehind: true
},
],
Expand Down Expand Up @@ -1028,7 +1028,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
inside: Prism.languages.javascript
},
{
pattern: /((?:\b|\s|^)(?!(?: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|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
pattern: /((?:\b|\s|^)(?!(?: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|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:[_$A-Za-z\xA0-\uFFFF][$\w\xA0-\uFFFF]*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()]|\([^()]*\))+?(?=\s*\)\s*\{)/,
lookbehind: true,
inside: Prism.languages.javascript
}
Expand Down
115 changes: 115 additions & 0 deletions tests/languages/javascript/getter-setter_feature.test
@@ -0,0 +1,115 @@
const obj = {
get name() { return 'bar'; },
get [expr]() { return 'bar'; },
async get name() { return 'bar'; },
set name(val) { },
set [expr](val) { },
async set [expr](val) { },
};

// not keywords
get();
set(foo);

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

[
["keyword", "const"],
" obj ",
["operator", "="],
["punctuation", "{"],

["keyword", "get"],
["function", "name"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
["string", "'bar'"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ","],

["keyword", "get"],
["punctuation", "["],
"expr",
["punctuation", "]"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
["string", "'bar'"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ","],

["keyword", "async"],
["keyword", "get"],
["function", "name"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
["string", "'bar'"],
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ","],

["keyword", "set"],
["function", "name"],
["punctuation", "("],
["parameter", [
"val"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],

["keyword", "set"],
["punctuation", "["],
"expr",
["punctuation", "]"],
["punctuation", "("],
["parameter", [
"val"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],

["keyword", "async"],
["keyword", "set"],
["punctuation", "["],
"expr",
["punctuation", "]"],
["punctuation", "("],
["parameter", [
"val"
]],
["punctuation", ")"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ","],

["punctuation", "}"],
["punctuation", ";"],

["comment", "// not keywords"],

["function", "get"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["function", "set"],
["punctuation", "("],
"foo",
["punctuation", ")"],
["punctuation", ";"]
]

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

Checks for getters and setters.
6 changes: 2 additions & 4 deletions tests/languages/javascript/keyword_feature.test
Expand Up @@ -5,10 +5,10 @@ as; await; break; case;
class; const; continue; debugger;
default; delete; do; else; enum;
export; extends; for;
from; get; if; implements;
from; if; implements;
import; in; instanceof; interface; let;
new; null; of; package; private;
protected; public; return; set; static;
protected; public; return; static;
super; switch; this; throw; try;
typeof; undefined; var; void; while;
with; yield;
Expand Down Expand Up @@ -39,7 +39,6 @@ with; yield;
["keyword", "extends"], ["punctuation", ";"],
["keyword", "for"], ["punctuation", ";"],
["keyword", "from"], ["punctuation", ";"],
["keyword", "get"], ["punctuation", ";"],
["keyword", "if"], ["punctuation", ";"],
["keyword", "implements"], ["punctuation", ";"],
["keyword", "import"], ["punctuation", ";"],
Expand All @@ -55,7 +54,6 @@ with; yield;
["keyword", "protected"], ["punctuation", ";"],
["keyword", "public"], ["punctuation", ";"],
["keyword", "return"], ["punctuation", ";"],
["keyword", "set"], ["punctuation", ";"],
["keyword", "static"], ["punctuation", ";"],
["keyword", "super"], ["punctuation", ";"],
["keyword", "switch"], ["punctuation", ";"],
Expand Down

0 comments on commit ed71515

Please sign in to comment.