Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indent-legacy.js 44KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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 indent for array block content or object block content
  611. * @param {ASTNode} node node to examine
  612. * @returns {void}
  613. */
  614. function checkIndentInArrayOrObjectBlock(node) {
  615. // Skip inline
  616. if (isSingleLineNode(node)) {
  617. return;
  618. }
  619. let elements = (node.type === "ArrayExpression") ? node.elements : node.properties;
  620. // filter out empty elements example would be [ , 2] so remove first element as espree considers it as null
  621. elements = elements.filter(elem => elem !== null);
  622. let nodeIndent;
  623. let elementsIndent;
  624. const parentVarNode = getVariableDeclaratorNode(node);
  625. // TODO - come up with a better strategy in future
  626. if (isNodeFirstInLine(node)) {
  627. const parent = node.parent;
  628. nodeIndent = getNodeIndent(parent).goodChar;
  629. if (!parentVarNode || parentVarNode.loc.start.line !== node.loc.start.line) {
  630. if (parent.type !== "VariableDeclarator" || parentVarNode === parentVarNode.parent.declarations[0]) {
  631. if (parent.type === "VariableDeclarator" && parentVarNode.loc.start.line === parent.loc.start.line) {
  632. nodeIndent += (indentSize * options.VariableDeclarator[parentVarNode.parent.kind]);
  633. } else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") {
  634. const parentElements = node.parent.type === "ObjectExpression" ? node.parent.properties : node.parent.elements;
  635. if (parentElements[0] &&
  636. parentElements[0].loc.start.line === parent.loc.start.line &&
  637. parentElements[0].loc.end.line !== parent.loc.start.line) {
  638. /*
  639. * If the first element of the array spans multiple lines, don't increase the expected indentation of the rest.
  640. * e.g. [{
  641. * foo: 1
  642. * },
  643. * {
  644. * bar: 1
  645. * }]
  646. * the second object is not indented.
  647. */
  648. } else if (typeof options[parent.type] === "number") {
  649. nodeIndent += options[parent.type] * indentSize;
  650. } else {
  651. nodeIndent = parentElements[0].loc.start.column;
  652. }
  653. } else if (parent.type === "CallExpression" || parent.type === "NewExpression") {
  654. if (typeof options.CallExpression.arguments === "number") {
  655. nodeIndent += options.CallExpression.arguments * indentSize;
  656. } else if (options.CallExpression.arguments === "first") {
  657. if (parent.arguments.indexOf(node) !== -1) {
  658. nodeIndent = parent.arguments[0].loc.start.column;
  659. }
  660. } else {
  661. nodeIndent += indentSize;
  662. }
  663. } else if (parent.type === "LogicalExpression" || parent.type === "ArrowFunctionExpression") {
  664. nodeIndent += indentSize;
  665. }
  666. }
  667. }
  668. checkFirstNodeLineIndent(node, nodeIndent);
  669. } else {
  670. nodeIndent = getNodeIndent(node).goodChar;
  671. }
  672. if (options[node.type] === "first") {
  673. elementsIndent = elements.length ? elements[0].loc.start.column : 0; // If there are no elements, elementsIndent doesn't matter.
  674. } else {
  675. elementsIndent = nodeIndent + indentSize * options[node.type];
  676. }
  677. /*
  678. * Check if the node is a multiple variable declaration; if so, then
  679. * make sure indentation takes that into account.
  680. */
  681. if (isNodeInVarOnTop(node, parentVarNode)) {
  682. elementsIndent += indentSize * options.VariableDeclarator[parentVarNode.parent.kind];
  683. }
  684. checkNodesIndent(elements, elementsIndent);
  685. if (elements.length > 0) {
  686. // Skip last block line check if last item in same line
  687. if (elements[elements.length - 1].loc.end.line === node.loc.end.line) {
  688. return;
  689. }
  690. }
  691. checkLastNodeLineIndent(node, nodeIndent +
  692. (isNodeInVarOnTop(node, parentVarNode) ? options.VariableDeclarator[parentVarNode.parent.kind] * indentSize : 0));
  693. }
  694. /**
  695. * Check if the node or node body is a BlockStatement or not
  696. * @param {ASTNode} node node to test
  697. * @returns {boolean} True if it or its body is a block statement
  698. */
  699. function isNodeBodyBlock(node) {
  700. return node.type === "BlockStatement" || node.type === "ClassBody" || (node.body && node.body.type === "BlockStatement") ||
  701. (node.consequent && node.consequent.type === "BlockStatement");
  702. }
  703. /**
  704. * Check indentation for blocks
  705. * @param {ASTNode} node node to check
  706. * @returns {void}
  707. */
  708. function blockIndentationCheck(node) {
  709. // Skip inline blocks
  710. if (isSingleLineNode(node)) {
  711. return;
  712. }
  713. if (node.parent && (
  714. node.parent.type === "FunctionExpression" ||
  715. node.parent.type === "FunctionDeclaration" ||
  716. node.parent.type === "ArrowFunctionExpression")
  717. ) {
  718. checkIndentInFunctionBlock(node);
  719. return;
  720. }
  721. let indent;
  722. let nodesToCheck = [];
  723. /*
  724. * For this statements we should check indent from statement beginning,
  725. * not from the beginning of the block.
  726. */
  727. const statementsWithProperties = [
  728. "IfStatement", "WhileStatement", "ForStatement", "ForInStatement", "ForOfStatement", "DoWhileStatement", "ClassDeclaration", "TryStatement"
  729. ];
  730. if (node.parent && statementsWithProperties.indexOf(node.parent.type) !== -1 && isNodeBodyBlock(node)) {
  731. indent = getNodeIndent(node.parent).goodChar;
  732. } else if (node.parent && node.parent.type === "CatchClause") {
  733. indent = getNodeIndent(node.parent.parent).goodChar;
  734. } else {
  735. indent = getNodeIndent(node).goodChar;
  736. }
  737. if (node.type === "IfStatement" && node.consequent.type !== "BlockStatement") {
  738. nodesToCheck = [node.consequent];
  739. } else if (Array.isArray(node.body)) {
  740. nodesToCheck = node.body;
  741. } else {
  742. nodesToCheck = [node.body];
  743. }
  744. if (nodesToCheck.length > 0) {
  745. checkNodesIndent(nodesToCheck, indent + indentSize);
  746. }
  747. if (node.type === "BlockStatement") {
  748. checkLastNodeLineIndent(node, indent);
  749. }
  750. }
  751. /**
  752. * Filter out the elements which are on the same line of each other or the node.
  753. * basically have only 1 elements from each line except the variable declaration line.
  754. * @param {ASTNode} node Variable declaration node
  755. * @returns {ASTNode[]} Filtered elements
  756. */
  757. function filterOutSameLineVars(node) {
  758. return node.declarations.reduce((finalCollection, elem) => {
  759. const lastElem = finalCollection[finalCollection.length - 1];
  760. if ((elem.loc.start.line !== node.loc.start.line && !lastElem) ||
  761. (lastElem && lastElem.loc.start.line !== elem.loc.start.line)) {
  762. finalCollection.push(elem);
  763. }
  764. return finalCollection;
  765. }, []);
  766. }
  767. /**
  768. * Check indentation for variable declarations
  769. * @param {ASTNode} node node to examine
  770. * @returns {void}
  771. */
  772. function checkIndentInVariableDeclarations(node) {
  773. const elements = filterOutSameLineVars(node);
  774. const nodeIndent = getNodeIndent(node).goodChar;
  775. const lastElement = elements[elements.length - 1];
  776. const elementsIndent = nodeIndent + indentSize * options.VariableDeclarator[node.kind];
  777. checkNodesIndent(elements, elementsIndent);
  778. // Only check the last line if there is any token after the last item
  779. if (sourceCode.getLastToken(node).loc.end.line <= lastElement.loc.end.line) {
  780. return;
  781. }
  782. const tokenBeforeLastElement = sourceCode.getTokenBefore(lastElement);
  783. if (tokenBeforeLastElement.value === ",") {
  784. // Special case for comma-first syntax where the semicolon is indented
  785. checkLastNodeLineIndent(node, getNodeIndent(tokenBeforeLastElement).goodChar);
  786. } else {
  787. checkLastNodeLineIndent(node, elementsIndent - indentSize);
  788. }
  789. }
  790. /**
  791. * Check and decide whether to check for indentation for blockless nodes
  792. * Scenarios are for or while statements without braces around them
  793. * @param {ASTNode} node node to examine
  794. * @returns {void}
  795. */
  796. function blockLessNodes(node) {
  797. if (node.body.type !== "BlockStatement") {
  798. blockIndentationCheck(node);
  799. }
  800. }
  801. /**
  802. * Returns the expected indentation for the case statement
  803. * @param {ASTNode} node node to examine
  804. * @param {int} [providedSwitchIndent] indent for switch statement
  805. * @returns {int} indent size
  806. */
  807. function expectedCaseIndent(node, providedSwitchIndent) {
  808. const switchNode = (node.type === "SwitchStatement") ? node : node.parent;
  809. const switchIndent = typeof providedSwitchIndent === "undefined"
  810. ? getNodeIndent(switchNode).goodChar
  811. : providedSwitchIndent;
  812. let caseIndent;
  813. if (caseIndentStore[switchNode.loc.start.line]) {
  814. return caseIndentStore[switchNode.loc.start.line];
  815. }
  816. if (switchNode.cases.length > 0 && options.SwitchCase === 0) {
  817. caseIndent = switchIndent;
  818. } else {
  819. caseIndent = switchIndent + (indentSize * options.SwitchCase);
  820. }
  821. caseIndentStore[switchNode.loc.start.line] = caseIndent;
  822. return caseIndent;
  823. }
  824. /**
  825. * Checks whether a return statement is wrapped in ()
  826. * @param {ASTNode} node node to examine
  827. * @returns {boolean} the result
  828. */
  829. function isWrappedInParenthesis(node) {
  830. const regex = /^return\s*?\(\s*?\);*?/u;
  831. const statementWithoutArgument = sourceCode.getText(node).replace(
  832. sourceCode.getText(node.argument), ""
  833. );
  834. return regex.test(statementWithoutArgument);
  835. }
  836. return {
  837. Program(node) {
  838. if (node.body.length > 0) {
  839. // Root nodes should have no indent
  840. checkNodesIndent(node.body, getNodeIndent(node).goodChar);
  841. }
  842. },
  843. ClassBody: blockIndentationCheck,
  844. BlockStatement: blockIndentationCheck,
  845. WhileStatement: blockLessNodes,
  846. ForStatement: blockLessNodes,
  847. ForInStatement: blockLessNodes,
  848. ForOfStatement: blockLessNodes,
  849. DoWhileStatement: blockLessNodes,
  850. IfStatement(node) {
  851. if (node.consequent.type !== "BlockStatement" && node.consequent.loc.start.line > node.loc.start.line) {
  852. blockIndentationCheck(node);
  853. }
  854. },
  855. VariableDeclaration(node) {
  856. if (node.declarations[node.declarations.length - 1].loc.start.line > node.declarations[0].loc.start.line) {
  857. checkIndentInVariableDeclarations(node);
  858. }
  859. },
  860. ObjectExpression(node) {
  861. checkIndentInArrayOrObjectBlock(node);
  862. },
  863. ArrayExpression(node) {
  864. checkIndentInArrayOrObjectBlock(node);
  865. },
  866. MemberExpression(node) {
  867. if (typeof options.MemberExpression === "undefined") {
  868. return;
  869. }
  870. if (isSingleLineNode(node)) {
  871. return;
  872. }
  873. /*
  874. * The typical layout of variable declarations and assignments
  875. * alter the expectation of correct indentation. Skip them.
  876. * TODO: Add appropriate configuration options for variable
  877. * declarations and assignments.
  878. */
  879. if (getParentNodeByType(node, "VariableDeclarator", ["FunctionExpression", "ArrowFunctionExpression"])) {
  880. return;
  881. }
  882. if (getParentNodeByType(node, "AssignmentExpression", ["FunctionExpression"])) {
  883. return;
  884. }
  885. const propertyIndent = getNodeIndent(node).goodChar + indentSize * options.MemberExpression;
  886. const checkNodes = [node.property];
  887. const dot = sourceCode.getTokenBefore(node.property);
  888. if (dot.type === "Punctuator" && dot.value === ".") {
  889. checkNodes.push(dot);
  890. }
  891. checkNodesIndent(checkNodes, propertyIndent);
  892. },
  893. SwitchStatement(node) {
  894. // Switch is not a 'BlockStatement'
  895. const switchIndent = getNodeIndent(node).goodChar;
  896. const caseIndent = expectedCaseIndent(node, switchIndent);
  897. checkNodesIndent(node.cases, caseIndent);
  898. checkLastNodeLineIndent(node, switchIndent);
  899. },
  900. SwitchCase(node) {
  901. // Skip inline cases
  902. if (isSingleLineNode(node)) {
  903. return;
  904. }
  905. const caseIndent = expectedCaseIndent(node);
  906. checkNodesIndent(node.consequent, caseIndent + indentSize);
  907. },
  908. FunctionDeclaration(node) {
  909. if (isSingleLineNode(node)) {
  910. return;
  911. }
  912. if (options.FunctionDeclaration.parameters === "first" && node.params.length) {
  913. checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
  914. } else if (options.FunctionDeclaration.parameters !== null) {
  915. checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionDeclaration.parameters);
  916. }
  917. },
  918. FunctionExpression(node) {
  919. if (isSingleLineNode(node)) {
  920. return;
  921. }
  922. if (options.FunctionExpression.parameters === "first" && node.params.length) {
  923. checkNodesIndent(node.params.slice(1), node.params[0].loc.start.column);
  924. } else if (options.FunctionExpression.parameters !== null) {
  925. checkNodesIndent(node.params, getNodeIndent(node).goodChar + indentSize * options.FunctionExpression.parameters);
  926. }
  927. },
  928. ReturnStatement(node) {
  929. if (isSingleLineNode(node)) {
  930. return;
  931. }
  932. const firstLineIndent = getNodeIndent(node).goodChar;
  933. // in case if return statement is wrapped in parenthesis
  934. if (isWrappedInParenthesis(node)) {
  935. checkLastReturnStatementLineIndent(node, firstLineIndent);
  936. } else {
  937. checkNodeIndent(node, firstLineIndent);
  938. }
  939. },
  940. CallExpression(node) {
  941. if (isSingleLineNode(node)) {
  942. return;
  943. }
  944. if (options.CallExpression.arguments === "first" && node.arguments.length) {
  945. checkNodesIndent(node.arguments.slice(1), node.arguments[0].loc.start.column);
  946. } else if (options.CallExpression.arguments !== null) {
  947. checkNodesIndent(node.arguments, getNodeIndent(node).goodChar + indentSize * options.CallExpression.arguments);
  948. }
  949. }
  950. };
  951. }
  952. };