PageRenderTime 33ms CodeModel.GetById 20ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/test/compilation.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 175 lines | 143 code | 25 blank | 7 comment | 8 complexity | 567989a1b10bc43f6438d5c61c5f8877 MD5 | raw file
  1# Compilation
  2# -----------
  3
  4# Helper to pipe the CoffeeScript compiler’s output through a transpiler.
  5transpile = (method, code, options = {}) ->
  6  # `method` should be 'compile' or 'eval' or 'run'
  7  options.bare = yes
  8  options.transpile =
  9    # Target Internet Explorer 6, which supports no ES2015+ features.
 10    presets: [['@babel/env', {targets: browsers: ['ie 6']}]]
 11  CoffeeScript[method] code, options
 12
 13
 14test "ensure that carriage returns don't break compilation on Windows", ->
 15  doesNotThrowCompileError 'one\r\ntwo', bare: on
 16
 17test "#3089 - don't mutate passed in options to compile", ->
 18  opts = {}
 19  CoffeeScript.compile '1 + 1', opts
 20  ok !opts.scope
 21
 22test "--bare", ->
 23  eq -1, CoffeeScript.compile('x = y', bare: on).indexOf 'function'
 24  ok 'passed' is CoffeeScript.eval '"passed"', bare: on, filename: 'test'
 25
 26test "header (#1778)", ->
 27  header = "// Generated by CoffeeScript #{CoffeeScript.VERSION}\n"
 28  eq 0, CoffeeScript.compile('x = y', header: on).indexOf header
 29
 30test "header is disabled by default", ->
 31  header = "// Generated by CoffeeScript #{CoffeeScript.VERSION}\n"
 32  eq -1, CoffeeScript.compile('x = y').indexOf header
 33
 34test "multiple generated references", ->
 35  a = {b: []}
 36  a.b[true] = -> this == a.b
 37  c = 0
 38  d = []
 39  ok a.b[0<++c<2] d...
 40
 41test "splat on a line by itself is invalid", ->
 42  throwsCompileError "x 'a'\n...\n"
 43
 44test "Issue 750", ->
 45
 46  throwsCompileError 'f(->'
 47
 48  throwsCompileError 'a = (break)'
 49
 50  throwsCompileError 'a = (return 5 for item in list)'
 51
 52  throwsCompileError 'a = (return 5 while condition)'
 53
 54  throwsCompileError 'a = for x in y\n  return 5'
 55
 56test "Issue #986: Unicode identifiers", ->
 57  λ = 5
 58  eq λ, 5
 59
 60test "#2516: Unicode spaces should not be part of identifiers", ->
 61  a = (x) -> x * 2
 62  b = 3
 63  eq 6, a b # U+00A0 NO-BREAK SPACE
 64  eq 6, ab # U+1680 OGHAM SPACE MARK
 65  eq 6, a b # U+2000 EN QUAD
 66  eq 6, ab # U+2001 EM QUAD
 67  eq 6, ab # U+2002 EN SPACE
 68  eq 6, ab # U+2003 EM SPACE
 69  eq 6, ab # U+2004 THREE-PER-EM SPACE
 70  eq 6, ab # U+2005 FOUR-PER-EM SPACE
 71  eq 6, ab # U+2006 SIX-PER-EM SPACE
 72  eq 6, ab # U+2007 FIGURE SPACE
 73  eq 6, ab # U+2008 PUNCTUATION SPACE
 74  eq 6, ab # U+2009 THIN SPACE
 75  eq 6, ab # U+200A HAIR SPACE
 76  eq 6, ab # U+202F NARROW NO-BREAK SPACE
 77  eq 6, ab # U+205F MEDIUM MATHEMATICAL SPACE
 78  eq 6, a b # U+3000 IDEOGRAPHIC SPACE
 79
 80  # #3560: Non-breaking space (U+00A0) (before `'c'`)
 81  eq 5, {c: 5}[ 'c' ]
 82
 83  # A line where every space in non-breaking
 84  eq 1 + 1, 2  
 85
 86test "don't accidentally stringify keywords", ->
 87  ok (-> this == 'this')() is false
 88
 89test "#1026: no if/else/else allowed", ->
 90  throwsCompileError '''
 91    if a
 92      b
 93    else
 94      c
 95    else
 96      d
 97  '''
 98
 99test "#1050: no closing asterisk comments from within block comments", ->
