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

Expand type definitions for path.{get,set}Data to cover symbols #13044

Merged
merged 1 commit into from Mar 24, 2021
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.ts
Expand Up @@ -105,14 +105,14 @@ class NodePath<T extends t.Node = t.Node> {
return this.isScope() ? new Scope(this) : scope;
}

setData(key: string, val: any): any {
setData(key: string | symbol, val: any): any {
if (this.data == null) {
this.data = Object.create(null);
}
return (this.data[key] = val);
}

getData(key: string, def?: any): any {
getData(key: string | symbol, def?: any): any {
if (this.data == null) {
this.data = Object.create(null);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-traverse/src/scope/index.ts
Expand Up @@ -824,15 +824,15 @@ export default class Scope {
* Set some arbitrary data on the current scope.
*/

setData(key: string, val: any) {
setData(key: string | symbol, val: any) {
return (this.data[key] = val);
}

/**
* Recursively walk up scope tree looking for the data `key`.
*/

getData(key: string): any {
getData(key: string | symbol): any {
let scope: Scope = this;
do {
const data = scope.data[key];
Expand Down
8 changes: 8 additions & 0 deletions packages/babel-traverse/test/path/index.js
Expand Up @@ -39,5 +39,13 @@ describe("NodePath", () => {

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

it("can use symbols as keys", () => {
const path = new NodePath({}, {});
const symbol = Symbol("foo");
path.setData(symbol, 42);

expect(path.getData(symbol)).toBe(42);
});
});
});