Skip to content

Commit

Permalink
fix corner case in parsing private field/method (#4952)
Browse files Browse the repository at this point in the history
fixes #4951
  • Loading branch information
alexlamsl committed May 22, 2021
1 parent de376c3 commit d2a45ba
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ var PUNC_AFTER_EXPRESSION = PUNC_SEPARATORS + PUNC_CLOSERS;
var PUNC_BEFORE_EXPRESSION = PUNC_OPENERS + PUNC_SEPARATORS;
var PUNC_CHARS = PUNC_BEFORE_EXPRESSION + "`" + PUNC_CLOSERS;
var WHITESPACE_CHARS = NEWLINE_CHARS + " \u00a0\t\f\u000b\u200b\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000\uFEFF";
var NON_IDENTIFIER_CHARS = makePredicate(characters("./'\"" + OPERATOR_CHARS + PUNC_CHARS + WHITESPACE_CHARS));
var NON_IDENTIFIER_CHARS = makePredicate(characters("./'\"#" + OPERATOR_CHARS + PUNC_CHARS + WHITESPACE_CHARS));

NEWLINE_CHARS = makePredicate(characters(NEWLINE_CHARS));
OPERATOR_CHARS = makePredicate(characters(OPERATOR_CHARS));
Expand Down Expand Up @@ -468,7 +468,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
});

function read_name() {
var backslash = false, name = "", ch, escaped = false, hex;
var backslash = false, ch, escaped = false, name = peek() == "#" ? next() : "";
while (ch = peek()) {
if (!backslash) {
if (ch == "\\") escaped = backslash = true, next();
Expand All @@ -483,7 +483,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
}
}
if (KEYWORDS[name] && escaped) {
hex = name.charCodeAt(0).toString(16).toUpperCase();
var hex = name.charCodeAt(0).toString(16).toUpperCase();
name = "\\u" + "0000".substr(hex.length) + hex + name.slice(1);
}
return name;
Expand Down Expand Up @@ -618,7 +618,7 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
if (PUNC_CHARS[ch]) return token("punc", next());
if (looking_at("=>")) return token("punc", next() + next());
if (OPERATOR_CHARS[ch]) return read_operator();
if (code == 92 || !NON_IDENTIFIER_CHARS[ch]) return read_word();
if (code == 35 || code == 92 || !NON_IDENTIFIER_CHARS[ch]) return read_word();
break;
}
parse_error("Unexpected character '" + ch + "'");
Expand Down
31 changes: 29 additions & 2 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fields: {
console.log(k, o[k]);
console.log(o.q);
}
expect_exact: 'var o=new class A{"#p";static #p="PASS";async;get q(){return A.#p}[6*7]=console?"foo":"bar"};for(var k in o)console.log(k,o[k]);console.log(o.q);'
expect_exact: 'var o=new class A{"#p";static#p="PASS";async;get q(){return A.#p}[6*7]=console?"foo":"bar"};for(var k in o)console.log(k,o[k]);console.log(o.q);'
expect_stdout: [
"42 foo",
"#p undefined",
Expand Down Expand Up @@ -136,7 +136,7 @@ private_methods: {
}
}().q.then(console.log);
}
expect_exact: "(new class A{static*#f(){yield 3*A.#p}async #g(){for(var a of A.#f())return a*await 2}static get #p(){return 7}get q(){return this.#g()}}).q.then(console.log);"
expect_exact: "(new class A{static*#f(){yield 3*A.#p}async#g(){for(var a of A.#f())return a*await 2}static get#p(){return 7}get q(){return this.#g()}}).q.then(console.log);"
expect_stdout: "42"
node_version: ">=14.6"
}
Expand Down Expand Up @@ -1563,3 +1563,30 @@ drop_unused_self_reference: {
expect_stdout: "PASS"
node_version: ">=4"
}

issue_4951_1: {
input: {
class A {
static#p = console.log("PASS");
}
}
expect_exact: 'class A{static#p=console.log("PASS")}'
expect_stdout: "PASS"
node_version: ">=12"
}

issue_4951_2: {
input: {
new class {
constructor() {
this.#f().then(console.log);
}
async#f() {
return await "PASS";
}
}();
}
expect_exact: 'new class{constructor(){this.#f().then(console.log)}async#f(){return await"PASS"}};'
expect_stdout: "PASS"
node_version: ">=14.6"
}
2 changes: 1 addition & 1 deletion test/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.run_code = semver.satisfies(process.version, "0.8") ? function(code, top
if ([
/\basync[ \t]*\([\s\S]*?\)[ \t]*=>/,
/\b(async[ \t]+function|setImmediate|setInterval|setTimeout)\b/,
/\basync([ \t]+|[ \t]*\*[ \t]*)[^\s()[\]{},.&|!~=*%/+-]+(\s*\(|[ \t]*=>)/,
/\basync([ \t]+|[ \t]*#|[ \t]*\*[ \t]*)[^\s()[\]{},.&|!~=*%/+-]+(\s*\(|[ \t]*=>)/,
].some(function(pattern) {
return pattern.test(code);
})) {
Expand Down

0 comments on commit d2a45ba

Please sign in to comment.