index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. var test = require('tape')
  2. var assert = require('assert')
  3. var parse = require('acorn').parse
  4. var getAssignedIdentifiers = require('../')
  5. function getName (node) {
  6. assert.equal(node.type, 'Identifier', 'Returned node must be an Identifier')
  7. return node.name
  8. }
  9. test('example', function (t) {
  10. t.plan(1)
  11. var ast = parse(`
  12. var { a, b: [ c,, ...x ], d } = whatever()
  13. `)
  14. var node = ast.body[0].declarations[0].id
  15. t.deepEqual(getAssignedIdentifiers(node).map(getName), [
  16. 'a',
  17. 'c',
  18. 'x',
  19. 'd'
  20. ])
  21. })
  22. test('simple identifiers', function (t) {
  23. t.plan(1)
  24. var ast = parse(`
  25. var xyz = whatever()
  26. `)
  27. var node = ast.body[0].declarations[0].id
  28. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'xyz' ])
  29. })
  30. test('array destructuring', function (t) {
  31. t.plan(1)
  32. var ast = parse(`
  33. var [a, b, c] = whatever()
  34. `)
  35. var node = ast.body[0].declarations[0].id
  36. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'c' ])
  37. })
  38. test('array destructuring with rest element', function (t) {
  39. t.plan(1)
  40. var ast = parse(`
  41. var [a, b, ...rest] = whatever()
  42. `)
  43. var node = ast.body[0].declarations[0].id
  44. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'rest' ])
  45. })
  46. test('array destructuring with holes', function (t) {
  47. t.plan(1)
  48. var ast = parse(`
  49. var [a, b,,,,,, boop] = whatever()
  50. `)
  51. var node = ast.body[0].declarations[0].id
  52. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'boop' ])
  53. })
  54. test('nested array destructuring', function (t) {
  55. t.plan(1)
  56. var ast = parse(`
  57. var [a, [[[b]], ...c], boop] = whatever()
  58. `)
  59. var node = ast.body[0].declarations[0].id
  60. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b', 'c', 'boop' ])
  61. })
  62. test('object destructuring', function (t) {
  63. t.plan(1)
  64. var ast = parse(`
  65. var {a, b} = whatever()
  66. `)
  67. var node = ast.body[0].declarations[0].id
  68. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b' ])
  69. })
  70. test('object destructuring with different names', function (t) {
  71. t.plan(1)
  72. var ast = parse(`
  73. var {a: b, b: lol} = whatever()
  74. `)
  75. var node = ast.body[0].declarations[0].id
  76. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'b', 'lol' ])
  77. })
  78. test('nested object destructuring', function (t) {
  79. t.plan(1)
  80. var ast = parse(`
  81. var {a: {b}, b: lol, c: {
  82. d, e: { f: g }
  83. }} = whatever()
  84. `)
  85. var node = ast.body[0].declarations[0].id
  86. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'b', 'lol', 'd', 'g' ])
  87. })
  88. test('object rest destructuring', function (t) {
  89. t.plan(1)
  90. var ast = parse(`
  91. var {a, ...b} = whatever()
  92. `, { ecmaVersion: 9 })
  93. var node = ast.body[0].declarations[0].id
  94. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'a', 'b' ])
  95. })
  96. test('import declarations', function (t) {
  97. t.plan(2)
  98. var ast = parse(`
  99. import x, { y, z as a } from 'module'
  100. `, { sourceType: 'module' })
  101. var node = ast.body[0]
  102. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'x', 'y', 'a' ])
  103. ast = parse(`
  104. import * as ns from 'module'
  105. `, { sourceType: 'module' })
  106. node = ast.body[0]
  107. t.deepEqual(getAssignedIdentifiers(node).map(getName), [ 'ns' ])
  108. })