]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/lib/rules/key-spacing.js
import 8.41.0 source
[pve-eslint.git] / eslint / lib / rules / key-spacing.js
index b764b7282ef8312ec33052ba83b42332176897ed..0b51eb3fe137eee5bce79e794851f55178f905cd 100644 (file)
@@ -9,13 +9,7 @@
 //------------------------------------------------------------------------------
 
 const astUtils = require("./utils/ast-utils");
-const GraphemeSplitter = require("grapheme-splitter");
-
-const splitter = new GraphemeSplitter();
-
-//------------------------------------------------------------------------------
-// Helpers
-//------------------------------------------------------------------------------
+const { getGraphemeCount } = require("../shared/string-utils");
 
 /**
  * Checks whether a string contains a line terminator as defined in
@@ -144,7 +138,7 @@ module.exports = {
         docs: {
             description: "Enforce consistent spacing between keys and values in object literal properties",
             recommended: false,
-            url: "https://eslint.org/docs/rules/key-spacing"
+            url: "https://eslint.org/docs/latest/rules/key-spacing"
         },
 
         fixable: "whitespace",
@@ -332,7 +326,54 @@ module.exports = {
             singleLineOptions = ruleOptions.singleLine,
             alignmentOptions = ruleOptions.align || null;
 
-        const sourceCode = context.getSourceCode();
+        const sourceCode = context.sourceCode;
+
+        /**
+         * Determines if the given property is key-value property.
+         * @param {ASTNode} property Property node to check.
+         * @returns {boolean} Whether the property is a key-value property.
+         */
+        function isKeyValueProperty(property) {
+            return !(
+                (property.method ||
+                property.shorthand ||
+                property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"
+            );
+        }
+
+        /**
+         * Starting from the given node (a property.key node here) looks forward
+         * until it finds the colon punctuator and returns it.
+         * @param {ASTNode} node The node to start looking from.
+         * @returns {ASTNode} The colon punctuator.
+         */
+        function getNextColon(node) {
+            return sourceCode.getTokenAfter(node, astUtils.isColonToken);
+        }
+
+        /**
+         * Starting from the given node (a property.key node here) looks forward
+         * until it finds the last token before a colon punctuator and returns it.
+         * @param {ASTNode} node The node to start looking from.
+         * @returns {ASTNode} The last token before a colon punctuator.
+         */
+        function getLastTokenBeforeColon(node) {
+            const colonToken = getNextColon(node);
+
+            return sourceCode.getTokenBefore(colonToken);
+        }
+
+        /**
+         * Starting from the given node (a property.key node here) looks forward
+         * until it finds the first token after a colon punctuator and returns it.
+         * @param {ASTNode} node The node to start looking from.
+         * @returns {ASTNode} The first token after a colon punctuator.
+         */
+        function getFirstTokenAfterColon(node) {
+            const colonToken = getNextColon(node);
+
+            return sourceCode.getTokenAfter(colonToken);
+        }
 
         /**
          * Checks whether a property is a member of the property group it follows.
@@ -342,9 +383,9 @@ module.exports = {
          */
         function continuesPropertyGroup(lastMember, candidate) {
             const groupEndLine = lastMember.loc.start.line,
-                candidateStartLine = candidate.loc.start.line;
+                candidateValueStartLine = (isKeyValueProperty(candidate) ? getFirstTokenAfterColon(candidate.key) : candidate).loc.start.line;
 
-            if (candidateStartLine - groupEndLine <= 1) {
+            if (candidateValueStartLine - groupEndLine <= 1) {
                 return true;
             }
 
@@ -358,7 +399,7 @@ module.exports = {
             if (
                 leadingComments.length &&
                 leadingComments[0].loc.start.line - groupEndLine <= 1 &&
-                candidateStartLine - last(leadingComments).loc.end.line <= 1
+                candidateValueStartLine - last(leadingComments).loc.end.line <= 1
             ) {
                 for (let i = 1; i < leadingComments.length; i++) {
                     if (leadingComments[i].loc.start.line - leadingComments[i - 1].loc.end.line > 1) {
@@ -371,41 +412,6 @@ module.exports = {
             return false;
         }
 
-        /**
-         * Determines if the given property is key-value property.
-         * @param {ASTNode} property Property node to check.
-         * @returns {boolean} Whether the property is a key-value property.
-         */
-        function isKeyValueProperty(property) {
-            return !(
-                (property.method ||
-                property.shorthand ||
-                property.kind !== "init" || property.type !== "Property") // Could be "ExperimentalSpreadProperty" or "SpreadElement"
-            );
-        }
-
-        /**
-         * Starting from the given a node (a property.key node here) looks forward
-         * until it finds the last token before a colon punctuator and returns it.
-         * @param {ASTNode} node The node to start looking from.
-         * @returns {ASTNode} The last token before a colon punctuator.
-         */
-        function getLastTokenBeforeColon(node) {
-            const colonToken = sourceCode.getTokenAfter(node, astUtils.isColonToken);
-
-            return sourceCode.getTokenBefore(colonToken);
-        }
-
-        /**
-         * Starting from the given a node (a property.key node here) looks forward
-         * until it finds the colon punctuator and returns it.
-         * @param {ASTNode} node The node to start looking from.
-         * @returns {ASTNode} The colon punctuator.
-         */
-        function getNextColon(node) {
-            return sourceCode.getTokenAfter(node, astUtils.isColonToken);
-        }
-
         /**
          * Gets an object literal property's key as the identifier name or string value.
          * @param {ASTNode} property Property node whose key to retrieve.
@@ -511,7 +517,7 @@ module.exports = {
             const startToken = sourceCode.getFirstToken(property);
             const endToken = getLastTokenBeforeColon(property.key);
 
-            return splitter.countGraphemes(sourceCode.getText().slice(startToken.range[0], endToken.range[1]));
+            return getGraphemeCount(sourceCode.getText().slice(startToken.range[0], endToken.range[1]));
         }
 
         /**