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

This commit is contained in:
Anna Henningsen 2021-03-24 14:04:13 +01:00 committed by GitHub
parent 238ce0bac8
commit 1ef78ca55b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 4 deletions

View File

@ -105,14 +105,14 @@ class NodePath<T extends t.Node = t.Node> {
return this.isScope() ? new Scope(this) : scope; return this.isScope() ? new Scope(this) : scope;
} }
setData(key: string, val: any): any { setData(key: string | symbol, val: any): any {
if (this.data == null) { if (this.data == null) {
this.data = Object.create(null); this.data = Object.create(null);
} }
return (this.data[key] = val); return (this.data[key] = val);
} }
getData(key: string, def?: any): any { getData(key: string | symbol, def?: any): any {
if (this.data == null) { if (this.data == null) {
this.data = Object.create(null); this.data = Object.create(null);
} }

View File

@ -824,7 +824,7 @@ export default class Scope {
* Set some arbitrary data on the current 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); return (this.data[key] = val);
} }
@ -832,7 +832,7 @@ export default class Scope {
* Recursively walk up scope tree looking for the data `key`. * Recursively walk up scope tree looking for the data `key`.
*/ */
getData(key: string): any { getData(key: string | symbol): any {
let scope: Scope = this; let scope: Scope = this;
do { do {
const data = scope.data[key]; const data = scope.data[key];

View File

@ -39,5 +39,13 @@ describe("NodePath", () => {
expect(path.getData("__proto__", "test")).toBe("test"); 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);
});
}); });
}); });