From cdabd70e000b2fa976c7f4118757736e023b93f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Fri, 18 May 2018 10:31:02 +0200 Subject: [PATCH] Add opts.wildcard --- README.md | 8 ++++++++ index.js | 11 +++++++++-- test/index.js | 13 +++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bfcb9a7..919abba 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,14 @@ Find undeclared identifiers and properties that are used in the `source` string. Set `opts.properties` to false to only return identifiers. +When `opts.wildcard` is true, unknown uses of undeclared identifiers will be added to `res.properties` as `'VarName.*'`. + +```js +undeclaredIdentifiers('Buffer(), Buffer.from()', { wildcard: true }) +// { identifiers: ['Buffer'], +// properties: ['Buffer.*', 'Buffer.from'] } +``` + ## License [Apache-2.0](LICENSE.md) diff --git a/index.js b/index.js index a869a44..dc34a70 100644 --- a/index.js +++ b/index.js @@ -51,6 +51,11 @@ var bindingVisitor = { } state.undeclared[node.name] = true + if (state.wildcard && + !(parent.type === 'VariableDeclarator' && parent.id === node) && + !(parent.type === 'AssignmentExpression' && parent.left === node)) { + state.undeclaredProps[node.name + '.*'] = true + } }, MemberExpression: function (node, state, ancestors) { if (!state.properties) return @@ -68,14 +73,16 @@ var bindingVisitor = { module.exports = function findUndeclared (src, opts) { opts = xtend({ identifiers: true, - properties: true + properties: true, + wildcard: false }, opts) var state = { undeclared: {}, undeclaredProps: {}, identifiers: opts.identifiers, - properties: opts.properties + properties: opts.properties, + wildcard: opts.wildcard } var ast = acorn.parse(src) diff --git a/test/index.js b/test/index.js index 4a6b245..4c7279a 100644 --- a/test/index.js +++ b/test/index.js @@ -23,6 +23,19 @@ test('undeclared properties', function (t) { t.end() }) +test('wildcard use of undeclared name', function (t) { + t.deepEqual(find(` + function func () {} + new A() + A.from() + func(b) + `, { wildcard: true }), { + identifiers: ['A', 'b'], + properties: ['A.*', 'A.from', 'b.*'] + }) + t.end() +}) + test('function names', function (t) { t.deepEqual(find(` function x () {