Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Traverse.has always return true if the node is undefined #20

Closed
jfmmm opened this issue Jan 15, 2025 · 1 comment
Closed

Traverse.has always return true if the node is undefined #20

jfmmm opened this issue Jan 15, 2025 · 1 comment

Comments

@jfmmm
Copy link

jfmmm commented Jan 15, 2025

A bit of an edge case, but we were passing an object to traverse that could possibly be undefined, then using has to check if the object contained or not a path. Then if it returns false we would do something else.

It worked fine in 0.6.8, but since we updated has always returned true in those cases, which obviously seems wrong.

I tracked it to here where node && was added in the for loop, causing it to always return true when the node is undefined.

/** @type {(ps: PropertyKey[]) => boolean} */
Traverse.prototype.has = function (ps) {
	var node = this.value;
	for (var i = 0; node && i < ps.length; i++) {
		var key = ps[i];
		if (!hasOwnProperty.call(node, key) || (!this.options.includeSymbols && typeof key === 'symbol')) {
			return false;
		}
		node = node[key];
	}
	return true;
};

Here an example

import traverse from "traverse";

const tr = traverse(undefined);
const hasPath = tr.has(['my', 'path']);

console.log(hasPath); // return true

I'm gonna fix it on our side since there's no point in even calling traverse in those cases, but felt I should report it anyway.

@ljharb
Copy link
Owner

ljharb commented Jan 16, 2025

require('.')(undefined).has([]) seems to return true for me in every traverse version, including v0.6.8 - however, require('.')(undefined).has(['my', 'path']) indeed returns false until v0.6.9.

Looks like this broke in #6. I'll fix it ASAP.

@ljharb ljharb closed this as completed in 462bb06 Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants