123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- var test = require('tape')
- var acorn = require('acorn-node')
- var find = require('../')
- test('undeclared variables', function (t) {
- t.deepEqual(find(`
- var a, b
- a, b, c
- `), {
- identifiers: ['c'],
- properties: []
- })
- t.end()
- })
- test('pass in a parsed ast', function (t) {
- t.deepEqual(find(acorn.parse(`
- var a, b
- a, b, c
- `)), {
- identifiers: ['c'],
- properties: []
- })
- t.end()
- })
- test('undeclared properties', function (t) {
- t.deepEqual(find(`
- var a, b
- a, b, c, d.e
- `), {
- identifiers: ['c', 'd'],
- properties: ['d.e']
- })
- t.end()
- })
- test('wildcard use of undeclared name', function (t) {
- t.deepEqual(find(`
- function func () {}
- new A()
- A.from()
- func(b)
- C.from()
- `, { wildcard: true }), {
- identifiers: ['A', 'b', 'C'],
- properties: ['A.*', 'A.from', 'b.*', 'C.from']
- })
- t.end()
- })
- test('wildcard use of undeclared name (different order)', function (t) {
- t.deepEqual(find(`
- A.from()
- new A()
- `, { wildcard: true }), {
- identifiers: ['A'],
- properties: ['A.from', 'A.*']
- })
- t.end()
- })
- test('function names', function (t) {
- t.deepEqual(find(`
- function x () {
- return x
- }
- x()
- `), {
- identifiers: [],
- properties: []
- })
- t.deepEqual(find(`
- function x () {
- return x
- }
- y()
- `), {
- identifiers: ['y'],
- properties: []
- })
- t.end()
- })
- test('class names', function (t) {
- t.deepEqual(find(`
- class X {}
- new X()
- `), {
- identifiers: [],
- properties: []
- })
- t.deepEqual(find(`
- class X extends Y {}
- new X()
- `), {
- identifiers: ['Y'],
- properties: []
- })
- t.deepEqual(find(`
- class Y {}
- class X extends Y {}
- new X()
- `), {
- identifiers: [],
- properties: []
- })
- t.end()
- })
- test('class methods', function (t) {
- t.deepEqual(find(`
- class X {
- constructor() { u }
- bar() { v }
- static foo() { w }
- }
- `), {
- identifiers: ['u', 'v', 'w'],
- properties: []
- })
- t.end()
- })
- test('super', function (t) {
- t.deepEqual(find(`
- class X extends Y {
- constructor() { super() }
- }
- `), {
- identifiers: ['Y'],
- properties: []
- })
- t.deepEqual(find(`
- class X {
- foo() { super.foo }
- }
- `), {
- identifiers: [],
- properties: []
- })
- t.end()
- })
- test('scope', function (t) {
- t.deepEqual(find(`
- function y () {
- function x () {
- return x
- }
- }
- x(y(x.y))
- `), {
- identifiers: ['x'],
- properties: ['x.y']
- })
- t.end()
- })
- test('block scope', function (t) {
- t.deepEqual(find(`
- var x
- { var y; let z }
- x, y, z
- `), {
- identifiers: ['z'],
- properties: []
- })
- t.end()
- })
- test('function parameters', function (t) {
- t.deepEqual(find(`
- function a (a1, a2) { a, a1, a2, a3 }
- ;(function b (b1, b2) { b, b1, b2, b3 })
- ;((c1, c2) => { c1, c2, c3 })
- `), {
- identifiers: ['a3', 'b3', 'c3'],
- properties: []
- })
- t.deepEqual(find(`
- function a (a1, a2) { }
- a, a1, a2
- `), {
- identifiers: ['a1', 'a2'],
- properties: []
- })
- t.end()
- })
- test('assignment', function (t) {
- t.deepEqual(find(`
- var a
- b = 2
- b.c = 3
- `), {
- identifiers: ['b'],
- properties: ['b.c']
- })
- t.end()
- })
- test('catch', function (t) {
- t.deepEqual(find(`
- try { var a } catch (err) { err }
- try { let b } catch (orr) { orr }
- a, b
- `), {
- identifiers: ['b'],
- properties: []
- })
- t.end()
- })
- test('object prototype names', function (t) {
- t.deepEqual(find(`
- var propertyIsEnumerable, hasOwnProperty
- isPrototypeOf
- `), {
- identifiers: ['isPrototypeOf'],
- properties: []
- })
- t.end()
- })
- test('labels', function (t) {
- t.deepEqual(find(`
- a: a;
- b: a;
- c: a;
- `), {
- identifiers: ['a'],
- properties: []
- })
- t.end()
- })
- test('property keys', function (t) {
- t.deepEqual(find(`
- ({ a: a,
- b: a, [d]: a,
- c: a, })
- `), {
- identifiers: ['a', 'd'],
- properties: []
- })
- t.end()
- })
- test('string property access', function (t) {
- t.deepEqual(find(`
- Buffer["isBuffer"]
- `), {
- identifiers: ['Buffer'],
- properties: ['Buffer.isBuffer']
- })
- t.end()
- })
|