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

JavaScript: Added hashbang and private getters/setters #2815

Merged
merged 2 commits into from
Mar 28, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion components/prism-javascript.js
Original file line number Diff line number Diff line change
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|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|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/,
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|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|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 @@ -70,6 +70,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {
});

Prism.languages.insertBefore('javascript', 'string', {
'hashbang': {
pattern: /^#!.*/,
greedy: true,
alias: 'comment'
},
'template-string': {
pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|(?!\${)[^\\`])*`/,
greedy: true,
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.

7 changes: 6 additions & 1 deletion prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -1515,7 +1515,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|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|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/,
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|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|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 @@ -1573,6 +1573,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {
});

Prism.languages.insertBefore('javascript', 'string', {
'hashbang': {
pattern: /^#!.*/,
greedy: true,
alias: 'comment'
},
'template-string': {
pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|(?!\${)[^\\`])*`/,
greedy: true,
Expand Down
236 changes: 121 additions & 115 deletions tests/languages/javascript/getter-setter_feature.test
Original file line number Diff line number Diff line change
@@ -1,115 +1,121 @@
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.
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) { },
get #x() { return #xValue; },
};

// 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", ","],

["keyword", "get"],
["function", "#x"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
" #xValue",
["punctuation", ";"],
["punctuation", "}"],
["punctuation", ","],

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

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

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

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

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

Checks for getters and setters.
23 changes: 23 additions & 0 deletions tests/languages/javascript/hashbang_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(1);

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

[
["hashbang", "#!/usr/bin/env node"],

["comment", "// in the Script Goal"],

["string", "'use strict'"],
["punctuation", ";"],

"\r\nconsole",
["punctuation", "."],
["function", "log"],
["punctuation", "("],
["number", "1"],
["punctuation", ")"],
["punctuation", ";"]
]