-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
source.js
172 lines (153 loc) · 5.56 KB
/
source.js
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
/* global describe, it */
const CovSource = require('../lib/source')
const { TraceMap } = require('@jridgewell/trace-mapping')
require('tap').mochaGlobals()
require('should')
describe('Source', () => {
describe('relativeToOffset', () => {
it('returns an offset in the middle of a file', () => {
const sourceRaw = `const a = 99
const b = 33
for (var i = 0; i < 99; i++) {
console.info('hello world')
}
return a`
const source = new CovSource(sourceRaw, 0)
source.relativeToOffset(3, 8).should.equal(42)
})
it('returns EOF if line is greater than lines in file', () => {
const sourceRaw = `
const a = 99`
const source = new CovSource(sourceRaw, 0)
source.relativeToOffset(5, 8).should.equal(21)
})
it('returns end of line, if column offset greater than line length', () => {
const sourceRaw = `a
const a = 99`
const source = new CovSource(sourceRaw, 0)
source.relativeToOffset(2, 50).should.equal(22)
source.relativeToOffset(1, Infinity).should.equal(1)
})
it('returns empty object for out of range params', () => {
const sourceRaw = ''
const source = new CovSource(sourceRaw, 0)
source.offsetToOriginalRelative(undefined, Infinity, Infinity).should.deepEqual({})
})
it('accepts null', () => {
const sourceRaw = null
new CovSource(sourceRaw, 0).should.ok()
})
it('range crossing two sourcemaps', () => {
const sourceRaw = `\
(() => {
// hello.ts
function hello() {
console.log("hello world");
}
// greet.ts
hello();
})();
//# sourceMappingURL=greet.js.map\
`
const source = new CovSource(sourceRaw, 0)
const sourceMap = new TraceMap({
version: 3,
sources: ['../hello.ts', '../greet.ts'],
sourcesContent: ['export function hello() {\r\n console.log("hello world")\r\n}', 'import {hello} from "./hello"\r\n\r\nhello()\r\n'],
mappings: ';;AAAO,mBAAiB;AACtB,YAAQ,IAAI;AAAA;;;ACCd;',
names: []
})
source.offsetToOriginalRelative(sourceMap, 25, 97).should.deepEqual({})
})
})
for (const prefix of ['c8', 'v8']) {
describe(`ignore hint ${prefix}`, () => {
it(`ignores the next line if /* ${prefix} ignore next */ is on its own line`, () => {
const sourceRaw = `
const a = 33
/* ${prefix} ignore next */
const a = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(false)
source.lines[2].ignore.should.equal(true)
source.lines[3].ignore.should.equal(true)
})
it(`ignores the next N lines if /* ${prefix} ignore next N */ is used`, () => {
const sourceRaw = `
/* ${prefix} ignore next 2 */
const a = 33
const a = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(true)
source.lines[2].ignore.should.equal(true)
source.lines[3].ignore.should.equal(true)
})
it(`ignores a line that contains /* ${prefix} ignore next */`, () => {
const sourceRaw = `
const a = foo ? true /* ${prefix} ignore next */ : false
const b = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(true)
source.lines[2].ignore.should.equal(false)
})
it('ignores lines between start and stop', () => {
const sourceRaw = `
/* ${prefix} ignore start */
function ignoreMe() {
// ...
}
/* ${prefix} ignore stop */
function doNotIgnoreMe() {
// ...
}
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(true)
source.lines[2].ignore.should.equal(true)
source.lines[3].ignore.should.equal(true)
source.lines[4].ignore.should.equal(true)
source.lines[5].ignore.should.equal(true)
source.lines[6].ignore.should.equal(false)
source.lines[7].ignore.should.equal(false)
source.lines[8].ignore.should.equal(false)
source.lines[9].ignore.should.equal(false)
})
it('ignore hint accepts other text content', () => {
const sourceRaw = `
const a = 33
/* ${prefix} ignore next -- reasoning why this is ignored */
const b = 99
/* ${prefix} ignore start: reasoning here */
function ignoreMe() {
// ...
}
/* ${prefix} ignore stop -- @preserve */
const c = a ? true /* ${prefix} ignore next reasoning here */ : false
/* ${prefix} ignore next 2 -- ignores next two lines */
const a = 33
const a = 99
`
const source = new CovSource(sourceRaw, 0)
source.lines[1].ignore.should.equal(false)
source.lines[2].ignore.should.equal(false)
source.lines[3].ignore.should.equal(true)
source.lines[4].ignore.should.equal(true)
source.lines[5].ignore.should.equal(false)
source.lines[6].ignore.should.equal(true)
source.lines[7].ignore.should.equal(true)
source.lines[8].ignore.should.equal(true)
source.lines[9].ignore.should.equal(true)
source.lines[10].ignore.should.equal(true)
source.lines[11].ignore.should.equal(false)
source.lines[12].ignore.should.equal(true)
source.lines[13].ignore.should.equal(false)
source.lines[14].ignore.should.equal(true)
source.lines[15].ignore.should.equal(true)
source.lines[16].ignore.should.equal(true)
})
})
}
})