Skip to content

Commit

Permalink
Expand type definitions for path.{get,set}Data to cover symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Mar 23, 2021
1 parent 6ee87ee commit 17bef52
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
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);
});
});
});

0 comments on commit 17bef52

Please sign in to comment.