jsx-pascal-case.js 4.1 KB

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