-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmangle.coffee
202 lines (162 loc) · 9.02 KB
/
mangle.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
# Copyright (C) 2014 Yusuke Suzuki <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'use strict'
esshorten = require '../'
expect = require('chai').expect
esprima = require('esprima')
describe 'mangle:', ->
describe 'basic functionality:', ->
it 'does not touch top-level variable declarations', ->
program = esprima.parse 'var foo, bar, baz;'
result = esshorten.mangle program
expect(result.body[0].declarations[0].id.name).to.equal 'foo'
expect(result.body[0].declarations[1].id.name).to.equal 'bar'
expect(result.body[0].declarations[2].id.name).to.equal 'baz'
it 'shortens local variable declarations', ->
program = esprima.parse 'function f() { var foo, bar, baz; }'
result = esshorten.mangle program
statements = result.body[0].body.body
expect(statements[0].declarations[0].id.name).to.equal 'a'
expect(statements[0].declarations[1].id.name).to.equal 'b'
expect(statements[0].declarations[2].id.name).to.equal 'c'
it 'shortens parameter names', ->
program = esprima.parse 'function f(foo, bar, baz) { foo = 1; bar = 2; baz = 3; }'
result = esshorten.mangle program
params = result.body[0].params
expect(params[0].name).to.equal 'a'
expect(params[1].name).to.equal 'b'
expect(params[2].name).to.equal 'c'
statements = result.body[0].body.body
expect(statements[0].expression.left.name).to.equal 'a'
expect(statements[1].expression.left.name).to.equal 'b'
expect(statements[2].expression.left.name).to.equal 'c'
it 'does not mangle implicit globals', ->
program = esprima.parse 'function f(bar) { foo = 1; bar = 2; baz = 3; }'
result = esshorten.mangle program
expect(result.body[0].params[0].name).to.equal 'a'
statements = result.body[0].body.body
expect(statements[0].expression.left.name).to.equal 'foo'
expect(statements[1].expression.left.name).to.equal 'a'
expect(statements[2].expression.left.name).to.equal 'baz'
it 'does not overwrite existing identifiers', ->
program = esprima.parse 'function f(foo) { function a(b) { c; } }'
result = esshorten.mangle program
f = result.body[0]
a = result.body[0].body.body[0]
expect(f.params[0].name).not.to.equal a.id.name
expect(f.params[0].name).not.to.equal a.body.body[0].expression.name
expect(a.id.name).not.to.equal a.body.body[0].expression.name
describe 'nested scope handling:', ->
it 'shortens nested function names', ->
program = esprima.parse 'function f() { function g() {} }'
result = esshorten.mangle program
expect(result.body[0].id.name).to.equal 'f'
expect(result.body[0].body.body[0].id.name).to.equal 'a'
it 'shortens parameters in nested functions', ->
program = esprima.parse 'function f(foo) { function g(foo) { foo; } }'
result = esshorten.mangle program
expect(result.body[0].params[0].name).to.equal 'a'
g = result.body[0].body.body[0]
expect(g.id.name).to.equal 'b'
expect(g.params[0].name).to.equal 'a'
expect(g.body.body[0].expression.name).to.equal 'a'
it 'shortens variable names in nested functions', ->
program = esprima.parse 'function f(foo) { var bar = 1; var baz = function inner(foo) { foo; var qux; } }'
result = esshorten.mangle program
f = result.body[0]
expect(f.id.name).to.equal 'f'
expect(f.params[0].name.length).to.equal 1
statements = f.body.body
expect(statements[0].declarations[0].id.name.length).to.equal 1
expect(statements[1].declarations[0].id.name.length).to.equal 1
inner = statements[1].declarations[0].init;
expect(inner.id.name.length).to.equal 1
expect(inner.params[0].name.length).to.equal 1
innerStatements = inner.body.body
expect(innerStatements[0].expression.name.length).to.equal 1
expect(innerStatements[1].declarations[0].id.name.length).to.equal 1
describe '`destructive` option:', ->
fixture = 'function f() { var foo, bar, baz; }'
it 'defaults to `true`', ->
program = esprima.parse fixture
result = esshorten.mangle program
expect(result).to.equal program
it 'accepts `true`', ->
program = esprima.parse fixture
result = esshorten.mangle program,
destructive: yes
expect(result).to.equal program
it 'accepts `false`', ->
program = esprima.parse fixture
json = JSON.stringify program
result = esshorten.mangle program,
destructive: no
expect(result).not.to.equal program
expect(JSON.stringify program).to.equal json
describe '`distinguishFunctionExpressionScope` option:', ->
program = esprima.parse '(function name() { var i = 42; });'
it 'defaults to `false`', ->
result = esshorten.mangle program,
destructive: no
expect(result.body[0].expression.id.name).to.equal 'a'
expect(result.body[0].expression.body.body[0].declarations[0].id.name).to.equal 'b'
it 'accepts `false`', ->
result = esshorten.mangle program,
destructive: no
distinguishFunctionExpressionScope: no
expect(result.body[0].expression.id.name).to.equal 'a'
expect(result.body[0].expression.body.body[0].declarations[0].id.name).to.equal 'b'
it 'accepts `true`', ->
result = esshorten.mangle program,
destructive: no
distinguishFunctionExpressionScope: yes
expect(result.body[0].expression.id.name).to.equal 'a'
expect(result.body[0].expression.body.body[0].declarations[0].id.name).to.equal 'a'
describe '`shouldRename` option:', ->
it 'renames by default', ->
program = esprima.parse '(function name() { var foo, bar, baz; });'
result = esshorten.mangle program
expect(result.body[0].expression.id.name).to.equal 'a'
it 'renames if it returns `true`', ->
program = esprima.parse '(function name() { var foo, bar, baz; });'
result = esshorten.mangle program,
shouldRename: (id) ->
return id == 'name'
expect(result.body[0].expression.id.name).to.equal 'a'
it 'does not rename if it returns `false`', ->
program = esprima.parse '(function name() { var foo, bar, baz; });'
result = esshorten.mangle program,
shouldRename: (id) ->
return id != 'name'
expect(result.body[0].expression.id.name).to.equal 'name'
describe '`renamePrefix` option:', ->
it 'prefixes identifier with the given value', ->
program = esprima.parse '(function name() { var i = 42; });'
result = esshorten.mangle program,
renamePrefix: 'foo_'
expect(result.body[0].expression.id.name.indexOf('foo_')).to.equal 0
it 'prefixes identifier inside IIFE without multiplying prefixes', ->
program = esprima.parse '(function name() { var i = 42; var a = 5; })();'
result = esshorten.mangle program,
renamePrefix: 'foo_'
expect(result.body[0].expression.callee.body.body[1].declarations[0].id.name.indexOf('foo_')).to.equal 0
expect(result.body[0].expression.callee.body.body[1].declarations[0].id.name.indexOf('foo_foo_')).to.equal -1