Fix: comprehension as postfix conditional (#5310)

Co-authored-by: Geoffrey Booth <webmaster@geoffreybooth.com>
This commit is contained in:
Julian Rosse 2020-03-10 10:29:00 -07:00 committed by GitHub
parent 3136bde928
commit 75d376f2ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 7 deletions

View File

@ -6761,10 +6761,14 @@
// flexibility or more speed than a comprehension can provide.
exports.While = While = (function() {
class While extends Base {
constructor(condition1, {invert, guard, isLoop} = {}) {
constructor(condition1, {
invert: inverted,
guard,
isLoop
} = {}) {
super();
this.condition = condition1;
this.invert = invert;
this.inverted = inverted;
this.guard = guard;
this.isLoop = isLoop;
}
@ -6839,7 +6843,7 @@
}
processedCondition() {
return this.processedConditionCache != null ? this.processedConditionCache : this.processedConditionCache = this.invert ? this.condition.invert() : this.condition;
return this.processedConditionCache != null ? this.processedConditionCache : this.processedConditionCache = this.inverted ? this.condition.invert() : this.condition;
}
astType() {
@ -6852,7 +6856,7 @@
test: this.condition.ast(o, LEVEL_PAREN),
body: this.body.ast(o, LEVEL_TOP),
guard: (ref1 = (ref2 = this.guard) != null ? ref2.ast(o) : void 0) != null ? ref1 : null,
inverted: !!this.invert,
inverted: !!this.inverted,
postfix: !!this.postfix,
loop: !!this.isLoop
};

View File

@ -4527,7 +4527,7 @@ exports.Elision = class Elision extends Base
# it, all other loops can be manufactured. Useful in cases where you need more
# flexibility or more speed than a comprehension can provide.
exports.While = class While extends Base
constructor: (@condition, {@invert, @guard, @isLoop} = {}) ->
constructor: (@condition, {invert: @inverted, @guard, @isLoop} = {}) ->
super()
children: ['condition', 'guard', 'body']
@ -4578,7 +4578,7 @@ exports.While = class While extends Base
answer
processedCondition: ->
@processedConditionCache ?= if @invert then @condition.invert() else @condition
@processedConditionCache ?= if @inverted then @condition.invert() else @condition
astType: -> 'WhileStatement'
@ -4587,7 +4587,7 @@ exports.While = class While extends Base
test: @condition.ast o, LEVEL_PAREN
body: @body.ast o, LEVEL_TOP
guard: @guard?.ast(o) ? null
inverted: !!@invert
inverted: !!@inverted
postfix: !!@postfix
loop: !!@isLoop

View File

@ -613,3 +613,8 @@ test "for pattern variables can linebreak/indent", ->
anotherNonexistentElement
] in listOfArrays
eq a, 2
test "#5309: comprehension as postfix condition", ->
doesNotThrowCompileError """
throw new Error "DOOM was called with a null element" unless elm? for elm in elms
"""