github-pages-deploy-action/node_modules/eslint/lib/rules/one-var-declaration-per-line.js

93 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to check multiple var declarations per line
* @author Alberto Rodríguez
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "require or disallow newlines around variable declarations",
category: "Stylistic Issues",
recommended: false,
url: "https://eslint.org/docs/rules/one-var-declaration-per-line"
},
schema: [
{
enum: ["always", "initializations"]
}
],
2020-05-15 05:33:08 +08:00
fixable: "whitespace",
messages: {
expectVarOnNewline: "Expected variable declaration to be on a new line."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
const always = context.options[0] === "always";
//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------
/**
* Determine if provided keyword is a variant of for specifiers
* @private
2020-03-31 20:40:00 +08:00
* @param {string} keyword keyword to test
2020-03-07 11:45:40 +08:00
* @returns {boolean} True if `keyword` is a variant of for specifier
*/
function isForTypeSpecifier(keyword) {
return keyword === "ForStatement" || keyword === "ForInStatement" || keyword === "ForOfStatement";
}
/**
* Checks newlines around variable declarations.
* @private
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node `VariableDeclaration` node to test
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function checkForNewLine(node) {
if (isForTypeSpecifier(node.parent.type)) {
return;
}
const declarations = node.declarations;
let prev;
declarations.forEach(current => {
if (prev && prev.loc.end.line === current.loc.start.line) {
if (always || prev.init || current.init) {
context.report({
node,
2020-05-15 05:33:08 +08:00
messageId: "expectVarOnNewline",
2020-06-06 22:11:37 +08:00
loc: current.loc,
2020-03-07 11:45:40 +08:00
fix: fixer => fixer.insertTextBefore(current, "\n")
});
}
}
prev = current;
});
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
return {
VariableDeclaration: checkForNewLine
};
}
};