Commit Graph

22 Commits

Author SHA1 Message Date
Simon Lydell 8ab15d7372 Fix #1500, #1574, #3318: Name generated vars 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. (#1500, #1574)

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

As opposed to the somewhat complex implementations discussed in #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). (#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.
2015-01-10 23:25:01 +01:00
Simon Lydell 0dcff507fb Refactor interpolation (and string and regex) handling in lexer
- Fix #3394: Unclosed single-quoted strings (both regular ones and heredocs)
  used to pass through the lexer, causing a parsing error later, while
  double-quoted strings caused an error already in the lexing phase. Now both
  single and double-quoted unclosed strings error out in the lexer (which is the
  more logical option) with consistent error messages. This also fixes the last
  comment by @satyr in #3301.

- Similar to the above, unclosed heregexes also used to pass through the lexer
  and not error until in the parsing phase, which resulted in confusing error
  messages. This has been fixed, too.

- Fix #3348, by adding passing tests.

- Fix #3529: If a string starts with an interpolation, an empty string is no
  longer emitted before the interpolation (unless it is needed to coerce the
  interpolation into a string).

- Block comments cannot contain `*/`. Now the error message also shows exactly
  where the offending `*/`. This improvement might seem unrelated, but I had to
  touch that code anyway to refactor string and regex related code, and the
  change was very trivial. Moreover, it's consistent with the next two points.

- Regexes cannot start with `*`. Now the error message also shows exactly where
  the offending `*` is. (It might actually not be exatly at the start in
  heregexes.) It is a very minor improvement, but it was trivial to add.

- Octal escapes in strings are forbidden in CoffeeScript (just like in
  JavaScript strict mode). However, this used to be the case only for regular
  strings. Now they are also forbidden in heredocs. Moreover, the errors now
  point at the offending octal escape.

- Invalid regex flags are no longer allowed. This includes repeated modifiers
  and unknown ones. Moreover, invalid modifiers do not stop a heregex from
  being matched, which results in better error messages.

- Fix #3621: `///a#{1}///` compiles to `RegExp("a" + 1)`. So does
  `RegExp("a#{1}")`. Still, those two code snippets used to generate different
  tokens, which is a bit weird, but more importantly causes problems for
  coffeelint (see clutchski/coffeelint#340). This required lots of tests in
  test/location.coffee to be updated. Note that some updates to those tests are
  unrelated to this point; some have been updated to be more consistent (I
  discovered this because the refactored code happened to be seemingly more
  correct).

- Regular regex literals used to erraneously allow newlines to be escaped,
  causing invalid JavaScript output. This has been fixed.

- Heregexes may now be completely empty (`//////`), instead of erroring out with
  a confusing message.

- Fix #2388: Heredocs and heregexes used to be lexed simply, which meant that
  you couldn't nest a heredoc within a heredoc (double-quoted, that is) or a
  heregex inside a heregex.

- Fix #2321: If you used division inside interpolation and then a slash later in
  the string containing that interpolation, the division slash and the latter
  slash was erraneously matched as a regex. This has been fixed.

- Indentation inside interpolations in heredocs no longer affect how much
  indentation is removed from each line of the heredoc (which is more
  intuitive).

- Whitespace is now correctly trimmed from the start and end of strings in a few
  edge cases.

- Last but not least, the lexing of interpolated strings now seems to be more
  efficient. For a regular double-quoted string, we used to use a custom
  function to find the end of it (taking interpolations and interpolations
  within interpolations etc. into account). Then we used to re-find the
  interpolations and recursively lex their contents. In effect, the same string
  was processed twice, or even more in the case of deeper nesting of
  interpolations. Now the same string is processed just once.

- Code duplication between regular strings, heredocs, regular regexes and
  heregexes has been reduced.

- The above two points should result in more easily read code, too.
2015-01-04 07:47:09 +01:00
Andreas Lubbe 7906a2b6c1 removed yield from the reserved words 2013-11-29 20:58:43 -08:00
Michael Ficarra 7c29ea4d38 removing code that restricts duplicate key names and associated tests 2012-05-21 13:49:00 -04:00
Michael Ficarra 6c6c8bd454 typo in test case for #2333 2012-05-21 13:28:18 -04:00
Michael Ficarra 81f780f1fb finally put #2333 to rest by resorting to using indirect eval
related: #1772, #1776; we're relying on the underlying engine having the
string escaping behaviour we want instead of implementing it manually.
2012-05-21 13:26:29 -04:00
Michael Ficarra df54c63b1b yet another small cleanup and obscure bugfix related to #2333 2012-05-16 15:29:00 -04:00
Michael Ficarra 1810d9f318 object key dupe checking again: support newlines and \a in strings 2012-05-16 12:53:28 -04:00
Michael Ficarra 29b9c3bb29 correcting broken fix for #2333 regarding string escape sequences
Sorry for all the commits! It should really be done this time.
2012-05-16 10:36:00 -04:00
Michael Ficarra f31ff7774a fix escaping in test for #2333 2012-05-16 08:19:06 -04:00
Michael Ficarra fa82859814 another refactoring for #2333 2012-05-16 07:02:16 -04:00
Michael Ficarra dc9565f54a fix to #2333 greatly improved, but still depends on eval :( 2012-05-16 02:03:02 -04:00
Michael Ficarra c264bf04cc fixes #2333: fix prohibition of duplicate object properties 2012-05-16 01:07:10 -04:00
Gerald Lewis 99394e1011 Fixes issue where destructured assignment params were incorrectly identified as duplicates. 2012-04-23 20:41:56 -04:00
Michael Ficarra 46ff7705ee corrections for octal escape sequences; allows "\0" alone; see #1547
Relevant sections of the spec:
* http://es5.github.com/#C
* http://es5.github.com/#B.1.2
* http://es5.github.com/#x7.8.4
2012-04-20 18:29:40 -04:00
clutchski 0ca255b7b8 Whitespace clean-up. 2012-02-03 19:31:26 -05:00
Gerald Lewis c3a8a4f81f Issue #2054 "{arguments}"
Fixes error message: SyntaxError: variable name may not be "true"

Permits assigning to "arguments" and "eval" properties in
object literals.
2012-01-19 11:33:43 -05:00
Gerald Lewis bf8e0aa1ea Issue #1547 'use strict' style tweaks, cleanup, and compiled output 2012-01-16 17:35:21 -05:00
Gerald Lewis 4372138fdd Issue #1547 'use strict' Python-style octal literal notation 0o777
Allows octals in the form '0o777' and '0O777'

Case insensitive

Disallows decimals prefixed with '0'
2012-01-16 17:22:19 -05:00
Gerald Lewis 8b179fb391 Issue #1547 'use strict' eval and arguments use restricted 2012-01-16 17:20:55 -05:00
Gerald Lewis 3a694d7dfa Issue #1547 'use strict' octal escape sequences prohibited
RegExp updated (thanks @michaelficarra)
and hex escapes for colors in Cakefile

tests updated (thanks @satyr)

error message conforms to existing Lexer SyntaxErrors
2012-01-16 17:18:13 -05:00
Gerald Lewis 66eb186a74 Issue #1547 'use strict' tests 2012-01-16 17:17:48 -05:00