Skip to content

Commit 58be99f

Browse files
chore: enable prefer-object-spread (typescript-eslint#9541)
* prefer-object-spread * as const --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 5d895b7 commit 58be99f

File tree

8 files changed

+44
-41
lines changed

8 files changed

+44
-41
lines changed

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export default tseslint.config(
216216
'operator-assignment': 'error',
217217
'prefer-arrow-callback': 'error',
218218
'prefer-const': 'error',
219+
'prefer-object-spread': 'error',
219220
'prefer-rest-params': 'error',
220221

221222
//

packages/eslint-plugin/src/rules/ban-types.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,10 @@ export default createRule<Options, MessageIds>({
207207
create(context, [options]) {
208208
const extendDefaults = options.extendDefaults ?? true;
209209
const customTypes = options.types ?? {};
210-
const types = Object.assign(
211-
{},
212-
extendDefaults ? defaultTypes : {},
213-
customTypes,
214-
);
210+
const types = {
211+
...(extendDefaults && defaultTypes),
212+
...customTypes,
213+
};
215214
const bannedTypes = new Map(
216215
Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
217216
);

packages/eslint-plugin/src/rules/indent.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -181,16 +181,17 @@ export default createRule<Options, MessageIds>({
181181
} as TSESTree.PropertyDefinition;
182182
}
183183

184-
return Object.assign({}, rules, {
184+
return {
185+
...rules,
185186
// overwrite the base rule here so we can use our KNOWN_NODES list instead
186-
'*:exit'(node: TSESTree.Node) {
187+
'*:exit'(node: TSESTree.Node): void {
187188
// For nodes we care about, skip the default handling, because it just marks the node as ignored...
188189
if (!KNOWN_NODES.has(node.type)) {
189190
rules['*:exit'](node);
190191
}
191192
},
192193

193-
VariableDeclaration(node: TSESTree.VariableDeclaration) {
194+
VariableDeclaration(node: TSESTree.VariableDeclaration): void {
194195
// https://github.com/typescript-eslint/typescript-eslint/issues/441
195196
if (node.declarations.length === 0) {
196197
return;
@@ -199,7 +200,7 @@ export default createRule<Options, MessageIds>({
199200
return rules.VariableDeclaration(node);
200201
},
201202

202-
TSAsExpression(node: TSESTree.TSAsExpression) {
203+
TSAsExpression(node: TSESTree.TSAsExpression): void {
203204
// transform it to a BinaryExpression
204205
return rules['BinaryExpression, LogicalExpression']({
205206
type: AST_NODE_TYPES.BinaryExpression,
@@ -215,7 +216,7 @@ export default createRule<Options, MessageIds>({
215216
});
216217
},
217218

218-
TSConditionalType(node: TSESTree.TSConditionalType) {
219+
TSConditionalType(node: TSESTree.TSConditionalType): void {
219220
// transform it to a ConditionalExpression
220221
return rules.ConditionalExpression({
221222
type: AST_NODE_TYPES.ConditionalExpression,
@@ -245,7 +246,7 @@ export default createRule<Options, MessageIds>({
245246

246247
'TSEnumDeclaration, TSTypeLiteral'(
247248
node: TSESTree.TSEnumDeclaration | TSESTree.TSTypeLiteral,
248-
) {
249+
): void {
249250
// transform it to an ObjectExpression
250251
return rules['ObjectExpression, ObjectPattern']({
251252
type: AST_NODE_TYPES.ObjectExpression,
@@ -263,7 +264,9 @@ export default createRule<Options, MessageIds>({
263264
});
264265
},
265266

266-
TSImportEqualsDeclaration(node: TSESTree.TSImportEqualsDeclaration) {
267+
TSImportEqualsDeclaration(
268+
node: TSESTree.TSImportEqualsDeclaration,
269+
): void {
267270
// transform it to an VariableDeclaration
268271
// use VariableDeclaration instead of ImportDeclaration because it's essentially the same thing
269272
const { id, moduleReference } = node;
@@ -317,7 +320,7 @@ export default createRule<Options, MessageIds>({
317320
});
318321
},
319322

320-
TSIndexedAccessType(node: TSESTree.TSIndexedAccessType) {
323+
TSIndexedAccessType(node: TSESTree.TSIndexedAccessType): void {
321324
// convert to a MemberExpression
322325
return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
323326
type: AST_NODE_TYPES.MemberExpression,
@@ -333,7 +336,7 @@ export default createRule<Options, MessageIds>({
333336
});
334337
},
335338

336-
TSInterfaceBody(node: TSESTree.TSInterfaceBody) {
339+
TSInterfaceBody(node: TSESTree.TSInterfaceBody): void {
337340
// transform it to an ClassBody
338341
return rules['BlockStatement, ClassBody']({
339342
type: AST_NODE_TYPES.ClassBody,
@@ -354,7 +357,7 @@ export default createRule<Options, MessageIds>({
354357

355358
'TSInterfaceDeclaration[extends.length > 0]'(
356359
node: TSESTree.TSInterfaceDeclaration,
357-
) {
360+
): void {
358361
// transform it to a ClassDeclaration
359362
return rules[
360363
'ClassDeclaration[superClass], ClassExpression[superClass]'
@@ -379,7 +382,7 @@ export default createRule<Options, MessageIds>({
379382
});
380383
},
381384

382-
TSMappedType(node: TSESTree.TSMappedType) {
385+
TSMappedType(node: TSESTree.TSMappedType): void {
383386
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
384387
const squareBracketStart = context.sourceCode.getTokenBefore(
385388
node.typeParameter,
@@ -423,7 +426,7 @@ export default createRule<Options, MessageIds>({
423426
});
424427
},
425428

426-
TSModuleBlock(node: TSESTree.TSModuleBlock) {
429+
TSModuleBlock(node: TSESTree.TSModuleBlock): void {
427430
// transform it to a BlockStatement
428431
return rules['BlockStatement, ClassBody']({
429432
type: AST_NODE_TYPES.BlockStatement,
@@ -436,7 +439,7 @@ export default createRule<Options, MessageIds>({
436439
});
437440
},
438441

439-
TSQualifiedName(node: TSESTree.TSQualifiedName) {
442+
TSQualifiedName(node: TSESTree.TSQualifiedName): void {
440443
return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
441444
type: AST_NODE_TYPES.MemberExpression,
442445
object: node.left as any,
@@ -451,7 +454,7 @@ export default createRule<Options, MessageIds>({
451454
});
452455
},
453456

454-
TSTupleType(node: TSESTree.TSTupleType) {
457+
TSTupleType(node: TSESTree.TSTupleType): void {
455458
// transform it to an ArrayExpression
456459
return rules['ArrayExpression, ArrayPattern']({
457460
type: AST_NODE_TYPES.ArrayExpression,
@@ -464,7 +467,9 @@ export default createRule<Options, MessageIds>({
464467
});
465468
},
466469

467-
TSTypeParameterDeclaration(node: TSESTree.TSTypeParameterDeclaration) {
470+
TSTypeParameterDeclaration(
471+
node: TSESTree.TSTypeParameterDeclaration,
472+
): void {
468473
if (!node.params.length) {
469474
return;
470475
}
@@ -487,6 +492,6 @@ export default createRule<Options, MessageIds>({
487492
loc: node.loc,
488493
});
489494
},
490-
});
495+
};
491496
},
492497
});

packages/eslint-plugin/src/rules/no-extra-parens.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,6 @@ export default createRule<Options, MessageIds>({
303303
}
304304
},
305305
};
306-
return Object.assign({}, rules, overrides);
306+
return { ...rules, ...overrides };
307307
},
308308
});

packages/eslint-plugin/src/util/getFunctionHeadLoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export function getFunctionHeadLoc(
197197
}
198198

199199
return {
200-
start: Object.assign({}, start),
201-
end: Object.assign({}, end),
200+
start: { ...start },
201+
end: { ...end },
202202
};
203203
}

packages/parser/tests/test-utils/test-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const defaultConfig = {
1010
tokens: true,
1111
comment: true,
1212
errorOnUnknownASTType: true,
13-
sourceType: 'module',
13+
sourceType: 'module' as const,
1414
};
1515

1616
/**
@@ -40,7 +40,7 @@ export function createSnapshotTestBlock(
4040
code: string,
4141
config: ParserOptions = {},
4242
): () => void {
43-
config = Object.assign({}, defaultConfig, config);
43+
config = { ...defaultConfig, ...config };
4444

4545
/**
4646
* @returns the AST object
@@ -72,7 +72,7 @@ export function createSnapshotTestBlock(
7272
* @param config The configuration object for the parser
7373
*/
7474
export function testServices(code: string, config: ParserOptions = {}): void {
75-
config = Object.assign({}, defaultConfig, config);
75+
config = { ...defaultConfig, ...config };
7676

7777
const services = parser.parseForESLint(code, config).services;
7878
expect(services).toBeDefined();

packages/rule-tester/src/RuleTester.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,19 +399,17 @@ export class RuleTester extends TestFramework {
399399
emitLegacyRuleAPIWarning(ruleName);
400400
}
401401

402-
this.#linter.defineRule(
403-
ruleName,
404-
Object.assign({}, rule, {
405-
// Create a wrapper rule that freezes the `context` properties.
406-
create(context: RuleContext<MessageIds, Options>) {
407-
freezeDeeply(context.options);
408-
freezeDeeply(context.settings);
409-
freezeDeeply(context.parserOptions);
410-
411-
return (typeof rule === 'function' ? rule : rule.create)(context);
412-
},
413-
}),
414-
);
402+
this.#linter.defineRule(ruleName, {
403+
...rule,
404+
// Create a wrapper rule that freezes the `context` properties.
405+
create(context: RuleContext<MessageIds, Options>) {
406+
freezeDeeply(context.options);
407+
freezeDeeply(context.settings);
408+
freezeDeeply(context.parserOptions);
409+
410+
return (typeof rule === 'function' ? rule : rule.create)(context);
411+
},
412+
});
415413

416414
this.#linter.defineRules(this.#rules);
417415

packages/utils/tests/eslint-utils/deepMerge.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('deepMerge', () => {
3838
},
3939
};
4040

41-
expect(ESLintUtils.deepMerge(a, b)).toStrictEqual(Object.assign({}, a, b));
41+
expect(ESLintUtils.deepMerge(a, b)).toStrictEqual({ ...a, ...b });
4242
});
4343

4444
it('deeply overwrites properties in the first one with the second', () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy