Skip to content

Commit

Permalink
[refactor] jsx-pascal-case: Remove xregexp
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito authored and ljharb committed May 8, 2020
1 parent 25bf6cc commit 03df672
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
59 changes: 49 additions & 10 deletions lib/rules/jsx-pascal-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 03df672

Please sign in to comment.