[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Specify the maximum number of statements allowed per line.
|
---|
| 3 | * @author Kenneth Williams
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Requirements
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | const astUtils = require("./utils/ast-utils");
|
---|
| 13 |
|
---|
| 14 | //------------------------------------------------------------------------------
|
---|
| 15 | // Rule Definition
|
---|
| 16 | //------------------------------------------------------------------------------
|
---|
| 17 |
|
---|
| 18 | /** @type {import('../shared/types').Rule} */
|
---|
| 19 | module.exports = {
|
---|
| 20 | meta: {
|
---|
| 21 | deprecated: true,
|
---|
| 22 | replacedBy: [],
|
---|
| 23 | type: "layout",
|
---|
| 24 |
|
---|
| 25 | docs: {
|
---|
| 26 | description: "Enforce a maximum number of statements allowed per line",
|
---|
| 27 | recommended: false,
|
---|
| 28 | url: "https://eslint.org/docs/latest/rules/max-statements-per-line"
|
---|
| 29 | },
|
---|
| 30 |
|
---|
| 31 | schema: [
|
---|
| 32 | {
|
---|
| 33 | type: "object",
|
---|
| 34 | properties: {
|
---|
| 35 | max: {
|
---|
| 36 | type: "integer",
|
---|
| 37 | minimum: 1,
|
---|
| 38 | default: 1
|
---|
| 39 | }
|
---|
| 40 | },
|
---|
| 41 | additionalProperties: false
|
---|
| 42 | }
|
---|
| 43 | ],
|
---|
| 44 | messages: {
|
---|
| 45 | exceed: "This line has {{numberOfStatementsOnThisLine}} {{statements}}. Maximum allowed is {{maxStatementsPerLine}}."
|
---|
| 46 | }
|
---|
| 47 | },
|
---|
| 48 |
|
---|
| 49 | create(context) {
|
---|
| 50 |
|
---|
| 51 | const sourceCode = context.sourceCode,
|
---|
| 52 | options = context.options[0] || {},
|
---|
| 53 | maxStatementsPerLine = typeof options.max !== "undefined" ? options.max : 1;
|
---|
| 54 |
|
---|
| 55 | let lastStatementLine = 0,
|
---|
| 56 | numberOfStatementsOnThisLine = 0,
|
---|
| 57 | firstExtraStatement;
|
---|
| 58 |
|
---|
| 59 | //--------------------------------------------------------------------------
|
---|
| 60 | // Helpers
|
---|
| 61 | //--------------------------------------------------------------------------
|
---|
| 62 |
|
---|
| 63 | const SINGLE_CHILD_ALLOWED = /^(?:(?:DoWhile|For|ForIn|ForOf|If|Labeled|While)Statement|Export(?:Default|Named)Declaration)$/u;
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Reports with the first extra statement, and clears it.
|
---|
| 67 | * @returns {void}
|
---|
| 68 | */
|
---|
| 69 | function reportFirstExtraStatementAndClear() {
|
---|
| 70 | if (firstExtraStatement) {
|
---|
| 71 | context.report({
|
---|
| 72 | node: firstExtraStatement,
|
---|
| 73 | messageId: "exceed",
|
---|
| 74 | data: {
|
---|
| 75 | numberOfStatementsOnThisLine,
|
---|
| 76 | maxStatementsPerLine,
|
---|
| 77 | statements: numberOfStatementsOnThisLine === 1 ? "statement" : "statements"
|
---|
| 78 | }
|
---|
| 79 | });
|
---|
| 80 | }
|
---|
| 81 | firstExtraStatement = null;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Gets the actual last token of a given node.
|
---|
| 86 | * @param {ASTNode} node A node to get. This is a node except EmptyStatement.
|
---|
| 87 | * @returns {Token} The actual last token.
|
---|
| 88 | */
|
---|
| 89 | function getActualLastToken(node) {
|
---|
| 90 | return sourceCode.getLastToken(node, astUtils.isNotSemicolonToken);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * Addresses a given node.
|
---|
| 95 | * It updates the state of this rule, then reports the node if the node violated this rule.
|
---|
| 96 | * @param {ASTNode} node A node to check.
|
---|
| 97 | * @returns {void}
|
---|
| 98 | */
|
---|
| 99 | function enterStatement(node) {
|
---|
| 100 | const line = node.loc.start.line;
|
---|
| 101 |
|
---|
| 102 | /*
|
---|
| 103 | * Skip to allow non-block statements if this is direct child of control statements.
|
---|
| 104 | * `if (a) foo();` is counted as 1.
|
---|
| 105 | * But `if (a) foo(); else foo();` should be counted as 2.
|
---|
| 106 | */
|
---|
| 107 | if (SINGLE_CHILD_ALLOWED.test(node.parent.type) &&
|
---|
| 108 | node.parent.alternate !== node
|
---|
| 109 | ) {
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | // Update state.
|
---|
| 114 | if (line === lastStatementLine) {
|
---|
| 115 | numberOfStatementsOnThisLine += 1;
|
---|
| 116 | } else {
|
---|
| 117 | reportFirstExtraStatementAndClear();
|
---|
| 118 | numberOfStatementsOnThisLine = 1;
|
---|
| 119 | lastStatementLine = line;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | // Reports if the node violated this rule.
|
---|
| 123 | if (numberOfStatementsOnThisLine === maxStatementsPerLine + 1) {
|
---|
| 124 | firstExtraStatement = firstExtraStatement || node;
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /**
|
---|
| 129 | * Updates the state of this rule with the end line of leaving node to check with the next statement.
|
---|
| 130 | * @param {ASTNode} node A node to check.
|
---|
| 131 | * @returns {void}
|
---|
| 132 | */
|
---|
| 133 | function leaveStatement(node) {
|
---|
| 134 | const line = getActualLastToken(node).loc.end.line;
|
---|
| 135 |
|
---|
| 136 | // Update state.
|
---|
| 137 | if (line !== lastStatementLine) {
|
---|
| 138 | reportFirstExtraStatementAndClear();
|
---|
| 139 | numberOfStatementsOnThisLine = 1;
|
---|
| 140 | lastStatementLine = line;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | //--------------------------------------------------------------------------
|
---|
| 145 | // Public API
|
---|
| 146 | //--------------------------------------------------------------------------
|
---|
| 147 |
|
---|
| 148 | return {
|
---|
| 149 | BreakStatement: enterStatement,
|
---|
| 150 | ClassDeclaration: enterStatement,
|
---|
| 151 | ContinueStatement: enterStatement,
|
---|
| 152 | DebuggerStatement: enterStatement,
|
---|
| 153 | DoWhileStatement: enterStatement,
|
---|
| 154 | ExpressionStatement: enterStatement,
|
---|
| 155 | ForInStatement: enterStatement,
|
---|
| 156 | ForOfStatement: enterStatement,
|
---|
| 157 | ForStatement: enterStatement,
|
---|
| 158 | FunctionDeclaration: enterStatement,
|
---|
| 159 | IfStatement: enterStatement,
|
---|
| 160 | ImportDeclaration: enterStatement,
|
---|
| 161 | LabeledStatement: enterStatement,
|
---|
| 162 | ReturnStatement: enterStatement,
|
---|
| 163 | SwitchStatement: enterStatement,
|
---|
| 164 | ThrowStatement: enterStatement,
|
---|
| 165 | TryStatement: enterStatement,
|
---|
| 166 | VariableDeclaration: enterStatement,
|
---|
| 167 | WhileStatement: enterStatement,
|
---|
| 168 | WithStatement: enterStatement,
|
---|
| 169 | ExportNamedDeclaration: enterStatement,
|
---|
| 170 | ExportDefaultDeclaration: enterStatement,
|
---|
| 171 | ExportAllDeclaration: enterStatement,
|
---|
| 172 |
|
---|
| 173 | "BreakStatement:exit": leaveStatement,
|
---|
| 174 | "ClassDeclaration:exit": leaveStatement,
|
---|
| 175 | "ContinueStatement:exit": leaveStatement,
|
---|
| 176 | "DebuggerStatement:exit": leaveStatement,
|
---|
| 177 | "DoWhileStatement:exit": leaveStatement,
|
---|
| 178 | "ExpressionStatement:exit": leaveStatement,
|
---|
| 179 | "ForInStatement:exit": leaveStatement,
|
---|
| 180 | "ForOfStatement:exit": leaveStatement,
|
---|
| 181 | "ForStatement:exit": leaveStatement,
|
---|
| 182 | "FunctionDeclaration:exit": leaveStatement,
|
---|
| 183 | "IfStatement:exit": leaveStatement,
|
---|
| 184 | "ImportDeclaration:exit": leaveStatement,
|
---|
| 185 | "LabeledStatement:exit": leaveStatement,
|
---|
| 186 | "ReturnStatement:exit": leaveStatement,
|
---|
| 187 | "SwitchStatement:exit": leaveStatement,
|
---|
| 188 | "ThrowStatement:exit": leaveStatement,
|
---|
| 189 | "TryStatement:exit": leaveStatement,
|
---|
| 190 | "VariableDeclaration:exit": leaveStatement,
|
---|
| 191 | "WhileStatement:exit": leaveStatement,
|
---|
| 192 | "WithStatement:exit": leaveStatement,
|
---|
| 193 | "ExportNamedDeclaration:exit": leaveStatement,
|
---|
| 194 | "ExportDefaultDeclaration:exit": leaveStatement,
|
---|
| 195 | "ExportAllDeclaration:exit": leaveStatement,
|
---|
| 196 | "Program:exit": reportFirstExtraStatementAndClear
|
---|
| 197 | };
|
---|
| 198 | }
|
---|
| 199 | };
|
---|