sort-default-props.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * @fileoverview Enforce default props alphabetical sorting
  3. * @author Vladimir Kattsov
  4. * @deprecated
  5. */
  6. 'use strict';
  7. const variableUtil = require('../util/variable');
  8. const docsUrl = require('../util/docsUrl');
  9. const report = require('../util/report');
  10. // ------------------------------------------------------------------------------
  11. // Rule Definition
  12. // ------------------------------------------------------------------------------
  13. const messages = {
  14. propsNotSorted: 'Default prop types declarations should be sorted alphabetically',
  15. };
  16. module.exports = {
  17. meta: {
  18. docs: {
  19. description: 'Enforce defaultProps declarations alphabetical sorting',
  20. category: 'Stylistic Issues',
  21. recommended: false,
  22. url: docsUrl('sort-default-props'),
  23. },
  24. // fixable: 'code',
  25. messages,
  26. schema: [{
  27. type: 'object',
  28. properties: {
  29. ignoreCase: {
  30. type: 'boolean',
  31. },
  32. },
  33. additionalProperties: false,
  34. }],
  35. },
  36. create(context) {
  37. const configuration = context.options[0] || {};
  38. const ignoreCase = configuration.ignoreCase || false;
  39. /**
  40. * Get properties name
  41. * @param {Object} node - Property.
  42. * @returns {String} Property name.
  43. */
  44. function getPropertyName(node) {
  45. if (node.key || ['MethodDefinition', 'Property'].indexOf(node.type) !== -1) {
  46. return node.key.name;
  47. }
  48. if (node.type === 'MemberExpression') {
  49. return node.property.name;
  50. // Special case for class properties
  51. // (babel-eslint@5 does not expose property name so we have to rely on tokens)
  52. }
  53. if (node.type === 'ClassProperty') {
  54. const tokens = context.getSourceCode().getFirstTokens(node, 2);
  55. return tokens[1] && tokens[1].type === 'Identifier' ? tokens[1].value : tokens[0].value;
  56. }
  57. return '';
  58. }
  59. /**
  60. * Checks if the Identifier node passed in looks like a defaultProps declaration.
  61. * @param {ASTNode} node The node to check. Must be an Identifier node.
  62. * @returns {Boolean} `true` if the node is a defaultProps declaration, `false` if not
  63. */
  64. function isDefaultPropsDeclaration(node) {
  65. const propName = getPropertyName(node);
  66. return (propName === 'defaultProps' || propName === 'getDefaultProps');
  67. }
  68. function getKey(node) {
  69. return context.getSourceCode().getText(node.key || node.argument);
  70. }
  71. /**
  72. * Find a variable by name in the current scope.
  73. * @param {string} name Name of the variable to look for.
  74. * @returns {ASTNode|null} Return null if the variable could not be found, ASTNode otherwise.
  75. */
  76. function findVariableByName(name) {
  77. const variable = variableUtil.variablesInScope(context).find((item) => item.name === name);
  78. if (!variable || !variable.defs[0] || !variable.defs[0].node) {
  79. return null;
  80. }
  81. if (variable.defs[0].node.type === 'TypeAlias') {
  82. return variable.defs[0].node.right;
  83. }
  84. return variable.defs[0].node.init;
  85. }
  86. /**
  87. * Checks if defaultProps declarations are sorted
  88. * @param {Array} declarations The array of AST nodes being checked.
  89. * @returns {void}
  90. */
  91. function checkSorted(declarations) {
  92. // function fix(fixer) {
  93. // return propTypesSortUtil.fixPropTypesSort(fixer, context, declarations, ignoreCase);
  94. // }
  95. declarations.reduce((prev, curr, idx, decls) => {
  96. if (/Spread(?:Property|Element)$/.test(curr.type)) {
  97. return decls[idx + 1];
  98. }
  99. let prevPropName = getKey(prev);
  100. let currentPropName = getKey(curr);
  101. if (ignoreCase) {
  102. prevPropName = prevPropName.toLowerCase();
  103. currentPropName = currentPropName.toLowerCase();
  104. }
  105. if (currentPropName < prevPropName) {
  106. report(context, messages.propsNotSorted, 'propsNotSorted', {
  107. node: curr,
  108. // fix
  109. });
  110. return prev;
  111. }
  112. return curr;
  113. }, declarations[0]);
  114. }
  115. function checkNode(node) {
  116. if (!node) {
  117. return;
  118. }
  119. if (node.type === 'ObjectExpression') {
  120. checkSorted(node.properties);
  121. } else if (node.type === 'Identifier') {
  122. const propTypesObject = findVariableByName(node.name);
  123. if (propTypesObject && propTypesObject.properties) {
  124. checkSorted(propTypesObject.properties);
  125. }
  126. }
  127. }
  128. // --------------------------------------------------------------------------
  129. // Public API
  130. // --------------------------------------------------------------------------
  131. return {
  132. 'ClassProperty, PropertyDefinition'(node) {
  133. if (!isDefaultPropsDeclaration(node)) {
  134. return;
  135. }
  136. checkNode(node.value);
  137. },
  138. MemberExpression(node) {
  139. if (!isDefaultPropsDeclaration(node)) {
  140. return;
  141. }
  142. checkNode(node.parent.right);
  143. },
  144. };
  145. },
  146. };