|
| 1 | +/* |
| 2 | + * eslint-plugin-sonarjs |
| 3 | + * Copyright (C) 2018 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +// https://jira.sonarsource.com/browse/RSPEC-2428 |
| 21 | + |
| 22 | +import { Rule, SourceCode } from "eslint"; |
| 23 | +import { Node, Statement, Program, Identifier, BlockStatement, Expression } from "estree"; |
| 24 | +import { |
| 25 | + isModuleDeclaration, |
| 26 | + isVariableDeclaration, |
| 27 | + isObjectExpression, |
| 28 | + isExpressionStatement, |
| 29 | + isAssignmentExpression, |
| 30 | + isMemberExpression, |
| 31 | + isIdentifier, |
| 32 | +} from "../utils/nodes"; |
| 33 | +import { areEquivalent } from "../utils/equivalence"; |
| 34 | + |
| 35 | +const MESSAGE = |
| 36 | + "Declare one or more properties of this object inside of the object literal syntax instead of using separate statements."; |
| 37 | + |
| 38 | +const rule: Rule.RuleModule = { |
| 39 | + create(context: Rule.RuleContext) { |
| 40 | + return { |
| 41 | + BlockStatement: (node: Node) => checkObjectInitialization((node as BlockStatement).body, context), |
| 42 | + Program: (node: Node) => { |
| 43 | + const statements = (node as Program).body.filter( |
| 44 | + (statement): statement is Statement => !isModuleDeclaration(statement), |
| 45 | + ); |
| 46 | + checkObjectInitialization(statements, context); |
| 47 | + }, |
| 48 | + }; |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +function checkObjectInitialization(statements: Statement[], context: Rule.RuleContext) { |
| 53 | + let index = 0; |
| 54 | + while (index < statements.length - 1) { |
| 55 | + const objectDeclaration = getObjectDeclaration(statements[index]); |
| 56 | + if (objectDeclaration && isIdentifier(objectDeclaration.id)) { |
| 57 | + if (isPropertyAssignement(statements[index + 1], objectDeclaration.id, context.getSourceCode())) { |
| 58 | + context.report({ message: MESSAGE, node: objectDeclaration }); |
| 59 | + } |
| 60 | + } |
| 61 | + index++; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function getObjectDeclaration(statement: Statement) { |
| 66 | + if (isVariableDeclaration(statement)) { |
| 67 | + return statement.declarations.find(declaration => !!declaration.init && isEmptyObjectExpression(declaration.init)); |
| 68 | + } |
| 69 | + return undefined; |
| 70 | +} |
| 71 | + |
| 72 | +function isEmptyObjectExpression(expression: Expression) { |
| 73 | + return isObjectExpression(expression) && expression.properties.length === 0; |
| 74 | +} |
| 75 | + |
| 76 | +function isPropertyAssignement(statement: Statement, objectIdentifier: Identifier, sourceCode: SourceCode) { |
| 77 | + if (isExpressionStatement(statement) && isAssignmentExpression(statement.expression)) { |
| 78 | + const { left, right } = statement.expression; |
| 79 | + if (isMemberExpression(left)) { |
| 80 | + return ( |
| 81 | + !left.computed && |
| 82 | + isSingleLineExpression(right, sourceCode) && |
| 83 | + areEquivalent(left.object, objectIdentifier, sourceCode) |
| 84 | + ); |
| 85 | + } |
| 86 | + } |
| 87 | + return false; |
| 88 | +} |
| 89 | + |
| 90 | +function isSingleLineExpression(expression: Expression, sourceCode: SourceCode) { |
| 91 | + const first = sourceCode.getFirstToken(expression)!.loc; |
| 92 | + const last = sourceCode.getLastToken(expression)!.loc; |
| 93 | + return first.start.line === last.end.line; |
| 94 | +} |
| 95 | + |
| 96 | +export = rule; |
0 commit comments