indent-legacy.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /**
  2. * @fileoverview This option sets a specific tab width for your code
  3. *
  4. * This rule has been ported and modified from nodeca.
  5. * @author Vitaly Puzrin
  6. * @author Gyandeep Singh
  7. */
  8. "use strict";
  9. //------------------------------------------------------------------------------
  10. // Requirements
  11. //------------------------------------------------------------------------------
  12. const astUtils = require("./utils/ast-utils");
  13. //------------------------------------------------------------------------------
  14. // Rule Definition
  15. //------------------------------------------------------------------------------
  16. /* istanbul ignore next: this rule has known coverage issues, but it's deprecated and shouldn't be updated in the future anyway. */
  17. module.exports = {
  18. meta: {
  19. type: "layout",
  20. docs: {
  21. description: "enforce consistent indentation",
  22. category: "Stylistic Issues",
  23. recommended: false,
  24. url: "https://eslint.org/docs/rules/indent-legacy"
  25. },
  26. deprecated: true,
  27. replacedBy: ["indent"],
  28. fixable: "whitespace",
  29. schema: [
  30. {
  31. oneOf: [
  32. {
  33. enum: ["tab"]
  34. },
  35. {
  36. type: "integer",
  37. minimum: 0
  38. }
  39. ]
  40. },
  41. {
  42. type: "object",
  43. properties: {
  44. SwitchCase: {
  45. type: "integer",
  46. minimum: 0
  47. },
  48. VariableDeclarator: {
  49. oneOf: [
  50. {
  51. type: "integer",
  52. minimum: 0
  53. },
  54. {
  55. type: "object",
  56. properties: {
  57. var: {
  58. type: "integer",
  59. minimum: 0
  60. },
  61. let: {
  62. type: "integer",
  63. minimum: 0
  64. },
  65. const: {
  66. type: "integer",
  67. minimum: 0
  68. }
  69. }
  70. }
  71. ]
  72. },
  73. outerIIFEBody: {
  74. type: "integer",
  75. minimum: 0
  76. },
  77. MemberExpression: {
  78. type: "integer",
  79. minimum: 0
  80. },
  81. FunctionDeclaration: {
  82. type: "object",
  83. properties: {
  84. parameters: {
  85. oneOf: [
  86. {
  87. type: "integer",
  88. minimum: 0
  89. },
  90. {
  91. enum: ["first"]
  92. }
  93. ]
  94. },
  95. body: {
  96. type: "integer",
  97. minimum: 0
  98. }
  99. }
  100. },
  101. FunctionExpression: {
  102. type: "object",
  103. properties: {
  104. parameters: {
  105. oneOf: [
  106. {
  107. type: "integer",
  108. minimum: 0
  109. },
  110. {
  111. enum: ["first"]
  112. }
  113. ]
  114. },
  115. body: {
  116. type: "integer",
  117. minimum: 0
  118. }
  119. }
  120. },
  121. CallExpression: {
  122. type: "object",
  123. properties: {
  124. parameters: {
  125. oneOf: [
  126. {
  127. type: "integer",
  128. minimum: 0
  129. },
  130. {
  131. enum: ["first"]
  132. }
  133. ]
  134. }
  135. }
  136. },
  137. ArrayExpression: {
  138. oneOf: [
  139. {
  140. type: "integer",
  141. minimum: 0
  142. },
  143. {
  144. enum: ["first"]
  145. }
  146. ]
  147. },
  148. ObjectExpression: {
  149. oneOf: [
  150. {
  151. type: "integer",
  152. minimum: 0
  153. },
  154. {
  155. enum: ["first"]
  156. }
  157. ]
  158. }
  159. },
  160. additionalProperties: false
  161. }
  162. ],
  163. messages: {
  164. expected: "Expected indentation of {{expected}} but found {{actual}}."
  165. }
  166. },
  167. create(context) {
  168. const DEFAULT_VARIABLE_INDENT = 1;
  169. const DEFAULT_PARAMETER_INDENT = null; // For backwards compatibility, don't check parameter indentation unless specified in the config
  170. const DEFAULT_FUNCTION_BODY_INDENT = 1;
  171. let indentType = "space";
  172. let indentSize = 4;
  173. const options = {
  174. SwitchCase: 0,
  175. VariableDeclarator: {
  176. var: DEFAULT_VARIABLE_INDENT,
  177. let: DEFAULT_VARIABLE_INDENT,
  178. const: DEFAULT_VARIABLE_INDENT
  179. },
  180. outerIIFEBody: null,
  181. FunctionDeclaration: {
  182. parameters: DEFAULT_PARAMETER_INDENT,
  183. body: DEFAULT_FUNCTION_BODY_INDENT
  184. },
  185. FunctionExpression: {
  186. parameters: DEFAULT_PARAMETER_INDENT,
  187. body: DEFAULT_FUNCTION_BODY_INDENT
  188. },
  189. CallExpression: {
  190. arguments: DEFAULT_PARAMETER_INDENT
  191. },
  192. ArrayExpression: 1,
  193. ObjectExpression: 1
  194. };
  195. const sourceCode = context.getSourceCode();
  196. if (context.options.length) {
  197. if (context.options[0] === "tab") {
  198. indentSize = 1;
  199. indentType = "tab";
  200. } else /* istanbul ignore else : this will be caught by options validation */ if (typeof context.options[0] === "number") {
  201. indentSize = context.options[0];
  202. indentType = "space";
  203. }
  204. if (context.options[1]) {
  205. const opts = context.options[1];
  206. options.SwitchCase = opts.SwitchCase || 0;
  207. const variableDeclaratorRules = opts.VariableDeclarator;
  208. if (typeof variableDeclaratorRules === "number") {
  209. options.VariableDeclarator = {
  210. var: variableDeclaratorRules,
  211. let: variableDeclaratorRules,
  212. const: variableDeclaratorRules
  213. };
  214. } else if (typeof variableDeclaratorRules === "object") {
  215. Object.assign(options.VariableDeclarator, variableDeclaratorRules);
  216. }
  217. if (typeof opts.outerIIFEBody === "number") {
  218. options.outerIIFEBody = opts.outerIIFEBody;
  219. }
  220. if (typeof opts.MemberExpression === "number") {
  221. options.MemberExpression = opts.MemberExpression;
  222. }
  223. if (typeof opts.FunctionDeclaration === "object") {
  224. Object.assign(options.FunctionDeclaration, opts.FunctionDeclaration);
  225. }
  226. if (typeof opts.FunctionExpression === "object") {
  227. Object.assign(options.FunctionExpression, opts.FunctionExpression);
  228. }
  229. if (typeof opts.CallExpression === "object") {
  230. Object.assign(options.CallExpression, opts.CallExpression);
  231. }
  232. if (typeof opts.ArrayExpression === "number" || typeof opts.ArrayExpression === "string") {
  233. options.ArrayExpression = opts.ArrayExpression;
  234. }
  235. if (typeof opts.ObjectExpression === "number" || typeof opts.ObjectExpression === "string") {
  236. options.ObjectExpression = opts.ObjectExpression;
  237. }
  238. }
  239. }
  240. const caseIndentStore = {};
  241. /**
  242. * Creates an error message for a line, given the expected/actual indentation.
  243. * @param {int} expectedAmount The expected amount of indentation characters for this line
  244. * @param {int} actualSpaces The actual number of indentation spaces that were found on this line
  245. * @param {int} actualTabs The actual number of indentation tabs that were found on this line
  246. * @returns {string} An error message for this line
  247. */
  248. function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) {
  249. const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs"
  250. const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space"
  251. const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs"
  252. let foundStatement;
  253. if (actualSpaces > 0 && actualTabs > 0) {
  254. foundStatement = `${actualSpaces} ${foundSpacesWord} and ${actualTabs} ${foundTabsWord}`; // e.g. "1 space and 2 tabs"
  255. } else if (actualSpaces > 0) {
  256. /*
  257. * Abbreviate the message if the expected indentation is also spaces.
  258. * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
  259. */
  260. foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`;
  261. } else if (actualTabs > 0) {
  262. foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`;
  263. } else {
  264. foundStatement = "0";
  265. }
  266. return {
  267. expected: expectedStatement,
  268. actual: foundStatement
  269. };
  270. }
  271. /**
  272. * Reports a given indent violation
  273. * @param {ASTNode} node Node violating the indent rule
  274. * @param {int} needed Expected indentation character count
  275. * @param {int} gottenSpaces Indentation space count in the actual node/code
  276. * @param {int} gottenTabs Indentation tab count in the actual node/code
  277. * @param {Object} [loc] Error line and column location
  278. * @param {boolean} isLastNodeCheck Is the error for last node check
  279. * @returns {void}
  280. */
  281. function report(node, needed, gottenSpaces, gottenTabs, loc, isLastNodeCheck) {
  282. if (gottenSpaces && gottenTabs) {
  283. // To avoid conflicts with `no-mixed-spaces-and-tabs`, don't report lines that have both spaces and tabs.
  284. return;
  285. }
  286. const desiredIndent = (indentType === "space" ? " " : "\t").repeat(needed);
  287. const textRange = isLastNodeCheck
  288. ? [node.range[1] - node.loc.end.column, node.range[1] - node.loc.end.column + gottenSpaces + gottenTabs]
  289. : [node.range[0] - node.loc.start.column, node.range[0] - node.loc.start.column + gottenSpaces + gottenTabs];
  290. context.report({
  291. node,
  292. loc,
  293. messageId: "expected",
  294. data: createErrorMessageData(needed, gottenSpaces, gottenTabs),
  295. fix: fixer => fixer.replaceTextRange(textRange, desiredIndent)
  296. });
  297. }
  298. /**
  299. * Get the actual indent of node
  300. * @param {ASTNode|Token} node Node to examine
  301. * @param {boolean} [byLastLine=false] get indent of node's last line
  302. * @returns {Object} The node's indent. Contains keys `space` and `tab`, representing the indent of each character. Also
  303. * contains keys `goodChar` and `badChar`, where `goodChar` is the amount of the user's desired indentation character, and
  304. * `badChar` is the amount of the other indentation character.
  305. */
  306. function getNodeIndent(node, byLastLine) {
  307. const token = byLastLine ? sourceCode.getLastToken(node) : sourceCode.getFirstToken(node);
  308. const srcCharsBeforeNode = sourceCode.getText(token, token.loc.start.column).split("");
  309. const indentChars = srcCharsBeforeNode.slice(0, srcCharsBeforeNode.findIndex(char => char !== " " && char !== "\t"));
  310. const spaces = indentChars.filter(char => char === " ").length;
  311. const tabs = indentChars.filter(char => char === "\t").length;
  312. return {
  313. space: spaces,
  314. tab: tabs,
  315. goodChar: indentType === "space" ? spaces : tabs,
  316. badChar: indentType === "space" ? tabs : spaces
  317. };
  318. }
  319. /**
  320. * Checks node is the first in its own start line. By default it looks by start line.
  321. * @param {ASTNode} node The node to check
  322. * @param {boolean} [byEndLocation=false] Lookup based on start position or end
  323. * @returns {boolean} true if its the first in the its start line
  324. */
  325. function isNodeFirstInLine(node, byEndLocation) {
  326. const firstToken = byEndLocation === true ? sourceCode.getLastToken(node, 1) : sourceCode.getTokenBefore(node),
  327. startLine = byEndLocation === true ? node.loc.end.line : node.loc.start.line,
  328. endLine = firstToken ? firstToken.loc.end.line : -1;
  329. return startLine !== endLine;
  330. }
  331. /**
  332. * Check indent for node
  333. * @param {ASTNode} node Node to check
  334. * @param {int} neededIndent needed indent
  335. * @returns {void}
  336. */
  337. function checkNodeIndent(node, neededIndent) {
  338. const actualIndent = getNodeIndent(node, false);
  339. if (
  340. node.type !== "ArrayExpression" &&
  341. node.type !== "ObjectExpression" &&
  342. (actualIndent.goodChar !== neededIndent || actualIndent.badChar !== 0) &&
  343. isNodeFirstInLine(node)
  344. ) {
  345. report(node, neededIndent, actualIndent.space, actualIndent.tab);
  346. }
  347. if (node.type === "IfStatement" && node.alternate) {
  348. const elseToken = sourceCode.getTokenBefore(node.alternate);
  349. checkNodeIndent(elseToken, neededIndent);
  350. if (!isNodeFirstInLine(node.alternate)) {
  351. checkNodeIndent(node.alternate, neededIndent);
  352. }
  353. }
  354. if (node.type === "TryStatement" && node.handler) {
  355. const catchToken = sourceCode.getFirstToken(node.handler);
  356. checkNodeIndent(catchToken, neededIndent);
  357. }
  358. if (node.type === "TryStatement" && node.finalizer) {
  359. const finallyToken = sourceCode.getTokenBefore(node.finalizer);
  360. checkNodeIndent(finallyToken, neededIndent);
  361. }
  362. if (node.type === "DoWhileStatement") {
  363. const whileToken = sourceCode.getTokenAfter(node.body);
  364. checkNodeIndent(whileToken, neededIndent);
  365. }
  366. }
  367. /**
  368. * Check indent for nodes list
  369. * @param {ASTNode[]} nodes list of node objects
  370. * @param {int} indent needed indent
  371. * @returns {void}
  372. */
  373. function checkNodesIndent(nodes, indent) {
  374. nodes.forEach(node => checkNodeIndent(node, indent));
  375. }
  376. /**
  377. * Check last node line indent this detects, that block closed correctly
  378. * @param {ASTNode} node Node to examine
  379. * @param {int} lastLineIndent needed indent
  380. * @returns {void}
  381. */
  382. function checkLastNodeLineIndent(node, lastLineIndent) {
  383. const lastToken = sourceCode.getLastToken(node);
  384. const endIndent = getNodeIndent(lastToken, true);
  385. if ((endIndent.goodChar !== lastLineIndent || endIndent.badChar !== 0) && isNodeFirstInLine(node, true)) {
  386. report(
  387. node,
  388. lastLineIndent,
  389. endIndent.space,
  390. endIndent.tab,
  391. { line: lastToken.loc.start.line, column: lastToken.loc.start.column },
  392. true
  393. );
  394. }
  395. }
  396. /**
  397. * Check last node line indent this detects, that block closed correctly
  398. * This function for more complicated return statement case, where closing parenthesis may be followed by ';'
  399. * @param {ASTNode} node Node to examine
  400. * @param {int} firstLineIndent first line needed indent
  401. * @returns {void}
  402. */
  403. function checkLastReturnStatementLineIndent(node, firstLineIndent) {
  404. /*
  405. * in case if return statement ends with ');' we have traverse back to ')'
  406. * otherwise we'll measure indent for ';' and replace ')'
  407. */
  408. const lastToken = sourceCode.getLastToken(node, astUtils.isClosingParenToken);
  409. const textBeforeClosingParenthesis = sourceCode.getText(lastToken, lastToken.loc.start.column).slice(0, -1);
  410. if (textBeforeClosingParenthesis.trim()) {
  411. // There are tokens before the closing paren, don't report this case
  412. return;
  413. }
  414. const endIndent = getNodeIndent(lastToken, true);
  415. if (endIndent.goodChar !== firstLineIndent) {
  416. report(
  417. node,
  418. firstLineIndent,
  419. endIndent.space,
  420. endIndent.tab,
  421. { line: lastToken.loc.start.line, column: lastToken.loc.start.column },
  422. true
  423. );
  424. }
  425. }
  426. /**
  427. * Check first node line indent is correct
  428. * @param {ASTNode} node Node to examine
  429. * @param {int} firstLineIndent needed indent
  430. * @returns {void}
  431. */
  432. function checkFirstNodeLineIndent(node, firstLineIndent) {
  433. const startIndent = getNodeIndent(node, false);
  434. if ((startIndent.goodChar !== firstLineIndent || startIndent.badChar !== 0) && isNodeFirstInLine(node)) {
  435. report(
  436. node,
  437. firstLineIndent,
  438. startIndent.space,
  439. startIndent.tab,
  440. { line: node.loc.start.line, column: node.loc.start.column }
  441. );
  442. }
  443. }
  444. /**
  445. * Returns a parent node of given node based on a specified type
  446. * if not present then return null
  447. * @param {ASTNode} node node to examine
  448. * @param {string} type type that is being looked for
  449. * @param {string} stopAtList end points for the evaluating code
  450. * @returns {ASTNode|void} if found then node otherwise null
  451. */
  452. function getParentNodeByType(node, type, stopAtList) {
  453. let parent = node.parent;
  454. const stopAtSet = new Set(stopAtList || ["Program"]);
  455. while (parent.type !== type && !stopAtSet.has(parent.type) && parent.type !== "Program") {
  456. parent = parent.parent;
  457. }
  458. return parent.type === type ? parent : null;
  459. }
  460. /**
  461. * Returns the VariableDeclarator based on the current node
  462. * if not present then return null
  463. * @param {ASTNode} node node to examine
  464. * @returns {ASTNode|void} if found then node otherwise null
  465. */
  466. function getVariableDeclaratorNode(node) {
  467. return getParentNodeByType(node, "VariableDeclarator");
  468. }
  469. /**
  470. * Check to see if the node is part of the multi-line variable declaration.
  471. * Also if its on the same line as the varNode
  472. * @param {ASTNode} node node to check
  473. * @param {ASTNode} varNode variable declaration node to check against
  474. * @returns {boolean} True if all the above condition satisfy
  475. */
  476. function isNodeInVarOnTop(node, varNode) {
  477. return varNode &&
  478. varNode.parent.loc.start.line === node.loc.start.line &&
  479. varNode.parent.declarations.length > 1;
  480. }
  481. /**
  482. * Check to see if the argument before the callee node is multi-line and
  483. * there should only be 1 argument before the callee node
  484. * @param {ASTNode} node node to check
  485. * @returns {boolean} True if arguments are multi-line
  486. */
  487. function isArgBeforeCalleeNodeMultiline(node) {
  488. const parent = node.parent;
  489. if (parent.arguments.length >= 2 && parent.arguments[1] === node) {
  490. return parent.arguments[0].loc.end.line > parent.arguments[0].loc.start.line;
  491. }
  492. return false;
  493. }
  494. /**
  495. * Check to see if the node is a file level IIFE
  496. * @param {ASTNode} node The function node to check.
  497. * @returns {boolean} True if the node is the outer IIFE
  498. */
  499. function isOuterIIFE(node) {
  500. const parent = node.parent;
  501. let stmt = parent.parent;
  502. /*
  503. * Verify that the node is an IIEF
  504. */
  505. if (
  506. parent.type !== "CallExpression" ||
  507. parent.callee !== node) {
  508. return false;
  509. }
  510. /*
  511. * Navigate legal ancestors to determine whether this IIEF is outer
  512. */
  513. while (
  514. stmt.type === "UnaryExpression" && (
  515. stmt.operator === "!" ||
  516. stmt.operator === "~" ||
  517. stmt.operator === "+" ||
  518. stmt.operator === "-") ||
  519. stmt.type === "AssignmentExpression" ||
  520. stmt.type === "LogicalExpression" ||
  521. stmt.type === "SequenceExpression" ||
  522. stmt.type === "VariableDeclarator") {
  523. stmt = stmt.parent;
  524. }
  525. return ((
  526. stmt.type === "ExpressionStatement" ||
  527. stmt.type === "VariableDeclaration") &&
  528. stmt.parent && stmt.parent.type === "Program"
  529. );
  530. }
  531. /**
  532. * Check indent for function block content
  533. * @param {ASTNode} node A BlockStatement node that is inside of a function.
  534. * @returns {void}
  535. */
  536. function checkIndentInFunctionBlock(node) {
  537. /*
  538. * Search first caller in chain.
  539. * Ex.:
  540. *
  541. * Models <- Identifier
  542. * .User
  543. * .find()
  544. * .exec(function() {
  545. * // function body
  546. * });
  547. *
  548. * Looks for 'Models'
  549. */
  550. const calleeNode = node.parent; // FunctionExpression
  551. let indent;
  552. if (calleeNode.parent &&
  553. (calleeNode.parent.type === "Property" ||
  554. calleeNode.parent.type === "ArrayExpression")) {
  555. // If function is part of array or object, comma can be put at left
  556. indent = getNodeIndent(calleeNode, false).goodChar;
  557. } else {
  558. // If function is standalone, simple calculate indent
  559. indent = getNodeIndent(calleeNode).goodChar;
  560. }
  561. if (calleeNode.parent.type === "CallExpression") {
  562. const calleeParent = calleeNode.parent;
  563. if (calleeNode.type !== "FunctionExpression" && calleeNode.type !== "ArrowFunctionExpression") {
  564. if (calleeParent && calleeParent.loc.start.line < node.loc.start.line) {
  565. indent = getNodeIndent(calleeParent).goodChar;
  566. }
  567. } else {
  568. if (isArgBeforeCalleeNodeMultiline(calleeNode) &&
  569. calleeParent.callee.loc.start.line === calleeParent.callee.loc.end.line &&
  570. !isNodeFirstInLine(calleeNode)) {
  571. indent = getNodeIndent(calleeParent).goodChar;
  572. }
  573. }
  574. }
  575. /*
  576. * function body indent should be indent + indent size, unless this
  577. * is a FunctionDeclaration, FunctionExpression, or outer IIFE and the corresponding options are enabled.
  578. */
  579. let functionOffset = indentSize;
  580. if (options.outerIIFEBody !== null && isOuterIIFE(calleeNode)) {
  581. functionOffset = options.outerIIFEBody * indentSize;
  582. } else if (calleeNode.type === "FunctionExpression") {
  583. functionOffset = options.FunctionExpression.body * indentSize;
  584. } else if (calleeNode.type === "FunctionDeclaration") {
  585. functionOffset = options.FunctionDeclaration.body * indentSize;
  586. }
  587. indent += functionOffset;
  588. // check if the node is inside a variable
  589. const parentVarNode = getVariableDeclaratorNode(node);
  590. if (parentVarNode && isNodeInVarOnTop(node, parentVarNode)) {
  591. indent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind];
  592. }
  593. if (node.body.length > 0) {
  594. checkNodesIndent(node.body, indent);
  595. }
  596. checkLastNodeLineIndent(node, indent - functionOffset);
  597. }
  598. /**
  599. * Checks if the given node starts and ends on the same line
  600. * @param {ASTNode} node The node to check
  601. * @returns {boolean} Whether or not the block starts and ends on the same line.
  602. */
  603. function isSingleLineNode(node) {
  604. const lastToken = sourceCode.getLastToken(node),
  605. startLine = node.loc.start.line,
  606. endLine = lastToken.loc.end.line;
  607. return startLine === endLine;
  608. }
  609. /**
  610. * Check to see if the first element inside an array is an object and on the same line as the node
  611. * If the node is not an array then it will return false.
  612. * @param {ASTNode} node node to check
  613. * @returns {boolean} success/failure
  614. */
  615. function isFirstArrayElementOnSameLine(node) {
  616. if (node.type === "ArrayExpression" && node.elements[0]) {
  617. return node.elements[0].loc.start.line === node.loc.start.line && node.elements[0].type === "ObjectExpression";
  618. }
  619. return false;
  620. }
  621. /**
  622. * Check indent for array block content or object block content
  623. * @param {ASTNode} node node to examine
  624. * @returns {void}
  625. */
  626. function checkIndentInArrayOrObjectBlock(node) {
  627. // Skip inline
  628. if (isSingleLineNode(node)) {
  629. return;
  630. }
  631. let elements = (node.type === "ArrayExpression") ? node.elements : node.properties;
  632. // filter out empty elements example would be [ , 2] so remove first element as espree considers it as null
  633. elements = elements.filter(elem => elem !== null);
  634. let nodeIndent;
  635. let elementsIndent;
  636. const parentVarNode = getVariableDeclaratorNode(node);
  637. // TODO - come up with a better strategy in future
  638. if (isNodeFirstInLine(node)) {
  639. const parent = node.parent;
  640. nodeIndent = getNodeIndent(parent).goodChar;
  641. if (!parentVarNode || parentVarNode.loc.start.line !== node.loc.start.line) {
  642. if (parent.type !== "VariableDeclarator" || parentVarNode === parentVarNode.parent.declarations[0]) {
  643. if (parent.type === "VariableDeclarator" && parentVarNode.loc.start.line === parent.loc.start.line) {
  644. nodeIndent += (indentSize * options.VariableDeclarator[parentVarNode.parent.kind]);
  645. } else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") {
  646. const parentElements = node.parent.type === "ObjectExpression" ? node.parent.properties : node.parent.elements;
  647. if (parentElements[0] &&
  648. parentElements[0].loc.start.line === parent.loc.start.line &&
  649. parentElements[0].loc.end.line !== parent.loc.start.line) {
  650. /*
  651. * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest.
  652. * e.g. [{
  653. * foo: 1
  654. * },
  655. * {
  656. * bar: 1
  657. * }]
  658. * the second object is not indented.
  659. */
  660. } else if (typeof options[parent.type] === "number") {
  661. nodeIndent += options[parent.type] * indentSize;
  662. } else {
  663. nodeIndent = parentElements[0].loc.start.column;
  664. }
  665. } else if (parent.type === "CallExpression" || parent.type === "NewExpression") {
  666. if (typeof options.CallExpression.arguments === "number") {
  667. nodeIndent += options.CallExpression.arguments * indentSize;
  668. } else if (options.CallExpression.arguments === "first") {
  669. if (parent.arguments.indexOf(node) !== -1) {
  670. nodeIndent = parent.arguments[0].loc.start.column;
  671. }
  672. } else {
  673. nodeIndent += indentSize;
  674. }
  675. } else if (parent.type === "LogicalExpression" || parent.type === "ArrowFunctionExpression") {
  676. nodeIndent += indentSize;
  677. }
  678. }
  679. } else if (!parentVarNode && !isFirstArrayElementOnSameLine(parent) && parent.type !== "MemberExpression" && parent.type !== "ExpressionStatement" && parent.type !== "AssignmentExpression" && parent.type !== "Property") {
  680. nodeIndent += indentSize;
  681. }
  682. checkFirstNodeLineIndent(node, nodeIndent);
  683. } else {
  684. nodeIndent = getNodeIndent(node).goodChar;
  685. }
  686. if (options[node.type] === "first") {
  687. elementsIndent = elements.length ? elements[0].loc.start.column : 0; // If there are no elements, elementsIndent doesn't matter.
  688. } else {
  689. elementsIndent = nodeIndent + indentSize * options[node.type];
  690. }
  691. /*
  692. * Check if the node is a multiple variable declaration; if so, then
  693. * make sure indentation takes that into account.
  694. */
  695. if (isNodeInVarOnTop(node, parentVarNode)) {
  696. elementsIndent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind];
  697. }
  698. checkNodesIndent(elements, elementsIndent);
  699. if (elements.length > 0) {
  700. // Skip last block line check if last item in same line
  701. if (elements[elements.length - 1].loc.end.line === node.loc.end.line) {
  702. return;
  703. }
  704. }
  705. checkLastNodeLineIndent(node, nodeIndent +
  706. (isNodeInVarOnTop(node, parentVarNode) ? options.VariableDeclarator[parentVarNode.parent.kind] * indentSize : 0));
  707. }
  708. /**
  709. * Check if the node or node body is a BlockStatement or not
  710. * @param {ASTNode} node node to test
  711. * @returns {boolean} True if it or its body is a block statement
  712. */
  713. function isNodeBodyBlock(node) {
  714. return node.type === "BlockStatement" || node.type === "ClassBody" || (node.body && node.body.type === "BlockStatement") ||
  715. (node.consequent && node.consequent.type === "BlockStatement");
  716. }
  717. /**
  718. * Check indentation for blocks
  719. * @param {ASTNode} node node to check
  720. * @returns {void}
  721. */
  722. function blockIndentationCheck(node) {
  723. // Skip inline blocks
  724. if (isSingleLineNode(node)) {
  725. return;
  726. }
  727. if (node.parent && (
  728. node.parent.type === "FunctionExpression" ||
  729. node.parent.type === "FunctionDeclaration" ||
  730. node.parent.type === "ArrowFunctionExpression")
  731. ) {
  732. checkIndentInFunctionBlock(node);
  733. return;
  734. }
  735. let indent;
  736. let nodesToCheck = [];
  737. /*
  738. * For this statements we should check indent from statement beginning,
  739. * not from the beginning of the block.
  740. */
  741. const statementsWithProperties = [
  742. "IfStatement", "WhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "DoWhileStatement", "ClassDeclaration", "TryStatement"
  743. ];
  744. if (node.parent && statementsWithProperties.indexOf(node.parent.type) !== -1 && isNodeBodyBlock(node)) {
  745. indent = getNodeIndent(node.parent).goodChar;
  746. } else if (node.parent && node.parent.type === "CatchClause") {
  747. indent = getNodeIndent(node.parent.parent).goodChar;
  748. } else {
  749. indent = getNodeIndent(node).goodChar;
  750. }
  751. if (node.type === "IfStatement" && node.consequent.type !== "BlockStatement") {
  752. nodesToCheck = [node.consequent];
  753. } else if (Array.isArray(node.body)) {
  754. nodesToCheck = node.body;
  755. } else {
  756. nodesToCheck = [node.body];
  757. }
  758. if (nodesToCheck.length > 0) {
  759. checkNodesIndent(nodesToCheck, indent + indentSize);
  760. }
  761. if (node.type === "BlockStatement") {
  762. checkLastNodeLineIndent(node, indent);
  763. }
  764. }
  765. /**
  766. * Filter out the elements which are on the same line of each other or the node.
  767. * basically have only 1 elements from each line except the variable declaration line.
  768. * @param {ASTNode} node Variable declaration node
  769. * @returns {ASTNode[]} Filtered elements
  770. */
  771. function filterOutSameLineVars(node) {
  772. return node.declarations.reduce((finalCollection, elem) => {
  773. const lastElem = finalCollection[finalCollection.length - 1];
  774. if ((elem.loc.start.line !== node.loc.start.line && !lastElem) ||
  775. (lastElem && lastElem.loc.start.line !== elem.loc.start.line)) {
  776. finalCollection.push(elem);
  777. }
  778. return finalCollection;
  779. }, []);
  780. }
  781. /**
  782. * Check indentation for variable declarations
  783. * @param {ASTNode} node node to examine
  784. * @returns {void}
  785. */
  786. function checkIndentInVariableDeclarations(node) {
  787. const elements = filterOutSameLineVars(node);
  788. const nodeIndent = getNodeIndent(node).goodChar;
  789. const lastElement = elements[elements.length - 1];
  790. const elementsIndent = nodeIndent + indentSize * options.VariableDeclarator[node.kind];
  791. checkNodesIndent(elements, elementsIndent);
  792. // Only check the last line if there is any token after the last item
  793. if (sourceCode.getLastToken(node).loc.end.line <= lastElement.loc.end.line) {
  794. return;
  795. }
  796. const tokenBeforeLastElement = sourceCode.getTokenBefore(lastElement);
  797. if (tokenBeforeLastElement.value === ",") {
  798. // Special case for comma-first syntax where the semicolon is indented
  799. checkLastNodeLineIndent(node, getNodeIndent(tokenBeforeLastElement).goodChar);
  800. } else {
  801. checkLastNodeLineIndent(node, elementsIndent - indentSize);
  802. }
  803. }
  804. /**
  805. * Check and decide whether to check for indentation for blockless nodes
  806. * Scenarios are for or while statements without braces around them
  807. * @param {ASTNode} node node to examine
  808. * @returns {void}
  809. */
  810. function blockLessNodes(node) {
  811. if (node.body.type !== "BlockStatement") {
  812. blockIndentationCheck(node);
  813. }
  814. }
  815. /**
  816. * Returns the expected indentation for the case statement
  817. * @param {ASTNode} node node to examine
  818. * @param {int} [providedSwitchIndent] indent for switch statement
  819. * @returns {int} indent size
  820. */
  821. function expectedCaseIndent(node, providedSwitchIndent) {
  822. const switchNode = (node.type === "SwitchStatement") ? node : node.parent;
  823. const switchIndent = typeof providedSwitchIndent === "undefined"
  824. ? getNodeIndent(switchNode).goodChar
  825. : providedSwitchIndent;
  826. let caseIndent;
  827. if (caseIndentStore[switchNode.loc.start.line]) {
  828. return caseIndentStore[switchNode.loc.start.line];
  829. }
  830. if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
  831. caseIndent = switchIndent;
  832. } else {
  833. caseIndent = switchIndent + (indentSize * options.SwitchCase);
  834. }
  835. caseIndentStore[switchNode.loc.start.line] = caseIndent;
  836. return caseIndent;
  837. }
  838. /**
  839. * Checks wether a return statement is wrapped in ()
  840. * @param {ASTNode} node node to examine
  841. * @returns {boolean} the result
  842. */
  843. function isWrappedInParenthesis(node) {
  844. const regex = /^return\s*?\(\s*?\);*?/u;
  845. const statementWithoutArgument = sourceCode.getText(node).replace(
  846. sourceCode.getText(node.argument), ""
  847. );
  848. return regex.test(statementWithoutArgument);
  849. }
  850. return {
  851. Program(node) {
  852. if (node.body.length > 0) {
  853. // Root nodes should have no indent
  854. checkNodesIndent(node.body, getNodeIndent(node).goodChar);
  855. }
  856. },
  857. ClassBody: blockIndentationCheck,
  858. BlockStatement: blockIndentationCheck,
  859. WhileStatement: blockLessNodes,
  860. ForStatement: blockLessNodes,
  861. ForInStatement: blockLessNodes,
  862. ForOfStatement: blockLessNodes,
  863. DoWhileStatement: blockLessNodes,
  864. IfStatement(node) {
  865. if (node.consequent.type !== "BlockStatement" && node.consequent.loc.start.line > node.loc.start.line) {
  866. blockIndentationCheck(node);
  867. }
  868. },
  869. VariableDeclaration(node) {
  870. if (node.declarations[node.declarations.length - 1].loc.start.line > node.declarations[0].loc.start.line) {
  871. checkIndentInVariableDeclarations(node);
  872. }
  873. },
  874. ObjectExpression(node) {
  875. checkIndentInArrayOrObjectBlock(node);
  876. },
  877. ArrayExpression(node) {
  878. checkIndentInArrayOrObjectBlock(node);
  879. },
  880. MemberExpression(node) {
  881. if (typeof options.MemberExpression === "undefined") {
  882. return;
  883. }
  884. if (isSingleLineNode(node)) {
  885. return;
  886. }
  887. /*
  888. * The typical layout of variable declarations and assignments
  889. * alter the expectation of correct indentation. Skip them.
  890. * TODO: Add appropriate configuration options for variable
  891. * declarations and assignments.
  892. */
  893. if (getParentNodeByType(node, "VariableDeclarator", ["FunctionExpression", "ArrowFunctionExpression"])) {
  894. return;
  895. }
  896. if (getParentNodeByType(node, "AssignmentExpression", ["FunctionExpression"])) {
  897. return;
  898. }
  899. const propertyIndent = getNodeIndent(node).goodChar + indentSize * options.MemberExpression;
  900. const checkNodes = [node.property];
  901. const dot = sourceCode.getTokenBefore(node.property);
  902. if (dot.type === "Punctuator" && dot.value === ".") {
  903. checkNodes.push(dot);
  904. }
  905. checkNodesIndent(checkNodes, propertyIndent);
  906. },
  907. SwitchStatement(node) {
  908. // Switch is not a 'BlockStatement'
  909. const switchIndent = getNodeIndent(node).goodChar;
  910. const caseIndent = expectedCaseIndent(node, switchIndent);
  911. checkNodesIndent(node.cases, caseIndent);
  912. checkLastNodeLineIndent(node, switchIndent);
  913. },
  914. SwitchCase(node) {
  915. // Skip inline cases
  916. if (isSingleLineNode(node)) {
  917. return;
  918. }
  919. const caseIndent = expectedCaseIndent(node);
  920. checkNodesIndent(node.consequent, caseIndent + indentSize);
  921. },
  922. FunctionDeclaration(node) {
  923. if (isSingleLineNode(node)) {
  924. return;
  925. }
  926. if (options.FunctionDeclaration.parameters === "first" && node.params.length) {
  927. checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
  928. } else if (options.FunctionDeclaration.parameters !== null) {
  929. checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionDeclaration.parameters);
  930. }
  931. },
  932. FunctionExpression(node) {
  933. if (isSingleLineNode(node)) {
  934. return;
  935. }
  936. if (options.FunctionExpression.parameters === "first" && node.params.length) {
  937. checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
  938. } else if (options.FunctionExpression.parameters !== null) {
  939. checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionExpression.parameters);
  940. }
  941. },
  942. ReturnStatement(node) {
  943. if (isSingleLineNode(node)) {
  944. return;
  945. }
  946. const firstLineIndent = getNodeIndent(node).goodChar;
  947. // in case if return statement is wrapped in parenthesis
  948. if (isWrappedInParenthesis(node)) {
  949. checkLastReturnStatementLineIndent(node, firstLineIndent);
  950. } else {
  951. checkNodeIndent(node, firstLineIndent);
  952. }
  953. },
  954. CallExpression(node) {
  955. if (isSingleLineNode(node)) {
  956. return;
  957. }
  958. if (options.CallExpression.arguments === "first" && node.arguments.length) {
  959. checkNodesIndent(node.arguments.slice(1), node.arguments[0].loc.start.column);
  960. } else if (options.CallExpression.arguments !== null) {
  961. checkNodesIndent(node.arguments, getNodeIndent(node).goodChar + indentSize * options.CallExpression.arguments);
  962. }
  963. }
  964. };
  965. }
  966. };