diff options
Diffstat (limited to 'tools/node_modules/eslint/lib/rules/id-length.js')
-rw-r--r-- | tools/node_modules/eslint/lib/rules/id-length.js | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/tools/node_modules/eslint/lib/rules/id-length.js b/tools/node_modules/eslint/lib/rules/id-length.js index 99f833fc73..bd269b998f 100644 --- a/tools/node_modules/eslint/lib/rules/id-length.js +++ b/tools/node_modules/eslint/lib/rules/id-length.js @@ -7,6 +7,45 @@ "use strict"; //------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ +const GraphemeSplitter = require("grapheme-splitter"); + +//------------------------------------------------------------------------------ +// Helpers +//------------------------------------------------------------------------------ + +/** + * Checks if the string given as argument is ASCII or not. + * @param {string} value A string that you want to know if it is ASCII or not. + * @returns {boolean} `true` if `value` is ASCII string. + */ +function isASCII(value) { + if (typeof value !== "string") { + return false; + } + return /^[\u0020-\u007f]*$/u.test(value); +} + +/** @type {GraphemeSplitter | undefined} */ +let splitter; + +/** + * Gets the length of the string. If the string is not in ASCII, counts graphemes. + * @param {string} value A string that you want to get the length. + * @returns {number} The length of `value`. + */ +function getStringLength(value) { + if (isASCII(value)) { + return value.length; + } + if (!splitter) { + splitter = new GraphemeSplitter(); + } + return splitter.countGraphemes(value); +} + +//------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -130,8 +169,10 @@ module.exports = { const name = node.name; const parent = node.parent; - const isShort = name.length < minLength; - const isLong = name.length > maxLength; + const nameLength = getStringLength(name); + + const isShort = nameLength < minLength; + const isLong = nameLength > maxLength; if (!(isShort || isLong) || exceptions.has(name) || matchesExceptionPattern(name)) { return; // Nothing to report |