File: sourcemap.coffee

package info (click to toggle)
coffeescript 2.7.0%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,368 kB
  • sloc: makefile: 20; xml: 9; javascript: 5; sh: 1
file content (185 lines) | stat: -rw-r--r-- 5,390 bytes parent folder | download | duplicates (2)
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
return if global.testingBrowser

{spawn, fork} = require('child_process')
SourceMap = require '../src/sourcemap'

vlqEncodedValues = [
    [1, 'C'],
    [-1, 'D'],
    [2, 'E'],
    [-2, 'F'],
    [0, 'A'],
    [16, 'gB'],
    [948, 'o7B']
]

test "encodeVlq tests", ->
  for pair in vlqEncodedValues
    eq ((new SourceMap).encodeVlq pair[0]), pair[1]

test "SourceMap tests", ->
  map = new SourceMap
  map.add [0, 0], [0, 0]
  map.add [1, 5], [2, 4]
  map.add [1, 6], [2, 7]
  map.add [1, 9], [2, 8]
  map.add [3, 0], [3, 4]

  testWithFilenames = map.generate {
    sourceRoot: ''
    sourceFiles: ['source.coffee']
    generatedFile: 'source.js'
  }

  deepEqual testWithFilenames, {
    version: 3
    file: 'source.js'
    sourceRoot: ''
    sources: ['source.coffee']
    names: []
    mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
  }

  deepEqual map.generate(), {
    version: 3
    file: ''
    sourceRoot: ''
    sources: ['<anonymous>']
    names: []
    mappings: 'AAAA;;IACK,GAAC,CAAG;IAET'
  }

  # Look up a generated column - should get back the original source position.
  arrayEq map.sourceLocation([2,8]), [1,9]

  # Look up a point further along on the same line - should get back the same source position.
  arrayEq map.sourceLocation([2,10]), [1,9]

test "#3075: v3 source map fields", ->
  { js, v3SourceMap, sourceMap } = CoffeeScript.compile 'console.log Date.now()',
    filename: 'tempus_fugit.coffee'
    sourceMap: yes
    sourceRoot: './www_root/coffee/'

  v3SourceMap = JSON.parse v3SourceMap
  arrayEq v3SourceMap.sources, ['tempus_fugit.coffee']
  eq v3SourceMap.sourceRoot, './www_root/coffee/'

test "node --enable-source-map built in stack trace mapping", ->
  new Promise (resolve, reject) ->
    proc = fork './test/importing/error.coffee', [
      '--enable-source-maps'
    ], stdio: 'pipe'

    err = ''
    proc.stderr.setEncoding 'utf8'
    proc.stderr.on 'data', (str) -> err += str
    proc.on 'close', ->
      try
        match = err.match /error\.coffee:(\d+):(\d+)/
        throw new Error err unless match

        [_, line, column] = match
        equal line, 3 # Mapped source line
        equal column, 9 # Mapped source column

        resolve()
      catch exception
        reject exception

if Number(process.versions.node.split('.')[0]) >= 14
  test "NODE_OPTIONS=--enable-source-maps environment variable stack trace mapping", ->
    new Promise (resolve, reject) ->
      proc = fork './test/importing/error.coffee', [],
        env:
          NODE_OPTIONS: '--enable-source-maps'
        stdio: 'pipe'

      err = ''
      proc.stderr.setEncoding 'utf8'
      proc.stderr.on 'data', (str) -> err += str
      proc.on 'close', ->
        try
          match = err.match /error\.coffee:(\d+):(\d+)/
          throw new Error err unless match

          [_, line, column] = match
          equal line, 3 # Mapped source line
          equal column, 9 # Mapped source column

          resolve()
        catch exception
          reject exception

  test "generate correct stack traces with --enable-source-maps from bin/coffee", ->
    new Promise (resolve, reject) ->
      proc = fork 'test/importing/error.coffee',
        ['--enable-source-maps'],
        stdio: 'pipe'

      err = ""
      proc.stderr.setEncoding 'utf8'
      proc.stderr.on 'data', (str) -> err += str
      proc.on 'close', ->
        try
          match = err.match /error\.coffee:(\d+):(\d+)/
          throw new Error err unless match

          [_, line, column] = match
          equal line, 3 # Mapped source line
          equal column, 9 # Mapped source column

          resolve()
        catch exception
          reject exception

test "don't change stack traces if another library has patched `Error.prepareStackTrace`", ->
  new Promise (resolve, reject) ->
    # This uses `spawn` rather than the preferred `fork` because `fork` requires
    # loading code in a separate file. The `--eval` here shows exactly what is
    # executing without indirection.
    proc = spawn 'node', [
      '--eval', """
        const patchedPrepareStackTrace = Error.prepareStackTrace = function() {};
        require('./register.js');
        process.stdout.write(Error.prepareStackTrace === patchedPrepareStackTrace ? 'preserved' : 'overwritten');
      """
    ]

    out = ''
    proc.stdout.setEncoding 'utf8'
    proc.stdout.on 'data', (str) -> out += str

    proc.on 'close', (status) ->
      try
        equal status, 0
        equal out, 'preserved'

        resolve()
      catch exception
        reject exception

test "requiring 'CoffeeScript' doesn't change `Error.prepareStackTrace`", ->
  new Promise (resolve, reject) ->
    # This uses `spawn` rather than the preferred `fork` because `fork` requires
    # loading code in a separate file. The `--eval` here shows exactly what is
    # executing without indirection.
    proc = spawn 'node', [
      '--eval', """
        require('./lib/coffeescript/coffeescript.js');
        process.stdout.write(Error.prepareStackTrace === undefined || Error.prepareStackTrace.name == "ErrorPrepareStackTrace" ? 'unused' : 'defined');
      """
    ]

    out = ''
    proc.stdout.setEncoding 'utf8'
    proc.stdout.on 'data', (str) -> out += str

    proc.on 'close', (status) ->
      try
        equal status, 0
        equal out, 'unused'

        resolve()
      catch exception
        reject exception