-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathstrings.coffee
436 lines (364 loc) · 8.5 KB
/
strings.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# String Literals
# ---------------
# TODO: refactor string literal tests
# TODO: add indexing and method invocation tests: "string"["toString"] is String::toString, "string".toString() is "string"
# * Strings
# * Heredocs
test "backslash escapes", ->
eq "\\/\\\\", /\/\\/.source
eq '(((dollars)))', '\(\(\(dollars\)\)\)'
eq 'one two three', "one
two
three"
eq "four five", 'four
five'
test "#3229, multiline strings", ->
# Separate lines by default by a single space in literal strings.
eq 'one
two', 'one two'
eq "one
two", 'one two'
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'
eq 'trailing ws
doesn\'t matter', 'trailing ws doesn\'t matter'
# Use backslashes at the end of a line to specify whitespace between lines.
eq 'a \
b\
c \
d', 'a bc d'
eq "a \
b\
c \
d", 'a bc d'
eq 'ignore \
trailing whitespace', 'ignore trailing whitespace'
# Backslash at the beginning of a literal string.
eq '\
ok', 'ok'
eq ' \
ok', ' ok'
# #1273, empty strings.
eq '\
', ''
eq '
', ''
eq '
', ''
eq ' ', ' '
# Same behavior in interpolated strings.
eq "interpolation #{1}
follows #{2} \
too #{3}\
!", 'interpolation 1 follows 2 too 3!'
eq "a #{
'string ' + "inside
interpolation"
}", "a string inside interpolation"
eq "
#{1}
", '1'
# Handle escaped backslashes correctly.
eq '\\', `'\\'`
eq 'escaped backslash at EOL\\
next line', 'escaped backslash at EOL\\ next line'
eq '\\
next line', '\\ next line'
eq '\\
', '\\'
eq '\\\\\\
', '\\\\\\'
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'
eq 'several escaped backslashes\\\\\\
ok', 'several escaped backslashes\\\\\\ ok'
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'
# Backslashes at beginning of lines.
eq 'first line
\ backslash at BOL', 'first line \ backslash at BOL'
eq 'first line\
\ backslash at BOL', 'first line\ backslash at BOL'
# 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'
# Edge case.
eq 'lone
\
backslash', 'lone backslash'
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'
# Changed from #647, trailing backslash.
eq '''
Hello, World\
''', 'Hello, World'
eq '''
\\
''', '\\'
# 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!'
eq """
#{1} #{2}
""", '\n1 2\n'
# Handle escaped backslashes correctly.
eq '''
escaped backslash at EOL\\
next line
''', 'escaped backslash at EOL\\\n next line'
eq '''\\
''', '\\\n'
# 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'
# 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'
# Edge cases.
eq '''lone
\
backslash''', 'lone\n\n backslash'
eq '''\
''', ''
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 '
#647
eq "''Hello, World\\''", '''
'\'Hello, World\\\''
'''
eq '""Hello, World\\""', """
"\"Hello, World\\\""
"""
test "#1273, escaping quotes at the end of heredocs.", ->
# """\""" no longer compiles
eq """\\""", '\\'
eq """\\\"""", '\\\"'
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"
a = """""surrounded by two quotes"\""""
ok a is '""surrounded by two quotes""'
a = '''''surrounded by two apostrophes'\''''
ok a is "''surrounded by two apostrophes''"
# 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
''', ' line 0\nshould not be relevant\n to the indent level'
eq """
interpolation #{
"contents"
}
should not be relevant
to the indent level
""", 'interpolation contents\nshould not be relevant\n to the indent level'
eq ''' '\\\' ''', " '\\' "
eq """ "\\\" """, ' "\\" '
eq ''' <- keep these spaces -> ''', ' <- keep these spaces -> '
eq '''undefined''', 'undefined'
eq """undefined""", 'undefined'
test "#1046, empty string interpolations", ->
eq "#{ }", ''
test "strings are not callable", ->
throwsCompileError '"a"()'
throwsCompileError '"a#{b}"()'
throwsCompileError '"a" 1'
throwsCompileError '"a#{b}" 1'
throwsCompileError '''
"a"
k: v
'''
throwsCompileError '''
"a#{b}"
k: v
'''
test "#3795: Escape otherwise invalid characters", ->
eq '
', '\u2028'
eq '
', '\u2029'
eq '\0\
1', '\x001'
eq "
", '\u2028'
eq "
", '\u2029'
eq "\0\
1", '\x001'
eq "\0\
9", '\x009'
eq "\0#{}0", '\x000'
eq '''
''', '\u2028'
eq '''
''', '\u2029'
eq '''\0\
1''', '\x001'
eq '''\0\
9''', '\x009'
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'
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"""
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}'}"
# don't rewrite code point escapes
eqJS """
'\\u{bcdef}\\u{abc}'
""",
"""
'\\u{bcdef}\\u{abc}';
"""
eqJS """
"#{ 'a' }\\u{bcdef}"
""",
"""
"a\\u{bcdef}";
"""