Skip to content

Commit 0030c30

Browse files
authored
Don't treat spreading an array of objects into Object.assign() as an error (#91)
1 parent a98bb70 commit 0030c30

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

src/rules/prefer-spread-syntax.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ ruleTester.run('prefer-spread-syntax (untyped)', preferSpreadSyntax as never, {
5959
'Object.assign({__proto__: null}, obj);',
6060
'Object.assign({__proto__: proto, foo: 1}, obj);',
6161

62+
// Object.assign with spread element argument — cannot be safely converted
63+
// Object.assign({}, ...objs) !== {...objs} (the latter spreads the array as an object)
64+
'Object.assign({}, ...objs);',
65+
'Object.assign({}, ...objs, extra);',
66+
6267
// apply with context
6368
'fn.apply(context, args);',
6469
'obj.method.apply(obj, args);',

src/rules/prefer-spread-syntax.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,17 @@ export const preferSpreadSyntax: TSESLint.RuleModule<MessageIds, []> = {
120120
);
121121

122122
if (!hasUnquotedProto) {
123-
const spreadArgs = node.arguments
124-
.slice(1)
123+
const restArgs = node.arguments.slice(1);
124+
125+
// If any argument is already a spread element (e.g. ...objs),
126+
// we can't safely convert since Object.assign({}, ...objs) spreads
127+
// array elements as individual arguments, which has no simple
128+
// equivalent in object spread syntax.
129+
if (restArgs.some((arg) => arg.type === 'SpreadElement')) {
130+
return;
131+
}
132+
133+
const spreadArgs = restArgs
125134
.map((arg) => `...${sourceCode.getText(arg)}`)
126135
.join(', ');
127136

0 commit comments

Comments
 (0)