Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@babel/traverse: Fix NodePath.getData (#9415)
* @babel/traverse: Fix NodePath.getData Currently, if the obtained value is `false`, it will be replaced by the given default value, which is invalid. This makes sure that we only set the default value when the value is `undefined`, instead of falsy. * Add test and fix object protoype * Allow false as default value
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); | ||
}); |