index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. var test = require('tape')
  2. var acorn = require('acorn-node')
  3. var find = require('../')
  4. test('undeclared variables', function (t) {
  5. t.deepEqual(find(`
  6. var a, b
  7. a, b, c
  8. `), {
  9. identifiers: ['c'],
  10. properties: []
  11. })
  12. t.end()
  13. })
  14. test('pass in a parsed ast', function (t) {
  15. t.deepEqual(find(acorn.parse(`
  16. var a, b
  17. a, b, c
  18. `)), {
  19. identifiers: ['c'],
  20. properties: []
  21. })
  22. t.end()
  23. })
  24. test('undeclared properties', function (t) {
  25. t.deepEqual(find(`
  26. var a, b
  27. a, b, c, d.e
  28. `), {
  29. identifiers: ['c', 'd'],
  30. properties: ['d.e']
  31. })
  32. t.end()
  33. })
  34. test('wildcard use of undeclared name', function (t) {
  35. t.deepEqual(find(`
  36. function func () {}
  37. new A()
  38. A.from()
  39. func(b)
  40. C.from()
  41. `, { wildcard: true }), {
  42. identifiers: ['A', 'b', 'C'],
  43. properties: ['A.*', 'A.from', 'b.*', 'C.from']
  44. })
  45. t.end()
  46. })
  47. test('wildcard use of undeclared name (different order)', function (t) {
  48. t.deepEqual(find(`
  49. A.from()
  50. new A()
  51. `, { wildcard: true }), {
  52. identifiers: ['A'],
  53. properties: ['A.from', 'A.*']
  54. })
  55. t.end()
  56. })
  57. test('function names', function (t) {
  58. t.deepEqual(find(`
  59. function x () {
  60. return x
  61. }
  62. x()
  63. `), {
  64. identifiers: [],
  65. properties: []
  66. })
  67. t.deepEqual(find(`
  68. function x () {
  69. return x
  70. }
  71. y()
  72. `), {
  73. identifiers: ['y'],
  74. properties: []
  75. })
  76. t.end()
  77. })
  78. test('class names', function (t) {
  79. t.deepEqual(find(`
  80. class X {}
  81. new X()
  82. `), {
  83. identifiers: [],
  84. properties: []
  85. })
  86. t.deepEqual(find(`
  87. class X extends Y {}
  88. new X()
  89. `), {
  90. identifiers: ['Y'],
  91. properties: []
  92. })
  93. t.deepEqual(find(`
  94. class Y {}
  95. class X extends Y {}
  96. new X()
  97. `), {
  98. identifiers: [],
  99. properties: []
  100. })
  101. t.end()
  102. })
  103. test('class methods', function (t) {
  104. t.deepEqual(find(`
  105. class X {
  106. constructor() { u }
  107. bar() { v }
  108. static foo() { w }
  109. }
  110. `), {
  111. identifiers: ['u', 'v', 'w'],
  112. properties: []
  113. })
  114. t.end()
  115. })
  116. test('super', function (t) {
  117. t.deepEqual(find(`
  118. class X extends Y {
  119. constructor() { super() }
  120. }
  121. `), {
  122. identifiers: ['Y'],
  123. properties: []
  124. })
  125. t.deepEqual(find(`
  126. class X {
  127. foo() { super.foo }
  128. }
  129. `), {
  130. identifiers: [],
  131. properties: []
  132. })
  133. t.end()
  134. })
  135. test('scope', function (t) {
  136. t.deepEqual(find(`
  137. function y () {
  138. function x () {
  139. return x
  140. }
  141. }
  142. x(y(x.y))
  143. `), {
  144. identifiers: ['x'],
  145. properties: ['x.y']
  146. })
  147. t.end()
  148. })
  149. test('block scope', function (t) {
  150. t.deepEqual(find(`
  151. var x
  152. { var y; let z }
  153. x, y, z
  154. `), {
  155. identifiers: ['z'],
  156. properties: []
  157. })
  158. t.end()
  159. })
  160. test('function parameters', function (t) {
  161. t.deepEqual(find(`
  162. function a (a1, a2) { a, a1, a2, a3 }
  163. ;(function b (b1, b2) { b, b1, b2, b3 })
  164. ;((c1, c2) => { c1, c2, c3 })
  165. `), {
  166. identifiers: ['a3', 'b3', 'c3'],
  167. properties: []
  168. })
  169. t.deepEqual(find(`
  170. function a (a1, a2) { }
  171. a, a1, a2
  172. `), {
  173. identifiers: ['a1', 'a2'],
  174. properties: []
  175. })
  176. t.end()
  177. })
  178. test('assignment', function (t) {
  179. t.deepEqual(find(`
  180. var a
  181. b = 2
  182. b.c = 3
  183. `), {
  184. identifiers: ['b'],
  185. properties: ['b.c']
  186. })
  187. t.end()
  188. })
  189. test('catch', function (t) {
  190. t.deepEqual(find(`
  191. try { var a } catch (err) { err }
  192. try { let b } catch (orr) { orr }
  193. a, b
  194. `), {
  195. identifiers: ['b'],
  196. properties: []
  197. })
  198. t.end()
  199. })
  200. test('object prototype names', function (t) {
  201. t.deepEqual(find(`
  202. var propertyIsEnumerable, hasOwnProperty
  203. isPrototypeOf
  204. `), {
  205. identifiers: ['isPrototypeOf'],
  206. properties: []
  207. })
  208. t.end()
  209. })
  210. test('labels', function (t) {
  211. t.deepEqual(find(`
  212. a: a;
  213. b: a;
  214. c: a;
  215. `), {
  216. identifiers: ['a'],
  217. properties: []
  218. })
  219. t.end()
  220. })
  221. test('property keys', function (t) {
  222. t.deepEqual(find(`
  223. ({ a: a,
  224. b: a, [d]: a,
  225. c: a, })
  226. `), {
  227. identifiers: ['a', 'd'],
  228. properties: []
  229. })
  230. t.end()
  231. })
  232. test('string property access', function (t) {
  233. t.deepEqual(find(`
  234. Buffer["isBuffer"]
  235. `), {
  236. identifiers: ['Buffer'],
  237. properties: ['Buffer.isBuffer']
  238. })
  239. t.end()
  240. })