Skip to content

Commit

Permalink
Add opts.wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed May 18, 2018
1 parent fa8613e commit cdabd70
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit cdabd70

Please sign in to comment.