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

Commit 7808c0d

Browse files
Fix FP S2428 (prefer-object-literal): Ignore circular reference assignments (#427)
Co-authored-by: Ilia Kebets <[email protected]>
1 parent 75f01d8 commit 7808c0d

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

ruling/snapshots/prefer-object-literal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ src/brackets/src/extensibility/ExtensionManager.js: 313
99
src/brackets/src/extensions/default/HealthData/HealthDataManager.js: 53
1010
src/brackets/src/extensions/default/InlineColorEditor/ColorEditor.js: 557,570,582
1111
src/brackets/src/extensions/default/JavaScriptRefactoring/RefactoringUtils.js: 365
12-
src/jest/packages/pretty-format/perf/test.js: 182
1312
src/jquery/external/sinon/sinon.js: 4500
1413
src/reveal.js/plugin/search/search.js: 165
1514
src/spectrum/api/models/user.js: 407

src/rules/prefer-object-literal.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ function checkObjectInitialization(
6969
const objectDeclaration = getObjectDeclaration(statements[index]);
7070
// eslint-disable-next-line sonarjs/no-collapsible-if
7171
if (objectDeclaration && isIdentifier(objectDeclaration.id)) {
72-
if (
73-
isPropertyAssignment(statements[index + 1], objectDeclaration.id, context.getSourceCode())
74-
) {
72+
const nextStmt = statements[index + 1];
73+
if (isPropertyAssignment(nextStmt, objectDeclaration.id, context.getSourceCode())) {
7574
context.report({ messageId: 'declarePropertiesInsideObject', node: objectDeclaration });
7675
}
7776
}
@@ -103,17 +102,29 @@ function isPropertyAssignment(
103102
return (
104103
!left.computed &&
105104
isSingleLineExpression(right, sourceCode) &&
106-
areEquivalent(left.object, objectIdentifier, sourceCode)
105+
areEquivalent(left.object, objectIdentifier, sourceCode) &&
106+
!isCircularReference(left, right, sourceCode)
107107
);
108108
}
109109
}
110110
return false;
111-
}
112111

113-
function isSingleLineExpression(expression: TSESTree.Expression, sourceCode: TSESLint.SourceCode) {
114-
const first = sourceCode.getFirstToken(expression)!.loc;
115-
const last = sourceCode.getLastToken(expression)!.loc;
116-
return first.start.line === last.end.line;
112+
function isSingleLineExpression(
113+
expression: TSESTree.Expression,
114+
sourceCode: TSESLint.SourceCode,
115+
) {
116+
const first = sourceCode.getFirstToken(expression)!.loc;
117+
const last = sourceCode.getLastToken(expression)!.loc;
118+
return first.start.line === last.end.line;
119+
}
120+
121+
function isCircularReference(
122+
left: TSESTree.MemberExpression,
123+
right: TSESTree.Expression,
124+
sourceCode: TSESLint.SourceCode,
125+
) {
126+
return areEquivalent(left.object, right, sourceCode);
127+
}
117128
}
118129

119130
export = rule;

tests/rules/prefer-object-literal.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ ruleTester.run('prefer-literal', rule, {
7575
{
7676
code: `var x = {a: 2}; x.b = 5;`,
7777
},
78+
{
79+
code: `
80+
const O = {};
81+
O.self = O;`,
82+
},
7883
],
7984
invalid: [
8085
{

0 commit comments

Comments
 (0)