Skip to content

Commit

Permalink
enhance booleans (#5365)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Feb 21, 2022
1 parent d5afe16 commit dd3b81d
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 167 deletions.
53 changes: 30 additions & 23 deletions lib/ast.js
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;
}
}
};

0 comments on commit dd3b81d

Please sign in to comment.