2010-12-29 00:48:54 -05:00
|
|
|
# Interpolation
|
|
|
|
# -------------
|
|
|
|
|
2010-12-29 14:06:57 -05:00
|
|
|
# * String Interpolation
|
|
|
|
# * Regular Expression Interpolation
|
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# String Interpolation
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
# TODO: refactor string interpolation tests
|
|
|
|
|
|
|
|
eq 'multiline nested "interpolations" work', """multiline #{
|
|
|
|
"nested #{
|
|
|
|
ok true
|
|
|
|
"\"interpolations\""
|
|
|
|
}"
|
|
|
|
} work"""
|
|
|
|
|
|
|
|
# Issue #923: Tricky interpolation.
|
|
|
|
eq "#{ "{" }", "{"
|
|
|
|
eq "#{ '#{}}' } }", '#{}} }'
|
|
|
|
eq "#{"'#{ ({a: "b#{1}"}['a']) }'"}", "'b1'"
|
|
|
|
|
2011-06-19 13:01:01 -04:00
|
|
|
# Issue #1150: String interpolation regression
|
|
|
|
eq "#{'"/'}", '"/'
|
|
|
|
eq "#{"/'"}", "/'"
|
|
|
|
eq "#{/'"/}", '/\'"/'
|
|
|
|
eq "#{"'/" + '/"' + /"'/}", '\'//"/"\'/'
|
|
|
|
eq "#{"'/"}#{'/"'}#{/"'/}", '\'//"/"\'/'
|
|
|
|
eq "#{6 / 2}", '3'
|
|
|
|
eq "#{6 / 2}#{6 / 2}", '33' # parsed as division
|
|
|
|
eq "#{6 + /2}#{6/ + 2}", '6/2}#{6/2' # parsed as a regex
|
|
|
|
eq "#{6/2}
|
2013-11-17 23:32:15 -05:00
|
|
|
#{6/2}", '3 3' # newline cannot be part of a regex, so it's division
|
2011-06-19 13:01:01 -04:00
|
|
|
eq "#{/// "'/'"/" ///}", '/"\'\\/\'"\\/"/' # heregex, stuffed with spicy characters
|
2011-06-22 12:37:56 -04:00
|
|
|
eq "#{/\\'/}", "/\\\\'/"
|
2011-06-19 13:01:01 -04: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
|
|
|
# Issue #2321: Regex/division conflict in interpolation
|
|
|
|
eq "#{4/2}/", '2/'
|
|
|
|
curWidth = 4
|
|
|
|
eq "<i style='left:#{ curWidth/2 }%;'></i>", "<i style='left:2%;'></i>"
|
|
|
|
throws -> CoffeeScript.compile '''
|
|
|
|
"<i style='left:#{ curWidth /2 }%;'></i>"'''
|
|
|
|
# valid regex--^^^^^^^^^^^ ^--unclosed string
|
|
|
|
eq "<i style='left:#{ curWidth/2 }%;'></i>", "<i style='left:2%;'></i>"
|
|
|
|
eq "<i style='left:#{ curWidth/ 2 }%;'></i>", "<i style='left:2%;'></i>"
|
|
|
|
eq "<i style='left:#{ curWidth / 2 }%;'></i>", "<i style='left:2%;'></i>"
|
|
|
|
|
2010-12-29 00:48:54 -05:00
|
|
|
hello = 'Hello'
|
|
|
|
world = 'World'
|
|
|
|
ok '#{hello} #{world}!' is '#{hello} #{world}!'
|
|
|
|
ok "#{hello} #{world}!" is 'Hello World!'
|
|
|
|
ok "[#{hello}#{world}]" is '[HelloWorld]'
|
|
|
|
ok "#{hello}##{world}" is 'Hello#World'
|
|
|
|
ok "Hello #{ 1 + 2 } World" is 'Hello 3 World'
|
|
|
|
ok "#{hello} #{ 1 + 2 } #{world}" is "Hello 3 World"
|
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
|
|
|
ok 1 + "#{2}px" is '12px'
|
|
|
|
ok isNaN "a#{2}" * 2
|
|
|
|
ok "#{2}" is '2'
|
|
|
|
ok "#{2}#{2}" is '22'
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
[s, t, r, i, n, g] = ['s', 't', 'r', 'i', 'n', 'g']
|
|
|
|
ok "#{s}#{t}#{r}#{i}#{n}#{g}" is 'string'
|
|
|
|
ok "\#{s}\#{t}\#{r}\#{i}\#{n}\#{g}" is '#{s}#{t}#{r}#{i}#{n}#{g}'
|
|
|
|
ok "\#{string}" is '#{string}'
|
|
|
|
|
|
|
|
ok "\#{Escaping} first" is '#{Escaping} first'
|
|
|
|
ok "Escaping \#{in} middle" is 'Escaping #{in} middle'
|
|
|
|
ok "Escaping \#{last}" is 'Escaping #{last}'
|
|
|
|
|
|
|
|
ok "##" is '##'
|
|
|
|
ok "#{}" is ''
|
|
|
|
ok "#{}A#{} #{} #{}B#{}" is 'A B'
|
|
|
|
ok "\\\#{}" is '\\#{}'
|
|
|
|
|
|
|
|
ok "I won ##{20} last night." is 'I won #20 last night.'
|
|
|
|
ok "I won ##{'#20'} last night." is 'I won ##20 last night.'
|
|
|
|
|
|
|
|
ok "#{hello + world}" is 'HelloWorld'
|
|
|
|
ok "#{hello + ' ' + world + '!'}" is 'Hello World!'
|
|
|
|
|
|
|
|
list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
|
|
ok "values: #{list.join(', ')}, length: #{list.length}." is 'values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, length: 10.'
|
|
|
|
ok "values: #{list.join ' '}" is 'values: 0 1 2 3 4 5 6 7 8 9'
|
|
|
|
|
|
|
|
obj = {
|
|
|
|
name: 'Joe'
|
|
|
|
hi: -> "Hello #{@name}."
|
|
|
|
cya: -> "Hello #{@name}.".replace('Hello','Goodbye')
|
|
|
|
}
|
|
|
|
ok obj.hi() is "Hello Joe."
|
|
|
|
ok obj.cya() is "Goodbye Joe."
|
|
|
|
|
|
|
|
ok "With #{"quotes"}" is 'With quotes'
|
|
|
|
ok 'With #{"quotes"}' is 'With #{"quotes"}'
|
|
|
|
|
|
|
|
ok "Where is #{obj["name"] + '?'}" is 'Where is Joe?'
|
|
|
|
|
|
|
|
ok "Where is #{"the nested #{obj["name"]}"}?" is 'Where is the nested Joe?'
|
|
|
|
ok "Hello #{world ? "#{hello}"}" is 'Hello World'
|
|
|
|
|
|
|
|
ok "Hello #{"#{"#{obj["name"]}" + '!'}"}" is 'Hello Joe!'
|
|
|
|
|
|
|
|
a = """
|
|
|
|
Hello #{ "Joe" }
|
|
|
|
"""
|
|
|
|
ok a is "Hello Joe"
|
|
|
|
|
|
|
|
a = 1
|
|
|
|
b = 2
|
|
|
|
c = 3
|
|
|
|
ok "#{a}#{b}#{c}" is '123'
|
|
|
|
|
|
|
|
result = null
|
|
|
|
stash = (str) -> result = str
|
|
|
|
stash "a #{ ('aa').replace /a/g, 'b' } c"
|
|
|
|
ok result is 'a bb c'
|
|
|
|
|
|
|
|
foo = "hello"
|
|
|
|
ok "#{foo.replace("\"", "")}" is 'hello'
|
|
|
|
|
|
|
|
val = 10
|
|
|
|
a = """
|
|
|
|
basic heredoc #{val}
|
|
|
|
on two lines
|
|
|
|
"""
|
|
|
|
b = '''
|
|
|
|
basic heredoc #{val}
|
|
|
|
on two lines
|
|
|
|
'''
|
|
|
|
ok a is "basic heredoc 10\non two lines"
|
|
|
|
ok b is "basic heredoc \#{val}\non two lines"
|
|
|
|
|
|
|
|
eq 'multiline nested "interpolations" work', """multiline #{
|
|
|
|
"nested #{(->
|
|
|
|
ok yes
|
|
|
|
"\"interpolations\""
|
|
|
|
)()}"
|
|
|
|
} work"""
|
|
|
|
|
2015-01-12 14:10:54 -05:00
|
|
|
eq 'function(){}', "#{->}".replace /\s/g, ''
|
|
|
|
ok /^a[\s\S]+b$/.test "a#{=>}b"
|
2018-03-30 03:47:40 -04:00
|
|
|
ok /^a[\s\S]+b$/.test "a#{ (x) -> x %% 2 }b"
|
2010-12-29 00:48:54 -05:00
|
|
|
|
2011-03-11 21:41:12 -05:00
|
|
|
# Regular Expression Interpolation
|
2010-12-29 00:48:54 -05:00
|
|
|
|
|
|
|
# TODO: improve heregex interpolation tests
|
|
|
|
|
|
|
|
test "heregex interpolation", ->
|
2015-02-05 11:23:03 -05:00
|
|
|
eq /\\#{}\\"/ + '', ///
|
2010-12-29 00:48:54 -05:00
|
|
|
#{
|
|
|
|
"#{ '\\' }" # normal comment
|
|
|
|
}
|
|
|
|
# regex comment
|
|
|
|
\#{}
|
2015-02-05 11:23:03 -05:00
|
|
|
\\ "
|
2010-12-29 00:48:54 -05:00
|
|
|
/// + ''
|