Skip to content

Commit 7bd0832

Browse files
JavaScript: Private fields (#1950)
This adds support for private fields in both JS and JS Extras. [Proposal](https://github.com/tc39/proposal-class-fields#private-fields).
1 parent 9d9e2ca commit 7bd0832

File tree

9 files changed

+98
-7
lines changed

9 files changed

+98
-7
lines changed

components/prism-javascript.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
1818
],
1919
'number': /\b(?:(?:0[xX](?:[\dA-Fa-f](?:_[\dA-Fa-f])?)+|0[bB](?:[01](?:_[01])?)+|0[oO](?:[0-7](?:_[0-7])?)+)n?|(?:\d(?:_\d)?)+n|NaN|Infinity)\b|(?:\b(?:\d(?:_\d)?)+\.?(?:\d(?:_\d)?)*|\B\.(?:\d(?:_\d)?)+)(?:[Ee][+-]?(?:\d(?:_\d)?)+)?/,
2020
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
21-
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
21+
'function': /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
2222
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
2323
});
2424

@@ -32,7 +32,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
3232
},
3333
// This must be declared before keyword because we use "function" inside the look-forward
3434
'function-variable': {
35-
pattern: /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
35+
pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
3636
alias: 'function'
3737
},
3838
'parameter': [

components/prism-javascript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/prism-js-extras.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
Prism.languages.insertBefore('javascript', 'punctuation', {
6262
'property-access': {
63-
pattern: /(\.\s*)[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
63+
pattern: /(\.\s*)#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
6464
lookbehind: true
6565
},
6666
'maybe-class-name': {

components/prism-js-extras.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prism.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
785785
],
786786
'number': /\b(?:(?:0[xX](?:[\dA-Fa-f](?:_[\dA-Fa-f])?)+|0[bB](?:[01](?:_[01])?)+|0[oO](?:[0-7](?:_[0-7])?)+)n?|(?:\d(?:_\d)?)+n|NaN|Infinity)\b|(?:\b(?:\d(?:_\d)?)+\.?(?:\d(?:_\d)?)*|\B\.(?:\d(?:_\d)?)+)(?:[Ee][+-]?(?:\d(?:_\d)?)+)?/,
787787
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
788-
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
788+
'function': /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
789789
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
790790
});
791791

@@ -799,7 +799,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
799799
},
800800
// This must be declared before keyword because we use "function" inside the look-forward
801801
'function-variable': {
802-
pattern: /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
802+
pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
803803
alias: 'function'
804804
},
805805
'parameter': [

tests/languages/javascript!+js-extras/method-variable_feature.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
foo.bar = function() {};
22
foo.bar = async () => {};
3+
this.bar = () => {};
34

45
----------------------------------------------------
56

@@ -29,6 +30,19 @@ foo.bar = async () => {};
2930
["arrow", "=>"],
3031
["punctuation", "{"],
3132
["punctuation", "}"],
33+
["punctuation", ";"],
34+
35+
["keyword", "this"],
36+
["punctuation", "."],
37+
["method-variable", [
38+
"bar"
39+
]],
40+
["operator", "="],
41+
["punctuation", "("],
42+
["punctuation", ")"],
43+
["arrow", "=>"],
44+
["punctuation", "{"],
45+
["punctuation", "}"],
3246
["punctuation", ";"]
3347
]
3448

tests/languages/javascript!+js-extras/method_feature.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
foo.bar();
22
foo.bar.call();
3+
this.#bar();
34

45
----------------------------------------------------
56

@@ -24,6 +25,15 @@ foo.bar.call();
2425
]],
2526
["punctuation", "("],
2627
["punctuation", ")"],
28+
["punctuation", ";"],
29+
30+
["keyword", "this"],
31+
["punctuation", "."],
32+
["method", [
33+
"#bar"
34+
]],
35+
["punctuation", "("],
36+
["punctuation", ")"],
2737
["punctuation", ";"]
2838
]
2939

tests/languages/javascript!+js-extras/property-access_feature.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
foo.bar.baz = 0;
22

3+
this.#foo;
4+
35
----------------------------------------------------
46

57
[
@@ -14,6 +16,13 @@ foo.bar.baz = 0;
1416
]],
1517
["operator", "="],
1618
["number", "0"],
19+
["punctuation", ";"],
20+
21+
["keyword", "this"],
22+
["punctuation", "."],
23+
["property-access", [
24+
"#foo"
25+
]],
1726
["punctuation", ";"]
1827
]
1928

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Foo {
2+
#foo = function () {
3+
return this.#bar;
4+
}
5+
#bar = 9;
6+
7+
get value() {
8+
return this.#foo();
9+
}
10+
}
11+
12+
----------------------------------------------------
13+
14+
[
15+
["keyword", "class"],
16+
["class-name", [
17+
"Foo"
18+
]],
19+
["punctuation", "{"],
20+
21+
["function-variable", "#foo"],
22+
["operator", "="],
23+
["keyword", "function"],
24+
["punctuation", "("],
25+
["punctuation", ")"],
26+
["punctuation", "{"],
27+
["keyword", "return"],
28+
["keyword", "this"],
29+
["punctuation", "."],
30+
"#bar",
31+
["punctuation", ";"],
32+
["punctuation", "}"],
33+
34+
"\r\n\t#bar ",
35+
["operator", "="],
36+
["number", "9"],
37+
["punctuation", ";"],
38+
39+
["keyword", "get"],
40+
["function", "value"],
41+
["punctuation", "("],
42+
["punctuation", ")"],
43+
["punctuation", "{"],
44+
["keyword", "return"],
45+
["keyword", "this"],
46+
["punctuation", "."],
47+
["function", "#foo"],
48+
["punctuation", "("],
49+
["punctuation", ")"],
50+
["punctuation", ";"],
51+
["punctuation", "}"],
52+
53+
["punctuation", "}"]
54+
]
55+
56+
----------------------------------------------------
57+
58+
Checks for the private field syntax.

0 commit comments

Comments
 (0)