Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS: Improved get/set and parameter detection #2387

Merged
merged 2 commits into from May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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