Support objects from other contexts in t.valueToNode (#13275)
This commit is contained in:
parent
4c1b8cc751
commit
4c725f8cf8
@ -40,11 +40,18 @@ function isRegExp(value): value is RegExp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function isPlainObject(value): value is object {
|
function isPlainObject(value): value is object {
|
||||||
if (typeof value !== "object" || value === null) {
|
if (
|
||||||
|
typeof value !== "object" ||
|
||||||
|
value === null ||
|
||||||
|
Object.prototype.toString.call(value) !== "[object Object]"
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const proto = Object.getPrototypeOf(value);
|
const proto = Object.getPrototypeOf(value);
|
||||||
return proto === null || proto === Object.prototype;
|
// Object.prototype's __proto__ is null. Every other class's __proto__.__proto__ is
|
||||||
|
// not null by default. We cannot check if proto === Object.prototype because it
|
||||||
|
// could come from another realm.
|
||||||
|
return proto === null || Object.getPrototypeOf(proto) === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function valueToNode(value: unknown): t.Expression {
|
function valueToNode(value: unknown): t.Expression {
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import * as t from "../lib";
|
import * as t from "../lib";
|
||||||
|
import * as vm from "vm";
|
||||||
|
|
||||||
describe("regressions", () => {
|
describe("regressions", () => {
|
||||||
const babel7 = process.env.BABEL_TYPES_8_BREAKING ? it.skip : it;
|
const babel7 = process.env.BABEL_TYPES_8_BREAKING ? it.skip : it;
|
||||||
@ -8,4 +9,23 @@ describe("regressions", () => {
|
|||||||
t.file(t.program([]), [{ type: "Line" }]);
|
t.file(t.program([]), [{ type: "Line" }]);
|
||||||
}).not.toThrow();
|
}).not.toThrow();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it(".valueToNode works with objects from different contexts", () => {
|
||||||
|
const context = {};
|
||||||
|
const script = new vm.Script(`this.obj = { foo: 2 }`);
|
||||||
|
script.runInNewContext(context);
|
||||||
|
|
||||||
|
expect(t.valueToNode(context.obj)).toMatchObject({
|
||||||
|
properties: [
|
||||||
|
{
|
||||||
|
computed: false,
|
||||||
|
key: { name: "foo", type: "Identifier" },
|
||||||
|
shorthand: false,
|
||||||
|
type: "ObjectProperty",
|
||||||
|
value: { type: "NumericLiteral", value: 2 },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
type: "ObjectExpression",
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user