UNPKG

910 BJavaScriptView Raw
1var falafel = require('../');
2var test = require('tape');
3
4test('parent', function (t) {
5 t.plan(5);
6
7 var src = '(function () {'
8 + 'var xs = [ 1, 2, 3 ];'
9 + 'fn(ys);'
10 + '})()';
11
12 var output = falafel(src, function (node) {
13 if (node.type === 'ArrayExpression') {
14 t.equal(node.parent.type, 'VariableDeclarator');
15 t.equal(
16 ffBracket(node.parent.source()),
17 'xs = [ 1, 2, 3 ]'
18 );
19 t.equal(node.parent.parent.type, 'VariableDeclaration');
20 t.equal(
21 ffBracket(node.parent.parent.source()),
22 'var xs = [ 1, 2, 3 ];'
23 );
24 node.parent.update('ys = 4;');
25 }
26 });
27
28 Function(['fn'], output)(function (x) { t.equal(x, 4) });
29});
30
31function ffBracket (s) {
32 return s.replace(/\[\s*/, '[ ').replace(/\s*\]/, ' ]');
33}