eslint-plugin-stylistic-compat
Version:
Compatibility version of @stylistic/eslint-plugin for Node.js >= 12 and ESLint v8
194 lines (190 loc) • 8.54 kB
JavaScript
'use strict';
var _someInstanceProperty = require('/runtime-corejs3/core-js/instance/some');
var _sliceInstanceProperty = require('/runtime-corejs3/core-js/instance/slice');
var _includesInstanceProperty = require('/runtime-corejs3/core-js/instance/includes');
var _startsWithInstanceProperty = require('/runtime-corejs3/core-js/instance/starts-with');
var utils = require('../utils.js');
var vendor = require('../vendor.js');
require('/runtime-corejs3/core-js/object/from-entries');
require('/runtime-corejs3/core-js/instance/map');
require('/runtime-corejs3/core-js/instance/filter');
require('/runtime-corejs3/core-js/object/entries');
require('/runtime-corejs3/core-js/set');
require('/runtime-corejs3/core-js/instance/flags');
require('/runtime-corejs3/core-js/array/from');
require('/runtime-corejs3/core-js/array/is-array');
require('/runtime-corejs3/core-js/instance/concat');
require('/runtime-corejs3/core-js/object/keys');
require('/runtime-corejs3/core-js/instance/reduce');
require('/runtime-corejs3/core-js/object/assign');
require('/runtime-corejs3/core-js/instance/keys');
require('/runtime-corejs3/core-js/instance/find');
require('/runtime-corejs3/core-js/instance/reverse');
require('/runtime-corejs3/core-js/object/define-property');
require('/runtime-corejs3/core-js/object/create');
require('/runtime-corejs3/core-js/object/freeze');
require('/runtime-corejs3/core-js/instance/last-index-of');
require('/runtime-corejs3/core-js/object/define-properties');
require('/runtime-corejs3/core-js/instance/index-of');
require('/runtime-corejs3/core-js/instance/at');
require('/runtime-corejs3/core-js/symbol');
require('/runtime-corejs3/core-js/symbol/iterator');
require('/runtime-corejs3/core-js/parse-int');
require('/runtime-corejs3/core-js/parse-float');
require('/runtime-corejs3/core-js/weak-map');
require('/runtime-corejs3/core-js/global-this');
require('/runtime-corejs3/core-js/instance/for-each');
require('eslint/use-at-your-own-risk');
require('eslint');
require('/runtime-corejs3/core-js/object/get-own-property-descriptor');
require('/runtime-corejs3/core-js/map');
require('/runtime-corejs3/core-js/instance/every');
require('/runtime-corejs3/core-js/instance/sort');
require('/runtime-corejs3/core-js/array/of');
require('/runtime-corejs3/core-js/instance/entries');
require('/runtime-corejs3/core-js/instance/find-index');
require('/runtime-corejs3/core-js/instance/flat');
require('/runtime-corejs3/core-js/instance/values');
require('/runtime-corejs3/core-js/object/get-own-property-names');
require('/runtime-corejs3/core-js/number/is-finite');
require('/runtime-corejs3/core-js/number/is-nan');
require('/runtime-corejs3/core-js/number/parse-float');
require('/runtime-corejs3/core-js/number/parse-int');
require('/runtime-corejs3/core-js/object/is');
require('/runtime-corejs3/core-js/object/is-extensible');
require('/runtime-corejs3/core-js/object/is-frozen');
require('/runtime-corejs3/core-js/object/is-sealed');
require('/runtime-corejs3/core-js/object/values');
require('/runtime-corejs3/core-js/string/from-code-point');
require('/runtime-corejs3/core-js/string/raw');
require('/runtime-corejs3/core-js/instance/code-point-at');
require('/runtime-corejs3/core-js/instance/ends-with');
require('/runtime-corejs3/core-js/instance/pad-end');
require('/runtime-corejs3/core-js/instance/pad-start');
require('/runtime-corejs3/core-js/instance/trim');
require('/runtime-corejs3/core-js/instance/trim-end');
require('/runtime-corejs3/core-js/instance/trim-left');
require('/runtime-corejs3/core-js/instance/trim-right');
require('/runtime-corejs3/core-js/instance/trim-start');
require('/runtime-corejs3/core-js/symbol/for');
require('/runtime-corejs3/core-js/symbol/key-for');
require('/runtime-corejs3/core-js/object/prevent-extensions');
require('/runtime-corejs3/core-js/object/seal');
require('/runtime-corejs3/core-js/object/get-prototype-of');
require('/runtime-corejs3/core-js/symbol/replace');
require('/runtime-corejs3/core-js/instance/bind');
require('/runtime-corejs3/core-js/instance/splice');
require('/runtime-corejs3/core-js/instance/repeat');
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();
}
function testLowerCase(char) {
const lowerCase = char.toLowerCase();
return char === lowerCase && lowerCase !== char.toUpperCase();
}
function testPascalCase(name) {
if (!testUpperCase(name.charAt(0))) return false;
const anyNonAlphaNumeric = _someInstanceProperty(Array.prototype).call(_sliceInstanceProperty(name).call(name, 1), char => char.toLowerCase() === char.toUpperCase() && !testDigit(char));
if (anyNonAlphaNumeric) return false;
return _someInstanceProperty(Array.prototype).call(_sliceInstanceProperty(name).call(name, 1), char => testLowerCase(char) || testDigit(char));
}
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;
}
const messages = {
usePascalCase: "Imported JSX component {{name}} must be in PascalCase",
usePascalOrSnakeCase: "Imported JSX component {{name}} must be in PascalCase or SCREAMING_SNAKE_CASE"
};
var jsxPascalCase = utils.createRule({
name: "jsx-pascal-case",
package: "jsx",
meta: {
type: "suggestion",
docs: {
description: "Enforce PascalCase for user-defined JSX components"
// category: 'Stylistic Issues',
},
messages,
schema: [{
type: "object",
properties: {
allowAllCaps: {
type: "boolean"
},
allowLeadingUnderscore: {
type: "boolean"
},
allowNamespace: {
type: "boolean"
},
ignore: {
items: {
type: "string"
},
type: "array",
uniqueItems: true
}
},
additionalProperties: false
}]
},
create(context) {
const configuration = context.options[0] || {};
const {
allowAllCaps = false,
allowLeadingUnderscore = false,
allowNamespace = false,
ignore = []
} = configuration;
const isMatchIgnore = vendor.picomatch(ignore, {
noglobstar: true
});
function ignoreCheck(name) {
return isMatchIgnore(name) || _includesInstanceProperty(ignore).call(ignore, name);
}
return {
JSXOpeningElement(node) {
const isCompatTag = utils.isDOMComponent(node);
if (isCompatTag) return;
const name = utils.getElementType(node);
let checkNames = [name];
let index = 0;
if (_includesInstanceProperty(name).call(name, ":")) checkNames = name.split(":");else if (_includesInstanceProperty(name).call(name, ".")) checkNames = name.split(".");
do {
const splitName = checkNames[index];
if (splitName.length === 1) return;
const isIgnored = ignoreCheck(splitName);
const checkName = allowLeadingUnderscore && _startsWithInstanceProperty(splitName).call(splitName, "_") ? _sliceInstanceProperty(splitName).call(splitName, 1) : splitName;
const isPascalCase = testPascalCase(checkName);
const isAllowedAllCaps = allowAllCaps && testAllCaps(checkName);
if (!isPascalCase && !isAllowedAllCaps && !isIgnored) {
const messageId = allowAllCaps ? "usePascalOrSnakeCase" : "usePascalCase";
context.report({
messageId,
node,
data: {
name: splitName
}
});
break;
}
index += 1;
} while (index < checkNames.length && !allowNamespace);
}
};
}
});
module.exports = jsxPascalCase;