| 1 |
import * as t from "../lib/index.js"; |
| 2 |
import { parse } from "@babel/parser"; |
| 3 |
import { CodeGenerator } from "@babel/generator"; |
| 4 |
|
| 5 |
describe("cloneNode", function () { |
| 6 |
it("should handle undefined", function () { |
| 7 |
const node = undefined; |
| 8 |
const cloned = t.cloneNode(node); |
| 9 |
expect(cloned).toBeUndefined(); |
| 10 |
}); |
| 11 |
|
| 12 |
it("should handle null", function () { |
| 13 |
const node = null; |
| 14 |
const cloned = t.cloneNode(node); |
| 15 |
expect(cloned).toBeNull(); |
| 16 |
}); |
| 17 |
|
| 18 |
it("should handle simple cases", function () { |
| 19 |
const node = t.identifier("a"); |
| 20 |
const cloned = t.cloneNode(node); |
| 21 |
expect(node).not.toBe(cloned); |
| 22 |
expect(t.isNodesEquivalent(node, cloned)).toBe(true); |
| 23 |
}); |
| 24 |
|
| 25 |
it("should handle full programs", function () { |
| 26 |
const file = parse("1 + 1"); |
| 27 |
const cloned = t.cloneNode(file); |
| 28 |
expect(file).not.toBe(cloned); |
| 29 |
expect(file.program.body[0].expression.right).not.toBe( |
| 30 |
cloned.program.body[0].expression.right, |
| 31 |
); |
| 32 |
expect(file.program.body[0].expression.left).not.toBe( |
| 33 |
cloned.program.body[0].expression.left, |
| 34 |
); |
| 35 |
expect(t.isNodesEquivalent(file, cloned)).toBe(true); |
| 36 |
}); |
| 37 |
|
| 38 |
it("should handle complex programs", function () { |
| 39 |
const program = "'use strict'; function lol() { wow();return 1; }"; |
| 40 |
const node = parse(program); |
| 41 |
const cloned = t.cloneNode(node); |
| 42 |
expect(node).not.toBe(cloned); |
| 43 |
expect(t.isNodesEquivalent(node, cloned)).toBe(true); |
| 44 |
}); |
| 45 |
|
| 46 |
it("should handle deep cloning without loc of fragments", function () { |
| 47 |
const program = "foo();"; |
| 48 |
const node = parse(program); |
| 49 |
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ true); |
| 50 |
expect(node).not.toBe(cloned); |
| 51 |
expect(t.isNodesEquivalent(node, cloned)).toBe(true); |
| 52 |
}); |
| 53 |
|
| 54 |
it("should handle missing array element", function () { |
| 55 |
const node = parse("[,0]"); |
| 56 |
const cloned = t.cloneNode(node); |
| 57 |
expect(node).not.toBe(cloned); |
| 58 |
expect(t.isNodesEquivalent(node, cloned)).toBe(true); |
| 59 |
}); |
| 60 |
|
| 61 |
it("should support shallow cloning", function () { |
| 62 |
const node = t.memberExpression(t.identifier("foo"), t.identifier("bar")); |
| 63 |
const cloned = t.cloneNode(node, /* deep */ false); |
| 64 |
expect(node).not.toBe(cloned); |
| 65 |
expect(node.object).toBe(cloned.object); |
| 66 |
expect(node.property).toBe(cloned.property); |
| 67 |
}); |
| 68 |
|
| 69 |
it("should preserve type annotations", function () { |
| 70 |
const node = t.variableDeclaration("let", [ |
| 71 |
t.variableDeclarator({ |
| 72 |
...t.identifier("value"), |
| 73 |
typeAnnotation: t.anyTypeAnnotation(), |
| 74 |
}), |
| 75 |
]); |
| 76 |
const cloned = t.cloneNode(node, /* deep */ true); |
| 77 |
expect(cloned.declarations[0].id.typeAnnotation).toEqual( |
| 78 |
node.declarations[0].id.typeAnnotation, |
| 79 |
); |
| 80 |
expect(cloned.declarations[0].id.typeAnnotation).not.toBe( |
| 81 |
node.declarations[0].id.typeAnnotation, |
| 82 |
); |
| 83 |
}); |
| 84 |
|
| 85 |
it("should support shallow cloning without loc", function () { |
| 86 |
const node = t.variableDeclaration("let", [ |
| 87 |
t.variableDeclarator({ |
| 88 |
...t.identifier("value"), |
| 89 |
typeAnnotation: t.anyTypeAnnotation(), |
| 90 |
}), |
| 91 |
]); |
| 92 |
node.loc = {}; |
| 93 |
const cloned = t.cloneNode(node, /* deep */ false, /* withoutLoc */ true); |
| 94 |
expect(cloned.loc).toBeNull(); |
| 95 |
}); |
| 96 |
|
| 97 |
it("should support deep cloning without loc", function () { |
| 98 |
const node = t.variableDeclaration("let", [ |
| 99 |
t.variableDeclarator({ |
| 100 |
...t.identifier("value"), |
| 101 |
typeAnnotation: t.anyTypeAnnotation(), |
| 102 |
}), |
| 103 |
]); |
| 104 |
node.loc = {}; |
| 105 |
node.declarations[0].id.loc = {}; |
| 106 |
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ true); |
| 107 |
expect(cloned.loc).toBeNull(); |
| 108 |
expect(cloned.declarations[0].id.loc).toBeNull(); |
| 109 |
}); |
| 110 |
|
| 111 |
it("should support deep cloning for comments", function () { |
| 112 |
const node = t.variableDeclaration("let", [ |
| 113 |
t.variableDeclarator({ |
| 114 |
...t.identifier("value"), |
| 115 |
leadingComments: [{ loc: {} }], |
| 116 |
innerComments: [{ loc: {} }], |
| 117 |
trailingComments: [{ loc: {} }], |
| 118 |
}), |
| 119 |
]); |
| 120 |
node.loc = {}; |
| 121 |
node.declarations[0].id.loc = {}; |
| 122 |
|
| 123 |
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ false); |
| 124 |
expect(cloned.declarations[0].id.leadingComments[0]).not.toBe( |
| 125 |
node.declarations[0].id.leadingComments[0], |
| 126 |
); |
| 127 |
expect(cloned.declarations[0].id.leadingComments[0].loc).toBe( |
| 128 |
node.declarations[0].id.leadingComments[0].loc, |
| 129 |
); |
| 130 |
expect(cloned.declarations[0].id.innerComments[0].loc).toBe( |
| 131 |
node.declarations[0].id.innerComments[0].loc, |
| 132 |
); |
| 133 |
expect(cloned.declarations[0].id.trailingComments[0].loc).toBe( |
| 134 |
node.declarations[0].id.trailingComments[0].loc, |
| 135 |
); |
| 136 |
}); |
| 137 |
|
| 138 |
it("should support deep cloning for comments without loc", function () { |
| 139 |
const node = t.variableDeclaration("let", [ |
| 140 |
t.variableDeclarator({ |
| 141 |
...t.identifier("value"), |
| 142 |
leadingComments: [{ loc: {} }], |
| 143 |
innerComments: [{ loc: {} }], |
| 144 |
trailingComments: [{ loc: {} }], |
| 145 |
}), |
| 146 |
]); |
| 147 |
node.loc = {}; |
| 148 |
node.declarations[0].id.loc = {}; |
| 149 |
|
| 150 |
const cloned = t.cloneNode(node, /* deep */ true, /* withoutLoc */ true); |
| 151 |
expect(cloned.declarations[0].id.leadingComments[0].loc).toBe(null); |
| 152 |
expect(cloned.declarations[0].id.innerComments[0].loc).toBe(null); |
| 153 |
expect(cloned.declarations[0].id.trailingComments[0].loc).toBe(null); |
| 154 |
}); |
| 155 |
|
| 156 |
it("should generate same code after deep cloning", function () { |
| 157 |
let code = `//test1 |
| 158 |
/*test2*/var/*test3*/ a = 1/*test4*/;//test5 |
| 159 |
//test6 |
| 160 |
var b; |
| 161 |
`; |
| 162 |
code = new CodeGenerator(parse(code), { retainLines: true }).generate() |
| 163 |
.code; |
| 164 |
|
| 165 |
const ast = t.cloneNode( |
| 166 |
parse(code), |
| 167 |
/* deep */ true, |
| 168 |
/* withoutLoc */ false, |
| 169 |
); |
| 170 |
const newCode = new CodeGenerator(ast, { retainLines: true }).generate() |
| 171 |
.code; |
| 172 |
|
| 173 |
expect(newCode).toBe(code); |
| 174 |
}); |
| 175 |
}); |