Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Fix FP S2428 (prefer-object-literal): Ignore circular reference assignments #427

Merged
merged 2 commits into from
Nov 3, 2023
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: 0 additions & 1 deletion ruling/snapshots/prefer-object-literal
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ src/brackets/src/extensibility/ExtensionManager.js: 313
src/brackets/src/extensions/default/HealthData/HealthDataManager.js: 53
src/brackets/src/extensions/default/InlineColorEditor/ColorEditor.js: 557,570,582
src/brackets/src/extensions/default/JavaScriptRefactoring/RefactoringUtils.js: 365
src/jest/packages/pretty-format/perf/test.js: 182
src/jquery/external/sinon/sinon.js: 4500
src/reveal.js/plugin/search/search.js: 165
src/spectrum/api/models/user.js: 407
Expand Down
29 changes: 20 additions & 9 deletions src/rules/prefer-object-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ function checkObjectInitialization(
const objectDeclaration = getObjectDeclaration(statements[index]);
// eslint-disable-next-line sonarjs/no-collapsible-if
if (objectDeclaration && isIdentifier(objectDeclaration.id)) {
if (
isPropertyAssignment(statements[index + 1], objectDeclaration.id, context.getSourceCode())
) {
const nextStmt = statements[index + 1];
if (isPropertyAssignment(nextStmt, objectDeclaration.id, context.getSourceCode())) {
context.report({ messageId: 'declarePropertiesInsideObject', node: objectDeclaration });
}
}
Expand Down Expand Up @@ -103,17 +102,29 @@ function isPropertyAssignment(
return (
!left.computed &&
isSingleLineExpression(right, sourceCode) &&
areEquivalent(left.object, objectIdentifier, sourceCode)
areEquivalent(left.object, objectIdentifier, sourceCode) &&
!isCircularReference(left, right, sourceCode)
);
}
}
return false;
}

function isSingleLineExpression(expression: TSESTree.Expression, sourceCode: TSESLint.SourceCode) {
const first = sourceCode.getFirstToken(expression)!.loc;
const last = sourceCode.getLastToken(expression)!.loc;
return first.start.line === last.end.line;
function isSingleLineExpression(
expression: TSESTree.Expression,
sourceCode: TSESLint.SourceCode,
) {
const first = sourceCode.getFirstToken(expression)!.loc;
const last = sourceCode.getLastToken(expression)!.loc;
return first.start.line === last.end.line;
}

function isCircularReference(
left: TSESTree.MemberExpression,
right: TSESTree.Expression,
sourceCode: TSESLint.SourceCode,
) {
return areEquivalent(left.object, right, sourceCode);
}
}

export = rule;
5 changes: 5 additions & 0 deletions tests/rules/prefer-object-literal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ ruleTester.run('prefer-literal', rule, {
{
code: `var x = {a: 2}; x.b = 5;`,
},
{
code: `
const O = {};
O.self = O;`,
},
],
invalid: [
{
Expand Down