jsx-indent-props.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @fileoverview Validate props indentation in JSX
  3. * @author Yannick Croissant
  4. * This rule has been ported and modified from eslint and nodeca.
  5. * @author Vitaly Puzrin
  6. * @author Gyandeep Singh
  7. * @copyright 2015 Vitaly Puzrin. All rights reserved.
  8. * @copyright 2015 Gyandeep Singh. All rights reserved.
  9. Copyright (C) 2014 by Vitaly Puzrin
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the 'Software'), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. */
  26. 'use strict';
  27. const astUtil = require('../util/ast');
  28. const docsUrl = require('../util/docsUrl');
  29. // ------------------------------------------------------------------------------
  30. // Rule Definition
  31. // ------------------------------------------------------------------------------
  32. module.exports = {
  33. meta: {
  34. docs: {
  35. description: 'Validate props indentation in JSX',
  36. category: 'Stylistic Issues',
  37. recommended: false,
  38. url: docsUrl('jsx-indent-props')
  39. },
  40. fixable: 'code',
  41. schema: [{
  42. oneOf: [{
  43. enum: ['tab', 'first']
  44. }, {
  45. type: 'integer'
  46. }]
  47. }]
  48. },
  49. create: function(context) {
  50. const MESSAGE = 'Expected indentation of {{needed}} {{type}} {{characters}} but found {{gotten}}.';
  51. const extraColumnStart = 0;
  52. let indentType = 'space';
  53. let indentSize = 4;
  54. const sourceCode = context.getSourceCode();
  55. if (context.options.length) {
  56. if (context.options[0] === 'first') {
  57. indentSize = 'first';
  58. indentType = 'space';
  59. } else if (context.options[0] === 'tab') {
  60. indentSize = 1;
  61. indentType = 'tab';
  62. } else if (typeof context.options[0] === 'number') {
  63. indentSize = context.options[0];
  64. indentType = 'space';
  65. }
  66. }
  67. /**
  68. * Reports a given indent violation and properly pluralizes the message
  69. * @param {ASTNode} node Node violating the indent rule
  70. * @param {Number} needed Expected indentation character count
  71. * @param {Number} gotten Indentation character count in the actual node/code
  72. */
  73. function report(node, needed, gotten) {
  74. const msgContext = {
  75. needed: needed,
  76. type: indentType,
  77. characters: needed === 1 ? 'character' : 'characters',
  78. gotten: gotten
  79. };
  80. context.report({
  81. node: node,
  82. message: MESSAGE,
  83. data: msgContext,
  84. fix: function(fixer) {
  85. return fixer.replaceTextRange([node.range[0] - node.loc.start.column, node.range[0]],
  86. Array(needed + 1).join(indentType === 'space' ? ' ' : '\t'));
  87. }
  88. });
  89. }
  90. /**
  91. * Get node indent
  92. * @param {ASTNode} node Node to examine
  93. * @return {Number} Indent
  94. */
  95. function getNodeIndent(node) {
  96. let src = sourceCode.getText(node, node.loc.start.column + extraColumnStart);
  97. const lines = src.split('\n');
  98. src = lines[0];
  99. let regExp;
  100. if (indentType === 'space') {
  101. regExp = /^[ ]+/;
  102. } else {
  103. regExp = /^[\t]+/;
  104. }
  105. const indent = regExp.exec(src);
  106. return indent ? indent[0].length : 0;
  107. }
  108. /**
  109. * Check indent for nodes list
  110. * @param {ASTNode[]} nodes list of node objects
  111. * @param {Number} indent needed indent
  112. * @param {Boolean} excludeCommas skip comma on start of line
  113. */
  114. function checkNodesIndent(nodes, indent) {
  115. nodes.forEach(node => {
  116. const nodeIndent = getNodeIndent(node);
  117. if (
  118. node.type !== 'ArrayExpression' && node.type !== 'ObjectExpression' &&
  119. nodeIndent !== indent && astUtil.isNodeFirstInLine(context, node)
  120. ) {
  121. report(node, indent, nodeIndent);
  122. }
  123. });
  124. }
  125. return {
  126. JSXOpeningElement: function(node) {
  127. if (!node.attributes.length) {
  128. return;
  129. }
  130. let propIndent;
  131. if (indentSize === 'first') {
  132. const firstPropNode = node.attributes[0];
  133. propIndent = firstPropNode.loc.start.column;
  134. } else {
  135. const elementIndent = getNodeIndent(node);
  136. propIndent = elementIndent + indentSize;
  137. }
  138. checkNodesIndent(node.attributes, propIndent);
  139. }
  140. };
  141. }
  142. };