Skip to content

Commit

Permalink
[Fix] .has should not return true on a falsy node
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
ljharb committed Jan 16, 2025
1 parent cc24fd2 commit 462bb06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,10 @@ Traverse.prototype.get = function (ps) {
/** @type {(ps: PropertyKey[]) => boolean} */
Traverse.prototype.has = function (ps) {
var node = this.value;
// TODO: remove ps.length check
if (!node && ps.length > 0) {
return false;
}
for (var i = 0; node && i < ps.length; i++) {
var key = ps[i];
if (!hasOwnProperty.call(node, key) || (!this.options.includeSymbols && typeof key === 'symbol')) {
Expand Down
4 changes: 4 additions & 0 deletions test/has.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ test('has', function (t) {
t.equal(traverse(obj).has(['a']), true);
t.equal(traverse(obj).has(['a', 2]), false);

// TODO: change this to `false`
t.equal(traverse(undefined).has([]), true, 'traversing a non-object somehow returns true for no paths');
t.equal(traverse(undefined).has(['my', 'path']), false, 'traversing a non-object does not have any paths');

t.test('symbols', { skip: !v.hasSymbols }, function (st) {
/* eslint no-restricted-properties: 1 */
var globalSymbol = Symbol.for('d');
Expand Down

0 comments on commit 462bb06

Please sign in to comment.