Skip to content

Commit

Permalink
parse #private in syntax when adjacent to other operators. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Apr 21, 2024
1 parent 3621443 commit f038936
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/parse.js
Expand Up @@ -3279,7 +3279,7 @@ function parse($TEXT, options) {
var prec = op != null ? PRECEDENCE[op] : null;
if (prec != null && (prec > min_prec || (op === "**" && min_prec === prec))) {
next();
var right = expr_op(maybe_unary(true), prec, no_in);
var right = expr_ops(no_in, prec, true);
return expr_op(new AST_Binary({
start : left.start,
left : left,
Expand All @@ -3291,9 +3291,9 @@ function parse($TEXT, options) {
return left;
};

function expr_ops(no_in) {
function expr_ops(no_in, min_prec, allow_calls, allow_arrows) {
// maybe_unary won't return us a AST_SymbolPrivateProperty
if (!no_in && is("privatename")) {
if (!no_in && min_prec < PRECEDENCE["in"] && is("privatename")) {
if(!S.in_class) {
croak("Private field must be used in an enclosing class");
}
Expand All @@ -3310,19 +3310,19 @@ function parse($TEXT, options) {
const private_in = new AST_PrivateIn({
start,
key,
value: expr_op(maybe_unary(true), PRECEDENCE["in"], no_in),
value: expr_ops(no_in, PRECEDENCE["in"], true),
end: prev()
});

return expr_op(private_in, 0, no_in);
} else {
return expr_op(maybe_unary(true, true), 0, no_in);
return expr_op(maybe_unary(allow_calls, allow_arrows), min_prec, no_in);
}
}

var maybe_conditional = function(no_in) {
var start = S.token;
var expr = expr_ops(no_in);
var expr = expr_ops(no_in, 0, true, true);
if (is("operator", "?")) {
next();
var yes = expression(false);
Expand Down
52 changes: 52 additions & 0 deletions test/compress/class-properties.js
Expand Up @@ -574,3 +574,55 @@ parens_in_11: {
expect_exact: "class X{static{console.log(this in(#x in this))}}"
}

privatein_precedence: {
input: {
class X {
static { console.log(this && #x in this); }
}
}
expect_exact: "class X{static{console.log(this&&#x in this)}}"
}

privatein_precedence_2: {
input: {
class X {
static { console.log(1 === #x in this); }
}
}
expect_exact: "class X{static{console.log(1===#x in this)}}"
}

privatein_precedence_3: {
input: {
class X {
static { console.log(#x in this in 1); }
}
}
expect_exact: "class X{static{console.log(#x in this in 1)}}"
}

privatein_precedence_bad_1: {
bad_input: `
class X {
static { console.log(1 << #x in this); }
}
`
expect_error: ({
name: "SyntaxError",
line: 3,
col: 38,
})
}

privatein_precedence_bad_2: {
bad_input: `
class X {
static { console.log(1 in #x in this); }
}
`
expect_error: ({
name: "SyntaxError",
line: 3,
col: 38,
})
}

0 comments on commit f038936

Please sign in to comment.