Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80551 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*
16
* @emails [email protected]
17
*/
18
19
/*jshint evil:true*/
20
21
require('mock-modules').autoMockOff();
22
23
describe('jstransform-utils', function() {
24
var transform, utils;
25
var Syntax = require('esprima-fb').Syntax;
26
27
beforeEach(function() {
28
require('mock-modules').dumpCache();
29
transform = require('../jstransform').transform;
30
utils = require('../utils');
31
});
32
33
describe('temporary variables', function() {
34
it('should inject temporary variables at the start of functions', function() {
35
function visitFunctionBlock(traverse, node, path, state) {
36
utils.catchup(node.range[0] + 1, state);
37
var x = utils.injectTempVar(state);
38
var y = utils.injectTempVar(state);
39
traverse(node.body, path, state);
40
utils.append('return ' + x + ' + ' + y + ';', state);
41
utils.catchup(node.range[1], state);
42
return false;
43
}
44
visitFunctionBlock.test = function(node, path, state) {
45
var parentType = path.length && path[0].type;
46
return node.type === Syntax.BlockStatement && (
47
parentType === Syntax.FunctionDeclaration ||
48
parentType === Syntax.FunctionExpression
49
);
50
};
51
52
expect(transform(
53
[visitFunctionBlock],
54
'var x = function() {};'
55
).code).toEqual(
56
'var x = function() {var $__0, $__1;return $__0 + $__1;};'
57
);
58
59
expect(eval(transform(
60
[visitFunctionBlock],
61
'2 + (function sum(x, y)\t{ $__0 = x; $__1 = y; }(3, 5))'
62
).code)).toEqual(10);
63
});
64
});
65
66
});
67
68