diff --git a/lib/rules/jsx-pascal-case.js b/lib/rules/jsx-pascal-case.js index 343bcd3b55..c8be42538e 100644 --- a/lib/rules/jsx-pascal-case.js +++ b/lib/rules/jsx-pascal-case.js @@ -6,19 +6,58 @@ 'use strict'; const elementType = require('jsx-ast-utils/elementType'); -const XRegExp = require('xregexp'); const docsUrl = require('../util/docsUrl'); const jsxUtil = require('../util/jsx'); -// ------------------------------------------------------------------------------ -// Constants -// ------------------------------------------------------------------------------ +function testDigit(char) { + const charCode = char.charCodeAt(0); + return charCode >= 48 && charCode <= 57; +} + +function testUpperCase(char) { + const upperCase = char.toUpperCase(); + return char === upperCase && upperCase !== char.toLowerCase(); +} -// eslint-disable-next-line no-new -const hasU = (function hasU() { try { new RegExp('o', 'u'); return true; } catch (e) { return false; } }()); +function testLowerCase(char) { + const lowerCase = char.toLowerCase(); + return char === lowerCase && lowerCase !== char.toUpperCase(); +} -const PASCAL_CASE_REGEX = XRegExp('^(.*[.])*([\\p{Lu}]|[\\p{Lu}]+[\\p{Ll}0-9]+(?:[\\p{Lu}0-9]+[\\p{Ll}0-9]*)*)$', hasU ? 'u' : ''); -const ALL_CAPS_TAG_REGEX = XRegExp('^[\\p{Lu}0-9]+([\\p{Lu}0-9_]*[\\p{Lu}0-9]+)?$', hasU ? 'u' : ''); +function testPascalCase(name) { + if (!testUpperCase(name.charAt(0))) { + return false; + } + let atLeastOneLowerCase = false; + for (let i = 1; i < name.length; i += 1) { + const char = name.charAt(i); + if (!(char.toLowerCase() !== char.toUpperCase() || testDigit(char))) { + return false; + } + if (!atLeastOneLowerCase) { + atLeastOneLowerCase = testLowerCase(char); + } + } + return atLeastOneLowerCase; +} + +function testAllCaps(name) { + const firstChar = name.charAt(0); + if (!(testUpperCase(firstChar) || testDigit(firstChar))) { + return false; + } + for (let i = 1; i < name.length - 1; i += 1) { + const char = name.charAt(i); + if (!(testUpperCase(char) || testDigit(char) || char === '_')) { + return false; + } + } + const lastChar = name.charAt(name.length - 1); + if (!(testUpperCase(lastChar) || testDigit(lastChar))) { + return false; + } + return true; +} // ------------------------------------------------------------------------------ // Rule Definition @@ -67,8 +106,8 @@ module.exports = { name = name.substring(name.lastIndexOf('.') + 1); } - const isPascalCase = PASCAL_CASE_REGEX.test(name); - const isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(name); + const isPascalCase = testPascalCase(name); + const isAllowedAllCaps = allowAllCaps && testAllCaps(name); const isIgnored = ignore.indexOf(name) !== -1; if (!isPascalCase && !isAllowedAllCaps && !isIgnored) { diff --git a/package.json b/package.json index 610564f489..ba0286a6ce 100644 --- a/package.json +++ b/package.json @@ -37,8 +37,7 @@ "object.values": "^1.1.1", "prop-types": "^15.7.2", "resolve": "^1.15.1", - "string.prototype.matchall": "^4.0.2", - "xregexp": "^4.3.0" + "string.prototype.matchall": "^4.0.2" }, "devDependencies": { "@types/eslint": "^6.8.0",