UNPKG

13.4 kBJavaScriptView Raw
1/**
2 * @fileoverview Validate JSX indentation
3 * @author Yannick Croissant
4
5 * This rule has been ported and modified from eslint and nodeca.
6 * @author Vitaly Puzrin
7 * @author Gyandeep Singh
8 * @copyright 2015 Vitaly Puzrin. All rights reserved.
9 * @copyright 2015 Gyandeep Singh. All rights reserved.
10 Copyright (C) 2014 by Vitaly Puzrin
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the 'Software'), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in
20 all copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
29 */
30
31'use strict';
32
33const matchAll = require('string.prototype.matchall');
34
35const astUtil = require('../util/ast');
36const docsUrl = require('../util/docsUrl');
37
38// ------------------------------------------------------------------------------
39// Rule Definition
40// ------------------------------------------------------------------------------
41module.exports = {
42 meta: {
43 docs: {
44 description: 'Validate JSX indentation',
45 category: 'Stylistic Issues',
46 recommended: false,
47 url: docsUrl('jsx-indent')
48 },
49 fixable: 'whitespace',
50 schema: [{
51 oneOf: [{
52 enum: ['tab']
53 }, {
54 type: 'integer'
55 }]
56 }, {
57 type: 'object',
58 properties: {
59 checkAttributes: {
60 type: 'boolean'
61 },
62 indentLogicalExpressions: {
63 type: 'boolean'
64 }
65 },
66 additionalProperties: false
67 }]
68 },
69
70 create(context) {
71 const MESSAGE = 'Expected indentation of {{needed}} {{type}} {{characters}} but found {{gotten}}.';
72
73 const extraColumnStart = 0;
74 let indentType = 'space';
75 let indentSize = 4;
76
77 if (context.options.length) {
78 if (context.options[0] === 'tab') {
79 indentSize = 1;
80 indentType = 'tab';
81 } else if (typeof context.options[0] === 'number') {
82 indentSize = context.options[0];
83 indentType = 'space';
84 }
85 }
86
87 const indentChar = indentType === 'space' ? ' ' : '\t';
88 const options = context.options[1] || {};
89 const checkAttributes = options.checkAttributes || false;
90 const indentLogicalExpressions = options.indentLogicalExpressions || false;
91
92 /**
93 * Responsible for fixing the indentation issue fix
94 * @param {ASTNode} node Node violating the indent rule
95 * @param {Number} needed Expected indentation character count
96 * @returns {Function} function to be executed by the fixer
97 * @private
98 */
99 function getFixerFunction(node, needed) {
100 return function fix(fixer) {
101 const indent = Array(needed + 1).join(indentChar);
102 if (node.type === 'JSXText' || node.type === 'Literal') {
103 const regExp = /\n[\t ]*(\S)/g;
104 const fixedText = node.raw.replace(regExp, (match, p1) => `\n${indent}${p1}`);
105 return fixer.replaceText(node, fixedText);
106 }
107 return fixer.replaceTextRange(
108 [node.range[0] - node.loc.start.column, node.range[0]],
109 indent
110 );
111 };
112 }
113
114 /**
115 * Reports a given indent violation and properly pluralizes the message
116 * @param {ASTNode} node Node violating the indent rule
117 * @param {Number} needed Expected indentation character count
118 * @param {Number} gotten Indentation character count in the actual node/code
119 * @param {Object} [loc] Error line and column location
120 */
121 function report(node, needed, gotten, loc) {
122 const msgContext = {
123 needed,
124 type: indentType,
125 characters: needed === 1 ? 'character' : 'characters',
126 gotten
127 };
128
129 if (loc) {
130 context.report({
131 node,
132 loc,
133 message: MESSAGE,
134 data: msgContext,
135 fix: getFixerFunction(node, needed)
136 });
137 } else {
138 context.report({
139 node,
140 message: MESSAGE,
141 data: msgContext,
142 fix: getFixerFunction(node, needed)
143 });
144 }
145 }
146
147 /**
148 * Get node indent
149 * @param {ASTNode} node Node to examine
150 * @param {Boolean} [byLastLine] get indent of node's last line
151 * @param {Boolean} [excludeCommas] skip comma on start of line
152 * @return {Number} Indent
153 */
154 function getNodeIndent(node, byLastLine, excludeCommas) {
155 byLastLine = byLastLine || false;
156 excludeCommas = excludeCommas || false;
157
158 let src = context.getSourceCode().getText(node, node.loc.start.column + extraColumnStart);
159 const lines = src.split('\n');
160 if (byLastLine) {
161 src = lines[lines.length - 1];
162 } else {
163 src = lines[0];
164 }
165
166 const skip = excludeCommas ? ',' : '';
167
168 let regExp;
169 if (indentType === 'space') {
170 regExp = new RegExp(`^[ ${skip}]+`);
171 } else {
172 regExp = new RegExp(`^[\t${skip}]+`);
173 }
174
175 const indent = regExp.exec(src);
176 return indent ? indent[0].length : 0;
177 }
178
179 /**
180 * Check if the node is the right member of a logical expression
181 * @param {ASTNode} node The node to check
182 * @return {Boolean} true if its the case, false if not
183 */
184 function isRightInLogicalExp(node) {
185 return (
186 node.parent
187 && node.parent.parent
188 && node.parent.parent.type === 'LogicalExpression'
189 && node.parent.parent.right === node.parent
190 && !indentLogicalExpressions
191 );
192 }
193
194 /**
195 * Check if the node is the alternate member of a conditional expression
196 * @param {ASTNode} node The node to check
197 * @return {Boolean} true if its the case, false if not
198 */
199 function isAlternateInConditionalExp(node) {
200 return (
201 node.parent
202 && node.parent.parent
203 && node.parent.parent.type === 'ConditionalExpression'
204 && node.parent.parent.alternate === node.parent
205 && context.getSourceCode().getTokenBefore(node).value !== '('
206 );
207 }
208
209 /**
210 * Check if the node is within a DoExpression block but not the first expression (which need to be indented)
211 * @param {ASTNode} node The node to check
212 * @return {Boolean} true if its the case, false if not
213 */
214 function isSecondOrSubsequentExpWithinDoExp(node) {
215 /*
216 It returns true when node.parent.parent.parent.parent matches:
217
218 DoExpression({
219 ...,
220 body: BlockStatement({
221 ...,
222 body: [
223 ..., // 1-n times
224 ExpressionStatement({
225 ...,
226 expression: JSXElement({
227 ...,
228 openingElement: JSXOpeningElement() // the node
229 })
230 }),
231 ... // 0-n times
232 ]
233 })
234 })
235
236 except:
237
238 DoExpression({
239 ...,
240 body: BlockStatement({
241 ...,
242 body: [
243 ExpressionStatement({
244 ...,
245 expression: JSXElement({
246 ...,
247 openingElement: JSXOpeningElement() // the node
248 })
249 }),
250 ... // 0-n times
251 ]
252 })
253 })
254 */
255 const isInExpStmt = (
256 node.parent
257 && node.parent.parent
258 && node.parent.parent.type === 'ExpressionStatement'
259 );
260 if (!isInExpStmt) {
261 return false;
262 }
263
264 const expStmt = node.parent.parent;
265 const isInBlockStmtWithinDoExp = (
266 expStmt.parent
267 && expStmt.parent.type === 'BlockStatement'
268 && expStmt.parent.parent
269 && expStmt.parent.parent.type === 'DoExpression'
270 );
271 if (!isInBlockStmtWithinDoExp) {
272 return false;
273 }
274
275 const blockStmt = expStmt.parent;
276 const blockStmtFirstExp = blockStmt.body[0];
277 return !(blockStmtFirstExp === expStmt);
278 }
279
280 /**
281 * Check indent for nodes list
282 * @param {ASTNode} node The node to check
283 * @param {Number} indent needed indent
284 * @param {Boolean} [excludeCommas] skip comma on start of line
285 */
286 function checkNodesIndent(node, indent, excludeCommas) {
287 const nodeIndent = getNodeIndent(node, false, excludeCommas);
288 const isCorrectRightInLogicalExp = isRightInLogicalExp(node) && (nodeIndent - indent) === indentSize;
289 const isCorrectAlternateInCondExp = isAlternateInConditionalExp(node) && (nodeIndent - indent) === 0;
290 if (
291 nodeIndent !== indent
292 && astUtil.isNodeFirstInLine(context, node)
293 && !isCorrectRightInLogicalExp
294 && !isCorrectAlternateInCondExp
295 ) {
296 report(node, indent, nodeIndent);
297 }
298 }
299
300 /**
301 * Check indent for Literal Node or JSXText Node
302 * @param {ASTNode} node The node to check
303 * @param {Number} indent needed indent
304 */
305 function checkLiteralNodeIndent(node, indent) {
306 const value = node.value;
307 const regExp = indentType === 'space' ? /\n( *)[\t ]*\S/g : /\n(\t*)[\t ]*\S/g;
308 const nodeIndentsPerLine = Array.from(
309 matchAll(String(value), regExp),
310 (match) => (match[1] ? match[1].length : 0)
311 );
312 const hasFirstInLineNode = nodeIndentsPerLine.length > 0;
313 if (
314 hasFirstInLineNode
315 && !nodeIndentsPerLine.every((actualIndent) => actualIndent === indent)
316 ) {
317 nodeIndentsPerLine.forEach((nodeIndent) => {
318 report(node, indent, nodeIndent);
319 });
320 }
321 }
322
323 function handleOpeningElement(node) {
324 const sourceCode = context.getSourceCode();
325 let prevToken = sourceCode.getTokenBefore(node);
326 if (!prevToken) {
327 return;
328 }
329 // Use the parent in a list or an array
330 if (prevToken.type === 'JSXText' || prevToken.type === 'Punctuator' && prevToken.value === ',') {
331 prevToken = sourceCode.getNodeByRangeIndex(prevToken.range[0]);
332 prevToken = prevToken.type === 'Literal' || prevToken.type === 'JSXText' ? prevToken.parent : prevToken;
333 // Use the first non-punctuator token in a conditional expression
334 } else if (prevToken.type === 'Punctuator' && prevToken.value === ':') {
335 do {
336 prevToken = sourceCode.getTokenBefore(prevToken);
337 } while (prevToken.type === 'Punctuator' && prevToken.value !== '/');
338 prevToken = sourceCode.getNodeByRangeIndex(prevToken.range[0]);
339 while (prevToken.parent && prevToken.parent.type !== 'ConditionalExpression') {
340 prevToken = prevToken.parent;
341 }
342 }
343 prevToken = prevToken.type === 'JSXExpressionContainer' ? prevToken.expression : prevToken;
344 const parentElementIndent = getNodeIndent(prevToken);
345 const indent = (
346 prevToken.loc.start.line === node.loc.start.line
347 || isRightInLogicalExp(node)
348 || isAlternateInConditionalExp(node)
349 || isSecondOrSubsequentExpWithinDoExp(node)
350 ) ? 0 : indentSize;
351 checkNodesIndent(node, parentElementIndent + indent);
352 }
353
354 function handleClosingElement(node) {
355 if (!node.parent) {
356 return;
357 }
358 const peerElementIndent = getNodeIndent(node.parent.openingElement || node.parent.openingFragment);
359 checkNodesIndent(node, peerElementIndent);
360 }
361
362 function handleAttribute(node) {
363 if (!checkAttributes || (!node.value || node.value.type !== 'JSXExpressionContainer')) {
364 return;
365 }
366 const nameIndent = getNodeIndent(node.name);
367 const lastToken = context.getSourceCode().getLastToken(node.value);
368 const firstInLine = astUtil.getFirstNodeInLine(context, lastToken);
369 const indent = node.name.loc.start.line === firstInLine.loc.start.line ? 0 : nameIndent;
370 checkNodesIndent(firstInLine, indent);
371 }
372
373 function handleLiteral(node) {
374 if (!node.parent) {
375 return;
376 }
377 if (node.parent.type !== 'JSXElement' && node.parent.type !== 'JSXFragment') {
378 return;
379 }
380 const parentNodeIndent = getNodeIndent(node.parent);
381 checkLiteralNodeIndent(node, parentNodeIndent + indentSize);
382 }
383
384 return {
385 JSXOpeningElement: handleOpeningElement,
386 JSXOpeningFragment: handleOpeningElement,
387 JSXClosingElement: handleClosingElement,
388 JSXClosingFragment: handleClosingElement,
389 JSXAttribute: handleAttribute,
390 JSXExpressionContainer(node) {
391 if (!node.parent) {
392 return;
393 }
394 const parentNodeIndent = getNodeIndent(node.parent);
395 checkNodesIndent(node, parentNodeIndent + indentSize);
396 },
397 Literal: handleLiteral,
398 JSXText: handleLiteral
399 };
400 }
401};