Skip to content

Commit 9c610ae

Browse files
JavaScript: Added hashbang and private getters/setters (#2815)
1 parent e4ad22a commit 9c610ae

File tree

5 files changed

+157
-118
lines changed

5 files changed

+157
-118
lines changed

components/prism-javascript.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
1212
lookbehind: true
1313
},
1414
{
15-
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/,
15+
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/,
1616
lookbehind: true
1717
},
1818
],
@@ -70,6 +70,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {
7070
});
7171

7272
Prism.languages.insertBefore('javascript', 'string', {
73+
'hashbang': {
74+
pattern: /^#!.*/,
75+
greedy: true,
76+
alias: 'comment'
77+
},
7378
'template-string': {
7479
pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|(?!\${)[^\\`])*`/,
7580
greedy: true,

components/prism-javascript.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
15221522
lookbehind: true
15231523
},
15241524
{
1525-
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/,
1525+
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/,
15261526
lookbehind: true
15271527
},
15281528
],
@@ -1580,6 +1580,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {
15801580
});
15811581

15821582
Prism.languages.insertBefore('javascript', 'string', {
1583+
'hashbang': {
1584+
pattern: /^#!.*/,
1585+
greedy: true,
1586+
alias: 'comment'
1587+
},
15831588
'template-string': {
15841589
pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|(?!\${)[^\\`])*`/,
15851590
greedy: true,
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,121 @@
1-
const obj = {
2-
get name() { return 'bar'; },
3-
get [expr]() { return 'bar'; },
4-
async get name() { return 'bar'; },
5-
set name(val) { },
6-
set [expr](val) { },
7-
async set [expr](val) { },
8-
};
9-
10-
// not keywords
11-
get();
12-
set(foo);
13-
14-
----------------------------------------------------
15-
16-
[
17-
["keyword", "const"],
18-
" obj ",
19-
["operator", "="],
20-
["punctuation", "{"],
21-
22-
["keyword", "get"],
23-
["function", "name"],
24-
["punctuation", "("],
25-
["punctuation", ")"],
26-
["punctuation", "{"],
27-
["keyword", "return"],
28-
["string", "'bar'"],
29-
["punctuation", ";"],
30-
["punctuation", "}"],
31-
["punctuation", ","],
32-
33-
["keyword", "get"],
34-
["punctuation", "["],
35-
"expr",
36-
["punctuation", "]"],
37-
["punctuation", "("],
38-
["punctuation", ")"],
39-
["punctuation", "{"],
40-
["keyword", "return"],
41-
["string", "'bar'"],
42-
["punctuation", ";"],
43-
["punctuation", "}"],
44-
["punctuation", ","],
45-
46-
["keyword", "async"],
47-
["keyword", "get"],
48-
["function", "name"],
49-
["punctuation", "("],
50-
["punctuation", ")"],
51-
["punctuation", "{"],
52-
["keyword", "return"],
53-
["string", "'bar'"],
54-
["punctuation", ";"],
55-
["punctuation", "}"],
56-
["punctuation", ","],
57-
58-
["keyword", "set"],
59-
["function", "name"],
60-
["punctuation", "("],
61-
["parameter", [
62-
"val"
63-
]],
64-
["punctuation", ")"],
65-
["punctuation", "{"],
66-
["punctuation", "}"],
67-
["punctuation", ","],
68-
69-
["keyword", "set"],
70-
["punctuation", "["],
71-
"expr",
72-
["punctuation", "]"],
73-
["punctuation", "("],
74-
["parameter", [
75-
"val"
76-
]],
77-
["punctuation", ")"],
78-
["punctuation", "{"],
79-
["punctuation", "}"],
80-
["punctuation", ","],
81-
82-
["keyword", "async"],
83-
["keyword", "set"],
84-
["punctuation", "["],
85-
"expr",
86-
["punctuation", "]"],
87-
["punctuation", "("],
88-
["parameter", [
89-
"val"
90-
]],
91-
["punctuation", ")"],
92-
["punctuation", "{"],
93-
["punctuation", "}"],
94-
["punctuation", ","],
95-
96-
["punctuation", "}"],
97-
["punctuation", ";"],
98-
99-
["comment", "// not keywords"],
100-
101-
["function", "get"],
102-
["punctuation", "("],
103-
["punctuation", ")"],
104-
["punctuation", ";"],
105-
106-
["function", "set"],
107-
["punctuation", "("],
108-
"foo",
109-
["punctuation", ")"],
110-
["punctuation", ";"]
111-
]
112-
113-
----------------------------------------------------
114-
115-
Checks for getters and setters.
1+
const obj = {
2+
get name() { return 'bar'; },
3+
get [expr]() { return 'bar'; },
4+
async get name() { return 'bar'; },
5+
set name(val) { },
6+
set [expr](val) { },
7+
async set [expr](val) { },
8+
get #x() { return #xValue; },
9+
};
10+
11+
// not keywords
12+
get();
13+
set(foo);
14+
15+
----------------------------------------------------
16+
17+
[
18+
["keyword", "const"],
19+
" obj ",
20+
["operator", "="],
21+
["punctuation", "{"],
22+
23+
["keyword", "get"],
24+
["function", "name"],
25+
["punctuation", "("],
26+
["punctuation", ")"],
27+
["punctuation", "{"],
28+
["keyword", "return"],
29+
["string", "'bar'"],
30+
["punctuation", ";"],
31+
["punctuation", "}"],
32+
["punctuation", ","],
33+
34+
["keyword", "get"],
35+
["punctuation", "["],
36+
"expr",
37+
["punctuation", "]"],
38+
["punctuation", "("],
39+
["punctuation", ")"],
40+
["punctuation", "{"],
41+
["keyword", "return"],
42+
["string", "'bar'"],
43+
["punctuation", ";"],
44+
["punctuation", "}"],
45+
["punctuation", ","],
46+
47+
["keyword", "async"],
48+
["keyword", "get"],
49+
["function", "name"],
50+
["punctuation", "("],
51+
["punctuation", ")"],
52+
["punctuation", "{"],
53+
["keyword", "return"],
54+
["string", "'bar'"],
55+
["punctuation", ";"],
56+
["punctuation", "}"],
57+
["punctuation", ","],
58+
59+
["keyword", "set"],
60+
["function", "name"],
61+
["punctuation", "("],
62+
["parameter", ["val"]],
63+
["punctuation", ")"],
64+
["punctuation", "{"],
65+
["punctuation", "}"],
66+
["punctuation", ","],
67+
68+
["keyword", "set"],
69+
["punctuation", "["],
70+
"expr",
71+
["punctuation", "]"],
72+
["punctuation", "("],
73+
["parameter", ["val"]],
74+
["punctuation", ")"],
75+
["punctuation", "{"],
76+
["punctuation", "}"],
77+
["punctuation", ","],
78+
79+
["keyword", "async"],
80+
["keyword", "set"],
81+
["punctuation", "["],
82+
"expr",
83+
["punctuation", "]"],
84+
["punctuation", "("],
85+
["parameter", ["val"]],
86+
["punctuation", ")"],
87+
["punctuation", "{"],
88+
["punctuation", "}"],
89+
["punctuation", ","],
90+
91+
["keyword", "get"],
92+
["function", "#x"],
93+
["punctuation", "("],
94+
["punctuation", ")"],
95+
["punctuation", "{"],
96+
["keyword", "return"],
97+
" #xValue",
98+
["punctuation", ";"],
99+
["punctuation", "}"],
100+
["punctuation", ","],
101+
102+
["punctuation", "}"],
103+
["punctuation", ";"],
104+
105+
["comment", "// not keywords"],
106+
107+
["function", "get"],
108+
["punctuation", "("],
109+
["punctuation", ")"],
110+
["punctuation", ";"],
111+
112+
["function", "set"],
113+
["punctuation", "("],
114+
"foo",
115+
["punctuation", ")"],
116+
["punctuation", ";"]
117+
]
118+
119+
----------------------------------------------------
120+
121+
Checks for getters and setters.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
// in the Script Goal
3+
'use strict';
4+
console.log(1);
5+
6+
----------------------------------------------------
7+
8+
[
9+
["hashbang", "#!/usr/bin/env node"],
10+
11+
["comment", "// in the Script Goal"],
12+
13+
["string", "'use strict'"],
14+
["punctuation", ";"],
15+
16+
"\r\nconsole",
17+
["punctuation", "."],
18+
["function", "log"],
19+
["punctuation", "("],
20+
["number", "1"],
21+
["punctuation", ")"],
22+
["punctuation", ";"]
23+
]

0 commit comments

Comments
 (0)