| 1 |
import * as t from "../lib/index.js"; |
| 2 |
import { parse } from "@babel/parser"; |
| 3 |
|
| 4 |
describe("validators", function () { |
| 5 |
describe("isNodesEquivalent", function () { |
| 6 |
it("should handle simple cases", function () { |
| 7 |
const mem = t.memberExpression(t.identifier("a"), t.identifier("b")); |
| 8 |
expect(t.isNodesEquivalent(mem, mem)).toBe(true); |
| 9 |
|
| 10 |
const mem2 = t.memberExpression(t.identifier("a"), t.identifier("c")); |
| 11 |
expect(t.isNodesEquivalent(mem, mem2)).toBe(false); |
| 12 |
}); |
| 13 |
|
| 14 |
it("should handle full programs", function () { |
| 15 |
expect(t.isNodesEquivalent(parse("1 + 1"), parse("1+1"))).toBe(true); |
| 16 |
expect(t.isNodesEquivalent(parse("1 + 1"), parse("1+2"))).toBe(false); |
| 17 |
}); |
| 18 |
|
| 19 |
it("should handle complex programs", function () { |
| 20 |
const program = "'use strict'; function lol() { wow();return 1; }"; |
| 21 |
|
| 22 |
expect(t.isNodesEquivalent(parse(program), parse(program))).toBe(true); |
| 23 |
|
| 24 |
const program2 = "'use strict'; function lol() { wow();return -1; }"; |
| 25 |
|
| 26 |
expect(t.isNodesEquivalent(parse(program), parse(program2))).toBe(false); |
| 27 |
}); |
| 28 |
|
| 29 |
it("should handle nodes with object properties", function () { |
| 30 |
const original = t.templateElement({ raw: "\\'a", cooked: "'a" }, true); |
| 31 |
const identical = t.templateElement({ raw: "\\'a", cooked: "'a" }, true); |
| 32 |
const different = t.templateElement({ raw: "'a", cooked: "'a" }, true); |
| 33 |
expect(t.isNodesEquivalent(original, identical)).toBe(true); |
| 34 |
expect(t.isNodesEquivalent(original, different)).toBe(false); |
| 35 |
}); |
| 36 |
|
| 37 |
it("rejects 'await' as an identifier", function () { |
| 38 |
expect(t.isValidIdentifier("await")).toBe(false); |
| 39 |
}); |
| 40 |
}); |
| 41 |
|
| 42 |
describe("isCompatTag", function () { |
| 43 |
it("should handle lowercase tag names", function () { |
| 44 |
expect(t.react.isCompatTag("div")).toBe(true); |
| 45 |
expect(t.react.isCompatTag("a")).toBe(true); // one letter |
| 46 |
expect(t.react.isCompatTag("h3")).toBe(true); // letters and numbers |
| 47 |
}); |
| 48 |
|
| 49 |
it("should handle custom element tag names", function () { |
| 50 |
expect(t.react.isCompatTag("plastic-button")).toBe(true); // ascii letters |
| 51 |
expect(t.react.isCompatTag("math-α")).toBe(true); // non-latin chars |
| 52 |
expect(t.react.isCompatTag("img-viewer2")).toBe(true); // numbers |
| 53 |
expect(t.react.isCompatTag("emotion-😍")).toBe(true); // emoji |
| 54 |
}); |
| 55 |
|
| 56 |
it("accepts trailing dash '-' in custom element tag names", function () { |
| 57 |
expect(t.react.isCompatTag("div-")).toBe(true); |
| 58 |
expect(t.react.isCompatTag("a-")).toBe(true); |
| 59 |
expect(t.react.isCompatTag("h3-")).toBe(true); |
| 60 |
}); |
| 61 |
|
| 62 |
it("rejects empty or null tag names", function () { |
| 63 |
expect(t.react.isCompatTag(null)).toBe(false); |
| 64 |
expect(t.react.isCompatTag()).toBe(false); |
| 65 |
expect(t.react.isCompatTag(undefined)).toBe(false); |
| 66 |
expect(t.react.isCompatTag("")).toBe(false); |
| 67 |
}); |
| 68 |
|
| 69 |
it("rejects tag names starting with an uppercase letter", function () { |
| 70 |
expect(t.react.isCompatTag("Div")).toBe(false); |
| 71 |
expect(t.react.isCompatTag("A")).toBe(false); |
| 72 |
expect(t.react.isCompatTag("H3")).toBe(false); |
| 73 |
}); |
| 74 |
|
| 75 |
it("rejects all uppercase tag names", function () { |
| 76 |
expect(t.react.isCompatTag("DIV")).toBe(false); |
| 77 |
expect(t.react.isCompatTag("A")).toBe(false); |
| 78 |
expect(t.react.isCompatTag("H3")).toBe(false); |
| 79 |
}); |
| 80 |
|
| 81 |
it("rejects leading dash '-'", function () { |
| 82 |
expect(t.react.isCompatTag("-div")).toBe(false); |
| 83 |
expect(t.react.isCompatTag("-a")).toBe(false); |
| 84 |
expect(t.react.isCompatTag("-h3")).toBe(false); |
| 85 |
}); |
| 86 |
}); |
| 87 |
|
| 88 |
describe("patterns", function () { |
| 89 |
it("allows nested pattern structures", function () { |
| 90 |
const pattern = t.objectPattern([ |
| 91 |
t.objectProperty( |
| 92 |
t.identifier("a"), |
| 93 |
t.objectPattern([ |
| 94 |
t.objectProperty(t.identifier("b"), t.identifier("foo")), |
| 95 |
t.objectProperty( |
| 96 |
t.identifier("c"), |
| 97 |
t.arrayPattern([t.identifier("value")]), |
| 98 |
), |
| 99 |
]), |
| 100 |
), |
| 101 |
]); |
| 102 |
|
| 103 |
expect(t.isNodesEquivalent(pattern, pattern)).toBe(true); |
| 104 |
}); |
| 105 |
}); |
| 106 |
|
| 107 |
describe("isReferenced", function () { |
| 108 |
it("returns false if node is a key of ObjectTypeProperty", function () { |
| 109 |
const node = t.identifier("a"); |
| 110 |
const parent = t.objectTypeProperty(node, t.numberTypeAnnotation()); |
| 111 |
|
| 112 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 113 |
}); |
| 114 |
|
| 115 |
it("returns true if node is a value of ObjectTypeProperty", function () { |
| 116 |
const node = t.identifier("a"); |
| 117 |
const parent = t.objectTypeProperty( |
| 118 |
t.identifier("someKey"), |
| 119 |
t.genericTypeAnnotation(node), |
| 120 |
); |
| 121 |
|
| 122 |
expect(t.isReferenced(node, parent)).toBe(true); |
| 123 |
}); |
| 124 |
|
| 125 |
describe("ObjectProperty", () => { |
| 126 |
it("returns true if node is a value of ObjectProperty of an expression", function () { |
| 127 |
const node = t.identifier("a"); |
| 128 |
const parent = t.objectProperty(t.identifier("key"), node); |
| 129 |
const grandparent = t.objectExpression([parent]); |
| 130 |
|
| 131 |
expect(t.isReferenced(node, parent, grandparent)).toBe(true); |
| 132 |
}); |
| 133 |
|
| 134 |
it("returns false if node is a value of ObjectProperty of a pattern", function () { |
| 135 |
const node = t.identifier("a"); |
| 136 |
const parent = t.objectProperty(t.identifier("key"), node); |
| 137 |
const grandparent = t.objectPattern([parent]); |
| 138 |
|
| 139 |
expect(t.isReferenced(node, parent, grandparent)).toBe(false); |
| 140 |
}); |
| 141 |
|
| 142 |
it("returns true if node is computed property key of an expression", function () { |
| 143 |
const node = t.identifier("a"); |
| 144 |
const parent = t.objectProperty(node, t.identifier("value"), true); |
| 145 |
const grandparent = t.objectExpression([parent]); |
| 146 |
|
| 147 |
expect(t.isReferenced(node, parent, grandparent)).toBe(true); |
| 148 |
}); |
| 149 |
|
| 150 |
it("returns true if node is computed property key of a pattern", function () { |
| 151 |
const node = t.identifier("a"); |
| 152 |
const parent = t.objectProperty(node, t.identifier("value"), true); |
| 153 |
const grandparent = t.objectPattern([parent]); |
| 154 |
|
| 155 |
expect(t.isReferenced(node, parent, grandparent)).toBe(true); |
| 156 |
}); |
| 157 |
}); |
| 158 |
|
| 159 |
describe("TSPropertySignature", function () { |
| 160 |
it("returns false if node is a key", function () { |
| 161 |
// { A: string } |
| 162 |
const node = t.identifier("A"); |
| 163 |
const parent = t.tsPropertySignature( |
| 164 |
node, |
| 165 |
t.tsTypeAnnotation(t.tsStringKeyword()), |
| 166 |
); |
| 167 |
|
| 168 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 169 |
}); |
| 170 |
|
| 171 |
it("returns true if node is a value", function () { |
| 172 |
// { someKey: A } |
| 173 |
const node = t.identifier("A"); |
| 174 |
const parent = t.tsPropertySignature( |
| 175 |
t.identifier("someKey"), |
| 176 |
t.tsTypeAnnotation(t.tsTypeReference(node)), |
| 177 |
); |
| 178 |
|
| 179 |
expect(t.isReferenced(node, parent)).toBe(true); |
| 180 |
}); |
| 181 |
}); |
| 182 |
|
| 183 |
describe("TSEnumMember", function () { |
| 184 |
it("returns false if node is an id", function () { |
| 185 |
// enum X = { A }; |
| 186 |
const node = t.identifier("A"); |
| 187 |
const parent = t.tsEnumMember(node); |
| 188 |
|
| 189 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 190 |
}); |
| 191 |
|
| 192 |
it("returns true if node is a value", function () { |
| 193 |
// enum X = { Foo = A } |
| 194 |
const node = t.identifier("A"); |
| 195 |
const parent = t.tsEnumMember(t.identifier("Foo"), node); |
| 196 |
|
| 197 |
expect(t.isReferenced(node, parent)).toBe(true); |
| 198 |
}); |
| 199 |
}); |
| 200 |
|
| 201 |
describe("ObjectMethod", function () { |
| 202 |
it("returns false if node is method key", function () { |
| 203 |
const node = t.identifier("A"); |
| 204 |
const parent = t.objectMethod("method", node, [], t.blockStatement([])); |
| 205 |
|
| 206 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 207 |
}); |
| 208 |
|
| 209 |
it("returns true if node is computed method key", function () { |
| 210 |
const node = t.identifier("A"); |
| 211 |
const parent = t.objectMethod( |
| 212 |
"method", |
| 213 |
node, |
| 214 |
[], |
| 215 |
t.blockStatement([]), |
| 216 |
true, |
| 217 |
); |
| 218 |
|
| 219 |
expect(t.isReferenced(node, parent)).toBe(true); |
| 220 |
}); |
| 221 |
|
| 222 |
it("returns false if node is method param", function () { |
| 223 |
const node = t.identifier("A"); |
| 224 |
const parent = t.objectMethod( |
| 225 |
"method", |
| 226 |
t.identifier("foo"), |
| 227 |
[node], |
| 228 |
t.blockStatement([]), |
| 229 |
); |
| 230 |
|
| 231 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 232 |
}); |
| 233 |
}); |
| 234 |
|
| 235 |
describe("ClassMethod", function () { |
| 236 |
it("returns false if node is method key", function () { |
| 237 |
const node = t.identifier("A"); |
| 238 |
const parent = t.classMethod("method", node, [], t.blockStatement([])); |
| 239 |
|
| 240 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 241 |
}); |
| 242 |
|
| 243 |
it("returns true if node is computed method key", function () { |
| 244 |
const node = t.identifier("A"); |
| 245 |
const parent = t.classMethod( |
| 246 |
"method", |
| 247 |
node, |
| 248 |
[], |
| 249 |
t.blockStatement([]), |
| 250 |
true, |
| 251 |
); |
| 252 |
|
| 253 |
expect(t.isReferenced(node, parent)).toBe(true); |
| 254 |
}); |
| 255 |
|
| 256 |
it("returns false if node is method param", function () { |
| 257 |
const node = t.identifier("A"); |
| 258 |
const parent = t.classMethod( |
| 259 |
"method", |
| 260 |
t.identifier("foo"), |
| 261 |
[node], |
| 262 |
t.blockStatement([]), |
| 263 |
); |
| 264 |
|
| 265 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 266 |
}); |
| 267 |
}); |
| 268 |
|
| 269 |
describe("ClassDeclaration", () => { |
| 270 |
it("returns true if node is a class heritage", function () { |
| 271 |
const node = t.identifier("A"); |
| 272 |
const parent = t.classDeclaration( |
| 273 |
t.identifier("C"), |
| 274 |
node, |
| 275 |
t.classBody([]), |
| 276 |
[], |
| 277 |
); |
| 278 |
|
| 279 |
expect(t.isReferenced(node, parent)).toBe(true); |
| 280 |
}); |
| 281 |
}); |
| 282 |
|
| 283 |
describe("exports", function () { |
| 284 |
it("returns false for re-exports", function () { |
| 285 |
const node = t.identifier("foo"); |
| 286 |
const parent = t.exportSpecifier(node, t.identifier("bar")); |
| 287 |
const grandparent = t.exportNamedDeclaration( |
| 288 |
null, |
| 289 |
[parent], |
| 290 |
t.stringLiteral("library"), |
| 291 |
); |
| 292 |
|
| 293 |
expect(t.isReferenced(node, parent, grandparent)).toBe(false); |
| 294 |
}); |
| 295 |
|
| 296 |
it("returns true for local exports", function () { |
| 297 |
const node = t.identifier("foo"); |
| 298 |
const parent = t.exportSpecifier(node, t.identifier("bar")); |
| 299 |
const grandparent = t.exportNamedDeclaration(null, [parent]); |
| 300 |
|
| 301 |
expect(t.isReferenced(node, parent, grandparent)).toBe(true); |
| 302 |
}); |
| 303 |
}); |
| 304 |
|
| 305 |
describe("import attributes", function () { |
| 306 |
it("returns false for import attributes", function () { |
| 307 |
const node = t.identifier("foo"); |
| 308 |
const parent = t.importAttribute(node, t.stringLiteral("bar")); |
| 309 |
|
| 310 |
expect(t.isReferenced(node, parent)).toBe(false); |
| 311 |
}); |
| 312 |
}); |
| 313 |
}); |
| 314 |
|
| 315 |
describe("isBinding", function () { |
| 316 |
it("returns false if node id a value of ObjectProperty of an expression", function () { |
| 317 |
const node = t.identifier("a"); |
| 318 |
const parent = t.objectProperty(t.identifier("key"), node); |
| 319 |
const grandparent = t.objectExpression([parent]); |
| 320 |
|
| 321 |
expect(t.isBinding(node, parent, grandparent)).toBe(false); |
| 322 |
}); |
| 323 |
|
| 324 |
it("returns true if node id a value of ObjectProperty of a pattern", function () { |
| 325 |
const node = t.identifier("a"); |
| 326 |
const parent = t.objectProperty(t.identifier("key"), node); |
| 327 |
const grandparent = t.objectPattern([parent]); |
| 328 |
|
| 329 |
expect(t.isBinding(node, parent, grandparent)).toBe(true); |
| 330 |
}); |
| 331 |
}); |
| 332 |
|
| 333 |
describe("isType", function () { |
| 334 |
it("returns true if nodeType equals targetType", function () { |
| 335 |
expect(t.isType("Identifier", "Identifier")).toBe(true); |
| 336 |
}); |
| 337 |
it("returns false if targetType is a primary node type", function () { |
| 338 |
expect(t.isType("Expression", "ArrayExpression")).toBe(false); |
| 339 |
}); |
| 340 |
it("returns true if targetType is an alias of nodeType", function () { |
| 341 |
expect(t.isType("ArrayExpression", "Expression")).toBe(true); |
| 342 |
}); |
| 343 |
it("returns false if nodeType and targetType are unrelated", function () { |
| 344 |
expect(t.isType("ArrayExpression", "ClassBody")).toBe(false); |
| 345 |
}); |
| 346 |
it("returns false if nodeType is undefined", function () { |
| 347 |
expect(t.isType(undefined, "Expression")).toBe(false); |
| 348 |
}); |
| 349 |
}); |
| 350 |
|
| 351 |
describe("placeholders", function () { |
| 352 |
describe("isPlaceholderType", function () { |
| 353 |
describe("when placeholderType is a specific node type", function () { |
| 354 |
const placeholder = "Identifier"; |
| 355 |
|
| 356 |
it("returns true if targetType is placeholderType", function () { |
| 357 |
expect(t.isPlaceholderType(placeholder, "Identifier")).toBe(true); |
| 358 |
}); |
| 359 |
it("returns true if targetType an alias for placeholderType", function () { |
| 360 |
expect(t.isPlaceholderType(placeholder, "Expression")).toBe(true); |
| 361 |
}); |
| 362 |
it("returns false for unrelated types", function () { |
| 363 |
expect(t.isPlaceholderType(placeholder, "String")).toBe(false); |
| 364 |
}); |
| 365 |
}); |
| 366 |
|
| 367 |
describe("when placeholderType is a generic alias type", function () { |
| 368 |
const placeholder = "Pattern"; |
| 369 |
|
| 370 |
it("returns true if targetType is placeholderType", function () { |
| 371 |
expect(t.isPlaceholderType(placeholder, "Pattern")).toBe(true); |
| 372 |
}); |
| 373 |
it("returns true if targetType an alias for placeholderType", function () { |
| 374 |
expect(t.isPlaceholderType(placeholder, "LVal")).toBe(true); |
| 375 |
}); |
| 376 |
it("returns false for unrelated types", function () { |
| 377 |
expect(t.isPlaceholderType(placeholder, "Expression")).toBe(false); |
| 378 |
}); |
| 379 |
it("returns false if targetType is aliased by placeholderType", function () { |
| 380 |
// i.e. a Pattern might not be an Identifier |
| 381 |
expect(t.isPlaceholderType(placeholder, "Identifier")).toBe(false); |
| 382 |
}); |
| 383 |
}); |
| 384 |
}); |
| 385 |
|
| 386 |
describe("is", function () { |
| 387 |
describe("when the placeholder matches a specific node", function () { |
| 388 |
const identifier = t.placeholder("Identifier", t.identifier("foo")); |
| 389 |
|
| 390 |
it("returns false if targetType is expectedNode", function () { |
| 391 |
expect(t.is("Identifier", identifier)).toBe(false); |
| 392 |
}); |
| 393 |
it("returns true if targetType is an alias", function () { |
| 394 |
expect(t.is("LVal", identifier)).toBe(true); |
| 395 |
}); |
| 396 |
}); |
| 397 |
|
| 398 |
describe("when the placeholder matches a generic alias", function () { |
| 399 |
const pattern = t.placeholder("Pattern", t.identifier("bar")); |
| 400 |
|
| 401 |
it("returns false if targetType is aliased as expectedNode", function () { |
| 402 |
// i.e. a Pattern might not be an Identifier |
| 403 |
expect(t.is("Identifier", pattern)).toBe(false); |
| 404 |
}); |
| 405 |
it("returns true if targetType is expectedNode", function () { |
| 406 |
expect(t.is("Pattern", pattern)).toBe(true); |
| 407 |
}); |
| 408 |
it("returns true if targetType is an alias for expectedNode", function () { |
| 409 |
expect(t.is("LVal", pattern)).toBe(true); |
| 410 |
}); |
| 411 |
}); |
| 412 |
}); |
| 413 |
|
| 414 |
describe("is[Type]", function () { |
| 415 |
describe("when the placeholder matches a specific node", function () { |
| 416 |
const identifier = t.placeholder("Identifier", t.identifier("foo")); |
| 417 |
|
| 418 |
it("returns false if targetType is expectedNode", function () { |
| 419 |
expect(t.isIdentifier(identifier)).toBe(false); |
| 420 |
}); |
| 421 |
it("returns true if targetType is an alias", function () { |
| 422 |
expect(t.isLVal(identifier)).toBe(true); |
| 423 |
}); |
| 424 |
}); |
| 425 |
|
| 426 |
describe("when the placeholder matches a generic alias", function () { |
| 427 |
const pattern = t.placeholder("Pattern", t.identifier("bar")); |
| 428 |
|
| 429 |
it("returns false if targetType is aliased as expectedNode", function () { |
| 430 |
expect(t.isIdentifier(pattern)).toBe(false); |
| 431 |
}); |
| 432 |
it("returns true if targetType is expectedNode", function () { |
| 433 |
expect(t.isPattern(pattern)).toBe(true); |
| 434 |
}); |
| 435 |
it("returns true if targetType is an alias for expectedNode", function () { |
| 436 |
expect(t.isLVal(pattern)).toBe(true); |
| 437 |
}); |
| 438 |
}); |
| 439 |
}); |
| 440 |
}); |
| 441 |
}); |