Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance booleans #5365

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 30 additions & 23 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -2085,33 +2085,40 @@ TreeWalker.prototype = {
}
},
in_boolean_context: function() {
var self = this.self();
for (var i = 0, p; p = this.parent(i); i++) {
if (p instanceof AST_Conditional && p.condition === self
|| p instanceof AST_DWLoop && p.condition === self
|| p instanceof AST_For && p.condition === self
|| p instanceof AST_If && p.condition === self
|| p instanceof AST_Return && p.in_bool
|| p instanceof AST_Sequence && p.tail_node() !== self
|| p instanceof AST_SimpleStatement
|| p instanceof AST_UnaryPrefix && p.operator == "!" && p.expression === self) {
return true;
for (var drop = true, level = 0, parent, self = this.self(); parent = this.parent(level++); self = parent) {
if (parent instanceof AST_Binary) switch (parent.operator) {
case "&&":
case "||":
if (parent.left === self) drop = false;
continue;
default:
return false;
}
if (parent instanceof AST_Conditional) {
if (parent.condition === self) return true;
continue;
}
if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")
|| p instanceof AST_Conditional
|| p.tail_node() === self) {
self = p;
} else if (p instanceof AST_Return) {
for (var call, fn = p; call = this.parent(++i); fn = call) {
if (call.TYPE == "Call") {
if (!(fn instanceof AST_Lambda) || fn.name) return false;
} else if (fn instanceof AST_Lambda) {
return false;
if (parent instanceof AST_DWLoop) return parent.condition === self;
if (parent instanceof AST_For) return parent.condition === self;
if (parent instanceof AST_If) return parent.condition === self;
if (parent instanceof AST_Return) {
if (parent.in_bool) return true;
while (parent = this.parent(level++)) {
if (parent instanceof AST_Lambda) {
if (parent.name) return false;
parent = this.parent(level++);
if (parent.TYPE != "Call") return false;
break;
}
}
} else {
return false;
}
if (parent instanceof AST_Sequence) {
if (parent.tail_node() === self) continue;
return drop ? "d" : true;
}
if (parent instanceof AST_SimpleStatement) return drop ? "d" : true;
if (parent instanceof AST_UnaryPrefix) return parent.operator == "!";
return false;
}
}
};