jsx-pascal-case.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @fileoverview Enforce PascalCase for user-defined JSX components
  3. * @author Jake Marsh
  4. */
  5. 'use strict';
  6. const elementType = require('jsx-ast-utils/elementType');
  7. const minimatch = require('minimatch');
  8. const docsUrl = require('../util/docsUrl');
  9. const jsxUtil = require('../util/jsx');
  10. const report = require('../util/report');
  11. function testDigit(char) {
  12. const charCode = char.charCodeAt(0);
  13. return charCode >= 48 && charCode <= 57;
  14. }
  15. function testUpperCase(char) {
  16. const upperCase = char.toUpperCase();
  17. return char === upperCase && upperCase !== char.toLowerCase();
  18. }
  19. function testLowerCase(char) {
  20. const lowerCase = char.toLowerCase();
  21. return char === lowerCase && lowerCase !== char.toUpperCase();
  22. }
  23. function testPascalCase(name) {
  24. if (!testUpperCase(name.charAt(0))) {
  25. return false;
  26. }
  27. const anyNonAlphaNumeric = Array.prototype.some.call(
  28. name.slice(1),
  29. (char) => char.toLowerCase() === char.toUpperCase() && !testDigit(char)
  30. );
  31. if (anyNonAlphaNumeric) {
  32. return false;
  33. }
  34. return Array.prototype.some.call(
  35. name.slice(1),
  36. (char) => testLowerCase(char) || testDigit(char)
  37. );
  38. }
  39. function testAllCaps(name) {
  40. const firstChar = name.charAt(0);
  41. if (!(testUpperCase(firstChar) || testDigit(firstChar))) {
  42. return false;
  43. }
  44. for (let i = 1; i < name.length - 1; i += 1) {
  45. const char = name.charAt(i);
  46. if (!(testUpperCase(char) || testDigit(char) || char === '_')) {
  47. return false;
  48. }
  49. }
  50. const lastChar = name.charAt(name.length - 1);
  51. if (!(testUpperCase(lastChar) || testDigit(lastChar))) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. function ignoreCheck(ignore, name) {
  57. return ignore.some(
  58. (entry) => name === entry || minimatch(name, entry, { noglobstar: true })
  59. );
  60. }
  61. // ------------------------------------------------------------------------------
  62. // Rule Definition
  63. // ------------------------------------------------------------------------------
  64. const messages = {
  65. usePascalCase: 'Imported JSX component {{name}} must be in PascalCase',
  66. usePascalOrSnakeCase: 'Imported JSX component {{name}} must be in PascalCase or SCREAMING_SNAKE_CASE',
  67. };
  68. module.exports = {
  69. meta: {
  70. docs: {
  71. description: 'Enforce PascalCase for user-defined JSX components',
  72. category: 'Stylistic Issues',
  73. recommended: false,
  74. url: docsUrl('jsx-pascal-case'),
  75. },
  76. messages,
  77. schema: [{
  78. type: 'object',
  79. properties: {
  80. allowAllCaps: {
  81. type: 'boolean',
  82. },
  83. allowLeadingUnderscore: {
  84. type: 'boolean',
  85. },
  86. allowNamespace: {
  87. type: 'boolean',
  88. },
  89. ignore: {
  90. items: [
  91. {
  92. type: 'string',
  93. },
  94. ],
  95. minItems: 0,
  96. type: 'array',
  97. uniqueItems: true,
  98. },
  99. },
  100. additionalProperties: false,
  101. }],
  102. },
  103. create(context) {
  104. const configuration = context.options[0] || {};
  105. const allowAllCaps = configuration.allowAllCaps || false;
  106. const allowLeadingUnderscore = configuration.allowLeadingUnderscore || false;
  107. const allowNamespace = configuration.allowNamespace || false;
  108. const ignore = configuration.ignore || [];
  109. return {
  110. JSXOpeningElement(node) {
  111. const isCompatTag = jsxUtil.isDOMComponent(node);
  112. if (isCompatTag) return undefined;
  113. const name = elementType(node);
  114. let checkNames = [name];
  115. let index = 0;
  116. if (name.lastIndexOf(':') > -1) {
  117. checkNames = name.split(':');
  118. } else if (name.lastIndexOf('.') > -1) {
  119. checkNames = name.split('.');
  120. }
  121. do {
  122. const splitName = checkNames[index];
  123. if (splitName.length === 1) return undefined;
  124. const isIgnored = ignoreCheck(ignore, splitName);
  125. const checkName = allowLeadingUnderscore && splitName.startsWith('_') ? splitName.slice(1) : splitName;
  126. const isPascalCase = testPascalCase(checkName);
  127. const isAllowedAllCaps = allowAllCaps && testAllCaps(checkName);
  128. if (!isPascalCase && !isAllowedAllCaps && !isIgnored) {
  129. const messageId = allowAllCaps ? 'usePascalOrSnakeCase' : 'usePascalCase';
  130. report(context, messages[messageId], messageId, {
  131. node,
  132. data: {
  133. name: splitName,
  134. },
  135. });
  136. break;
  137. }
  138. index += 1;
  139. } while (index < checkNames.length && !allowNamespace);
  140. },
  141. };
  142. },
  143. };