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

@babel/traverse: Fix NodePath.getData #9415

Merged
merged 3 commits into from Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/babel-traverse/src/path/index.js
Expand Up @@ -28,7 +28,7 @@ export default class NodePath {
this.parent = parent;
this.hub = hub;
this.contexts = [];
this.data = {};
this.data = Object.create(null);
this.shouldSkip = false;
this.shouldStop = false;
this.removed = false;
Expand Down Expand Up @@ -116,7 +116,7 @@ export default class NodePath {

getData(key: string, def?: any): any {
let val = this.data[key];
if (!val && def) val = this.data[key] = def;
if (val === undefined && def !== undefined) val = this.data[key] = def;
return val;
}

Expand Down
43 changes: 43 additions & 0 deletions packages/babel-traverse/test/path/index.js
@@ -0,0 +1,43 @@
import { NodePath } from "../../lib";

describe("NodePath", () => {
describe("setData/getData", () => {
it("can set default value", () => {
const path = new NodePath({}, {});

expect(path.getData("foo", "test")).toBe("test");
});
it("can set false", () => {
const path = new NodePath({}, {});
path.setData("foo", false);

expect(path.getData("foo", true)).toBe(false);
});

it("can set true", () => {
const path = new NodePath({}, {});
path.setData("foo", true);

expect(path.getData("foo", false)).toBe(true);
});

it("can set null", () => {
const path = new NodePath({}, {});
path.setData("foo", null);

expect(path.getData("foo", true)).toBe(null);
});

it("can use false as default", () => {
const path = new NodePath({}, {});

expect(path.getData("foo", false)).toBe(false);
});

it("does not use object base properties", () => {
const path = new NodePath({}, {});

expect(path.getData("__proto__", "test")).toBe("test");
});
});
});