check for null? #460

Open
QwertyZW opened this Issue Nov 5, 2016 · 4 comments

Projects

None yet

3 participants

@QwertyZW
QwertyZW commented Nov 5, 2016

The javascript:

var x;
if (x == undefined){
  
}

transpiles to:

x = undefined
if x == undefined
else

and emits a "Operator '==' is not supported in coffescript"

expected transpilation:

x = undefined
if  typeof x is 'undefined'
else
@rstacruz
Member
rstacruz commented Nov 5, 2016

This is weird territory... Typeof x == 'undefined' is not a direct
translation because it's possible to redefine undefined. Also, x == undefined will be true if x is null, so the more accurate translation is
more like x is undefined or x is null I guess.

With that said, try === instead to get rid of the ambiguities :)

On Sat, Nov 5, 2016, 11:01 AM QwertyZW notifications@github.com wrote:

The javascript:

var x;
if (x == undefined){

}

transpiles to:

x = undefined
if x == undefined
else

and emits a "Operator '==' is not supported in coffescript"

expected transpilation:

x = undefined
if typeof x is 'undefined'
else


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#460, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEikciTeERuQYoXF0LDQQ0GwrrhrLJMks5q6_GdgaJpZM4KqJVr
.

@flying-sheep
Collaborator
flying-sheep commented Nov 5, 2016 edited

idk about x == undefined, but x == null is a common pattern that e.g. has a eslint rule preference (deprecated only because a more general preference can be used to emulate it)

i much rather use x == null than the cumbersome and typo-prone x === null || typeof x === 'undefined'.

@QwertyZW
QwertyZW commented Nov 6, 2016 edited

@rstacruz Yea that came up to me after giving it a bit more thought. I use x == null as a shorthand for x === null || typeof x === 'undefined' . I think what you can do in this case a temporary hack would be to create a transformation pass that transforms the js

expression == null

to the js

expression === null || typeof expression === 'undefined'

before transpiling it coffee script

Of course that's just a suggestion under the assumption that the current architecture allows something like this

(JFTR I'm by no means a compiler person)

@rstacruz
Member
rstacruz commented Nov 7, 2016 edited

yep, it allows for that! I have no time to do it at the moment though
:(

On Nov 6 2016, at 12:36 pm, QwertyZW notifications@github.com wrote:

@rstacruz Yea that came up to me after giving it a bit more thought. I use x == null as a shorthand for x === null || typeof x === 'undefined' . I think what you can do in this case a temporary hack would be to create a transformation pass that transforms the js

expression == null

to the js

expression === null || typeof expression === 'undefined'

before transpiling it coffee script

Of course that's just a suggestion under the assumption that the current architecture allows something like this

(JFTR I'm by no means a compiler person)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment