summaryrefslogtreecommitdiff
path: root/tools/node_modules/eslint/lib/rules/space-infix-ops.js
diff options
context:
space:
mode:
authorMichaël Zasso <targos@protonmail.com>2017-12-22 16:53:42 +0100
committerMichaël Zasso <targos@protonmail.com>2018-01-11 09:48:05 +0100
commit3dc30632755713179f345f4af024bd904c6162d0 (patch)
treef28c4f6dd6dfc5992edf301449d1a371d229755b /tools/node_modules/eslint/lib/rules/space-infix-ops.js
parenta2c7085dd4a8e60d1a47572aca8bb6fcb7a32f88 (diff)
downloadandroid-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.gz
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.tar.bz2
android-node-v8-3dc30632755713179f345f4af024bd904c6162d0.zip
tools: move eslint from tools to tools/node_modules
This is required because we need to add the babel-eslint dependency and it has to be able to resolve "eslint". babel-eslint is required to support future ES features such as async iterators and import.meta. Refs: https://github.com/nodejs/node/pull/17755 PR-URL: https://github.com/nodejs/node/pull/17820 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/space-infix-ops.js')
-rw-r--r--tools/node_modules/eslint/lib/rules/space-infix-ops.js167
1 files changed, 167 insertions, 0 deletions
diff --git a/tools/node_modules/eslint/lib/rules/space-infix-ops.js b/tools/node_modules/eslint/lib/rules/space-infix-ops.js
new file mode 100644
index 0000000000..b92c889c7f
--- /dev/null
+++ b/tools/node_modules/eslint/lib/rules/space-infix-ops.js
@@ -0,0 +1,167 @@
+/**
+ * @fileoverview Require spaces around infix operators
+ * @author Michael Ficarra
+ */
+"use strict";
+
+//------------------------------------------------------------------------------
+// Rule Definition
+//------------------------------------------------------------------------------
+
+module.exports = {
+ meta: {
+ docs: {
+ description: "require spacing around infix operators",
+ category: "Stylistic Issues",
+ recommended: false
+ },
+
+ fixable: "whitespace",
+
+ schema: [
+ {
+ type: "object",
+ properties: {
+ int32Hint: {
+ type: "boolean"
+ }
+ },
+ additionalProperties: false
+ }
+ ]
+ },
+
+ create(context) {
+ const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
+
+ const OPERATORS = [
+ "*", "/", "%", "+", "-", "<<", ">>", ">>>", "<", "<=", ">", ">=", "in",
+ "instanceof", "==", "!=", "===", "!==", "&", "^", "|", "&&", "||", "=",
+ "+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "&=", "^=", "|=",
+ "?", ":", ",", "**"
+ ];
+
+ const sourceCode = context.getSourceCode();
+
+ /**
+ * Returns the first token which violates the rule
+ * @param {ASTNode} left - The left node of the main node
+ * @param {ASTNode} right - The right node of the main node
+ * @returns {Object} The violator token or null
+ * @private
+ */
+ function getFirstNonSpacedToken(left, right) {
+ const tokens = sourceCode.getTokensBetween(left, right, 1);
+
+ for (let i = 1, l = tokens.length - 1; i < l; ++i) {
+ const op = tokens[i];
+
+ if (
+ (op.type === "Punctuator" || op.type === "Keyword") &&
+ OPERATORS.indexOf(op.value) >= 0 &&
+ (tokens[i - 1].range[1] >= op.range[0] || op.range[1] >= tokens[i + 1].range[0])
+ ) {
+ return op;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Reports an AST node as a rule violation
+ * @param {ASTNode} mainNode - The node to report
+ * @param {Object} culpritToken - The token which has a problem
+ * @returns {void}
+ * @private
+ */
+ function report(mainNode, culpritToken) {
+ context.report({
+ node: mainNode,
+ loc: culpritToken.loc.start,
+ message: "Infix operators must be spaced.",
+ fix(fixer) {
+ const previousToken = sourceCode.getTokenBefore(culpritToken);
+ const afterToken = sourceCode.getTokenAfter(culpritToken);
+ let fixString = "";
+
+ if (culpritToken.range[0] - previousToken.range[1] === 0) {
+ fixString = " ";
+ }
+
+ fixString += culpritToken.value;
+
+ if (afterToken.range[0] - culpritToken.range[1] === 0) {
+ fixString += " ";
+ }
+
+ return fixer.replaceText(culpritToken, fixString);
+ }
+ });
+ }
+
+ /**
+ * Check if the node is binary then report
+ * @param {ASTNode} node node to evaluate
+ * @returns {void}
+ * @private
+ */
+ function checkBinary(node) {
+ const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
+ const rightNode = node.right;
+
+ const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
+
+ if (nonSpacedNode) {
+ if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
+ report(node, nonSpacedNode);
+ }
+ }
+ }
+
+ /**
+ * Check if the node is conditional
+ * @param {ASTNode} node node to evaluate
+ * @returns {void}
+ * @private
+ */
+ function checkConditional(node) {
+ const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent);
+ const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate);
+
+ if (nonSpacedConsequesntNode) {
+ report(node, nonSpacedConsequesntNode);
+ } else if (nonSpacedAlternateNode) {
+ report(node, nonSpacedAlternateNode);
+ }
+ }
+
+ /**
+ * Check if the node is a variable
+ * @param {ASTNode} node node to evaluate
+ * @returns {void}
+ * @private
+ */
+ function checkVar(node) {
+ const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
+ const rightNode = node.init;
+
+ if (rightNode) {
+ const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
+
+ if (nonSpacedNode) {
+ report(node, nonSpacedNode);
+ }
+ }
+ }
+
+ return {
+ AssignmentExpression: checkBinary,
+ AssignmentPattern: checkBinary,
+ BinaryExpression: checkBinary,
+ LogicalExpression: checkBinary,
+ ConditionalExpression: checkConditional,
+ VariableDeclarator: checkVar
+ };
+
+ }
+};