5f41fc38297423a00b80023cec1254f1e887aad2.svn-base
2.3 KB
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
'use strict'
var test = require('tape')
var redeyed = require('..')
test('given i skip 2 more tokens after each semicolon', function(t) {
var calls = 0
var opts = {
Punctuator: {
';': function identity(s, info) {
// tell it to skip past second to last token that is 2 ahead of the current one
calls++
var skipToken = info.tokens[info.tokenIndex + 2]
return skipToken ? { replacement: s, skipPastToken: skipToken } : s
}
}
}
;[ { code: ';;;', expectedCalls: 1 }
, { code: ';;;;', expectedCalls: 2 }
, { code: '; ; ; ;', expectedCalls: 2 }
, { code: ';;; ;;; ;;; ;', expectedCalls: 4 }
, { code: ';;; ;;; ;;; ;;; ;', expectedCalls: 5 }
, { code: ';;; ;;; ;;; ;;; ;;;', expectedCalls: 5 }
, { code: ';;; ;;; ;;; ;;; ;;; ;', expectedCalls: 6 }
].forEach(function(x) {
calls = 0
redeyed(x.code, opts)
t.equals(calls, x.expectedCalls, 'calls ' + x.expectedCalls + ' times for ' + x.code)
})
t.end()
})
test('replace log', function(t) {
var opts = {
Identifier: {
console: function replaceLog(s, info) {
var code = info.code
var idx = info.tokenIndex
var tokens = info.tokens
var kind = tokens[idx + 2].value
var firstArgTkn = tokens[idx + 4]
var argIdx = idx + 3
var open
var tkn
open = 1
while (open) {
tkn = tokens[++argIdx]
if (tkn.value === '(') open++
if (tkn.value === ')') open--
}
var argsIncludingClosingParen = code.slice(firstArgTkn.range[0], tkn.range[1])
var result = 'log.' + kind + '("main-logger", ' + argsIncludingClosingParen
return { replacement: result, skipPastToken: tkn }
}
}
}
var origCode = [
'console.info("info ", 1);'
, 'console.warn("warn ", 3);'
, 'console.error("error ", new Error("oh my!"));'
].join('\n')
var expectedCode = [
'log.info("main-logger", "info ", 1));'
, 'log.warn("main-logger", "warn ", 3));'
, 'log.error("main-logger", "error ", new Error("oh my!")));'
].join('\n')
var code = redeyed(origCode, opts).code
t.equals(code, expectedCode, 'transforms all log statements')
t.end()
})