Skip to content

Commit

Permalink
Remove xregexp
Browse files Browse the repository at this point in the history
  • Loading branch information
yacinehmito committed May 9, 2020
1 parent b9d2eb5 commit 57c8ebb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
61 changes: 51 additions & 10 deletions lib/rules/jsx-pascal-case.js
Expand Up @@ -6,19 +6,60 @@
'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;
}
const {length} = name;
let atLeastOneLowerCase = false;
for (let i = 1; i < 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 {length} = name;
const firstChar = name.charAt(0);
if (!(testUpperCase(firstChar) || testDigit(firstChar))) {
return false;
}
for (let i = 1; i < length - 1; i += 1) {
const char = name.charAt(i);
if (!(testUpperCase(char) || testDigit(char) || char === '_')) {
return false;
}
}
const lastChar = name.charAt(length - 1);
if (!(testUpperCase(lastChar) || testDigit(lastChar))) {
return false;
}
return true;
}

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -64,9 +105,9 @@ module.exports = {
name = name.substring(0, name.indexOf('.'));
}

const isPascalCase = PASCAL_CASE_REGEX.test(name);
const isPascalCase = testPascalCase(name);
const isCompatTag = jsxUtil.isDOMComponent(node);
const isAllowedAllCaps = allowAllCaps && ALL_CAPS_TAG_REGEX.test(name);
const isAllowedAllCaps = allowAllCaps && testAllCaps(name);
const isIgnored = ignore.indexOf(name) !== -1;

if (!isPascalCase && !isCompatTag && !isAllowedAllCaps && !isIgnored) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
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.1.8",
Expand Down

0 comments on commit 57c8ebb

Please sign in to comment.