Skip to content

chore: enable prefer-object-spread #9541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export default tseslint.config(
'operator-assignment': 'error',
'prefer-arrow-callback': 'error',
'prefer-const': 'error',
'prefer-object-spread': 'error',
'prefer-rest-params': 'error',

//
Expand Down
9 changes: 4 additions & 5 deletions packages/eslint-plugin/src/rules/ban-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,10 @@ export default createRule<Options, MessageIds>({
create(context, [options]) {
const extendDefaults = options.extendDefaults ?? true;
const customTypes = options.types ?? {};
const types = Object.assign(
{},
extendDefaults ? defaultTypes : {},
customTypes,
);
const types = {
...(extendDefaults && defaultTypes),
...customTypes,
};
const bannedTypes = new Map(
Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
);
Expand Down
37 changes: 21 additions & 16 deletions packages/eslint-plugin/src/rules/indent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,17 @@ export default createRule<Options, MessageIds>({
} as TSESTree.PropertyDefinition;
}

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

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

TSAsExpression(node: TSESTree.TSAsExpression) {
TSAsExpression(node: TSESTree.TSAsExpression): void {
// transform it to a BinaryExpression
return rules['BinaryExpression, LogicalExpression']({
type: AST_NODE_TYPES.BinaryExpression,
Expand All @@ -215,7 +216,7 @@ export default createRule<Options, MessageIds>({
});
},

TSConditionalType(node: TSESTree.TSConditionalType) {
TSConditionalType(node: TSESTree.TSConditionalType): void {
// transform it to a ConditionalExpression
return rules.ConditionalExpression({
type: AST_NODE_TYPES.ConditionalExpression,
Expand Down Expand Up @@ -245,7 +246,7 @@ export default createRule<Options, MessageIds>({

'TSEnumDeclaration, TSTypeLiteral'(
node: TSESTree.TSEnumDeclaration | TSESTree.TSTypeLiteral,
) {
): void {
// transform it to an ObjectExpression
return rules['ObjectExpression, ObjectPattern']({
type: AST_NODE_TYPES.ObjectExpression,
Expand All @@ -263,7 +264,9 @@ export default createRule<Options, MessageIds>({
});
},

TSImportEqualsDeclaration(node: TSESTree.TSImportEqualsDeclaration) {
TSImportEqualsDeclaration(
node: TSESTree.TSImportEqualsDeclaration,
): void {
// transform it to an VariableDeclaration
// use VariableDeclaration instead of ImportDeclaration because it's essentially the same thing
const { id, moduleReference } = node;
Expand Down Expand Up @@ -317,7 +320,7 @@ export default createRule<Options, MessageIds>({
});
},

TSIndexedAccessType(node: TSESTree.TSIndexedAccessType) {
TSIndexedAccessType(node: TSESTree.TSIndexedAccessType): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@typescript-eslint/explicit-function-return-type began reporting on these once we stopped using Object.assign.

// convert to a MemberExpression
return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
type: AST_NODE_TYPES.MemberExpression,
Expand All @@ -333,7 +336,7 @@ export default createRule<Options, MessageIds>({
});
},

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

'TSInterfaceDeclaration[extends.length > 0]'(
node: TSESTree.TSInterfaceDeclaration,
) {
): void {
// transform it to a ClassDeclaration
return rules[
'ClassDeclaration[superClass], ClassExpression[superClass]'
Expand All @@ -379,7 +382,7 @@ export default createRule<Options, MessageIds>({
});
},

TSMappedType(node: TSESTree.TSMappedType) {
TSMappedType(node: TSESTree.TSMappedType): void {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const squareBracketStart = context.sourceCode.getTokenBefore(
node.typeParameter,
Expand Down Expand Up @@ -423,7 +426,7 @@ export default createRule<Options, MessageIds>({
});
},

TSModuleBlock(node: TSESTree.TSModuleBlock) {
TSModuleBlock(node: TSESTree.TSModuleBlock): void {
// transform it to a BlockStatement
return rules['BlockStatement, ClassBody']({
type: AST_NODE_TYPES.BlockStatement,
Expand All @@ -436,7 +439,7 @@ export default createRule<Options, MessageIds>({
});
},

TSQualifiedName(node: TSESTree.TSQualifiedName) {
TSQualifiedName(node: TSESTree.TSQualifiedName): void {
return rules['MemberExpression, JSXMemberExpression, MetaProperty']({
type: AST_NODE_TYPES.MemberExpression,
object: node.left as any,
Expand All @@ -451,7 +454,7 @@ export default createRule<Options, MessageIds>({
});
},

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

TSTypeParameterDeclaration(node: TSESTree.TSTypeParameterDeclaration) {
TSTypeParameterDeclaration(
node: TSESTree.TSTypeParameterDeclaration,
): void {
if (!node.params.length) {
return;
}
Expand All @@ -487,6 +492,6 @@ export default createRule<Options, MessageIds>({
loc: node.loc,
});
},
});
};
},
});
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/rules/no-extra-parens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,6 @@ export default createRule<Options, MessageIds>({
}
},
};
return Object.assign({}, rules, overrides);
return { ...rules, ...overrides };
},
});
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/util/getFunctionHeadLoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export function getFunctionHeadLoc(
}

return {
start: Object.assign({}, start),
end: Object.assign({}, end),
start: { ...start },
end: { ...end },
};
}
6 changes: 3 additions & 3 deletions packages/parser/tests/test-utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const defaultConfig = {
tokens: true,
comment: true,
errorOnUnknownASTType: true,
sourceType: 'module',
sourceType: 'module' as const,
};

/**
Expand Down Expand Up @@ -40,7 +40,7 @@ export function createSnapshotTestBlock(
code: string,
config: ParserOptions = {},
): () => void {
config = Object.assign({}, defaultConfig, config);
config = { ...defaultConfig, ...config };

/**
* @returns the AST object
Expand Down Expand Up @@ -72,7 +72,7 @@ export function createSnapshotTestBlock(
* @param config The configuration object for the parser
*/
export function testServices(code: string, config: ParserOptions = {}): void {
config = Object.assign({}, defaultConfig, config);
config = { ...defaultConfig, ...config };

const services = parser.parseForESLint(code, config).services;
expect(services).toBeDefined();
Expand Down
24 changes: 11 additions & 13 deletions packages/rule-tester/src/RuleTester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,19 +399,17 @@ export class RuleTester extends TestFramework {
emitLegacyRuleAPIWarning(ruleName);
}

this.#linter.defineRule(
ruleName,
Object.assign({}, rule, {
// Create a wrapper rule that freezes the `context` properties.
create(context: RuleContext<MessageIds, Options>) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);

return (typeof rule === 'function' ? rule : rule.create)(context);
},
}),
);
this.#linter.defineRule(ruleName, {
...rule,
// Create a wrapper rule that freezes the `context` properties.
create(context: RuleContext<MessageIds, Options>) {
freezeDeeply(context.options);
freezeDeeply(context.settings);
freezeDeeply(context.parserOptions);

return (typeof rule === 'function' ? rule : rule.create)(context);
},
});

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

Expand Down
2 changes: 1 addition & 1 deletion packages/utils/tests/eslint-utils/deepMerge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('deepMerge', () => {
},
};

expect(ESLintUtils.deepMerge(a, b)).toStrictEqual(Object.assign({}, a, b));
expect(ESLintUtils.deepMerge(a, b)).toStrictEqual({ ...a, ...b });
});

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