Skip to content

Commit

Permalink
JavaScript: Private fields (#1950)
Browse files Browse the repository at this point in the history
This adds support for private fields in both JS and JS Extras.

[Proposal](https://github.com/tc39/proposal-class-fields#private-fields).
  • Loading branch information
RunDevelopment authored Jul 13, 2019
1 parent 9d9e2ca commit 7bd0832
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 7 deletions.
4 changes: 2 additions & 2 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
'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)?)+)?/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
'function': /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

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

2 changes: 1 addition & 1 deletion components/prism-js-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

Prism.languages.insertBefore('javascript', 'punctuation', {
'property-access': {
pattern: /(\.\s*)[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
pattern: /(\.\s*)#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
lookbehind: true
},
'maybe-class-name': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-js-extras.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
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ Prism.languages.javascript = Prism.languages.extend('clike', {
],
'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)?)+)?/,
// Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444)
'function': /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
'function': /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,
'operator': /-[-=]?|\+[+=]?|!=?=?|<<?=?|>>?>?=?|=(?:==?|>)?|&[&=]?|\|[|=]?|\*\*?=?|\/=?|~|\^=?|%=?|\?|\.{3}/
});

Expand All @@ -799,7 +799,7 @@ Prism.languages.insertBefore('javascript', 'keyword', {
},
// This must be declared before keyword because we use "function" inside the look-forward
'function-variable': {
pattern: /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
pattern: /#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*)\s*=>))/,
alias: 'function'
},
'parameter': [
Expand Down
14 changes: 14 additions & 0 deletions tests/languages/javascript!+js-extras/method-variable_feature.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
foo.bar = function() {};
foo.bar = async () => {};
this.bar = () => {};

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

Expand Down Expand Up @@ -29,6 +30,19 @@ foo.bar = async () => {};
["arrow", "=>"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"],

["keyword", "this"],
["punctuation", "."],
["method-variable", [
"bar"
]],
["operator", "="],
["punctuation", "("],
["punctuation", ")"],
["arrow", "=>"],
["punctuation", "{"],
["punctuation", "}"],
["punctuation", ";"]
]

Expand Down
10 changes: 10 additions & 0 deletions tests/languages/javascript!+js-extras/method_feature.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
foo.bar();
foo.bar.call();
this.#bar();

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

Expand All @@ -24,6 +25,15 @@ foo.bar.call();
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["keyword", "this"],
["punctuation", "."],
["method", [
"#bar"
]],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"]
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
foo.bar.baz = 0;

this.#foo;

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

[
Expand All @@ -14,6 +16,13 @@ foo.bar.baz = 0;
]],
["operator", "="],
["number", "0"],
["punctuation", ";"],

["keyword", "this"],
["punctuation", "."],
["property-access", [
"#foo"
]],
["punctuation", ";"]
]

Expand Down
58 changes: 58 additions & 0 deletions tests/languages/javascript/private_fields_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Foo {
#foo = function () {
return this.#bar;
}
#bar = 9;

get value() {
return this.#foo();
}
}

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

[
["keyword", "class"],
["class-name", [
"Foo"
]],
["punctuation", "{"],

["function-variable", "#foo"],
["operator", "="],
["keyword", "function"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
["keyword", "this"],
["punctuation", "."],
"#bar",
["punctuation", ";"],
["punctuation", "}"],

"\r\n\t#bar ",
["operator", "="],
["number", "9"],
["punctuation", ";"],

["keyword", "get"],
["function", "value"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "return"],
["keyword", "this"],
["punctuation", "."],
["function", "#foo"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],
["punctuation", "}"],

["punctuation", "}"]
]

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

Checks for the private field syntax.

0 comments on commit 7bd0832

Please sign in to comment.