UNPKG

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
'use strict'; var _someInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/some'); var _sliceInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/slice'); var _includesInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/includes'); var _startsWithInstanceProperty = require('@babel/runtime-corejs3/core-js/instance/starts-with'); var utils = require('../utils.js'); var vendor = require('../vendor.js'); require('@babel/runtime-corejs3/core-js/object/from-entries'); require('@babel/runtime-corejs3/core-js/instance/map'); require('@babel/runtime-corejs3/core-js/instance/filter'); require('@babel/runtime-corejs3/core-js/object/entries'); require('@babel/runtime-corejs3/core-js/set'); require('@babel/runtime-corejs3/core-js/instance/flags'); require('@babel/runtime-corejs3/core-js/array/from'); require('@babel/runtime-corejs3/core-js/array/is-array'); require('@babel/runtime-corejs3/core-js/instance/concat'); require('@babel/runtime-corejs3/core-js/object/keys'); require('@babel/runtime-corejs3/core-js/instance/reduce'); require('@babel/runtime-corejs3/core-js/object/assign'); require('@babel/runtime-corejs3/core-js/instance/keys'); require('@babel/runtime-corejs3/core-js/instance/find'); require('@babel/runtime-corejs3/core-js/instance/reverse'); require('@babel/runtime-corejs3/core-js/object/define-property'); require('@babel/runtime-corejs3/core-js/object/create'); require('@babel/runtime-corejs3/core-js/object/freeze'); require('@babel/runtime-corejs3/core-js/instance/last-index-of'); require('@babel/runtime-corejs3/core-js/object/define-properties'); require('@babel/runtime-corejs3/core-js/instance/index-of'); require('@babel/runtime-corejs3/core-js/instance/at'); require('@babel/runtime-corejs3/core-js/symbol'); require('@babel/runtime-corejs3/core-js/symbol/iterator'); require('@babel/runtime-corejs3/core-js/parse-int'); require('@babel/runtime-corejs3/core-js/parse-float'); require('@babel/runtime-corejs3/core-js/weak-map'); require('@babel/runtime-corejs3/core-js/global-this'); require('@babel/runtime-corejs3/core-js/instance/for-each'); require('eslint/use-at-your-own-risk'); require('eslint'); require('@babel/runtime-corejs3/core-js/object/get-own-property-descriptor'); require('@babel/runtime-corejs3/core-js/map'); require('@babel/runtime-corejs3/core-js/instance/every'); require('@babel/runtime-corejs3/core-js/instance/sort'); require('@babel/runtime-corejs3/core-js/array/of'); require('@babel/runtime-corejs3/core-js/instance/entries'); require('@babel/runtime-corejs3/core-js/instance/find-index'); require('@babel/runtime-corejs3/core-js/instance/flat'); require('@babel/runtime-corejs3/core-js/instance/values'); require('@babel/runtime-corejs3/core-js/object/get-own-property-names'); require('@babel/runtime-corejs3/core-js/number/is-finite'); require('@babel/runtime-corejs3/core-js/number/is-nan'); require('@babel/runtime-corejs3/core-js/number/parse-float'); require('@babel/runtime-corejs3/core-js/number/parse-int'); require('@babel/runtime-corejs3/core-js/object/is'); require('@babel/runtime-corejs3/core-js/object/is-extensible'); require('@babel/runtime-corejs3/core-js/object/is-frozen'); require('@babel/runtime-corejs3/core-js/object/is-sealed'); require('@babel/runtime-corejs3/core-js/object/values'); require('@babel/runtime-corejs3/core-js/string/from-code-point'); require('@babel/runtime-corejs3/core-js/string/raw'); require('@babel/runtime-corejs3/core-js/instance/code-point-at'); require('@babel/runtime-corejs3/core-js/instance/ends-with'); require('@babel/runtime-corejs3/core-js/instance/pad-end'); require('@babel/runtime-corejs3/core-js/instance/pad-start'); require('@babel/runtime-corejs3/core-js/instance/trim'); require('@babel/runtime-corejs3/core-js/instance/trim-end'); require('@babel/runtime-corejs3/core-js/instance/trim-left'); require('@babel/runtime-corejs3/core-js/instance/trim-right'); require('@babel/runtime-corejs3/core-js/instance/trim-start'); require('@babel/runtime-corejs3/core-js/symbol/for'); require('@babel/runtime-corejs3/core-js/symbol/key-for'); require('@babel/runtime-corejs3/core-js/object/prevent-extensions'); require('@babel/runtime-corejs3/core-js/object/seal'); require('@babel/runtime-corejs3/core-js/object/get-prototype-of'); require('@babel/runtime-corejs3/core-js/symbol/replace'); require('@babel/runtime-corejs3/core-js/instance/bind'); require('@babel/runtime-corejs3/core-js/instance/splice'); require('@babel/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;