Skip to content

Commit

Permalink
Fix for #1216 ?= compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
geraldalewis committed May 10, 2011
1 parent d4d0271 commit 2212e95
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 17 deletions.
28 changes: 20 additions & 8 deletions lib/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 13 additions & 9 deletions src/nodes.coffee
Expand Up @@ -1009,7 +1009,7 @@ exports.Assign = class Assign extends Base
# more than once.
compileConditional: (o) ->
[left, rite] = @variable.cacheReference o
new Op(@context.slice(0, -1), left, new Assign(rite, @value, '=')).compile o
new Op(@context.slice(0, -1), left, new Assign(rite, @value, '='), undefined, "?" in @context ).compile o

# Compile the assignment from an array splice literal, using JavaScript's
# `Array#splice` method.
Expand Down Expand Up @@ -1218,7 +1218,10 @@ exports.While = class While extends Base
# Simple Arithmetic and logical operations. Performs some conversion from
# CoffeeScript operations into their JavaScript equivalents.
exports.Op = class Op extends Base
constructor: (op, first, second, flip) ->



constructor: (op, first, second, flip, @isExistentialEquals ) ->
return new In first, second if op is 'in'
if op is 'do'
call = new Call first, first.params or []
Expand Down Expand Up @@ -1313,7 +1316,10 @@ exports.Op = class Op extends Base
else
fst = @first
ref = fst
new If(new Existence(fst), ref, type: 'if').addElse(@second).compile o
if @isExistentialEquals
new If(new Existence(fst).invert(), @second, type: 'if').compile o
else
new If(new Existence(fst), ref, type: 'if').addElse(@second).compile o

# Compile a unary **Op**.
compileUnary: (o) ->
Expand Down Expand Up @@ -1426,13 +1432,11 @@ exports.Existence = class Existence extends Base
compileNode: (o) ->
code = @expression.compile o, LEVEL_OP
code = if IDENTIFIER.test(code) and not o.scope.check code
if @negated
"typeof #{code} === \"undefined\" || #{code} === null"
else
"typeof #{code} !== \"undefined\" && #{code} !== null"
[cmp, cnj] = if @negated then ['===', '||'] else ['!==', '&&']
"typeof #{code} #{cmp} \"undefined\" #{cnj} #{code} #{cmp} null"
else
sym = if @negated then '==' else '!='
"#{code} #{sym} null"
# do not use strict equality here; it will break existing code
"#{code} #{if @negated then '==' else '!='} null"
if o.level <= LEVEL_COND then code else "(#{code})"

#### Parens
Expand Down
35 changes: 35 additions & 0 deletions test/assignment.coffee
Expand Up @@ -268,3 +268,38 @@ test "existential assignment", ->
eq nonce, c
d ?= nonce
eq nonce, d

test "#1216 ?= compilation", ->
c = (s) -> CoffeeScript.compile( s, {bare:true} )

# ?= with locally scoped var defined
eq c('a = 0; a ?= b'),
'''var a;
a = 0;
if (a == null) {
a = b;
};'''

# ?= with locally scoped var not defined
eq c('a ?= b'),
'''if (typeof a === "undefined" || a === null) {
a = b;
};'''

# ? with locally scoped var defined
eq c('a = 0; return unless a?'),
'''
var a;
a = 0;
if (a == null) {
return;
}
'''

# ? with locally scoped var not defined
eq c('return unless a?'),
'''
if (typeof a === "undefined" || a === null) {
return;
}
'''

0 comments on commit 2212e95

Please sign in to comment.