Skip to content

Commit 22b09f5

Browse files
authored
fix: ignore __proto__ properties in prefer-object-spread (#21311)
* fix: ignore `__proto__` properties in `prefer-object-spread` * clarify isProtoProperty description * check `__proto__` properties in source arguments only
1 parent 24310e3 commit 22b09f5

2 files changed

Lines changed: 135 additions & 1 deletion

File tree

‎lib/rules/prefer-object-spread.js‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
const { CALL, ReferenceTracker } = require("@eslint-community/eslint-utils");
99
const {
10+
getStaticPropertyName,
1011
isCommaToken,
1112
isOpeningParenToken,
1213
isClosingParenToken,
@@ -57,6 +58,48 @@ function hasArgumentsWithAccessors(node) {
5758
.some(hasAccessors);
5859
}
5960

61+
/**
62+
* Determines whether the given node is a property with a `__proto__` key.
63+
* Such a property in a source object can't be inlined into the target literal, because
64+
* `__proto__` behaves differently in the two positions, and in opposite directions:
65+
* - A `__proto__:` initializer creates no own property, so `Object.assign` doesn't copy it
66+
* and the prototype is unaffected. Inlined into an object literal, it sets the prototype.
67+
* - Any other form (computed, shorthand, or method) creates an own property, which
68+
* `Object.assign` copies with `[[Set]]`, triggering the `__proto__` setter and changing
69+
* the target's prototype. Inlined into an object literal, it is an ordinary own property.
70+
* @param {ASTNode} node Node to check.
71+
* @returns {boolean} `true` if the node is a property named `__proto__`.
72+
*/
73+
function isProtoProperty(node) {
74+
return (
75+
node.type === "Property" && getStaticPropertyName(node) === "__proto__"
76+
);
77+
}
78+
79+
/**
80+
* Determines whether the given object expression node has a `__proto__` property.
81+
* @param {ASTNode} node `ObjectExpression` node to check.
82+
* @returns {boolean} `true` if the node has at least one `__proto__` property.
83+
*/
84+
function hasProtoProperty(node) {
85+
return node.properties.some(isProtoProperty);
86+
}
87+
88+
/**
89+
* Determines whether the given call expression node has object expression source arguments (all but the first) with `__proto__` properties.
90+
* The first argument is not checked. It is the target of the assignment, and its properties are
91+
* inlined into the resulting object literal with the same meaning they already had. Properties of
92+
* the other arguments are copied by `Object.assign` with `[[Set]]`, and that is the step the autofix removes.
93+
* @param {ASTNode} node `CallExpression` node to check.
94+
* @returns {boolean} `true` if the node has at least one source argument that is an object expression with a `__proto__` property.
95+
*/
96+
function hasArgumentsWithProtoProperty(node) {
97+
return node.arguments
98+
.slice(1)
99+
.filter(arg => arg.type === "ObjectExpression")
100+
.some(hasProtoProperty);
101+
}
102+
60103
/**
61104
* Helper that checks if the node needs parentheses to be valid JS.
62105
* The default is to wrap the node in parentheses to avoid parsing errors.
@@ -309,7 +352,8 @@ module.exports = {
309352
!hasArraySpread(refNode) &&
310353
!(
311354
refNode.arguments.length > 1 &&
312-
hasArgumentsWithAccessors(refNode)
355+
(hasArgumentsWithAccessors(refNode) ||
356+
hasArgumentsWithProtoProperty(refNode))
313357
)
314358
) {
315359
const messageId =

‎tests/lib/rules/prefer-object-spread.js‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ ruleTester.run("prefer-object-spread", rule, {
9898
"Object.assign({}, { set a(val) {} })",
9999
"Object.assign({}, { foo: 'bar', get a() {} }, {})",
100100
"Object.assign({ foo }, bar, {}, { baz: 'quux', set a(val) {}, quuux }, {})",
101+
102+
// ignore Object.assign() with > 1 arguments if any of the source arguments is an object expression with a `__proto__` property
103+
"Object.assign({}, { __proto__: proto })",
104+
'Object.assign({}, { "__proto__": proto })',
105+
'Object.assign({}, { ["__proto__"]: proto })',
106+
"Object.assign({}, { __proto__() {} })",
107+
"Object.assign({}, { __proto__ })",
108+
'Object.assign({}, foo, { ["__proto__"]: proto })',
109+
"Object.assign({ __proto__: proto }, { __proto__: other })",
101110
],
102111

103112
invalid: [
@@ -987,6 +996,87 @@ ruleTester.run("prefer-object-spread", rule, {
987996
],
988997
},
989998

999+
// report Object.assign() with a `__proto__` property if the function call has only 1 argument
1000+
{
1001+
code: "Object.assign({ __proto__: proto })",
1002+
output: "({__proto__: proto})",
1003+
errors: [
1004+
{
1005+
messageId: "useLiteralMessage",
1006+
line: 1,
1007+
column: 1,
1008+
},
1009+
],
1010+
},
1011+
{
1012+
code: 'Object.assign({ ["__proto__"]: proto })',
1013+
output: '({["__proto__"]: proto})',
1014+
errors: [
1015+
{
1016+
messageId: "useLiteralMessage",
1017+
line: 1,
1018+
column: 1,
1019+
},
1020+
],
1021+
},
1022+
1023+
// report Object.assign() with a `__proto__` property in the first argument (the target), as its meaning doesn't change
1024+
{
1025+
code: "Object.assign({ __proto__: proto }, foo)",
1026+
output: "({__proto__: proto, ...foo})",
1027+
errors: [
1028+
{
1029+
messageId: "useSpreadMessage",
1030+
line: 1,
1031+
column: 1,
1032+
},
1033+
],
1034+
},
1035+
{
1036+
code: 'Object.assign({ "__proto__": proto }, foo)',
1037+
output: '({"__proto__": proto, ...foo})',
1038+
errors: [
1039+
{
1040+
messageId: "useSpreadMessage",
1041+
line: 1,
1042+
column: 1,
1043+
},
1044+
],
1045+
},
1046+
{
1047+
code: 'Object.assign({ ["__proto__"]: proto }, { a: 1 })',
1048+
output: '({["__proto__"]: proto, a: 1})',
1049+
errors: [
1050+
{
1051+
messageId: "useSpreadMessage",
1052+
line: 1,
1053+
column: 1,
1054+
},
1055+
],
1056+
},
1057+
{
1058+
code: "Object.assign({ __proto__ }, foo)",
1059+
output: "({__proto__, ...foo})",
1060+
errors: [
1061+
{
1062+
messageId: "useSpreadMessage",
1063+
line: 1,
1064+
column: 1,
1065+
},
1066+
],
1067+
},
1068+
{
1069+
code: "Object.assign({ __proto__() {} }, { a: 1 })",
1070+
output: "({__proto__() {}, a: 1})",
1071+
errors: [
1072+
{
1073+
messageId: "useSpreadMessage",
1074+
line: 1,
1075+
column: 1,
1076+
},
1077+
],
1078+
},
1079+
9901080
// https://github.com/eslint/eslint/issues/13058
9911081
{
9921082
code: "const obj = Object.assign<{}, Record<string, string[]>>({}, getObject());",

0 commit comments

Comments
 (0)