jsx-max-props-per-line.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @fileoverview Limit maximum of props on a single line in JSX
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. const docsUrl = require('../util/docsUrl');
  7. // ------------------------------------------------------------------------------
  8. // Rule Definition
  9. // ------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: 'Limit maximum of props on a single line in JSX',
  14. category: 'Stylistic Issues',
  15. recommended: false,
  16. url: docsUrl('jsx-max-props-per-line')
  17. },
  18. fixable: 'code',
  19. messages: {
  20. newLine: 'Prop `{{prop}}` must be placed on a new line'
  21. },
  22. schema: [{
  23. type: 'object',
  24. properties: {
  25. maximum: {
  26. type: 'integer',
  27. minimum: 1
  28. },
  29. when: {
  30. type: 'string',
  31. enum: ['always', 'multiline']
  32. }
  33. }
  34. }]
  35. },
  36. create(context) {
  37. const configuration = context.options[0] || {};
  38. const maximum = configuration.maximum || 1;
  39. const when = configuration.when || 'always';
  40. function getPropName(propNode) {
  41. if (propNode.type === 'JSXSpreadAttribute') {
  42. return context.getSourceCode().getText(propNode.argument);
  43. }
  44. return propNode.name.name;
  45. }
  46. function generateFixFunction(line, max) {
  47. const sourceCode = context.getSourceCode();
  48. const output = [];
  49. const front = line[0].range[0];
  50. const back = line[line.length - 1].range[1];
  51. for (let i = 0; i < line.length; i += max) {
  52. const nodes = line.slice(i, i + max);
  53. output.push(nodes.reduce((prev, curr) => {
  54. if (prev === '') {
  55. return sourceCode.getText(curr);
  56. }
  57. return `${prev} ${sourceCode.getText(curr)}`;
  58. }, ''));
  59. }
  60. const code = output.join('\n');
  61. return function fix(fixer) {
  62. return fixer.replaceTextRange([front, back], code);
  63. };
  64. }
  65. return {
  66. JSXOpeningElement(node) {
  67. if (!node.attributes.length) {
  68. return;
  69. }
  70. if (when === 'multiline' && node.loc.start.line === node.loc.end.line) {
  71. return;
  72. }
  73. const firstProp = node.attributes[0];
  74. const linePartitionedProps = [[firstProp]];
  75. node.attributes.reduce((last, decl) => {
  76. if (last.loc.end.line === decl.loc.start.line) {
  77. linePartitionedProps[linePartitionedProps.length - 1].push(decl);
  78. } else {
  79. linePartitionedProps.push([decl]);
  80. }
  81. return decl;
  82. });
  83. linePartitionedProps.forEach((propsInLine) => {
  84. if (propsInLine.length > maximum) {
  85. const name = getPropName(propsInLine[maximum]);
  86. context.report({
  87. node: propsInLine[maximum],
  88. messageId: 'newLine',
  89. data: {
  90. prop: name
  91. },
  92. fix: generateFixFunction(propsInLine, maximum)
  93. });
  94. }
  95. });
  96. }
  97. };
  98. }
  99. };