Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

3 лет назад
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591
  1. /**
  2. * @fileoverview This rule sets a specific indentation style and 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("../util/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(
  776. [previousElement.range[1], element.range[1]],
  777. firstTokenOfPreviousElement,
  778. 0
  779. );
  780. }
  781. }
  782. });
  783. }
  784. /**
  785. * Check and decide whether to check for indentation for blockless nodes
  786. * Scenarios are for or while statements without braces around them
  787. * @param {ASTNode} node node to examine
  788. * @returns {void}
  789. */
  790. function addBlocklessNodeIndent(node) {
  791. if (node.type !== "BlockStatement") {
  792. const lastParentToken = sourceCode.getTokenBefore(node, astUtils.isNotOpeningParenToken);
  793. let firstBodyToken = sourceCode.getFirstToken(node);
  794. let lastBodyToken = sourceCode.getLastToken(node);
  795. while (
  796. astUtils.isOpeningParenToken(sourceCode.getTokenBefore(firstBodyToken)) &&
  797. astUtils.isClosingParenToken(sourceCode.getTokenAfter(lastBodyToken))
  798. ) {
  799. firstBodyToken = sourceCode.getTokenBefore(firstBodyToken);
  800. lastBodyToken = sourceCode.getTokenAfter(lastBodyToken);
  801. }
  802. offsets.setDesiredOffsets([firstBodyToken.range[0], lastBodyToken.range[1]], lastParentToken, 1);
  803. /*
  804. * For blockless nodes with semicolon-first style, don't indent the semicolon.
  805. * e.g.
  806. * if (foo) bar()
  807. * ; [1, 2, 3].map(foo)
  808. */
  809. const lastToken = sourceCode.getLastToken(node);
  810. if (node.type !== "EmptyStatement" && astUtils.isSemicolonToken(lastToken)) {
  811. offsets.setDesiredOffset(lastToken, lastParentToken, 0);
  812. }
  813. }
  814. }
  815. /**
  816. * Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`)
  817. * @param {ASTNode} node A CallExpression or NewExpression node
  818. * @returns {void}
  819. */
  820. function addFunctionCallIndent(node) {
  821. let openingParen;
  822. if (node.arguments.length) {
  823. openingParen = sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken);
  824. } else {
  825. openingParen = sourceCode.getLastToken(node, 1);
  826. }
  827. const closingParen = sourceCode.getLastToken(node);
  828. parameterParens.add(openingParen);
  829. parameterParens.add(closingParen);
  830. offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);
  831. addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments);
  832. }
  833. /**
  834. * Checks the indentation of parenthesized values, given a list of tokens in a program
  835. * @param {Token[]} tokens A list of tokens
  836. * @returns {void}
  837. */
  838. function addParensIndent(tokens) {
  839. const parenStack = [];
  840. const parenPairs = [];
  841. tokens.forEach(nextToken => {
  842. // Accumulate a list of parenthesis pairs
  843. if (astUtils.isOpeningParenToken(nextToken)) {
  844. parenStack.push(nextToken);
  845. } else if (astUtils.isClosingParenToken(nextToken)) {
  846. parenPairs.unshift({ left: parenStack.pop(), right: nextToken });
  847. }
  848. });
  849. parenPairs.forEach(pair => {
  850. const leftParen = pair.left;
  851. const rightParen = pair.right;
  852. // We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments.
  853. if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) {
  854. const parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen));
  855. parenthesizedTokens.forEach(token => {
  856. if (!parenthesizedTokens.has(offsets.getFirstDependency(token))) {
  857. offsets.setDesiredOffset(token, leftParen, 1);
  858. }
  859. });
  860. }
  861. offsets.setDesiredOffset(rightParen, leftParen, 0);
  862. });
  863. }
  864. /**
  865. * Ignore all tokens within an unknown node whose offset do not depend
  866. * on another token's offset within the unknown node
  867. * @param {ASTNode} node Unknown Node
  868. * @returns {void}
  869. */
  870. function ignoreNode(node) {
  871. const unknownNodeTokens = new Set(sourceCode.getTokens(node, { includeComments: true }));
  872. unknownNodeTokens.forEach(token => {
  873. if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) {
  874. const firstTokenOfLine = tokenInfo.getFirstTokenOfLine(token);
  875. if (token === firstTokenOfLine) {
  876. offsets.ignoreToken(token);
  877. } else {
  878. offsets.setDesiredOffset(token, firstTokenOfLine, 0);
  879. }
  880. }
  881. });
  882. }
  883. /**
  884. * Check whether the given token is on the first line of a statement.
  885. * @param {Token} token The token to check.
  886. * @param {ASTNode} leafNode The expression node that the token belongs directly.
  887. * @returns {boolean} `true` if the token is on the first line of a statement.
  888. */
  889. function isOnFirstLineOfStatement(token, leafNode) {
  890. let node = leafNode;
  891. while (node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration")) {
  892. node = node.parent;
  893. }
  894. node = node.parent;
  895. return !node || node.loc.start.line === token.loc.start.line;
  896. }
  897. /**
  898. * Check whether there are any blank (whitespace-only) lines between
  899. * two tokens on separate lines.
  900. * @param {Token} firstToken The first token.
  901. * @param {Token} secondToken The second token.
  902. * @returns {boolean} `true` if the tokens are on separate lines and
  903. * there exists a blank line between them, `false` otherwise.
  904. */
  905. function hasBlankLinesBetween(firstToken, secondToken) {
  906. const firstTokenLine = firstToken.loc.end.line;
  907. const secondTokenLine = secondToken.loc.start.line;
  908. if (firstTokenLine === secondTokenLine || firstTokenLine === secondTokenLine - 1) {
  909. return false;
  910. }
  911. for (let line = firstTokenLine + 1; line < secondTokenLine; ++line) {
  912. if (!tokenInfo.firstTokensByLineNumber.has(line)) {
  913. return true;
  914. }
  915. }
  916. return false;
  917. }
  918. const ignoredNodeFirstTokens = new Set();
  919. const baseOffsetListeners = {
  920. "ArrayExpression, ArrayPattern"(node) {
  921. const openingBracket = sourceCode.getFirstToken(node);
  922. const closingBracket = sourceCode.getTokenAfter(lodash.findLast(node.elements) || openingBracket, astUtils.isClosingBracketToken);
  923. addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression);
  924. },
  925. "ObjectExpression, ObjectPattern"(node) {
  926. const openingCurly = sourceCode.getFirstToken(node);
  927. const closingCurly = sourceCode.getTokenAfter(
  928. node.properties.length ? node.properties[node.properties.length - 1] : openingCurly,
  929. astUtils.isClosingBraceToken
  930. );
  931. addElementListIndent(node.properties, openingCurly, closingCurly, options.ObjectExpression);
  932. },
  933. ArrowFunctionExpression(node) {
  934. const firstToken = sourceCode.getFirstToken(node);
  935. if (astUtils.isOpeningParenToken(firstToken)) {
  936. const openingParen = firstToken;
  937. const closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken);
  938. parameterParens.add(openingParen);
  939. parameterParens.add(closingParen);
  940. addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
  941. }
  942. addBlocklessNodeIndent(node.body);
  943. },
  944. AssignmentExpression(node) {
  945. const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
  946. offsets.setDesiredOffsets([operator.range[0], node.range[1]], sourceCode.getLastToken(node.left), 1);
  947. offsets.ignoreToken(operator);
  948. offsets.ignoreToken(sourceCode.getTokenAfter(operator));
  949. },
  950. "BinaryExpression, LogicalExpression"(node) {
  951. const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
  952. /*
  953. * For backwards compatibility, don't check BinaryExpression indents, e.g.
  954. * var foo = bar &&
  955. * baz;
  956. */
  957. const tokenAfterOperator = sourceCode.getTokenAfter(operator);
  958. offsets.ignoreToken(operator);
  959. offsets.ignoreToken(tokenAfterOperator);
  960. offsets.setDesiredOffset(tokenAfterOperator, operator, 0);
  961. },
  962. "BlockStatement, ClassBody"(node) {
  963. let blockIndentLevel;
  964. if (node.parent && isOuterIIFE(node.parent)) {
  965. blockIndentLevel = options.outerIIFEBody;
  966. } else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) {
  967. blockIndentLevel = options.FunctionExpression.body;
  968. } else if (node.parent && node.parent.type === "FunctionDeclaration") {
  969. blockIndentLevel = options.FunctionDeclaration.body;
  970. } else {
  971. blockIndentLevel = 1;
  972. }
  973. /*
  974. * For blocks that aren't lone statements, ensure that the opening curly brace
  975. * is aligned with the parent.
  976. */
  977. if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) {
  978. offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0);
  979. }
  980. addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel);
  981. },
  982. CallExpression: addFunctionCallIndent,
  983. "ClassDeclaration[superClass], ClassExpression[superClass]"(node) {
  984. const classToken = sourceCode.getFirstToken(node);
  985. const extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken);
  986. offsets.setDesiredOffsets([extendsToken.range[0], node.body.range[0]], classToken, 1);
  987. },
  988. ConditionalExpression(node) {
  989. const firstToken = sourceCode.getFirstToken(node);
  990. // `flatTernaryExpressions` option is for the following style:
  991. // var a =
  992. // foo > 0 ? bar :
  993. // foo < 0 ? baz :
  994. // /*else*/ qiz ;
  995. if (!options.flatTernaryExpressions ||
  996. !astUtils.isTokenOnSameLine(node.test, node.consequent) ||
  997. isOnFirstLineOfStatement(firstToken, node)
  998. ) {
  999. const questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.type === "Punctuator" && token.value === "?");
  1000. const colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, token => token.type === "Punctuator" && token.value === ":");
  1001. const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken);
  1002. const lastConsequentToken = sourceCode.getTokenBefore(colonToken);
  1003. const firstAlternateToken = sourceCode.getTokenAfter(colonToken);
  1004. offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
  1005. offsets.setDesiredOffset(colonToken, firstToken, 1);
  1006. offsets.setDesiredOffset(firstConsequentToken, firstToken, 1);
  1007. /*
  1008. * The alternate and the consequent should usually have the same indentation.
  1009. * If they share part of a line, align the alternate against the first token of the consequent.
  1010. * This allows the alternate to be indented correctly in cases like this:
  1011. * foo ? (
  1012. * bar
  1013. * ) : ( // this '(' is aligned with the '(' above, so it's considered to be aligned with `foo`
  1014. * baz // as a result, `baz` is offset by 1 rather than 2
  1015. * )
  1016. */
  1017. if (lastConsequentToken.loc.end.line === firstAlternateToken.loc.start.line) {
  1018. offsets.setDesiredOffset(firstAlternateToken, firstConsequentToken, 0);
  1019. } else {
  1020. /**
  1021. * If the alternate and consequent do not share part of a line, offset the alternate from the first
  1022. * token of the conditional expression. For example:
  1023. * foo ? bar
  1024. * : baz
  1025. *
  1026. * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up
  1027. * having no expected indentation.
  1028. */
  1029. offsets.setDesiredOffset(firstAlternateToken, firstToken, 1);
  1030. }
  1031. }
  1032. },
  1033. "DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement": node => addBlocklessNodeIndent(node.body),
  1034. ExportNamedDeclaration(node) {
  1035. if (node.declaration === null) {
  1036. const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
  1037. // Indent the specifiers in `export {foo, bar, baz}`
  1038. addElementListIndent(node.specifiers, sourceCode.getFirstToken(node, { skip: 1 }), closingCurly, 1);
  1039. if (node.source) {
  1040. // Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'`
  1041. offsets.setDesiredOffsets([closingCurly.range[1], node.range[1]], sourceCode.getFirstToken(node), 1);
  1042. }
  1043. }
  1044. },
  1045. ForStatement(node) {
  1046. const forOpeningParen = sourceCode.getFirstToken(node, 1);
  1047. if (node.init) {
  1048. offsets.setDesiredOffsets(node.init.range, forOpeningParen, 1);
  1049. }
  1050. if (node.test) {
  1051. offsets.setDesiredOffsets(node.test.range, forOpeningParen, 1);
  1052. }
  1053. if (node.update) {
  1054. offsets.setDesiredOffsets(node.update.range, forOpeningParen, 1);
  1055. }
  1056. addBlocklessNodeIndent(node.body);
  1057. },
  1058. "FunctionDeclaration, FunctionExpression"(node) {
  1059. const closingParen = sourceCode.getTokenBefore(node.body);
  1060. const openingParen = sourceCode.getTokenBefore(node.params.length ? node.params[0] : closingParen);
  1061. parameterParens.add(openingParen);
  1062. parameterParens.add(closingParen);
  1063. addElementListIndent(node.params, openingParen, closingParen, options[node.type].parameters);
  1064. },
  1065. IfStatement(node) {
  1066. addBlocklessNodeIndent(node.consequent);
  1067. if (node.alternate && node.alternate.type !== "IfStatement") {
  1068. addBlocklessNodeIndent(node.alternate);
  1069. }
  1070. },
  1071. ImportDeclaration(node) {
  1072. if (node.specifiers.some(specifier => specifier.type === "ImportSpecifier")) {
  1073. const openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken);
  1074. const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
  1075. addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration);
  1076. }
  1077. const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from");
  1078. if (fromToken) {
  1079. offsets.setDesiredOffsets([fromToken.range[0], node.range[1]], sourceCode.getFirstToken(node), 1);
  1080. }
  1081. },
  1082. "MemberExpression, JSXMemberExpression, MetaProperty"(node) {
  1083. const object = node.type === "MetaProperty" ? node.meta : node.object;
  1084. const firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken);
  1085. const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken);
  1086. const objectParenCount = sourceCode.getTokensBetween(object, node.property, { filter: astUtils.isClosingParenToken }).length;
  1087. const firstObjectToken = objectParenCount
  1088. ? sourceCode.getTokenBefore(object, { skip: objectParenCount - 1 })
  1089. : sourceCode.getFirstToken(object);
  1090. const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken);
  1091. const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;
  1092. if (node.computed) {
  1093. // For computed MemberExpressions, match the closing bracket with the opening bracket.
  1094. offsets.setDesiredOffset(sourceCode.getLastToken(node), firstNonObjectToken, 0);
  1095. offsets.setDesiredOffsets(node.property.range, firstNonObjectToken, 1);
  1096. }
  1097. /*
  1098. * If the object ends on the same line that the property starts, match against the last token
  1099. * of the object, to ensure that the MemberExpression is not indented.
  1100. *
  1101. * Otherwise, match against the first token of the object, e.g.
  1102. * foo
  1103. * .bar
  1104. * .baz // <-- offset by 1 from `foo`
  1105. */
  1106. const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line
  1107. ? lastObjectToken
  1108. : firstObjectToken;
  1109. if (typeof options.MemberExpression === "number") {
  1110. // Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object.
  1111. offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression);
  1112. /*
  1113. * For computed MemberExpressions, match the first token of the property against the opening bracket.
  1114. * Otherwise, match the first token of the property against the object.
  1115. */
  1116. offsets.setDesiredOffset(secondNonObjectToken, node.computed ? firstNonObjectToken : offsetBase, options.MemberExpression);
  1117. } else {
  1118. // If the MemberExpression option is off, ignore the dot and the first token of the property.
  1119. offsets.ignoreToken(firstNonObjectToken);
  1120. offsets.ignoreToken(secondNonObjectToken);
  1121. // To ignore the property indentation, ensure that the property tokens depend on the ignored tokens.
  1122. offsets.setDesiredOffset(firstNonObjectToken, offsetBase, 0);
  1123. offsets.setDesiredOffset(secondNonObjectToken, firstNonObjectToken, 0);
  1124. }
  1125. },
  1126. NewExpression(node) {
  1127. // Only indent the arguments if the NewExpression has parens (e.g. `new Foo(bar)` or `new Foo()`, but not `new Foo`
  1128. if (node.arguments.length > 0 ||
  1129. astUtils.isClosingParenToken(sourceCode.getLastToken(node)) &&
  1130. astUtils.isOpeningParenToken(sourceCode.getLastToken(node, 1))) {
  1131. addFunctionCallIndent(node);
  1132. }
  1133. },
  1134. Property(node) {
  1135. if (!node.shorthand && !node.method && node.kind === "init") {
  1136. const colon = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isColonToken);
  1137. offsets.ignoreToken(sourceCode.getTokenAfter(colon));
  1138. }
  1139. },
  1140. SwitchStatement(node) {
  1141. const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken);
  1142. const closingCurly = sourceCode.getLastToken(node);
  1143. offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, options.SwitchCase);
  1144. if (node.cases.length) {
  1145. sourceCode.getTokensBetween(
  1146. node.cases[node.cases.length - 1],
  1147. closingCurly,
  1148. { includeComments: true, filter: astUtils.isCommentToken }
  1149. ).forEach(token => offsets.ignoreToken(token));
  1150. }
  1151. },
  1152. SwitchCase(node) {
  1153. if (!(node.consequent.length === 1 && node.consequent[0].type === "BlockStatement")) {
  1154. const caseKeyword = sourceCode.getFirstToken(node);
  1155. const tokenAfterCurrentCase = sourceCode.getTokenAfter(node);
  1156. offsets.setDesiredOffsets([caseKeyword.range[1], tokenAfterCurrentCase.range[0]], caseKeyword, 1);
  1157. }
  1158. },
  1159. TemplateLiteral(node) {
  1160. node.expressions.forEach((expression, index) => {
  1161. const previousQuasi = node.quasis[index];
  1162. const nextQuasi = node.quasis[index + 1];
  1163. const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line
  1164. ? sourceCode.getFirstToken(previousQuasi)
  1165. : null;
  1166. offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1);
  1167. offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0);
  1168. });
  1169. },
  1170. VariableDeclaration(node) {
  1171. const variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, node.kind)
  1172. ? options.VariableDeclarator[node.kind]
  1173. : DEFAULT_VARIABLE_INDENT;
  1174. if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) {
  1175. /*
  1176. * VariableDeclarator indentation is a bit different from other forms of indentation, in that the
  1177. * indentation of an opening bracket sometimes won't match that of a closing bracket. For example,
  1178. * the following indentations are correct:
  1179. *
  1180. * var foo = {
  1181. * ok: true
  1182. * };
  1183. *
  1184. * var foo = {
  1185. * ok: true,
  1186. * },
  1187. * bar = 1;
  1188. *
  1189. * Account for when exiting the AST (after indentations have already been set for the nodes in
  1190. * the declaration) by manually increasing the indentation level of the tokens in this declarator
  1191. * on the same line as the start of the declaration, provided that there are declarators that
  1192. * follow this one.
  1193. */
  1194. const firstToken = sourceCode.getFirstToken(node);
  1195. offsets.setDesiredOffsets(node.range, firstToken, variableIndent, true);
  1196. } else {
  1197. offsets.setDesiredOffsets(node.range, sourceCode.getFirstToken(node), variableIndent);
  1198. }
  1199. const lastToken = sourceCode.getLastToken(node);
  1200. if (astUtils.isSemicolonToken(lastToken)) {
  1201. offsets.ignoreToken(lastToken);
  1202. }
  1203. },
  1204. VariableDeclarator(node) {
  1205. if (node.init) {
  1206. const equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken);
  1207. const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator);
  1208. offsets.ignoreToken(equalOperator);
  1209. offsets.ignoreToken(tokenAfterOperator);
  1210. offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1);
  1211. offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0);
  1212. }
  1213. },
  1214. "JSXAttribute[value]"(node) {
  1215. const equalsToken = sourceCode.getFirstTokenBetween(node.name, node.value, token => token.type === "Punctuator" && token.value === "=");
  1216. offsets.setDesiredOffsets([equalsToken.range[0], node.value.range[1]], sourceCode.getFirstToken(node.name), 1);
  1217. },
  1218. JSXElement(node) {
  1219. if (node.closingElement) {
  1220. addElementListIndent(node.children, sourceCode.getFirstToken(node.openingElement), sourceCode.getFirstToken(node.closingElement), 1);
  1221. }
  1222. },
  1223. JSXOpeningElement(node) {
  1224. const firstToken = sourceCode.getFirstToken(node);
  1225. let closingToken;
  1226. if (node.selfClosing) {
  1227. closingToken = sourceCode.getLastToken(node, { skip: 1 });
  1228. offsets.setDesiredOffset(sourceCode.getLastToken(node), closingToken, 0);
  1229. } else {
  1230. closingToken = sourceCode.getLastToken(node);
  1231. }
  1232. offsets.setDesiredOffsets(node.name.range, sourceCode.getFirstToken(node));
  1233. addElementListIndent(node.attributes, firstToken, closingToken, 1);
  1234. },
  1235. JSXClosingElement(node) {
  1236. const firstToken = sourceCode.getFirstToken(node);
  1237. offsets.setDesiredOffsets(node.name.range, firstToken, 1);
  1238. },
  1239. JSXExpressionContainer(node) {
  1240. const openingCurly = sourceCode.getFirstToken(node);
  1241. const closingCurly = sourceCode.getLastToken(node);
  1242. offsets.setDesiredOffsets(
  1243. [openingCurly.range[1], closingCurly.range[0]],
  1244. openingCurly,
  1245. 1
  1246. );
  1247. },
  1248. "*"(node) {
  1249. const firstToken = sourceCode.getFirstToken(node);
  1250. // Ensure that the children of every node are indented at least as much as the first token.
  1251. if (firstToken && !ignoredNodeFirstTokens.has(firstToken)) {
  1252. offsets.setDesiredOffsets(node.range, firstToken, 0);
  1253. }
  1254. }
  1255. };
  1256. const listenerCallQueue = [];
  1257. /*
  1258. * To ignore the indentation of a node:
  1259. * 1. Don't call the node's listener when entering it (if it has a listener)
  1260. * 2. Don't set any offsets against the first token of the node.
  1261. * 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets.
  1262. */
  1263. const offsetListeners = lodash.mapValues(
  1264. baseOffsetListeners,
  1265. /*
  1266. * Offset listener calls are deferred until traversal is finished, and are called as
  1267. * part of the final `Program:exit` listener. This is necessary because a node might
  1268. * be matched by multiple selectors.
  1269. *
  1270. * Example: Suppose there is an offset listener for `Identifier`, and the user has
  1271. * specified in configuration that `MemberExpression > Identifier` should be ignored.
  1272. * Due to selector specificity rules, the `Identifier` listener will get called first. However,
  1273. * if a given Identifier node is supposed to be ignored, then the `Identifier` offset listener
  1274. * should not have been called at all. Without doing extra selector matching, we don't know
  1275. * whether the Identifier matches the `MemberExpression > Identifier` selector until the
  1276. * `MemberExpression > Identifier` listener is called.
  1277. *
  1278. * To avoid this, the `Identifier` listener isn't called until traversal finishes and all
  1279. * ignored nodes are known.
  1280. */
  1281. listener =>
  1282. node =>
  1283. listenerCallQueue.push({ listener, node })
  1284. );
  1285. // For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set.
  1286. const ignoredNodes = new Set();
  1287. /**
  1288. * Ignores a node
  1289. * @param {ASTNode} node The node to ignore
  1290. * @returns {void}
  1291. */
  1292. function addToIgnoredNodes(node) {
  1293. ignoredNodes.add(node);
  1294. ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node));
  1295. }
  1296. const ignoredNodeListeners = options.ignoredNodes.reduce(
  1297. (listeners, ignoredSelector) => Object.assign(listeners, { [ignoredSelector]: addToIgnoredNodes }),
  1298. {}
  1299. );
  1300. /*
  1301. * Join the listeners, and add a listener to verify that all tokens actually have the correct indentation
  1302. * at the end.
  1303. *
  1304. * Using Object.assign will cause some offset listeners to be overwritten if the same selector also appears
  1305. * in `ignoredNodeListeners`. This isn't a problem because all of the matching nodes will be ignored,
  1306. * so those listeners wouldn't be called anyway.
  1307. */
  1308. return Object.assign(
  1309. offsetListeners,
  1310. ignoredNodeListeners,
  1311. {
  1312. "*:exit"(node) {
  1313. // If a node's type is nonstandard, we can't tell how its children should be offset, so ignore it.
  1314. if (!KNOWN_NODES.has(node.type)) {
  1315. addToIgnoredNodes(node);
  1316. }
  1317. },
  1318. "Program:exit"() {
  1319. // If ignoreComments option is enabled, ignore all comment tokens.
  1320. if (options.ignoreComments) {
  1321. sourceCode.getAllComments()
  1322. .forEach(comment => offsets.ignoreToken(comment));
  1323. }
  1324. // Invoke the queued offset listeners for the nodes that aren't ignored.
  1325. listenerCallQueue
  1326. .filter(nodeInfo => !ignoredNodes.has(nodeInfo.node))
  1327. .forEach(nodeInfo => nodeInfo.listener(nodeInfo.node));
  1328. // Update the offsets for ignored nodes to prevent their child tokens from being reported.
  1329. ignoredNodes.forEach(ignoreNode);
  1330. addParensIndent(sourceCode.ast.tokens);
  1331. /*
  1332. * Create a Map from (tokenOrComment) => (precedingToken).
  1333. * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly.
  1334. */
  1335. const precedingTokens = sourceCode.ast.comments.reduce((commentMap, comment) => {
  1336. const tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { includeComments: true });
  1337. return commentMap.set(comment, commentMap.has(tokenOrCommentBefore) ? commentMap.get(tokenOrCommentBefore) : tokenOrCommentBefore);
  1338. }, new WeakMap());
  1339. sourceCode.lines.forEach((line, lineIndex) => {
  1340. const lineNumber = lineIndex + 1;
  1341. if (!tokenInfo.firstTokensByLineNumber.has(lineNumber)) {
  1342. // Don't check indentation on blank lines
  1343. return;
  1344. }
  1345. const firstTokenOfLine = tokenInfo.firstTokensByLineNumber.get(lineNumber);
  1346. if (firstTokenOfLine.loc.start.line !== lineNumber) {
  1347. // Don't check the indentation of multi-line tokens (e.g. template literals or block comments) twice.
  1348. return;
  1349. }
  1350. // If the token matches the expected expected indentation, don't report it.
  1351. if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) {
  1352. return;
  1353. }
  1354. if (astUtils.isCommentToken(firstTokenOfLine)) {
  1355. const tokenBefore = precedingTokens.get(firstTokenOfLine);
  1356. const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0];
  1357. const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine);
  1358. const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter);
  1359. // If a comment matches the expected indentation of the token immediately before or after, don't report it.
  1360. if (
  1361. mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
  1362. mayAlignWithAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))
  1363. ) {
  1364. return;
  1365. }
  1366. }
  1367. // Otherwise, report the token/comment.
  1368. report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine));
  1369. });
  1370. }
  1371. }
  1372. );
  1373. }
  1374. };