100  throwsCompileError "### */ ###"
101
102test "#1273: escaping quotes at the end of heredocs", ->
103  throwsCompileError '"""\\"""' # """\"""
104  throwsCompileError '"""\\\\\\"""' # """\\\"""
105
106test "#1106: __proto__ compilation", ->
107  object = eq
108  @["__proto__"] = true
109  ok __proto__
110
111test "reference named hasOwnProperty", ->
112  CoffeeScript.compile 'hasOwnProperty = 0; a = 1'
113
114test "#1055: invalid keys in real (but not work-product) objects", ->
115  throwsCompileError "@key: value"
116
117test "#1066: interpolated strings are not implicit functions", ->
118  throwsCompileError '"int#{er}polated" arg'
119
120test "#2846: while with empty body", ->
121  CoffeeScript.compile 'while 1 then', {sourceMap: true}
122
123test "#2944: implicit call with a regex argument", ->
124  CoffeeScript.compile 'o[key] /regex/'
125
126test "#3001: `own` shouldn't be allowed in a `for`-`in` loop", ->
127  throwsCompileError "a for own b in c"
128
129test "#2994: single-line `if` requires `then`", ->
130  throwsCompileError "if b else x"
131
132test "transpile option, for Node API CoffeeScript.compile", ->
133  return if global.testingBrowser
134  ok transpile('compile', "import fs from 'fs'").includes 'require'
135
136test "transpile option, for Node API CoffeeScript.eval", ->
137  return if global.testingBrowser
138  ok transpile 'eval', "import path from 'path'; path.sep in ['/', '\\\\']"
139
140test "transpile option, for Node API CoffeeScript.run", ->
141  return if global.testingBrowser
142  doesNotThrow -> transpile 'run', "import fs from 'fs'"
143
144test "transpile option has merged source maps", ->
145  return if global.testingBrowser
146  untranspiledOutput = CoffeeScript.compile "import path from 'path'\nconsole.log path.sep", sourceMap: yes
147  transpiledOutput   = transpile 'compile', "import path from 'path'\nconsole.log path.sep", sourceMap: yes
148  untranspiledOutput.v3SourceMap = JSON.parse untranspiledOutput.v3SourceMap
149  transpiledOutput.v3SourceMap   = JSON.parse transpiledOutput.v3SourceMap
150  ok untranspiledOutput.v3SourceMap.mappings isnt transpiledOutput.v3SourceMap.mappings
151  # Babel adds `'use strict';` to the top of files with the modules transform.
152  eq transpiledOutput.js.indexOf('use strict'), 1
153  # The `'use strict';` followed by two newlines results in the first two lines
154  # of the source map mappings being two blank/skipped lines.
155  eq transpiledOutput.v3SourceMap.mappings.indexOf(';;'), 0
156  # The number of lines in the transpiled code should match the number of lines
157  # in the source map.
158  eq transpiledOutput.js.split('\n').length, transpiledOutput.v3SourceMap.mappings.split(';').length
159
160test "using transpile from the Node API requires an object", ->
161  try
162    CoffeeScript.compile '', transpile: yes
163  catch exception
164    eq exception.message, 'The transpile option must be given an object with options to pass to Babel'
165
166test "transpile option applies to imported .coffee files", ->
167  return if global.testingBrowser
168  doesNotThrow -> transpile 'run', "import { getSep } from './test/importing/transpile_import'\ngetSep()"
169
170test "#3306: trailing comma in a function call in the last line", ->
171  eqJS '''
172  foo bar,
173  ''', '''
174  foo(bar);
175  '''