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

Soak operations should allow undeclared variables #801

Closed
alangpierce opened this issue Feb 6, 2017 · 0 comments
Closed

Soak operations should allow undeclared variables #801

alangpierce opened this issue Feb 6, 2017 · 0 comments
Assignees

Comments

@alangpierce
Copy link
Member

decaffeinate is producing the wrong JavaScript based on my CoffeeScript input:

cs = window?.CoffeeScript

(repl)

I get this output:

let cs = __guard__(window, x => x.CoffeeScript);
function __guard__(value, transform) {
  return (typeof value !== 'undefined' && value !== null) ? transform(value) : undefined;
}

Here's what I expect it to be instead:

let cs = typeof window !== "undefined" && window !== null ? window.CoffeeScript : undefined;

I think that this only happens with identifiers, and identifiers are always repeatable, so it should be reasonable to skip the __guard__ call in this case.

https://github.com/clutchski/coffeelint/blob/master/src/cache.coffee#L6

@alangpierce alangpierce self-assigned this Feb 6, 2017
@alangpierce alangpierce changed the title Guard operations should allow undeclared variables Soak operations should allow undeclared variables Feb 10, 2017
alangpierce added a commit to alangpierce/decaffeinate that referenced this issue Feb 18, 2017
Progress toward decaffeinate#801
Progress toward decaffeinate#336

Now, with a simple soak expression like `a?.b`, it will be expanded into a
ternary rather than using the `__guard__` function. This allows us to handle the
case where the variable is unbound (in which case the code should return
`undefined` rather than throwing an exception). More specifically, we use the
conditional form of soak operations

As described in decaffeinate#336, I separate the expression and statement cases and prefer
to wrap in an `if` if possible.

This only handles the case for normal soaked member access; a later commit will
expand it to soaked dynamic member access and soaked function application.

Also fix a subtle bug in repeatable expression patching where a paren could be
misplaced.
alangpierce added a commit to alangpierce/decaffeinate that referenced this issue Feb 18, 2017
Progress toward decaffeinate#801
Progress toward decaffeinate#336

Now, with a simple soak expression like `a?.b`, it will be expanded into a
ternary rather than using the `__guard__` function. This allows us to handle the
case where the variable is unbound (in which case the code should return
`undefined` rather than throwing an exception). More specifically, we use the
conditional form of soak operations

As described in decaffeinate#336, I separate the expression and statement cases and prefer
to wrap in an `if` if possible.

This only handles the case for normal soaked member access; a later commit will
expand it to soaked dynamic member access and soaked function application.

Also fix a subtle bug in repeatable expression patching where a paren could be
misplaced.
alangpierce added a commit that referenced this issue Feb 19, 2017
Progress toward #801
Progress toward #336

Now, with a simple soak expression like `a?.b`, it will be expanded into a
ternary rather than using the `__guard__` function. This allows us to handle the
case where the variable is unbound (in which case the code should return
`undefined` rather than throwing an exception). More specifically, we use the
conditional form of soak operations

As described in #336, I separate the expression and statement cases and prefer
to wrap in an `if` if possible.

This only handles the case for normal soaked member access; a later commit will
expand it to soaked dynamic member access and soaked function application.

Also fix a subtle bug in repeatable expression patching where a paren could be
misplaced.
alangpierce added a commit to alangpierce/decaffeinate that referenced this issue Feb 19, 2017
Fixes decaffeinate#801
Progress toward decaffeinate#336

We now use a conditional for soaked dynamic member access and soaked function
calls as long as the expression is repeatable and does not contain any soak
operations.

This is pretty much the same strategy as for soaked member accesses. For
function calls, the code naturally becomes method call syntax when necessary, so
we don't need a special case for methods like with the `__guardMethod__` stuff.

There's some duplicated code, but a lot of helper methods have been shared and
there are enough differences that it seems reasonable to have the duplication
for now and try to consolidate later if it becomes a bigger issue.
alangpierce added a commit that referenced this issue Feb 19, 2017
Fixes #801
Progress toward #336

We now use a conditional for soaked dynamic member access and soaked function
calls as long as the expression is repeatable and does not contain any soak
operations.

This is pretty much the same strategy as for soaked member accesses. For
function calls, the code naturally becomes method call syntax when necessary, so
we don't need a special case for methods like with the `__guardMethod__` stuff.

There's some duplicated code, but a lot of helper methods have been shared and
there are enough differences that it seems reasonable to have the duplication
for now and try to consolidate later if it becomes a bigger issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant