Allow # to start identifier, such as private fields in classes

To resolve #1734 , this change adds # which is hex 23 to begin an identifier name.
This commit is contained in:
Michael Dombrowski 2019-12-01 15:55:18 -08:00 committed by Michael Dombrowski
parent db4bc4d889
commit c26968ab88
5 changed files with 54 additions and 0 deletions

View File

@ -274,6 +274,12 @@ Tokenizer.prototype._read_non_javascript = function(c) {
this._input.next();
}
return this._create_token(TOKEN.WORD, sharp);
} else if (this._input.hasNext()) {
// Handle private field (ie. #privateExample)
resulting_string = this.__patterns.identifier.read();
if (resulting_string) {
return this._create_token(TOKEN.WORD, sharp + resulting_string);
}
}
this._input.back();

View File

@ -404,6 +404,20 @@ function run_javascript_tests(test_obj, Urlencoded, js_beautify, html_beautify,
'};');
//============================================================
// ES5 Class Private Fields
reset_options();
set_name('ES5 Class Private Fields');
bt('#foo');
bt(
'class X {\n' +
' #foo = null;\n' +
' get foo() {\n' +
' return this.#foo;\n' +
' }\n' +
'}');
//============================================================
// ES7 Decorators
reset_options();

View File

@ -408,6 +408,11 @@ class Tokenizer(BaseTokenizer):
self._input.next()
return self._create_token(TOKEN.WORD, sharp)
elif self._input.hasNext():
# Handle private field (ie. #privateExample)
resulting_string = self._patterns.identifier.read()
if resulting_string:
return self._create_token(TOKEN.WORD, sharp + resulting_string)
self._input.back()

View File

@ -211,6 +211,19 @@ class TestJSBeautifier(unittest.TestCase):
'};')
#============================================================
# ES5 Class Private Fields
self.reset_options()
bt('#foo')
bt(
'class X {\n' +
' #foo = null;\n' +
' get foo() {\n' +
' return this.#foo;\n' +
' }\n' +
'}')
#============================================================
# ES7 Decorators
self.reset_options()

View File

@ -169,6 +169,22 @@ exports.test_data = {
]
}
]
}, {
name: "ES5 Class Private Fields",
description: "Permit ES5 private class fields which are declared with a leading \"#\".",
tests: [
{ unchanged: '#foo' },
{
unchanged: [
'class X {',
' #foo = null;',
' get foo() {',
' return this.#foo;',
' }',
'}'
]
}
]
}, {
name: "ES7 Decorators",
description: "Permit ES7 decorators, which are invoked with a leading \"@\".",