fix: correctly transform __proto__ properties (#12664)

* fix: correctly transform `__proto__` properties

* fixup! fix: correctly transform `__proto__` properties
This commit is contained in:
ExE Boss 2021-01-22 10:58:33 +01:00 committed by GitHub
parent 2811b535d6
commit f1314a1683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 52 additions and 5 deletions

View File

@ -90,8 +90,6 @@ export default declare((api, options) => {
if (prop.kind === "get" || prop.kind === "set") { if (prop.kind === "get" || prop.kind === "set") {
pushMutatorDefine(info, prop); pushMutatorDefine(info, prop);
} else if (t.isStringLiteral(key, { value: "__proto__" })) {
pushAssign(objId, prop, body);
} else { } else {
if (computedProps.length === 1) { if (computedProps.length === 1) {
return t.callExpression(state.addHelper("defineProperty"), [ return t.callExpression(state.addHelper("defineProperty"), [

View File

@ -0,0 +1,11 @@
var shorthand = {
__proto__,
}
var method = {
__proto__() {}
}
var methodComputed = {
["__proto__"]() {}
}

View File

@ -0,0 +1,7 @@
{
"plugins": [
"external-helpers",
"transform-computed-properties",
"transform-shorthand-properties"
]
}

View File

@ -0,0 +1,3 @@
var shorthand = babelHelpers.defineProperty({}, "__proto__", __proto__);
var method = babelHelpers.defineProperty({}, "__proto__", function () {});
var methodComputed = babelHelpers.defineProperty({}, "__proto__", function () {});

View File

@ -0,0 +1,3 @@
var obj = {
['__proto__']: 123
};

View File

@ -0,0 +1 @@
var obj = babelHelpers.defineProperty({}, '__proto__', 123);

View File

@ -20,14 +20,25 @@ export default declare(api => {
); );
func.returnType = node.returnType; func.returnType = node.returnType;
const computedKey = t.toComputedKey(node);
if (t.isStringLiteral(computedKey, { value: "__proto__" })) {
path.replaceWith(t.objectProperty(computedKey, func, true));
} else {
path.replaceWith(t.objectProperty(node.key, func, node.computed)); path.replaceWith(t.objectProperty(node.key, func, node.computed));
} }
}
}, },
ObjectProperty({ node }) { ObjectProperty(path) {
const { node } = path;
if (node.shorthand) { if (node.shorthand) {
const computedKey = t.toComputedKey(node);
if (t.isStringLiteral(computedKey, { value: "__proto__" })) {
path.replaceWith(t.objectProperty(computedKey, node.value, true));
} else {
node.shorthand = false; node.shorthand = false;
} }
}
}, },
}, },
}; };

View File

@ -0,0 +1,7 @@
var shorthand = {
__proto__,
}
var method = {
__proto__() {}
}

View File

@ -0,0 +1,6 @@
var shorthand = {
["__proto__"]: __proto__
};
var method = {
["__proto__"]: function () {}
};