github-pages-deploy-action/node_modules/eslint/lib/rules/dot-location.js

106 lines
3.3 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Validates newlines before and after dots
* @author Greg Cochard
*/
"use strict";
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "layout",
docs: {
description: "enforce consistent newlines before and after dots",
category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/dot-location"
},
schema: [
{
enum: ["object", "property"]
}
],
fixable: "code",
messages: {
expectedDotAfterObject: "Expected dot to be on same line as object.",
expectedDotBeforeProperty: "Expected dot to be on same line as property."
}
},
create(context) {
const config = context.options[0];
// default to onObject if no preference is passed
const onObject = config === "object" || !config;
const sourceCode = context.getSourceCode();
/**
* Reports if the dot between object and property is on the correct loccation.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node The `MemberExpression` node.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
2020-03-31 20:40:00 +08:00
function checkDotLocation(node) {
const property = node.property;
2020-09-13 06:19:45 +08:00
const dotToken = sourceCode.getTokenBefore(property);
2020-03-31 20:40:00 +08:00
if (onObject) {
2020-09-13 06:19:45 +08:00
// `obj` expression can be parenthesized, but those paren tokens are not a part of the `obj` node.
const tokenBeforeDot = sourceCode.getTokenBefore(dotToken);
if (!astUtils.isTokenOnSameLine(tokenBeforeDot, dotToken)) {
2020-03-07 11:45:40 +08:00
context.report({
node,
2020-09-13 06:19:45 +08:00
loc: dotToken.loc,
2020-03-31 20:40:00 +08:00
messageId: "expectedDotAfterObject",
2020-09-13 06:19:45 +08:00
*fix(fixer) {
if (dotToken.value.startsWith(".") && astUtils.isDecimalIntegerNumericToken(tokenBeforeDot)) {
yield fixer.insertTextAfter(tokenBeforeDot, ` ${dotToken.value}`);
} else {
yield fixer.insertTextAfter(tokenBeforeDot, dotToken.value);
}
yield fixer.remove(dotToken);
}
2020-03-07 11:45:40 +08:00
});
}
2020-09-13 06:19:45 +08:00
} else if (!astUtils.isTokenOnSameLine(dotToken, property)) {
2020-03-31 20:40:00 +08:00
context.report({
node,
2020-09-13 06:19:45 +08:00
loc: dotToken.loc,
2020-03-31 20:40:00 +08:00
messageId: "expectedDotBeforeProperty",
2020-09-13 06:19:45 +08:00
*fix(fixer) {
yield fixer.remove(dotToken);
yield fixer.insertTextBefore(property, dotToken.value);
}
2020-03-31 20:40:00 +08:00
});
2020-03-07 11:45:40 +08:00
}
}
/**
* Checks the spacing of the dot within a member expression.
* @param {ASTNode} node The node to check.
* @returns {void}
*/
function checkNode(node) {
2020-03-31 20:40:00 +08:00
if (!node.computed) {
checkDotLocation(node);
}
2020-03-07 11:45:40 +08:00
}
return {
MemberExpression: checkNode
};
}
};