[d565449] | 1 | /**
|
---|
| 2 | * @fileoverview Rule to check multiple var declarations per line
|
---|
| 3 | * @author Alberto Rodríguez
|
---|
| 4 | * @deprecated in ESLint v8.53.0
|
---|
| 5 | */
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | //------------------------------------------------------------------------------
|
---|
| 9 | // Rule Definition
|
---|
| 10 | //------------------------------------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | /** @type {import('../shared/types').Rule} */
|
---|
| 13 | module.exports = {
|
---|
| 14 | meta: {
|
---|
| 15 | deprecated: true,
|
---|
| 16 | replacedBy: [],
|
---|
| 17 | type: "suggestion",
|
---|
| 18 |
|
---|
| 19 | docs: {
|
---|
| 20 | description: "Require or disallow newlines around variable declarations",
|
---|
| 21 | recommended: false,
|
---|
| 22 | url: "https://eslint.org/docs/latest/rules/one-var-declaration-per-line"
|
---|
| 23 | },
|
---|
| 24 |
|
---|
| 25 | schema: [
|
---|
| 26 | {
|
---|
| 27 | enum: ["always", "initializations"]
|
---|
| 28 | }
|
---|
| 29 | ],
|
---|
| 30 |
|
---|
| 31 | fixable: "whitespace",
|
---|
| 32 |
|
---|
| 33 | messages: {
|
---|
| 34 | expectVarOnNewline: "Expected variable declaration to be on a new line."
|
---|
| 35 | }
|
---|
| 36 | },
|
---|
| 37 |
|
---|
| 38 | create(context) {
|
---|
| 39 |
|
---|
| 40 | const always = context.options[0] === "always";
|
---|
| 41 |
|
---|
| 42 | //--------------------------------------------------------------------------
|
---|
| 43 | // Helpers
|
---|
| 44 | //--------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 |
|
---|
| 47 | /**
|
---|
| 48 | * Determine if provided keyword is a variant of for specifiers
|
---|
| 49 | * @private
|
---|
| 50 | * @param {string} keyword keyword to test
|
---|
| 51 | * @returns {boolean} True if `keyword` is a variant of for specifier
|
---|
| 52 | */
|
---|
| 53 | function isForTypeSpecifier(keyword) {
|
---|
| 54 | return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Checks newlines around variable declarations.
|
---|
| 59 | * @private
|
---|
| 60 | * @param {ASTNode} node `VariableDeclaration` node to test
|
---|
| 61 | * @returns {void}
|
---|
| 62 | */
|
---|
| 63 | function checkForNewLine(node) {
|
---|
| 64 | if (isForTypeSpecifier(node.parent.type)) {
|
---|
| 65 | return;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | const declarations = node.declarations;
|
---|
| 69 | let prev;
|
---|
| 70 |
|
---|
| 71 | declarations.forEach(current => {
|
---|
| 72 | if (prev && prev.loc.end.line === current.loc.start.line) {
|
---|
| 73 | if (always || prev.init || current.init) {
|
---|
| 74 | context.report({
|
---|
| 75 | node,
|
---|
| 76 | messageId: "expectVarOnNewline",
|
---|
| 77 | loc: current.loc,
|
---|
| 78 | fix: fixer => fixer.insertTextBefore(current, "\n")
|
---|
| 79 | });
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | prev = current;
|
---|
| 83 | });
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | //--------------------------------------------------------------------------
|
---|
| 87 | // Public
|
---|
| 88 | //--------------------------------------------------------------------------
|
---|
| 89 |
|
---|
| 90 | return {
|
---|
| 91 | VariableDeclaration: checkForNewLine
|
---|
| 92 | };
|
---|
| 93 |
|
---|
| 94 | }
|
---|
| 95 | };
|
---|