Skip to content

Commit

Permalink
JavaScript: Added hashbang and private getters/setters (#2815)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Mar 28, 2021
1 parent e4ad22a commit 9c610ae
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 118 deletions.
7 changes: 6 additions & 1 deletion 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|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
Expand Up @@ -1522,7 +1522,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 @@ -1580,6 +1580,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
@@ -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
@@ -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", ";"]
]

0 comments on commit 9c610ae

Please sign in to comment.