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

Commit

Permalink
Fix FP S2428 (prefer-object-literal): Ignore circular reference ass…
Browse files Browse the repository at this point in the history
…ignments (#427)

Co-authored-by: Ilia Kebets <104737176+ilia-kebets-sonarsource@users.noreply.github.com>
  • Loading branch information
1 parent 75f01d8 commit 7808c0d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
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

0 comments on commit 7808c0d

Please sign in to comment.