indent.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /**
  2. * @fileoverview This option sets a specific tab width for your code
  3. *
  4. * @author Teddy Katz
  5. * @author Vitaly Puzrin
  6. * @author Gyandeep Singh
  7. */
  8. "use strict";
  9. //------------------------------------------------------------------------------
  10. // Requirements
  11. //------------------------------------------------------------------------------
  12. const lodash = require("lodash");
  13. const astUtils = require("../ast-utils");
  14. const createTree = require("functional-red-black-tree");
  15. //------------------------------------------------------------------------------
  16. // Rule Definition
  17. //------------------------------------------------------------------------------
  18. const KNOWN_NODES = new Set([
  19. "AssignmentExpression",
  20. "AssignmentPattern",
  21. "ArrayExpression",
  22. "ArrayPattern",
  23. "ArrowFunctionExpression",
  24. "AwaitExpression",
  25. "BlockStatement",
  26. "BinaryExpression",
  27. "BreakStatement",
  28. "CallExpression",
  29. "CatchClause",
  30. "ClassBody",
  31. "ClassDeclaration",
  32. "ClassExpression",
  33. "ConditionalExpression",
  34. "ContinueStatement",
  35. "DoWhileStatement",
  36. "DebuggerStatement",
  37. "EmptyStatement",
  38. "ExperimentalRestProperty",
  39. "ExperimentalSpreadProperty",
  40. "ExpressionStatement",
  41. "ForStatement",
  42. "ForInStatement",
  43. "ForOfStatement",
  44. "FunctionDeclaration",
  45. "FunctionExpression",
  46. "Identifier",
  47. "IfStatement",
  48. "Literal",
  49. "LabeledStatement",
  50. "LogicalExpression",
  51. "MemberExpression",
  52. "MetaProperty",
  53. "MethodDefinition",
  54. "NewExpression",
  55. "ObjectExpression",
  56. "ObjectPattern",
  57. "Program",
  58. "Property",
  59. "RestElement",
  60. "ReturnStatement",
  61. "SequenceExpression",
  62. "SpreadElement",
  63. "Super",
  64. "SwitchCase",
  65. "SwitchStatement",
  66. "TaggedTemplateExpression",
  67. "TemplateElement",
  68. "TemplateLiteral",
  69. "ThisExpression",
  70. "ThrowStatement",
  71. "TryStatement",
  72. "UnaryExpression",
  73. "UpdateExpression",
  74. "VariableDeclaration",
  75. "VariableDeclarator",
  76. "WhileStatement",
  77. "WithStatement",
  78. "YieldExpression",
  79. "JSXIdentifier",
  80. "JSXNamespacedName",
  81. "JSXMemberExpression",
  82. "JSXEmptyExpression",
  83. "JSXExpressionContainer",
  84. "JSXElement",
  85. "JSXClosingElement",
  86. "JSXOpeningElement",
  87. "JSXAttribute",
  88. "JSXSpreadAttribute",
  89. "JSXText",
  90. "ExportDefaultDeclaration",
  91. "ExportNamedDeclaration",
  92. "ExportAllDeclaration",
  93. "ExportSpecifier",
  94. "ImportDeclaration",
  95. "ImportSpecifier",
  96. "ImportDefaultSpecifier",
  97. "ImportNamespaceSpecifier"
  98. ]);
  99. /*
  100. * General rule strategy:
  101. * 1. An OffsetStorage instance stores a map of desired offsets, where each token has a specified offset from another
  102. * specified token or to the first column.
  103. * 2. As the AST is traversed, modify the desired offsets of tokens accordingly. For example, when entering a
  104. * BlockStatement, offset all of the tokens in the BlockStatement by 1 indent level from the opening curly
  105. * brace of the BlockStatement.
  106. * 3. After traversing the AST, calculate the expected indentation levels of every token according to the
  107. * OffsetStorage container.
  108. * 4. For each line, compare the expected indentation of the first token to the actual indentation in the file,
  109. * and report the token if the two values are not equal.
  110. */
  111. /**
  112. * A mutable balanced binary search tree that stores (key, value) pairs. The keys are numeric, and must be unique.
  113. * This is intended to be a generic wrapper around a balanced binary search tree library, so that the underlying implementation
  114. * can easily be swapped out.
  115. */
  116. class BinarySearchTree {
  117. /**
  118. * Creates an empty tree
  119. */
  120. constructor() {
  121. this._rbTree = createTree();
  122. }
  123. /**
  124. * Inserts an entry into the tree.
  125. * @param {number} key The entry's key
  126. * @param {*} value The entry's value
  127. * @returns {void}
  128. */
  129. insert(key, value) {
  130. const iterator = this._rbTree.find(key);
  131. if (iterator.valid) {
  132. this._rbTree = iterator.update(value);
  133. } else {
  134. this._rbTree = this._rbTree.insert(key, value);
  135. }
  136. }
  137. /**
  138. * Finds the entry with the largest key less than or equal to the provided key
  139. * @param {number} key The provided key
  140. * @returns {{key: number, value: *}|null} The found entry, or null if no such entry exists.
  141. */
  142. findLe(key) {
  143. const iterator = this._rbTree.le(key);
  144. return iterator && { key: iterator.key, value: iterator.value };
  145. }
  146. /**
  147. * Deletes all of the keys in the interval [start, end)
  148. * @param {number} start The start of the range
  149. * @param {number} end The end of the range
  150. * @returns {void}
  151. */
  152. deleteRange(start, end) {
  153. // Exit without traversing the tree if the range has zero size.
  154. if (start === end) {
  155. return;
  156. }
  157. const iterator = this._rbTree.ge(start);
  158. while (iterator.valid && iterator.key < end) {
  159. this._rbTree = this._rbTree.remove(iterator.key);
  160. iterator.next();
  161. }
  162. }
  163. }
  164. /**
  165. * A helper class to get token-based info related to indentation
  166. */
  167. class TokenInfo {
  168. /**
  169. * @param {SourceCode} sourceCode A SourceCode object
  170. */
  171. constructor(sourceCode) {
  172. this.sourceCode = sourceCode;
  173. this.firstTokensByLineNumber = sourceCode.tokensAndComments.reduce((map, token) => {
  174. if (!map.has(token.loc.start.line)) {
  175. map.set(token.loc.start.line, token);
  176. }
  177. if (!map.has(token.loc.end.line) && sourceCode.text.slice(token.range[1] - token.loc.end.column, token.range[1]).trim()) {
  178. map.set(token.loc.end.line, token);
  179. }
  180. return map;
  181. }, new Map());
  182. }
  183. /**
  184. * Gets the first token on a given token's line
  185. * @param {Token|ASTNode} token a node or token
  186. * @returns {Token} The first token on the given line
  187. */
  188. getFirstTokenOfLine(token) {
  189. return this.firstTokensByLineNumber.get(token.loc.start.line);
  190. }
  191. /**
  192. * Determines whether a token is the first token in its line
  193. * @param {Token} token The token
  194. * @returns {boolean} `true` if the token is the first on its line
  195. */
  196. isFirstTokenOfLine(token) {
  197. return this.getFirstTokenOfLine(token) === token;
  198. }
  199. /**
  200. * Get the actual indent of a token
  201. * @param {Token} token Token to examine. This should be the first token on its line.
  202. * @returns {string} The indentation characters that precede the token
  203. */
  204. getTokenIndent(token) {
  205. return this.sourceCode.text.slice(token.range[0] - token.loc.start.column, token.range[0]);
  206. }
  207. }
  208. /**
  209. * A class to store information on desired offsets of tokens from each other
  210. */
  211. class OffsetStorage {
  212. /**
  213. * @param {TokenInfo} tokenInfo a TokenInfo instance
  214. * @param {number} indentSize The desired size of each indentation level
  215. * @param {string} indentType The indentation character
  216. */
  217. constructor(tokenInfo, indentSize, indentType) {
  218. this._tokenInfo = tokenInfo;
  219. this._indentSize = indentSize;
  220. this._indentType = indentType;
  221. this._tree = new BinarySearchTree();
  222. this._tree.insert(0, { offset: 0, from: null, force: false });
  223. this._lockedFirstTokens = new WeakMap();
  224. this._desiredIndentCache = new WeakMap();
  225. this._ignoredTokens = new WeakSet();
  226. }
  227. _getOffsetDescriptor(token) {
  228. return this._tree.findLe(token.range[0]).value;
  229. }
  230. /**
  231. * Sets the offset column of token B to match the offset column of token A.
  232. * **WARNING**: This matches a *column*, even if baseToken is not the first token on its line. In
  233. * most cases, `setDesiredOffset` should be used instead.
  234. * @param {Token} baseToken The first token
  235. * @param {Token} offsetToken The second token, whose offset should be matched to the first token
  236. * @returns {void}
  237. */
  238. matchOffsetOf(baseToken, offsetToken) {
  239. /*
  240. * lockedFirstTokens is a map from a token whose indentation is controlled by the "first" option to
  241. * the token that it depends on. For example, with the `ArrayExpression: first` option, the first
  242. * token of each element in the array after the first will be mapped to the first token of the first
  243. * element. The desired indentation of each of these tokens is computed based on the desired indentation
  244. * of the "first" element, rather than through the normal offset mechanism.
  245. */
  246. this._lockedFirstTokens.set(offsetToken, baseToken);
  247. }
  248. /**
  249. * Sets the desired offset of a token.
  250. *
  251. * This uses a line-based offset collapsing behavior to handle tokens on the same line.
  252. * For example, consider the following two cases:
  253. *
  254. * (
  255. * [
  256. * bar
  257. * ]
  258. * )
  259. *
  260. * ([
  261. * bar
  262. * ])
  263. *
  264. * Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from
  265. * the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is
  266. * the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces)
  267. * from the start of its line.
  268. *
  269. * However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level
  270. * between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the
  271. * `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented
  272. * by 1 indent level from the start of the line.
  273. *
  274. * This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node,
  275. * without needing to check which lines those tokens are on.
  276. *
  277. * Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive
  278. * behavior can occur. For example, consider the following cases:
  279. *
  280. * foo(
  281. * ).
  282. * bar(
  283. * baz
  284. * )
  285. *
  286. * foo(
  287. * ).bar(
  288. * baz
  289. * )
  290. *
  291. * Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz`
  292. * should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz`
  293. * being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no
  294. * collapsing would occur).
  295. *
  296. * Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and
  297. * offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed
  298. * in the second case.
  299. *
  300. * @param {Token} token The token
  301. * @param {Token} fromToken The token that `token` should be offset from
  302. * @param {number} offset The desired indent level
  303. * @returns {void}
  304. */
  305. setDesiredOffset(token, fromToken, offset) {
  306. return this.setDesiredOffsets(token.range, fromToken, offset);
  307. }
  308. /**
  309. * Sets the desired offset of all tokens in a range
  310. * It's common for node listeners in this file to need to apply the same offset to a large, contiguous range of tokens.
  311. * Moreover, the offset of any given token is usually updated multiple times (roughly once for each node that contains
  312. * it). This means that the offset of each token is updated O(AST depth) times.
  313. * It would not be performant to store and update the offsets for each token independently, because the rule would end
  314. * up having a time complexity of O(number of tokens * AST depth), which is quite slow for large files.
  315. *
  316. * Instead, the offset tree is represented as a collection of contiguous offset ranges in a file. For example, the following
  317. * list could represent the state of the offset tree at a given point:
  318. *
  319. * * Tokens starting in the interval [0, 15) are aligned with the beginning of the file
  320. * * Tokens starting in the interval [15, 30) are offset by 1 indent level from the `bar` token
  321. * * Tokens starting in the interval [30, 43) are offset by 1 indent level from the `foo` token
  322. * * Tokens starting in the interval [43, 820) are offset by 2 indent levels from the `bar` token
  323. * * Tokens starting in the interval [820, ∞) are offset by 1 indent level from the `baz` token
  324. *
  325. * The `setDesiredOffsets` methods inserts ranges like the ones above. The third line above would be inserted by using:
  326. * `setDesiredOffsets([30, 43], fooToken, 1);`
  327. *
  328. * @param {[number, number]} range A [start, end] pair. All tokens with range[0] <= token.start < range[1] will have the offset applied.
  329. * @param {Token} fromToken The token that this is offset from
  330. * @param {number} offset The desired indent level
  331. * @param {boolean} force `true` if this offset should not use the normal collapsing behavior. This should almost always be false.
  332. * @returns {void}
  333. */
  334. setDesiredOffsets(range, fromToken, offset, force) {
  335. /*
  336. * Offset ranges are stored as a collection of nodes, where each node maps a numeric key to an offset
  337. * descriptor. The tree for the example above would have the following nodes:
  338. *
  339. * * key: 0, value: { offset: 0, from: null }
  340. * * key: 15, value: { offset: 1, from: barToken }
  341. * * key: 30, value: { offset: 1, from: fooToken }
  342. * * key: 43, value: { offset: 2, from: barToken }
  343. * * key: 820, value: { offset: 1, from: bazToken }
  344. *
  345. * To find the offset descriptor for any given token, one needs to find the node with the largest key
  346. * which is <= token.start. To make this operation fast, the nodes are stored in a balanced binary
  347. * search tree indexed by key.
  348. */
  349. const descriptorToInsert = { offset, from: fromToken, force };
  350. const descriptorAfterRange = this._tree.findLe(range[1]).value;
  351. const fromTokenIsInRange = fromToken && fromToken.range[0] >= range[0] && fromToken.range[1] <= range[1];
  352. const fromTokenDescriptor = fromTokenIsInRange && this._getOffsetDescriptor(fromToken);
  353. // First, remove any existing nodes in the range from the tree.
  354. this._tree.deleteRange(range[0] + 1, range[1]);
  355. // Insert a new node into the tree for this range
  356. this._tree.insert(range[0], descriptorToInsert);
  357. /*
  358. * To avoid circular offset dependencies, keep the `fromToken` token mapped to whatever it was mapped to previously,
  359. * even if it's in the current range.
  360. */
  361. if (fromTokenIsInRange) {
  362. this._tree.insert(fromToken.range[0], fromTokenDescriptor);
  363. this._tree.insert(fromToken.range[1], descriptorToInsert);
  364. }
  365. /*
  366. * To avoid modifying the offset of tokens after the range, insert another node to keep the offset of the following
  367. * tokens the same as it was before.
  368. */
  369. this._tree.insert(range[1], descriptorAfterRange);
  370. }
  371. /**
  372. * Gets the desired indent of a token
  373. * @param {Token} token The token
  374. * @returns {string} The desired indent of the token
  375. */
  376. getDesiredIndent(token) {
  377. if (!this._desiredIndentCache.has(token)) {
  378. if (this._ignoredTokens.has(token)) {
  379. /*
  380. * If the token is ignored, use the actual indent of the token as the desired indent.
  381. * This ensures that no errors are reported for this token.
  382. */
  383. this._desiredIndentCache.set(
  384. token,
  385. this._tokenInfo.getTokenIndent(token)
  386. );
  387. } else if (this._lockedFirstTokens.has(token)) {
  388. const firstToken = this._lockedFirstTokens.get(token);
  389. this._desiredIndentCache.set(
  390. token,
  391. // (indentation for the first element's line)
  392. this.getDesiredIndent(this._tokenInfo.getFirstTokenOfLine(firstToken)) +
  393. // (space between the start of the first element's line and the first element)
  394. this._indentType.repeat(firstToken.loc.start.column - this._tokenInfo.getFirstTokenOfLine(firstToken).loc.start.column)
  395. );
  396. } else {
  397. const offsetInfo = this._getOffsetDescriptor(token);
  398. const offset = (
  399. offsetInfo.from &&
  400. offsetInfo.from.loc.start.line === token.loc.start.line &&
  401. !/^\s*?\n/.test(token.value) &&
  402. !offsetInfo.force
  403. ) ? 0 : offsetInfo.offset * this._indentSize;
  404. this._desiredIndentCache.set(
  405. token,
  406. (offsetInfo.from ? this.getDesiredIndent(offsetInfo.from) : "") + this._indentType.repeat(offset)
  407. );
  408. }
  409. }
  410. return this._desiredIndentCache.get(token);
  411. }
  412. /**
  413. * Ignores a token, preventing it from being reported.
  414. * @param {Token} token The token
  415. * @returns {void}
  416. */
  417. ignoreToken(token) {
  418. if (this._tokenInfo.isFirstTokenOfLine(token)) {
  419. this._ignoredTokens.add(token);
  420. }
  421. }
  422. /**
  423. * Gets the first token that the given token's indentation is dependent on
  424. * @param {Token} token The token
  425. * @returns {Token} The token that the given token depends on, or `null` if the given token is at the top level
  426. */
  427. getFirstDependency(token) {
  428. return this._getOffsetDescriptor(token).from;
  429. }
  430. }
  431. const ELEMENT_LIST_SCHEMA = {
  432. oneOf: [
  433. {
  434. type: "integer",
  435. minimum: 0
  436. },
  437. {
  438. enum: ["first", "off"]
  439. }
  440. ]
  441. };
  442. module.exports = {
  443. meta: {
  444. docs: {
  445. description: "enforce consistent indentation",
  446. category: "Stylistic Issues",
  447. recommended: false,
  448. url: "https://eslint.org/docs/rules/indent"
  449. },
  450. fixable: "whitespace",
  451. schema: [
  452. {
  453. oneOf: [
  454. {
  455. enum: ["tab"]
  456. },
  457. {
  458. type: "integer",
  459. minimum: 0
  460. }
  461. ]
  462. },
  463. {
  464. type: "object",
  465. properties: {
  466. SwitchCase: {
  467. type: "integer",
  468. minimum: 0
  469. },
  470. VariableDeclarator: {
  471. oneOf: [
  472. {
  473. type: "integer",
  474. minimum: 0
  475. },
  476. {
  477. type: "object",
  478. properties: {
  479. var: {
  480. type: "integer",
  481. minimum: 0
  482. },
  483. let: {
  484. type: "integer",
  485. minimum: 0
  486. },
  487. const: {
  488. type: "integer",
  489. minimum: 0
  490. }
  491. },
  492. additionalProperties: false
  493. }
  494. ]
  495. },
  496. outerIIFEBody: {
  497. type: "integer",
  498. minimum: 0
  499. },
  500. MemberExpression: {
  501. oneOf: [
  502. {
  503. type: "integer",
  504. minimum: 0
  505. },
  506. {
  507. enum: ["off"]
  508. }
  509. ]
  510. },
  511. FunctionDeclaration: {
  512. type: "object",
  513. properties: {
  514. parameters: ELEMENT_LIST_SCHEMA,
  515. body: {
  516. type: "integer",
  517. minimum: 0
  518. }
  519. },
  520. additionalProperties: false
  521. },
  522. FunctionExpression: {
  523. type: "object",
  524. properties: {
  525. parameters: ELEMENT_LIST_SCHEMA,
  526. body: {
  527. type: "integer",
  528. minimum: 0
  529. }
  530. },
  531. additionalProperties: false
  532. },
  533. CallExpression: {
  534. type: "object",
  535. properties: {
  536. arguments: ELEMENT_LIST_SCHEMA
  537. },
  538. additionalProperties: false
  539. },
  540. ArrayExpression: ELEMENT_LIST_SCHEMA,
  541. ObjectExpression: ELEMENT_LIST_SCHEMA,
  542. ImportDeclaration: ELEMENT_LIST_SCHEMA,
  543. flatTernaryExpressions: {
  544. type: "boolean"
  545. },
  546. ignoredNodes: {
  547. type: "array",
  548. items: {
  549. type: "string",
  550. not: {
  551. pattern: ":exit$"
  552. }
  553. }
  554. },
  555. ignoreComments: {
  556. type: "boolean"
  557. }
  558. },
  559. additionalProperties: false
  560. }
  561. ]
  562. },
  563. create(context) {
  564. const DEFAULT_VARIABLE_INDENT = 1;
  565. const DEFAULT_PARAMETER_INDENT = 1;
  566. const DEFAULT_FUNCTION_BODY_INDENT = 1;
  567. let indentType = "space";
  568. let indentSize = 4;
  569. const options = {
  570. SwitchCase: 0,
  571. VariableDeclarator: {
  572. var: DEFAULT_VARIABLE_INDENT,
  573. let: DEFAULT_VARIABLE_INDENT,
  574. const: DEFAULT_VARIABLE_INDENT
  575. },
  576. outerIIFEBody: 1,
  577. FunctionDeclaration: {
  578. parameters: DEFAULT_PARAMETER_INDENT,
  579. body: DEFAULT_FUNCTION_BODY_INDENT
  580. },
  581. FunctionExpression: {
  582. parameters: DEFAULT_PARAMETER_INDENT,
  583. body: DEFAULT_FUNCTION_BODY_INDENT
  584. },
  585. CallExpression: {
  586. arguments: DEFAULT_PARAMETER_INDENT
  587. },
  588. MemberExpression: 1,
  589. ArrayExpression: 1,
  590. ObjectExpression: 1,
  591. ImportDeclaration: 1,
  592. flatTernaryExpressions: false,
  593. ignoredNodes: [],
  594. ignoreComments: false
  595. };
  596. if (context.options.length) {
  597. if (context.options[0] === "tab") {
  598. indentSize = 1;
  599. indentType = "tab";
  600. } else {
  601. indentSize = context.options[0];
  602. indentType = "space";
  603. }
  604. if (context.options[1]) {
  605. lodash.merge(options, context.options[1]);
  606. if (typeof options.VariableDeclarator === "number") {
  607. options.VariableDeclarator = {
  608. var: options.VariableDeclarator,
  609. let: options.VariableDeclarator,
  610. const: options.VariableDeclarator
  611. };
  612. }
  613. }
  614. }
  615. const sourceCode = context.getSourceCode();
  616. const tokenInfo = new TokenInfo(sourceCode);
  617. const offsets = new OffsetStorage(tokenInfo, indentSize, indentType === "space" ? " " : "\t");
  618. const parameterParens = new WeakSet();
  619. /**
  620. * Creates an error message for a line, given the expected/actual indentation.
  621. * @param {int} expectedAmount The expected amount of indentation characters for this line
  622. * @param {int} actualSpaces The actual number of indentation spaces that were found on this line
  623. * @param {int} actualTabs The actual number of indentation tabs that were found on this line
  624. * @returns {string} An error message for this line
  625. */
  626. function createErrorMessage(expectedAmount, actualSpaces, actualTabs) {
  627. const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs"
  628. const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space"
  629. const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs"
  630. let foundStatement;
  631. if (actualSpaces > 0) {
  632. /*
  633. * Abbreviate the message if the expected indentation is also spaces.
  634. * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
  635. */
  636. foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`;
  637. } else if (actualTabs > 0) {
  638. foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`;
  639. } else {
  640. foundStatement = "0";
  641. }
  642. return `Expected indentation of ${expectedStatement} but found ${foundStatement}.`;
  643. }
  644. /**
  645. * Reports a given indent violation
  646. * @param {Token} token Token violating the indent rule
  647. * @param {string} neededIndent Expected indentation string
  648. * @returns {void}
  649. */
  650. function report(token, neededIndent) {
  651. const actualIndent = Array.from(tokenInfo.getTokenIndent(token));
  652. const numSpaces = actualIndent.filter(char => char === " ").length;
  653. const numTabs = actualIndent.filter(char => char === "\t").length;
  654. context.report({
  655. node: token,
  656. message: createErrorMessage(neededIndent.length, numSpaces, numTabs),
  657. loc: {
  658. start: { line: token.loc.start.line, column: 0 },
  659. end: { line: token.loc.start.line, column: token.loc.start.column }
  660. },
  661. fix(fixer) {
  662. const range = [token.range[0] - token.loc.start.column, token.range[0]];
  663. const newText = neededIndent;
  664. return fixer.replaceTextRange(range, newText);
  665. }
  666. });
  667. }
  668. /**
  669. * Checks if a token's indentation is correct
  670. * @param {Token} token Token to examine
  671. * @param {string} desiredIndent Desired indentation of the string
  672. * @returns {boolean} `true` if the token's indentation is correct
  673. */
  674. function validateTokenIndent(token, desiredIndent) {
  675. const indentation = tokenInfo.getTokenIndent(token);
  676. return indentation === desiredIndent ||
  677. // To avoid conflicts with no-mixed-spaces-and-tabs, don't report mixed spaces and tabs.
  678. indentation.includes(" ") && indentation.includes("\t");
  679. }
  680. /**
  681. * Check to see if the node is a file level IIFE
  682. * @param {ASTNode} node The function node to check.
  683. * @returns {boolean} True if the node is the outer IIFE
  684. */
  685. function isOuterIIFE(node) {
  686. /*
  687. * Verify that the node is an IIFE
  688. */
  689. if (!node.parent || node.parent.type !== "CallExpression" || node.parent.callee !== node) {
  690. return false;
  691. }
  692. /*
  693. * Navigate legal ancestors to determine whether this IIFE is outer.
  694. * A "legal ancestor" is an expression or statement that causes the function to get executed immediately.
  695. * For example, `!(function(){})()` is an outer IIFE even though it is preceded by a ! operator.
  696. */
  697. let statement = node.parent && node.parent.parent;
  698. while (
  699. statement.type === "UnaryExpression" && ["!", "~", "+", "-"].indexOf(statement.operator) > -1 ||
  700. statement.type === "AssignmentExpression" ||
  701. statement.type === "LogicalExpression" ||
  702. statement.type === "SequenceExpression" ||
  703. statement.type === "VariableDeclarator"
  704. ) {
  705. statement = statement.parent;
  706. }
  707. return (statement.type === "ExpressionStatement" || statement.type === "VariableDeclaration") && statement.parent.type === "Program";
  708. }
  709. /**
  710. * Counts the number of linebreaks that follow the last non-whitespace character in a string
  711. * @param {string} string The string to check
  712. * @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character,
  713. * or the total number of linebreaks if the string is all whitespace.
  714. */
  715. function countTrailingLinebreaks(string) {
  716. const trailingWhitespace = string.match(/\s*$/)[0];
  717. const linebreakMatches = trailingWhitespace.match(astUtils.createGlobalLinebreakMatcher());
  718. return linebreakMatches === null ? 0 : linebreakMatches.length;
  719. }
  720. /**
  721. * Check indentation for lists of elements (arrays, objects, function params)
  722. * @param {ASTNode[]} elements List of elements that should be offset
  723. * @param {Token} startToken The start token of the list that element should be aligned against, e.g. '['
  724. * @param {Token} endToken The end token of the list, e.g. ']'
  725. * @param {number|string} offset The amount that the elements should be offset
  726. * @returns {void}
  727. */
  728. function addElementListIndent(elements, startToken, endToken, offset) {
  729. /**
  730. * Gets the first token of a given element, including surrounding parentheses.
  731. * @param {ASTNode} element A node in the `elements` list
  732. * @returns {Token} The first token of this element
  733. */
  734. function getFirstToken(element) {
  735. let token = sourceCode.getTokenBefore(element);
  736. while (astUtils.isOpeningParenToken(token) && token !== startToken) {
  737. token = sourceCode.getTokenBefore(token);
  738. }
  739. return sourceCode.getTokenAfter(token);
  740. }
  741. // Run through all the tokens in the list, and offset them by one indent level (mainly for comments, other things will end up overridden)
  742. offsets.setDesiredOffsets(
  743. [startToken.range[1], endToken.range[0]],
  744. startToken,
  745. typeof offset === "number" ? offset : 1
  746. );
  747. offsets.setDesiredOffset(endToken, startToken, 0);
  748. // If the preference is "first" but there is no first element (e.g. sparse arrays w/ empty first slot), fall back to 1 level.
  749. if (offset === "first" && elements.length && !elements[0]) {
  750. return;
  751. }
  752. elements.forEach((element, index) => {
  753. if (!element) {
  754. // Skip holes in arrays
  755. return;
  756. }
  757. if (offset === "off") {
  758. // Ignore the first token of every element if the "off" option is used
  759. offsets.ignoreToken(getFirstToken(element));
  760. }
  761. // Offset the following elements correctly relative to the first element
  762. if (index === 0) {
  763. return;
  764. }
  765. if (offset === "first" && tokenInfo.isFirstTokenOfLine(getFirstToken(element))) {
  766. offsets.matchOffsetOf(getFirstToken(elements[0]), getFirstToken(element));
  767. } else {
  768. const previousElement = elements[index - 1];
  769. const firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement);
  770. const previousElementLastToken = previousElement && sourceCode.getLastToken(previousElement);
  771. if (
  772. previousElement &&
  773. previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line
  774. ) {
  775. offsets.setDesiredOffsets(element.range, firstTokenOfPreviousElement, 0);
  776. }
  777. }
  778. });
  779. }
  780. /**
  781. * Check and decide whether to check for indentation for blockless nodes
  782. * Scenarios are for or while statements without braces around them
  783. * @param {ASTNode} node node to examine
  784. * @returns {void}
  785. */
  786. function addBlocklessNodeIndent(node) {
  787. if (node.type !== "BlockStatement") {
  788. const lastParentToken = sourceCode.getTokenBefore(node, astUtils.isNotOpeningParenToken);
  789. let firstBodyToken = sourceCode.getFirstToken(node);
  790. let lastBodyToken = sourceCode.getLastToken(node);
  791. while (
  792. astUtils.isOpeningParenToken(sourceCode.getTokenBefore(firstBodyToken)) &&
  793. astUtils.isClosingParenToken(sourceCode.getTokenAfter(lastBodyToken))
  794. ) {
  795. firstBodyToken = sourceCode.getTokenBefore(firstBodyToken);
  796. lastBodyToken = sourceCode.getTokenAfter(lastBodyToken);
  797. }
  798. offsets.setDesiredOffsets([firstBodyToken.range[0], lastBodyToken.range[1]], lastParentToken, 1);
  799. /*
  800. * For blockless nodes with semicolon-first style, don't indent the semicolon.
  801. * e.g.
  802. * if (foo) bar()
  803. * ; [1, 2, 3].map(foo)
  804. */
  805. const lastToken = sourceCode.getLastToken(node);
  806. if (node.type !== "EmptyStatement" && astUtils.isSemicolonToken(lastToken)) {
  807. offsets.setDesiredOffset(lastToken, lastParentToken, 0);
  808. }
  809. }
  810. }
  811. /**
  812. * Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`)
  813. * @param {ASTNode} node A CallExpression or NewExpression node
  814. * @returns {void}
  815. */
  816. function addFunctionCallIndent(node) {
  817. let openingParen;
  818. if (node.arguments.length) {
  819. openingParen = sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken);
  820. } else {
  821. openingParen = sourceCode.getLastToken(node, 1);
  822. }
  823. const closingParen = sourceCode.getLastToken(node);
  824. parameterParens.add(openingParen);
  825. parameterParens.add(closingParen);
  826. offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);
  827. addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments);
  828. }
  829. /**
  830. * Checks the indentation of parenthesized values, given a list of tokens in a program
  831. * @param {Token[]} tokens A list of tokens
  832. * @returns {void}
  833. */
  834. function addParensIndent(tokens) {
  835. const parenStack = [];
  836. const parenPairs = [];
  837. tokens.forEach(nextToken => {
  838. // Accumulate a list of parenthesis pairs
  839. if (astUtils.isOpeningParenToken(nextToken)) {
  840. parenStack.push(nextToken);
  841. } else if (astUtils.isClosingParenToken(nextToken)) {
  842. parenPairs.unshift({ left: parenStack.pop(), right: nextToken });
  843. }
  844. });
  845. parenPairs.forEach(pair => {
  846. const leftParen = pair.left;
  847. const rightParen = pair.right;
  848. // We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments.
  849. if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) {
  850. const parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen));
  851. parenthesizedTokens.forEach(token => {
  852. if (!parenthesizedTokens.has(offsets.getFirstDependency(token))) {
  853. offsets.setDesiredOffset(token, leftParen, 1);
  854. }
  855. });
  856. }
  857. offsets.setDesiredOffset(rightParen, leftParen, 0);
  858. });
  859. }
  860. /**
  861. * Ignore all tokens within an unknown node whose offset do not depend
  862. * on another token's offset within the unknown node
  863. * @param {ASTNode} node Unknown Node
  864. * @returns {void}
  865. */
  866. function ignoreNode(node) {
  867. const unknownNodeTokens = new Set(sourceCode.getTokens(node, { includeComments: true }));
  868. unknownNodeTokens.forEach(token => {
  869. if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) {
  870. const firstTokenOfLine = tokenInfo.getFirstTokenOfLine(token);
  871. if (token === firstTokenOfLine) {
  872. offsets.ignoreToken(token);
  873. } else {
  874. offsets.setDesiredOffset(token, firstTokenOfLine, 0);
  875. }
  876. }
  877. });
  878. }
  879. /**
  880. * Check whether the given token is on the first line of a statement.
  881. * @param {Token} token The token to check.
  882. * @param {ASTNode} leafNode The expression node that the token belongs directly.
  883. * @returns {boolean} `true` if the token is on the first line of a statement.
  884. */
  885. function isOnFirstLineOfStatement(token, leafNode) {
  886. let node = leafNode;
  887. while (node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration")) {
  888. node = node.parent;
  889. }
  890. node = node.parent;
  891. return !node || node.loc.start.line === token.loc.start.line;
  892. }
  893. const ignoredNodeFirstTokens = new Set();
  894. const baseOffsetListeners = {
  895. "ArrayExpression, ArrayPattern"(node) {
  896. const openingBracket = sourceCode.getFirstToken(node);
  897. const closingBracket = sourceCode.getTokenAfter(lodash.findLast(node.elements) || openingBracket, astUtils.isClosingBracketToken);
  898. addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression);
  899. },
  900. "ObjectExpression, ObjectPattern"(node) {
  901. const openingCurly = sourceCode.getFirstToken(node);
  902. const closingCurly = sourceCode.getTokenAfter(
  903. node.properties.length ? node.properties[node.properties.length - 1] : openingCurly,
  904. astUtils.isClosingBraceToken
  905. );
  906. addElementListIndent(node.properties, openingCurly, closingCurly, options.ObjectExpression);
  907. },
  908. ArrowFunctionExpression(node) {
  909. const firstToken = sourceCode.getFirstToken(node);
  910. if (astUtils.isOpeningParenToken(firstToken)) {
  911. const openingParen = firstToken;
  912. const closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken);
  913. parameterParens.add(openingParen);
  914. parameterParens.add(closingParen);
  915. addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
  916. }
  917. addBlocklessNodeIndent(node.body);
  918. },
  919. AssignmentExpression(node) {
  920. const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
  921. offsets.setDesiredOffsets([operator.range[0], node.range[1]], sourceCode.getLastToken(node.left), 1);
  922. offsets.ignoreToken(operator);
  923. offsets.ignoreToken(sourceCode.getTokenAfter(operator));
  924. },
  925. "BinaryExpression, LogicalExpression"(node) {
  926. const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
  927. /*
  928. * For backwards compatibility, don't check BinaryExpression indents, e.g.
  929. * var foo = bar &&
  930. * baz;
  931. */
  932. const tokenAfterOperator = sourceCode.getTokenAfter(operator);
  933. offsets.ignoreToken(operator);
  934. offsets.ignoreToken(tokenAfterOperator);
  935. offsets.setDesiredOffset(tokenAfterOperator, operator, 0);
  936. },
  937. "BlockStatement, ClassBody"(node) {
  938. let blockIndentLevel;
  939. if (node.parent && isOuterIIFE(node.parent)) {
  940. blockIndentLevel = options.outerIIFEBody;
  941. } else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) {
  942. blockIndentLevel = options.FunctionExpression.body;
  943. } else if (node.parent && node.parent.type === "FunctionDeclaration") {
  944. blockIndentLevel = options.FunctionDeclaration.body;
  945. } else {
  946. blockIndentLevel = 1;
  947. }
  948. /*
  949. * For blocks that aren't lone statements, ensure that the opening curly brace
  950. * is aligned with the parent.
  951. */
  952. if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) {
  953. offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0);
  954. }
  955. addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel);
  956. },
  957. CallExpression: addFunctionCallIndent,
  958. "ClassDeclaration[superClass], ClassExpression[superClass]"(node) {
  959. const classToken = sourceCode.getFirstToken(node);
  960. const extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken);
  961. offsets.setDesiredOffsets([extendsToken.range[0], node.body.range[0]], classToken, 1);
  962. },
  963. ConditionalExpression(node) {
  964. const firstToken = sourceCode.getFirstToken(node);
  965. // `flatTernaryExpressions` option is for the following style:
  966. // var a =
  967. // foo > 0 ? bar :
  968. // foo < 0 ? baz :
  969. // /*else*/ qiz ;
  970. if (!options.flatTernaryExpressions ||
  971. !astUtils.isTokenOnSameLine(node.test, node.consequent) ||
  972. isOnFirstLineOfStatement(firstToken, node)
  973. ) {
  974. const questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.type === "Punctuator" && token.value === "?");
  975. const colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, token => token.type === "Punctuator" && token.value === ":");
  976. const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken);
  977. const lastConsequentToken = sourceCode.getTokenBefore(colonToken);
  978. const firstAlternateToken = sourceCode.getTokenAfter(colonToken);
  979. offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
  980. offsets.setDesiredOffset(colonToken, firstToken, 1);
  981. offsets.setDesiredOffset(firstConsequentToken, firstToken, 1);
  982. /*
  983. * The alternate and the consequent should usually have the same indentation.
  984. * If they share part of a line, align the alternate against the first token of the consequent.
  985. * This allows the alternate to be indented correctly in cases like this:
  986. * foo ? (
  987. * bar
  988. * ) : ( // this '(' is aligned with the '(' above, so it's considered to be aligned with `foo`
  989. * baz // as a result, `baz` is offset by 1 rather than 2
  990. * )
  991. */
  992. if (lastConsequentToken.loc.end.line === firstAlternateToken.loc.start.line) {
  993. offsets.setDesiredOffset(firstAlternateToken, firstConsequentToken, 0);
  994. } else {
  995. /**
  996. * If the alternate and consequent do not share part of a line, offset the alternate from the first
  997. * token of the conditional expression. For example:
  998. * foo ? bar
  999. * : baz
  1000. *
  1001. * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up
  1002. * having no expected indentation.
  1003. */
  1004. offsets.setDesiredOffset(firstAlternateToken, firstToken, 1);
  1005. }
  1006. }
  1007. },
  1008. "DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement": node => addBlocklessNodeIndent(node.body),
  1009. ExportNamedDeclaration(node) {
  1010. if (node.declaration === null) {
  1011. const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
  1012. // Indent the specifiers in `export {foo, bar, baz}`
  1013. addElementListIndent(node.specifiers, sourceCode.getFirstToken(node, { skip: 1 }), closingCurly, 1);
  1014. if (node.source) {
  1015. // Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'`
  1016. offsets.setDesiredOffsets([closingCurly.range[1], node.range[1]], sourceCode.getFirstToken(node), 1);
  1017. }
  1018. }
  1019. },
  1020. ForStatement(node) {
  1021. const forOpeningParen = sourceCode.getFirstToken(node, 1);
  1022. if (node.init) {
  1023. offsets.setDesiredOffsets(node.init.range, forOpeningParen, 1);
  1024. }
  1025. if (node.test) {
  1026. offsets.setDesiredOffsets(node.test.range, forOpeningParen, 1);
  1027. }
  1028. if (node.update) {
  1029. offsets.setDesiredOffsets(node.update.range, forOpeningParen, 1);
  1030. }
  1031. addBlocklessNodeIndent(node.body);
  1032. },
  1033. "FunctionDeclaration, FunctionExpression"(node) {
  1034. const closingParen = sourceCode.getTokenBefore(node.body);
  1035. const openingParen = sourceCode.getTokenBefore(node.params.length ? node.params[0] : closingParen);
  1036. parameterParens.add(openingParen);
  1037. parameterParens.add(closingParen);
  1038. addElementListIndent(node.params, openingParen, closingParen, options[node.type].parameters);
  1039. },
  1040. IfStatement(node) {
  1041. addBlocklessNodeIndent(node.consequent);
  1042. if (node.alternate && node.alternate.type !== "IfStatement") {
  1043. addBlocklessNodeIndent(node.alternate);
  1044. }
  1045. },
  1046. ImportDeclaration(node) {
  1047. if (node.specifiers.some(specifier => specifier.type === "ImportSpecifier")) {
  1048. const openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken);
  1049. const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
  1050. addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration);
  1051. }
  1052. const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from");
  1053. if (fromToken) {
  1054. offsets.setDesiredOffsets([fromToken.range[0], node.range[1]], sourceCode.getFirstToken(node), 1);
  1055. }
  1056. },
  1057. "MemberExpression, JSXMemberExpression, MetaProperty"(node) {
  1058. const object = node.type === "MetaProperty" ? node.meta : node.object;
  1059. const firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken);
  1060. const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken);
  1061. const objectParenCount = sourceCode.getTokensBetween(object, node.property, { filter: astUtils.isClosingParenToken }).length;
  1062. const firstObjectToken = objectParenCount
  1063. ? sourceCode.getTokenBefore(object, { skip: objectParenCount - 1 })
  1064. : sourceCode.getFirstToken(object);
  1065. const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken);
  1066. const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;
  1067. if (node.computed) {
  1068. // For computed MemberExpressions, match the closing bracket with the opening bracket.
  1069. offsets.setDesiredOffset(sourceCode.getLastToken(node), firstNonObjectToken, 0);
  1070. offsets.setDesiredOffsets(node.property.range, firstNonObjectToken, 1);
  1071. }
  1072. /*
  1073. * If the object ends on the same line that the property starts, match against the last token
  1074. * of the object, to ensure that the MemberExpression is not indented.
  1075. *
  1076. * Otherwise, match against the first token of the object, e.g.
  1077. * foo
  1078. * .bar
  1079. * .baz // <-- offset by 1 from `foo`
  1080. */
  1081. const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line
  1082. ? lastObjectToken
  1083. : firstObjectToken;
  1084. if (typeof options.MemberExpression === "number") {
  1085. // Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object.
  1086. offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression);
  1087. /*
  1088. * For computed MemberExpressions, match the first token of the property against the opening bracket.
  1089. * Otherwise, match the first token of the property against the object.
  1090. */
  1091. offsets.setDesiredOffset(secondNonObjectToken, node.computed ? firstNonObjectToken : offsetBase, options.MemberExpression);
  1092. } else {
  1093. // If the MemberExpression option is off, ignore the dot and the first token of the property.
  1094. offsets.ignoreToken(firstNonObjectToken);
  1095. offsets.ignoreToken(secondNonObjectToken);
  1096. // To ignore the property indentation, ensure that the property tokens depend on the ignored tokens.
  1097. offsets.setDesiredOffset(firstNonObjectToken, offsetBase, 0);
  1098. offsets.setDesiredOffset(secondNonObjectToken, firstNonObjectToken, 0);
  1099. }
  1100. },
  1101. NewExpression(node) {
  1102. // Only indent the arguments if the NewExpression has parens (e.g. `new Foo(bar)` or `new Foo()`, but not `new Foo`
  1103. if (node.arguments.length > 0 ||
  1104. astUtils.isClosingParenToken(sourceCode.getLastToken(node)) &&
  1105. astUtils.isOpeningParenToken(sourceCode.getLastToken(node, 1))) {
  1106. addFunctionCallIndent(node);
  1107. }
  1108. },
  1109. Property(node) {
  1110. if (!node.shorthand && !node.method && node.kind === "init") {
  1111. const colon = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isColonToken);
  1112. offsets.ignoreToken(sourceCode.getTokenAfter(colon));
  1113. }
  1114. },
  1115. SwitchStatement(node) {
  1116. const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken);
  1117. const closingCurly = sourceCode.getLastToken(node);
  1118. offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, options.SwitchCase);
  1119. if (node.cases.length) {
  1120. sourceCode.getTokensBetween(
  1121. node.cases[node.cases.length - 1],
  1122. closingCurly,
  1123. { includeComments: true, filter: astUtils.isCommentToken }
  1124. ).forEach(token => offsets.ignoreToken(token));
  1125. }
  1126. },
  1127. SwitchCase(node) {
  1128. if (!(node.consequent.length === 1 && node.consequent[0].type === "BlockStatement")) {
  1129. const caseKeyword = sourceCode.getFirstToken(node);
  1130. const tokenAfterCurrentCase = sourceCode.getTokenAfter(node);
  1131. offsets.setDesiredOffsets([caseKeyword.range[1], tokenAfterCurrentCase.range[0]], caseKeyword, 1);
  1132. }
  1133. },
  1134. TemplateLiteral(node) {
  1135. node.expressions.forEach((expression, index) => {
  1136. const previousQuasi = node.quasis[index];
  1137. const nextQuasi = node.quasis[index + 1];
  1138. const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line ? sourceCode.getFirstToken(previousQuasi) : null;
  1139. offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1);
  1140. offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0);
  1141. });
  1142. },
  1143. VariableDeclaration(node) {
  1144. const variableIndent = options.VariableDeclarator.hasOwnProperty(node.kind) ? options.VariableDeclarator[node.kind] : DEFAULT_VARIABLE_INDENT;
  1145. if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) {
  1146. /*
  1147. * VariableDeclarator indentation is a bit different from other forms of indentation, in that the
  1148. * indentation of an opening bracket sometimes won't match that of a closing bracket. For example,
  1149. * the following indentations are correct:
  1150. *
  1151. * var foo = {
  1152. * ok: true
  1153. * };
  1154. *
  1155. * var foo = {
  1156. * ok: true,
  1157. * },
  1158. * bar = 1;
  1159. *
  1160. * Account for when exiting the AST (after indentations have already been set for the nodes in
  1161. * the declaration) by manually increasing the indentation level of the tokens in this declarator
  1162. * on the same line as the start of the declaration, provided that there are declarators that
  1163. * follow this one.
  1164. */
  1165. const firstToken = sourceCode.getFirstToken(node);
  1166. offsets.setDesiredOffsets(node.range, firstToken, variableIndent, true);
  1167. } else {
  1168. offsets.setDesiredOffsets(node.range, sourceCode.getFirstToken(node), variableIndent);
  1169. }
  1170. const lastToken = sourceCode.getLastToken(node);
  1171. if (astUtils.isSemicolonToken(lastToken)) {
  1172. offsets.ignoreToken(lastToken);
  1173. }
  1174. },
  1175. VariableDeclarator(node) {
  1176. if (node.init) {
  1177. const equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken);
  1178. const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator);
  1179. offsets.ignoreToken(equalOperator);
  1180. offsets.ignoreToken(tokenAfterOperator);
  1181. offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1);
  1182. offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0);
  1183. }
  1184. },
  1185. "JSXAttribute[value]"(node) {
  1186. const equalsToken = sourceCode.getFirstTokenBetween(node.name, node.value, token => token.type === "Punctuator" && token.value === "=");
  1187. offsets.setDesiredOffsets([equalsToken.range[0], node.value.range[1]], sourceCode.getFirstToken(node.name), 1);
  1188. },
  1189. JSXElement(node) {
  1190. if (node.closingElement) {
  1191. addElementListIndent(node.children, sourceCode.getFirstToken(node.openingElement), sourceCode.getFirstToken(node.closingElement), 1);
  1192. }
  1193. },
  1194. JSXOpeningElement(node) {
  1195. const firstToken = sourceCode.getFirstToken(node);
  1196. let closingToken;
  1197. if (node.selfClosing) {
  1198. closingToken = sourceCode.getLastToken(node, { skip: 1 });
  1199. offsets.setDesiredOffset(sourceCode.getLastToken(node), closingToken, 0);
  1200. } else {
  1201. closingToken = sourceCode.getLastToken(node);
  1202. }
  1203. offsets.setDesiredOffsets(node.name.range, sourceCode.getFirstToken(node));
  1204. addElementListIndent(node.attributes, firstToken, closingToken, 1);
  1205. },
  1206. JSXClosingElement(node) {
  1207. const firstToken = sourceCode.getFirstToken(node);
  1208. offsets.setDesiredOffsets(node.name.range, firstToken, 1);
  1209. },
  1210. JSXExpressionContainer(node) {
  1211. const openingCurly = sourceCode.getFirstToken(node);
  1212. const closingCurly = sourceCode.getLastToken(node);
  1213. offsets.setDesiredOffsets(
  1214. [openingCurly.range[1], closingCurly.range[0]],
  1215. openingCurly,
  1216. 1
  1217. );
  1218. },
  1219. "*"(node) {
  1220. const firstToken = sourceCode.getFirstToken(node);
  1221. // Ensure that the children of every node are indented at least as much as the first token.
  1222. if (firstToken && !ignoredNodeFirstTokens.has(firstToken)) {
  1223. offsets.setDesiredOffsets(node.range, firstToken, 0);
  1224. }
  1225. }
  1226. };
  1227. const listenerCallQueue = [];
  1228. /*
  1229. * To ignore the indentation of a node:
  1230. * 1. Don't call the node's listener when entering it (if it has a listener)
  1231. * 2. Don't set any offsets against the first token of the node.
  1232. * 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets.
  1233. */
  1234. const offsetListeners = lodash.mapValues(
  1235. baseOffsetListeners,
  1236. /*
  1237. * Offset listener calls are deferred until traversal is finished, and are called as
  1238. * part of the final `Program:exit` listener. This is necessary because a node might
  1239. * be matched by multiple selectors.
  1240. *
  1241. * Example: Suppose there is an offset listener for `Identifier`, and the user has
  1242. * specified in configuration that `MemberExpression > Identifier` should be ignored.
  1243. * Due to selector specificity rules, the `Identifier` listener will get called first. However,
  1244. * if a given Identifier node is supposed to be ignored, then the `Identifier` offset listener
  1245. * should not have been called at all. Without doing extra selector matching, we don't know
  1246. * whether the Identifier matches the `MemberExpression > Identifier` selector until the
  1247. * `MemberExpression > Identifier` listener is called.
  1248. *
  1249. * To avoid this, the `Identifier` listener isn't called until traversal finishes and all
  1250. * ignored nodes are known.
  1251. */
  1252. listener =>
  1253. node =>
  1254. listenerCallQueue.push({ listener, node })
  1255. );
  1256. // For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set.
  1257. const ignoredNodes = new Set();
  1258. /**
  1259. * Ignores a node
  1260. * @param {ASTNode} node The node to ignore
  1261. * @returns {void}
  1262. */
  1263. function addToIgnoredNodes(node) {
  1264. ignoredNodes.add(node);
  1265. ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node));
  1266. }
  1267. const ignoredNodeListeners = options.ignoredNodes.reduce(
  1268. (listeners, ignoredSelector) => Object.assign(listeners, { [ignoredSelector]: addToIgnoredNodes }),
  1269. {}
  1270. );
  1271. /*
  1272. * Join the listeners, and add a listener to verify that all tokens actually have the correct indentation
  1273. * at the end.
  1274. *
  1275. * Using Object.assign will cause some offset listeners to be overwritten if the same selector also appears
  1276. * in `ignoredNodeListeners`. This isn't a problem because all of the matching nodes will be ignored,
  1277. * so those listeners wouldn't be called anyway.
  1278. */
  1279. return Object.assign(
  1280. offsetListeners,
  1281. ignoredNodeListeners,
  1282. {
  1283. "*:exit"(node) {
  1284. // If a node's type is nonstandard, we can't tell how its children should be offset, so ignore it.
  1285. if (!KNOWN_NODES.has(node.type)) {
  1286. addToIgnoredNodes(node);
  1287. }
  1288. },
  1289. "Program:exit"() {
  1290. // If ignoreComments option is enabled, ignore all comment tokens.
  1291. if (options.ignoreComments) {
  1292. sourceCode.getAllComments()
  1293. .forEach(comment => offsets.ignoreToken(comment));
  1294. }
  1295. // Invoke the queued offset listeners for the nodes that aren't ignored.
  1296. listenerCallQueue
  1297. .filter(nodeInfo => !ignoredNodes.has(nodeInfo.node))
  1298. .forEach(nodeInfo => nodeInfo.listener(nodeInfo.node));
  1299. // Update the offsets for ignored nodes to prevent their child tokens from being reported.
  1300. ignoredNodes.forEach(ignoreNode);
  1301. addParensIndent(sourceCode.ast.tokens);
  1302. /*
  1303. * Create a Map from (tokenOrComment) => (precedingToken).
  1304. * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly.
  1305. */
  1306. const precedingTokens = sourceCode.ast.comments.reduce((commentMap, comment) => {
  1307. const tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { includeComments: true });
  1308. return commentMap.set(comment, commentMap.has(tokenOrCommentBefore) ? commentMap.get(tokenOrCommentBefore) : tokenOrCommentBefore);
  1309. }, new WeakMap());
  1310. sourceCode.lines.forEach((line, lineIndex) => {
  1311. const lineNumber = lineIndex + 1;
  1312. if (!tokenInfo.firstTokensByLineNumber.has(lineNumber)) {
  1313. // Don't check indentation on blank lines
  1314. return;
  1315. }
  1316. const firstTokenOfLine = tokenInfo.firstTokensByLineNumber.get(lineNumber);
  1317. if (firstTokenOfLine.loc.start.line !== lineNumber) {
  1318. // Don't check the indentation of multi-line tokens (e.g. template literals or block comments) twice.
  1319. return;
  1320. }
  1321. // If the token matches the expected expected indentation, don't report it.
  1322. if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) {
  1323. return;
  1324. }
  1325. if (astUtils.isCommentToken(firstTokenOfLine)) {
  1326. const tokenBefore = precedingTokens.get(firstTokenOfLine);
  1327. const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0];
  1328. // If a comment matches the expected indentation of the token immediately before or after, don't report it.
  1329. if (
  1330. tokenBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
  1331. tokenAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))
  1332. ) {
  1333. return;
  1334. }
  1335. }
  1336. // Otherwise, report the token/comment.
  1337. report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine));
  1338. });
  1339. }
  1340. }
  1341. );
  1342. }
  1343. };