Skip to content

Commit

Permalink
Fix jashkenas#1500, jashkenas#1574, jashkenas#3318: Name generated va…
Browse files Browse the repository at this point in the history
…rs uniquely

Any variables generated by CoffeeScript are now made sure to be named to
something not present in the source code being compiled. This way you can no
longer interfere with them, either on purpose or by mistake. (jashkenas#1500, jashkenas#1574)

For example, `({a}, _arg) ->` now compiles correctly. (jashkenas#1574)

As opposed to the somewhat complex implementations discussed in jashkenas#1500, this
commit takes a very simple approach by saving all used variables names using a
single pass over the token stream. Any generated variables are then made sure
not to exist in that list.

`(@A) -> a` used to be equivalent to `(@A) -> @a`, but now throws a runtime
`ReferenceError` instead (unless `a` exists in an upper scope of course). (jashkenas#3318)

`(@A) ->` used to compile to `(function(a) { this.a = a; })`. Now it compiles to
`(function(_at_a) { this.a = _at_a; })`. (But you cannot access `_at_a` either,
of course.)

Because of the above, `(@A, a) ->` is now valid; `@a` and `a` are not duplicate
parameters.

Duplicate this-parameters with a reserved word, such as `(@case, @case) ->`,
used to compile but now throws, just like regular duplicate parameters.
  • Loading branch information
lydell committed Jan 10, 2015
1 parent d11c14b commit ede89eb
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 119 deletions.
20 changes: 16 additions & 4 deletions lib/coffee-script/coffee-script.js

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

1 change: 1 addition & 0 deletions lib/coffee-script/lexer.js

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

150 changes: 70 additions & 80 deletions lib/coffee-script/nodes.js

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

0 comments on commit ede89eb

Please sign in to comment.