//------------------------------------------------------------------------------
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
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",
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.
*/
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;
}
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) {
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.
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]));
}
/**