Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on xregexp #2636

Merged
merged 1 commit into from Jun 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 49 additions & 10 deletions lib/rules/jsx-pascal-case.js
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
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