2010-12-29 00:48:54 -05:00
|
|
|
|
# String Literals
|
|
|
|
|
# ---------------
|
|
|
|
|
|
|
|
|
|
# TODO: refactor string literal tests
|
2011-01-03 04:17:00 -05:00
|
|
|
|
# TODO: add indexing and method invocation tests: "string"["toString"] is String::toString, "string".toString() is "string"
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
2011-01-03 04:28:47 -05:00
|
|
|
|
# * Strings
|
|
|
|
|
# * Heredocs
|
|
|
|
|
|
2010-12-30 22:48:31 -05:00
|
|
|
|
test "backslash escapes", ->
|
|
|
|
|
eq "\\/\\\\", /\/\\/.source
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
eq '(((dollars)))', '\(\(\(dollars\)\)\)'
|
|
|
|
|
eq 'one two three', "one
|
|
|
|
|
two
|
|
|
|
|
three"
|
|
|
|
|
eq "four five", 'four
|
|
|
|
|
|
|
|
|
|
five'
|
|
|
|
|
|
2013-11-18 19:04:17 -05:00
|
|
|
|
test "#3229, multiline strings", ->
|
|
|
|
|
# Separate lines by default by a single space in literal strings.
|
2013-11-17 23:32:15 -05:00
|
|
|
|
eq 'one
|
|
|
|
|
two', 'one two'
|
|
|
|
|
eq "one
|
|
|
|
|
two", 'one two'
|
2013-11-18 19:04:17 -05:00
|
|
|
|
eq '
|
|
|
|
|
a
|
|
|
|
|
b
|
|
|
|
|
', 'a b'
|
|
|
|
|
eq "
|
|
|
|
|
a
|
|
|
|
|
b
|
|
|
|
|
", 'a b'
|
|
|
|
|
eq 'one
|
|
|
|
|
|
|
|
|
|
two', 'one two'
|
|
|
|
|
eq "one
|
|
|
|
|
|
|
|
|
|
two", 'one two'
|
|
|
|
|
eq '
|
|
|
|
|
indentation
|
|
|
|
|
doesn\'t
|
|
|
|
|
matter', 'indentation doesn\'t matter'
|
2013-11-24 13:37:11 -05:00
|
|
|
|
eq 'trailing ws
|
|
|
|
|
doesn\'t matter', 'trailing ws doesn\'t matter'
|
2013-11-18 19:04:17 -05:00
|
|
|
|
|
|
|
|
|
# Use backslashes at the end of a line to specify whitespace between lines.
|
2013-11-17 23:32:15 -05:00
|
|
|
|
eq 'a \
|
|
|
|
|
b\
|
|
|
|
|
c \
|
|
|
|
|
d', 'a bc d'
|
|
|
|
|
eq "a \
|
|
|
|
|
b\
|
|
|
|
|
c \
|
|
|
|
|
d", 'a bc d'
|
2013-11-18 19:04:17 -05:00
|
|
|
|
eq 'ignore \
|
|
|
|
|
trailing whitespace', 'ignore trailing whitespace'
|
2013-11-17 23:32:15 -05:00
|
|
|
|
|
2013-11-18 19:04:17 -05:00
|
|
|
|
# Backslash at the beginning of a literal string.
|
|
|
|
|
eq '\
|
|
|
|
|
ok', 'ok'
|
|
|
|
|
eq ' \
|
|
|
|
|
ok', ' ok'
|
2013-11-17 23:32:15 -05:00
|
|
|
|
|
2013-11-26 14:29:13 -05:00
|
|
|
|
# #1273, empty strings.
|
|
|
|
|
eq '\
|
|
|
|
|
', ''
|
|
|
|
|
eq '
|
|
|
|
|
', ''
|
|
|
|
|
eq '
|
|
|
|
|
', ''
|
|
|
|
|
eq ' ', ' '
|
|
|
|
|
|
2013-11-18 19:04:17 -05:00
|
|
|
|
# Same behavior in interpolated strings.
|
2013-11-17 23:32:15 -05:00
|
|
|
|
eq "interpolation #{1}
|
|
|
|
|
follows #{2} \
|
|
|
|
|
too #{3}\
|
|
|
|
|
!", 'interpolation 1 follows 2 too 3!'
|
2013-11-18 10:25:11 -05:00
|
|
|
|
eq "a #{
|
|
|
|
|
'string ' + "inside
|
|
|
|
|
interpolation"
|
|
|
|
|
}", "a string inside interpolation"
|
2013-11-27 15:29:45 -05:00
|
|
|
|
eq "
|
|
|
|
|
#{1}
|
|
|
|
|
", '1'
|
2013-11-18 19:04:17 -05:00
|
|
|
|
|
|
|
|
|
# Handle escaped backslashes correctly.
|
2013-11-24 13:37:11 -05:00
|
|
|
|
eq '\\', `'\\'`
|
2013-11-18 19:04:17 -05:00
|
|
|
|
eq 'escaped backslash at EOL\\
|
|
|
|
|
next line', 'escaped backslash at EOL\\ next line'
|
|
|
|
|
eq '\\
|
|
|
|
|
next line', '\\ next line'
|
2013-11-26 14:29:13 -05:00
|
|
|
|
eq '\\
|
|
|
|
|
', '\\'
|
|
|
|
|
eq '\\\\\\
|
|
|
|
|
', '\\\\\\'
|
2013-11-18 19:04:17 -05:00
|
|
|
|
eq "#{1}\\
|
|
|
|
|
after interpolation", '1\\ after interpolation'
|
|
|
|
|
eq 'escaped backslash before slash\\ \
|
|
|
|
|
next line', 'escaped backslash before slash\\ next line'
|
|
|
|
|
eq 'triple backslash\\\
|
|
|
|
|
next line', 'triple backslash\\next line'
|
2013-11-19 18:41:43 -05:00
|
|
|
|
eq 'several escaped backslashes\\\\\\
|
|
|
|
|
ok', 'several escaped backslashes\\\\\\ ok'
|
2013-11-24 13:37:11 -05:00
|
|
|
|
eq 'several escaped backslashes slash\\\\\\\
|
|
|
|
|
ok', 'several escaped backslashes slash\\\\\\ok'
|
|
|
|
|
eq 'several escaped backslashes with trailing ws \\\\\\
|
|
|
|
|
ok', 'several escaped backslashes with trailing ws \\\\\\ ok'
|
2013-11-18 19:04:17 -05:00
|
|
|
|
|
2013-11-19 18:41:43 -05:00
|
|
|
|
# Backslashes at beginning of lines.
|
2013-11-18 19:04:17 -05:00
|
|
|
|
eq 'first line
|
|
|
|
|
\ backslash at BOL', 'first line \ backslash at BOL'
|
|
|
|
|
eq 'first line\
|
|
|
|
|
\ backslash at BOL', 'first line\ backslash at BOL'
|
|
|
|
|
|
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-03 17:40:43 -05:00
|
|
|
|
# Backslashes at end of strings.
|
|
|
|
|
eq 'first line \ ', 'first line '
|
|
|
|
|
eq 'first line
|
|
|
|
|
second line \
|
|
|
|
|
', 'first line second line '
|
|
|
|
|
eq 'first line
|
|
|
|
|
second line
|
|
|
|
|
\
|
|
|
|
|
', 'first line second line'
|
|
|
|
|
eq 'first line
|
|
|
|
|
second line
|
|
|
|
|
|
|
|
|
|
\
|
|
|
|
|
|
|
|
|
|
', 'first line second line'
|
|
|
|
|
|
2013-11-18 19:04:17 -05:00
|
|
|
|
# Edge case.
|
|
|
|
|
eq 'lone
|
|
|
|
|
|
|
|
|
|
\
|
|
|
|
|
|
|
|
|
|
backslash', 'lone backslash'
|
|
|
|
|
|
2013-11-24 13:37:11 -05:00
|
|
|
|
test "#3249, escape newlines in heredocs with backslashes", ->
|
|
|
|
|
# Ignore escaped newlines
|
|
|
|
|
eq '''
|
|
|
|
|
Set whitespace \
|
|
|
|
|
<- this is ignored\
|
|
|
|
|
none
|
|
|
|
|
normal indentation
|
|
|
|
|
''', 'Set whitespace <- this is ignorednone\n normal indentation'
|
|
|
|
|
eq """
|
|
|
|
|
Set whitespace \
|
|
|
|
|
<- this is ignored\
|
|
|
|
|
none
|
|
|
|
|
normal indentation
|
|
|
|
|
""", 'Set whitespace <- this is ignorednone\n normal indentation'
|
|
|
|
|
|
2013-11-26 14:29:13 -05:00
|
|
|
|
# Changed from #647, trailing backslash.
|
2013-11-24 13:37:11 -05:00
|
|
|
|
eq '''
|
|
|
|
|
Hello, World\
|
|
|
|
|
|
|
|
|
|
''', 'Hello, World'
|
2013-11-26 14:29:13 -05:00
|
|
|
|
eq '''
|
|
|
|
|
\\
|
|
|
|
|
''', '\\'
|
2013-11-24 13:37:11 -05:00
|
|
|
|
|
|
|
|
|
# Backslash at the beginning of a literal string.
|
|
|
|
|
eq '''\
|
|
|
|
|
ok''', 'ok'
|
|
|
|
|
eq ''' \
|
|
|
|
|
ok''', ' ok'
|
|
|
|
|
|
|
|
|
|
# Same behavior in interpolated strings.
|
|
|
|
|
eq """
|
|
|
|
|
interpolation #{1}
|
|
|
|
|
follows #{2} \
|
|
|
|
|
too #{3}\
|
|
|
|
|
!
|
|
|
|
|
""", 'interpolation 1\n follows 2 too 3!'
|
2013-11-27 15:29:45 -05:00
|
|
|
|
eq """
|
|
|
|
|
|
|
|
|
|
#{1} #{2}
|
|
|
|
|
|
|
|
|
|
""", '\n1 2\n'
|
2013-11-24 13:37:11 -05:00
|
|
|
|
|
|
|
|
|
# Handle escaped backslashes correctly.
|
|
|
|
|
eq '''
|
|
|
|
|
escaped backslash at EOL\\
|
|
|
|
|
next line
|
|
|
|
|
''', 'escaped backslash at EOL\\\n next line'
|
2013-11-26 14:29:13 -05:00
|
|
|
|
eq '''\\
|
|
|
|
|
|
|
|
|
|
''', '\\\n'
|
2013-11-24 13:37:11 -05:00
|
|
|
|
|
|
|
|
|
# Backslashes at beginning of lines.
|
|
|
|
|
eq '''first line
|
|
|
|
|
\ backslash at BOL''', 'first line\n\ backslash at BOL'
|
|
|
|
|
eq """first line\
|
|
|
|
|
\ backslash at BOL""", 'first line\ backslash at BOL'
|
|
|
|
|
|
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-03 17:40:43 -05:00
|
|
|
|
# Backslashes at end of strings.
|
|
|
|
|
eq '''first line \ ''', 'first line '
|
|
|
|
|
eq '''
|
|
|
|
|
first line
|
|
|
|
|
second line \
|
|
|
|
|
''', 'first line\nsecond line '
|
|
|
|
|
eq '''
|
|
|
|
|
first line
|
|
|
|
|
second line
|
|
|
|
|
\
|
|
|
|
|
''', 'first line\nsecond line'
|
|
|
|
|
eq '''
|
|
|
|
|
first line
|
|
|
|
|
second line
|
|
|
|
|
|
|
|
|
|
\
|
|
|
|
|
|
|
|
|
|
''', 'first line\nsecond line\n'
|
|
|
|
|
|
2013-11-26 14:29:13 -05:00
|
|
|
|
# Edge cases.
|
2013-11-24 13:37:11 -05:00
|
|
|
|
eq '''lone
|
|
|
|
|
|
|
|
|
|
\
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backslash''', 'lone\n\n backslash'
|
2013-11-26 14:29:13 -05:00
|
|
|
|
eq '''\
|
|
|
|
|
''', ''
|
2013-11-17 23:32:15 -05:00
|
|
|
|
|
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-03 17:40:43 -05:00
|
|
|
|
test '#2388: `"""` in heredoc interpolations', ->
|
|
|
|
|
eq """a heredoc #{
|
|
|
|
|
"inside \
|
|
|
|
|
interpolation"
|
|
|
|
|
}""", "a heredoc inside interpolation"
|
|
|
|
|
eq """a#{"""b"""}c""", 'abc'
|
|
|
|
|
eq """#{""""""}""", ''
|
|
|
|
|
|
|
|
|
|
test "trailing whitespace", ->
|
|
|
|
|
testTrailing = (str, expected) ->
|
|
|
|
|
eq CoffeeScript.eval(str.replace /\|$/gm, ''), expected
|
|
|
|
|
testTrailing '''" |
|
|
|
|
|
|
|
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
"''', 'a'
|
|
|
|
|
testTrailing """''' |
|
|
|
|
|
|
|
|
|
|
|
a |
|
|
|
|
|
|
|
|
|
|
|
'''""", ' \na \n '
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
#647
|
|
|
|
|
eq "''Hello, World\\''", '''
|
|
|
|
|
'\'Hello, World\\\''
|
|
|
|
|
'''
|
|
|
|
|
eq '""Hello, World\\""', """
|
|
|
|
|
"\"Hello, World\\\""
|
|
|
|
|
"""
|
|
|
|
|
|
2013-11-26 14:29:13 -05:00
|
|
|
|
test "#1273, escaping quotes at the end of heredocs.", ->
|
|
|
|
|
# """\""" no longer compiles
|
|
|
|
|
eq """\\""", '\\'
|
2013-11-28 10:46:00 -05:00
|
|
|
|
eq """\\\"""", '\\\"'
|
2013-11-26 14:29:13 -05:00
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
a = """
|
|
|
|
|
basic heredoc
|
|
|
|
|
on two lines
|
|
|
|
|
"""
|
|
|
|
|
ok a is "basic heredoc\non two lines"
|
|
|
|
|
|
|
|
|
|
a = '''
|
|
|
|
|
a
|
|
|
|
|
"b
|
|
|
|
|
c
|
|
|
|
|
'''
|
|
|
|
|
ok a is "a\n \"b\nc"
|
|
|
|
|
|
|
|
|
|
a = """
|
|
|
|
|
a
|
|
|
|
|
b
|
|
|
|
|
c
|
|
|
|
|
"""
|
|
|
|
|
ok a is "a\n b\n c"
|
|
|
|
|
|
|
|
|
|
a = '''one-liner'''
|
|
|
|
|
ok a is 'one-liner'
|
|
|
|
|
|
|
|
|
|
a = """
|
|
|
|
|
out
|
|
|
|
|
here
|
|
|
|
|
"""
|
|
|
|
|
ok a is "out\nhere"
|
|
|
|
|
|
|
|
|
|
a = '''
|
|
|
|
|
a
|
|
|
|
|
b
|
|
|
|
|
c
|
|
|
|
|
'''
|
|
|
|
|
ok a is " a\n b\nc"
|
|
|
|
|
|
|
|
|
|
a = '''
|
|
|
|
|
a
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b c
|
|
|
|
|
'''
|
|
|
|
|
ok a is "a\n\n\nb c"
|
|
|
|
|
|
|
|
|
|
a = '''more"than"one"quote'''
|
|
|
|
|
ok a is 'more"than"one"quote'
|
|
|
|
|
|
|
|
|
|
a = '''here's an apostrophe'''
|
|
|
|
|
ok a is "here's an apostrophe"
|
|
|
|
|
|
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-03 17:40:43 -05:00
|
|
|
|
a = """""surrounded by two quotes"\""""
|
|
|
|
|
ok a is '""surrounded by two quotes""'
|
|
|
|
|
|
|
|
|
|
a = '''''surrounded by two apostrophes'\''''
|
|
|
|
|
ok a is "''surrounded by two apostrophes''"
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
# The indentation detector ignores blank lines without trailing whitespace
|
|
|
|
|
a = """
|
|
|
|
|
one
|
|
|
|
|
two
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
ok a is "one\ntwo\n"
|
|
|
|
|
|
|
|
|
|
eq ''' line 0
|
|
|
|
|
should not be relevant
|
|
|
|
|
to the indent level
|
2013-11-18 11:26:48 -05:00
|
|
|
|
''', ' line 0\nshould not be relevant\n to the indent level'
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
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-03 17:40:43 -05:00
|
|
|
|
eq """
|
|
|
|
|
interpolation #{
|
|
|
|
|
"contents"
|
|
|
|
|
}
|
|
|
|
|
should not be relevant
|
|
|
|
|
to the indent level
|
|
|
|
|
""", 'interpolation contents\nshould not be relevant\n to the indent level'
|
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
|
eq ''' '\\\' ''', " '\\' "
|
|
|
|
|
eq """ "\\\" """, ' "\\" '
|
|
|
|
|
|
|
|
|
|
eq ''' <- keep these spaces -> ''', ' <- keep these spaces -> '
|
2011-01-15 10:57:50 -05:00
|
|
|
|
|
2015-02-18 11:40:40 -05:00
|
|
|
|
eq '''undefined''', 'undefined'
|
|
|
|
|
eq """undefined""", 'undefined'
|
|
|
|
|
|
2011-01-15 10:57:50 -05:00
|
|
|
|
|
|
|
|
|
test "#1046, empty string interpolations", ->
|
|
|
|
|
eq "#{ }", ''
|
2015-01-14 15:27:24 -05:00
|
|
|
|
|
|
|
|
|
test "strings are not callable", ->
|
|
|
|
|
throws -> CoffeeScript.compile '"a"()'
|
|
|
|
|
throws -> CoffeeScript.compile '"a#{b}"()'
|
|
|
|
|
throws -> CoffeeScript.compile '"a" 1'
|
|
|
|
|
throws -> CoffeeScript.compile '"a#{b}" 1'
|
|
|
|
|
throws -> CoffeeScript.compile '''
|
|
|
|
|
"a"
|
|
|
|
|
k: v
|
|
|
|
|
'''
|
|
|
|
|
throws -> CoffeeScript.compile '''
|
|
|
|
|
"a#{b}"
|
|
|
|
|
k: v
|
|
|
|
|
'''
|
2015-02-05 11:23:03 -05:00
|
|
|
|
|
|
|
|
|
test "#3795: Escape otherwise invalid characters", ->
|
|
|
|
|
eq '
', '\u2028'
|
|
|
|
|
eq '
', '\u2029'
|
|
|
|
|
eq '\0\
|
|
|
|
|
1', '\x001'
|
|
|
|
|
eq "
", '\u2028'
|
|
|
|
|
eq "
", '\u2029'
|
|
|
|
|
eq "\0\
|
|
|
|
|
1", '\x001'
|
|
|
|
|
eq '''
''', '\u2028'
|
|
|
|
|
eq '''
''', '\u2029'
|
|
|
|
|
eq '''\0\
|
|
|
|
|
1''', '\x001'
|
|
|
|
|
eq """
""", '\u2028'
|
|
|
|
|
eq """
""", '\u2029'
|
|
|
|
|
eq """\0\
|
|
|
|
|
1""", '\x001'
|
|
|
|
|
|
|
|
|
|
a = 'a'
|
|
|
|
|
eq "#{a}
", 'a\u2028'
|
|
|
|
|
eq "#{a}
", 'a\u2029'
|
|
|
|
|
eq "#{a}\0\
|
|
|
|
|
1", 'a\0' + '1'
|
|
|
|
|
eq """#{a}
""", 'a\u2028'
|
|
|
|
|
eq """#{a}
""", 'a\u2029'
|
|
|
|
|
eq """#{a}\0\
|
|
|
|
|
1""", 'a\0' + '1'
|
2016-09-26 11:10:38 -04:00
|
|
|
|
|
|
|
|
|
test "#4314: Whitespace less than or equal to stripped indentation", ->
|
|
|
|
|
# The odd indentation is intentional here, to test 1-space indentation.
|
|
|
|
|
eq ' ', """
|
|
|
|
|
#{} #{}
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
eq '1 2 3 4 5 end\na 0 b', """
|
|
|
|
|
#{1} #{2} #{3} #{4} #{5} end
|
|
|
|
|
a #{0} b"""
|
2017-04-20 02:03:06 -04:00
|
|
|
|
|
|
|
|
|
test "#4248: Unicode code point escapes", ->
|
|
|
|
|
eq '\u01ab\u00cd', '\u{1ab}\u{cd}'
|
|
|
|
|
eq '\u01ab', '\u{000001ab}'
|
|
|
|
|
eq 'a\u01ab', "#{ 'a' }\u{1ab}"
|
|
|
|
|
eq '\u01abc', '''\u{01ab}c'''
|
|
|
|
|
eq '\u01abc', """\u{1ab}#{ 'c' }"""
|
|
|
|
|
eq '\udab3\uddef', '\u{bcdef}'
|
|
|
|
|
eq '\udab3\uddef', '\u{0000bcdef}'
|
|
|
|
|
eq 'a\udab3\uddef', "#{ 'a' }\u{bcdef}"
|
|
|
|
|
eq '\udab3\uddefc', '''\u{0bcdef}c'''
|
|
|
|
|
eq '\udab3\uddefc', """\u{bcdef}#{ 'c' }"""
|
|
|
|
|
eq '\\u{123456}', "#{'\\'}#{'u{123456}'}"
|
|
|
|
|
|
2017-04-25 13:15:08 -04:00
|
|
|
|
# don't rewrite code point escapes
|
2017-06-07 02:33:46 -04:00
|
|
|
|
eqJS """
|
2017-04-20 02:03:06 -04:00
|
|
|
|
'\\u{bcdef}\\u{abc}'
|
2017-06-07 02:33:46 -04:00
|
|
|
|
""",
|
|
|
|
|
"""
|
2017-04-25 13:15:08 -04:00
|
|
|
|
'\\u{bcdef}\\u{abc}';
|
2017-04-20 02:03:06 -04:00
|
|
|
|
"""
|
|
|
|
|
|
2017-06-07 02:33:46 -04:00
|
|
|
|
eqJS """
|
2017-04-20 02:03:06 -04:00
|
|
|
|
"#{ 'a' }\\u{bcdef}"
|
2017-06-07 02:33:46 -04:00
|
|
|
|
""",
|
|
|
|
|
"""
|
2017-04-25 13:15:08 -04:00
|
|
|
|
"a\\u{bcdef}";
|
2017-04-20 02:03:06 -04:00
|
|
|
|
"""
|