2 * @fileoverview This rule sets a specific indentation style and width for your code
5 * @author Vitaly Puzrin
6 * @author Gyandeep Singh
11 //------------------------------------------------------------------------------
13 //------------------------------------------------------------------------------
15 const lodash = require("lodash");
16 const astUtils = require("./utils/ast-utils");
17 const createTree = require("functional-red-black-tree");
19 //------------------------------------------------------------------------------
21 //------------------------------------------------------------------------------
23 const KNOWN_NODES = new Set([
24 "AssignmentExpression",
28 "ArrowFunctionExpression",
38 "ConditionalExpression",
43 "ExperimentalRestProperty",
44 "ExperimentalSpreadProperty",
45 "ExpressionStatement",
49 "FunctionDeclaration",
71 "TaggedTemplateExpression",
79 "VariableDeclaration",
89 "JSXMemberExpression",
91 "JSXExpressionContainer",
98 "ExportDefaultDeclaration",
99 "ExportNamedDeclaration",
100 "ExportAllDeclaration",
104 "ImportDefaultSpecifier",
105 "ImportNamespaceSpecifier",
110 * General rule strategy:
111 * 1. An OffsetStorage instance stores a map of desired offsets, where each token has a specified offset from another
112 * specified token or to the first column.
113 * 2. As the AST is traversed, modify the desired offsets of tokens accordingly. For example, when entering a
114 * BlockStatement, offset all of the tokens in the BlockStatement by 1 indent level from the opening curly
115 * brace of the BlockStatement.
116 * 3. After traversing the AST, calculate the expected indentation levels of every token according to the
117 * OffsetStorage container.
118 * 4. For each line, compare the expected indentation of the first token to the actual indentation in the file,
119 * and report the token if the two values are not equal.
124 * A mutable balanced binary search tree that stores (key, value) pairs. The keys are numeric, and must be unique.
125 * This is intended to be a generic wrapper around a balanced binary search tree library, so that the underlying implementation
126 * can easily be swapped out.
128 class BinarySearchTree {
131 * Creates an empty tree
134 this._rbTree = createTree();
138 * Inserts an entry into the tree.
139 * @param {number} key The entry's key
140 * @param {*} value The entry's value
144 const iterator = this._rbTree.find(key);
146 if (iterator.valid) {
147 this._rbTree = iterator.update(value);
149 this._rbTree = this._rbTree.insert(key, value);
154 * Finds the entry with the largest key less than or equal to the provided key
155 * @param {number} key The provided key
156 * @returns {{key: number, value: *}|null} The found entry, or null if no such entry exists.
159 const iterator = this._rbTree.le(key);
161 return iterator && { key: iterator.key, value: iterator.value };
165 * Deletes all of the keys in the interval [start, end)
166 * @param {number} start The start of the range
167 * @param {number} end The end of the range
170 deleteRange(start, end) {
172 // Exit without traversing the tree if the range has zero size.
176 const iterator = this._rbTree.ge(start);
178 while (iterator.valid && iterator.key < end) {
179 this._rbTree = this._rbTree.remove(iterator.key);
186 * A helper class to get token-based info related to indentation
190 // eslint-disable-next-line jsdoc/require-description
192 * @param {SourceCode} sourceCode A SourceCode object
194 constructor(sourceCode) {
195 this.sourceCode = sourceCode;
196 this.firstTokensByLineNumber = sourceCode.tokensAndComments.reduce((map, token) => {
197 if (!map.has(token.loc.start.line)) {
198 map.set(token.loc.start.line, token);
200 if (!map.has(token.loc.end.line) && sourceCode.text.slice(token.range[1] - token.loc.end.column, token.range[1]).trim()) {
201 map.set(token.loc.end.line, token);
208 * Gets the first token on a given token's line
209 * @param {Token|ASTNode} token a node or token
210 * @returns {Token} The first token on the given line
212 getFirstTokenOfLine(token) {
213 return this.firstTokensByLineNumber.get(token.loc.start.line);
217 * Determines whether a token is the first token in its line
218 * @param {Token} token The token
219 * @returns {boolean} `true` if the token is the first on its line
221 isFirstTokenOfLine(token) {
222 return this.getFirstTokenOfLine(token) === token;
226 * Get the actual indent of a token
227 * @param {Token} token Token to examine. This should be the first token on its line.
228 * @returns {string} The indentation characters that precede the token
230 getTokenIndent(token) {
231 return this.sourceCode.text.slice(token.range[0] - token.loc.start.column, token.range[0]);
236 * A class to store information on desired offsets of tokens from each other
238 class OffsetStorage {
240 // eslint-disable-next-line jsdoc/require-description
242 * @param {TokenInfo} tokenInfo a TokenInfo instance
243 * @param {number} indentSize The desired size of each indentation level
244 * @param {string} indentType The indentation character
246 constructor(tokenInfo, indentSize, indentType) {
247 this._tokenInfo = tokenInfo;
248 this._indentSize = indentSize;
249 this._indentType = indentType;
251 this._tree = new BinarySearchTree();
252 this._tree.insert(0, { offset: 0, from: null, force: false });
254 this._lockedFirstTokens = new WeakMap();
255 this._desiredIndentCache = new WeakMap();
256 this._ignoredTokens = new WeakSet();
259 _getOffsetDescriptor(token) {
260 return this._tree.findLe(token.range[0]).value;
264 * Sets the offset column of token B to match the offset column of token A.
265 * **WARNING**: This matches a *column*, even if baseToken is not the first token on its line. In
266 * most cases, `setDesiredOffset` should be used instead.
267 * @param {Token} baseToken The first token
268 * @param {Token} offsetToken The second token, whose offset should be matched to the first token
271 matchOffsetOf(baseToken, offsetToken) {
274 * lockedFirstTokens is a map from a token whose indentation is controlled by the "first" option to
275 * the token that it depends on. For example, with the `ArrayExpression: first` option, the first
276 * token of each element in the array after the first will be mapped to the first token of the first
277 * element. The desired indentation of each of these tokens is computed based on the desired indentation
278 * of the "first" element, rather than through the normal offset mechanism.
280 this._lockedFirstTokens.set(offsetToken, baseToken);
284 * Sets the desired offset of a token.
286 * This uses a line-based offset collapsing behavior to handle tokens on the same line.
287 * For example, consider the following two cases:
299 * Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from
300 * the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is
301 * the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces)
302 * from the start of its line.
304 * However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level
305 * between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the
306 * `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented
307 * by 1 indent level from the start of the line.
309 * This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node,
310 * without needing to check which lines those tokens are on.
312 * Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive
313 * behavior can occur. For example, consider the following cases:
326 * Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz`
327 * should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz`
328 * being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no
329 * collapsing would occur).
331 * Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and
332 * offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed
333 * in the second case.
334 * @param {Token} token The token
335 * @param {Token} fromToken The token that `token` should be offset from
336 * @param {number} offset The desired indent level
339 setDesiredOffset(token, fromToken, offset) {
340 return this.setDesiredOffsets(token.range, fromToken, offset);
344 * Sets the desired offset of all tokens in a range
345 * It's common for node listeners in this file to need to apply the same offset to a large, contiguous range of tokens.
346 * Moreover, the offset of any given token is usually updated multiple times (roughly once for each node that contains
347 * it). This means that the offset of each token is updated O(AST depth) times.
348 * It would not be performant to store and update the offsets for each token independently, because the rule would end
349 * up having a time complexity of O(number of tokens * AST depth), which is quite slow for large files.
351 * Instead, the offset tree is represented as a collection of contiguous offset ranges in a file. For example, the following
352 * list could represent the state of the offset tree at a given point:
354 * * Tokens starting in the interval [0, 15) are aligned with the beginning of the file
355 * * Tokens starting in the interval [15, 30) are offset by 1 indent level from the `bar` token
356 * * Tokens starting in the interval [30, 43) are offset by 1 indent level from the `foo` token
357 * * Tokens starting in the interval [43, 820) are offset by 2 indent levels from the `bar` token
358 * * Tokens starting in the interval [820, ∞) are offset by 1 indent level from the `baz` token
360 * The `setDesiredOffsets` methods inserts ranges like the ones above. The third line above would be inserted by using:
361 * `setDesiredOffsets([30, 43], fooToken, 1);`
362 * @param {[number, number]} range A [start, end] pair. All tokens with range[0] <= token.start < range[1] will have the offset applied.
363 * @param {Token} fromToken The token that this is offset from
364 * @param {number} offset The desired indent level
365 * @param {boolean} force `true` if this offset should not use the normal collapsing behavior. This should almost always be false.
368 setDesiredOffsets(range, fromToken, offset, force) {
371 * Offset ranges are stored as a collection of nodes, where each node maps a numeric key to an offset
372 * descriptor. The tree for the example above would have the following nodes:
374 * * key: 0, value: { offset: 0, from: null }
375 * * key: 15, value: { offset: 1, from: barToken }
376 * * key: 30, value: { offset: 1, from: fooToken }
377 * * key: 43, value: { offset: 2, from: barToken }
378 * * key: 820, value: { offset: 1, from: bazToken }
380 * To find the offset descriptor for any given token, one needs to find the node with the largest key
381 * which is <= token.start. To make this operation fast, the nodes are stored in a balanced binary
382 * search tree indexed by key.
385 const descriptorToInsert = { offset, from: fromToken, force };
387 const descriptorAfterRange = this._tree.findLe(range[1]).value;
389 const fromTokenIsInRange = fromToken && fromToken.range[0] >= range[0] && fromToken.range[1] <= range[1];
390 const fromTokenDescriptor = fromTokenIsInRange && this._getOffsetDescriptor(fromToken);
392 // First, remove any existing nodes in the range from the tree.
393 this._tree.deleteRange(range[0] + 1, range[1]);
395 // Insert a new node into the tree for this range
396 this._tree.insert(range[0], descriptorToInsert);
399 * To avoid circular offset dependencies, keep the `fromToken` token mapped to whatever it was mapped to previously,
400 * even if it's in the current range.
402 if (fromTokenIsInRange) {
403 this._tree.insert(fromToken.range[0], fromTokenDescriptor);
404 this._tree.insert(fromToken.range[1], descriptorToInsert);
408 * To avoid modifying the offset of tokens after the range, insert another node to keep the offset of the following
409 * tokens the same as it was before.
411 this._tree.insert(range[1], descriptorAfterRange);
415 * Gets the desired indent of a token
416 * @param {Token} token The token
417 * @returns {string} The desired indent of the token
419 getDesiredIndent(token) {
420 if (!this._desiredIndentCache.has(token)) {
422 if (this._ignoredTokens.has(token)) {
425 * If the token is ignored, use the actual indent of the token as the desired indent.
426 * This ensures that no errors are reported for this token.
428 this._desiredIndentCache.set(
430 this._tokenInfo.getTokenIndent(token)
432 } else if (this._lockedFirstTokens.has(token)) {
433 const firstToken = this._lockedFirstTokens.get(token);
435 this._desiredIndentCache.set(
438 // (indentation for the first element's line)
439 this.getDesiredIndent(this._tokenInfo.getFirstTokenOfLine(firstToken)) +
441 // (space between the start of the first element's line and the first element)
442 this._indentType.repeat(firstToken.loc.start.column - this._tokenInfo.getFirstTokenOfLine(firstToken).loc.start.column)
445 const offsetInfo = this._getOffsetDescriptor(token);
448 offsetInfo.from.loc.start.line === token.loc.start.line &&
449 !/^\s*?\n/u.test(token.value) &&
451 ) ? 0 : offsetInfo.offset * this._indentSize;
453 this._desiredIndentCache.set(
455 (offsetInfo.from ? this.getDesiredIndent(offsetInfo.from) : "") + this._indentType.repeat(offset)
459 return this._desiredIndentCache.get(token);
463 * Ignores a token, preventing it from being reported.
464 * @param {Token} token The token
468 if (this._tokenInfo.isFirstTokenOfLine(token)) {
469 this._ignoredTokens.add(token);
474 * Gets the first token that the given token's indentation is dependent on
475 * @param {Token} token The token
476 * @returns {Token} The token that the given token depends on, or `null` if the given token is at the top level
478 getFirstDependency(token) {
479 return this._getOffsetDescriptor(token).from;
483 const ELEMENT_LIST_SCHEMA = {
490 enum: ["first", "off"]
500 description: "enforce consistent indentation",
501 category: "Stylistic Issues",
503 url: "https://eslint.org/docs/rules/indent"
506 fixable: "whitespace",
528 VariableDeclarator: {
534 var: ELEMENT_LIST_SCHEMA,
535 let: ELEMENT_LIST_SCHEMA,
536 const: ELEMENT_LIST_SCHEMA
538 additionalProperties: false
557 FunctionDeclaration: {
560 parameters: ELEMENT_LIST_SCHEMA,
566 additionalProperties: false
568 FunctionExpression: {
571 parameters: ELEMENT_LIST_SCHEMA,
577 additionalProperties: false
582 arguments: ELEMENT_LIST_SCHEMA
584 additionalProperties: false
586 ArrayExpression: ELEMENT_LIST_SCHEMA,
587 ObjectExpression: ELEMENT_LIST_SCHEMA,
588 ImportDeclaration: ELEMENT_LIST_SCHEMA,
589 flatTernaryExpressions: {
607 additionalProperties: false
611 wrongIndentation: "Expected indentation of {{expected}} but found {{actual}}."
616 const DEFAULT_VARIABLE_INDENT = 1;
617 const DEFAULT_PARAMETER_INDENT = 1;
618 const DEFAULT_FUNCTION_BODY_INDENT = 1;
620 let indentType = "space";
624 VariableDeclarator: {
625 var: DEFAULT_VARIABLE_INDENT,
626 let: DEFAULT_VARIABLE_INDENT,
627 const: DEFAULT_VARIABLE_INDENT
630 FunctionDeclaration: {
631 parameters: DEFAULT_PARAMETER_INDENT,
632 body: DEFAULT_FUNCTION_BODY_INDENT
634 FunctionExpression: {
635 parameters: DEFAULT_PARAMETER_INDENT,
636 body: DEFAULT_FUNCTION_BODY_INDENT
639 arguments: DEFAULT_PARAMETER_INDENT
644 ImportDeclaration: 1,
645 flatTernaryExpressions: false,
647 ignoreComments: false
650 if (context.options.length) {
651 if (context.options[0] === "tab") {
655 indentSize = context.options[0];
656 indentType = "space";
659 if (context.options[1]) {
660 Object.assign(options, context.options[1]);
662 if (typeof options.VariableDeclarator === "number" || options.VariableDeclarator === "first") {
663 options.VariableDeclarator = {
664 var: options.VariableDeclarator,
665 let: options.VariableDeclarator,
666 const: options.VariableDeclarator
672 const sourceCode = context.getSourceCode();
673 const tokenInfo = new TokenInfo(sourceCode);
674 const offsets = new OffsetStorage(tokenInfo, indentSize, indentType === "space" ? " " : "\t");
675 const parameterParens = new WeakSet();
678 * Creates an error message for a line, given the expected/actual indentation.
679 * @param {int} expectedAmount The expected amount of indentation characters for this line
680 * @param {int} actualSpaces The actual number of indentation spaces that were found on this line
681 * @param {int} actualTabs The actual number of indentation tabs that were found on this line
682 * @returns {string} An error message for this line
684 function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) {
685 const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs"
686 const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space"
687 const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs"
690 if (actualSpaces > 0) {
693 * Abbreviate the message if the expected indentation is also spaces.
694 * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
696 foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`;
697 } else if (actualTabs > 0) {
698 foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`;
700 foundStatement = "0";
703 expected: expectedStatement,
704 actual: foundStatement
709 * Reports a given indent violation
710 * @param {Token} token Token violating the indent rule
711 * @param {string} neededIndent Expected indentation string
714 function report(token, neededIndent) {
715 const actualIndent = Array.from(tokenInfo.getTokenIndent(token));
716 const numSpaces = actualIndent.filter(char => char === " ").length;
717 const numTabs = actualIndent.filter(char => char === "\t").length;
721 messageId: "wrongIndentation",
722 data: createErrorMessageData(neededIndent.length, numSpaces, numTabs),
724 start: { line: token.loc.start.line, column: 0 },
725 end: { line: token.loc.start.line, column: token.loc.start.column }
728 const range = [token.range[0] - token.loc.start.column, token.range[0]];
729 const newText = neededIndent;
731 return fixer.replaceTextRange(range, newText);
737 * Checks if a token's indentation is correct
738 * @param {Token} token Token to examine
739 * @param {string} desiredIndent Desired indentation of the string
740 * @returns {boolean} `true` if the token's indentation is correct
742 function validateTokenIndent(token, desiredIndent) {
743 const indentation = tokenInfo.getTokenIndent(token);
745 return indentation === desiredIndent ||
747 // To avoid conflicts with no-mixed-spaces-and-tabs, don't report mixed spaces and tabs.
748 indentation.includes(" ") && indentation.includes("\t");
752 * Check to see if the node is a file level IIFE
753 * @param {ASTNode} node The function node to check.
754 * @returns {boolean} True if the node is the outer IIFE
756 function isOuterIIFE(node) {
759 * Verify that the node is an IIFE
761 if (!node.parent || node.parent.type !== "CallExpression" || node.parent.callee !== node) {
766 * Navigate legal ancestors to determine whether this IIFE is outer.
767 * A "legal ancestor" is an expression or statement that causes the function to get executed immediately.
768 * For example, `!(function(){})()` is an outer IIFE even though it is preceded by a ! operator.
770 let statement = node.parent && node.parent.parent;
773 statement.type === "UnaryExpression" && ["!", "~", "+", "-"].indexOf(statement.operator) > -1 ||
774 statement.type === "AssignmentExpression" ||
775 statement.type === "LogicalExpression" ||
776 statement.type === "SequenceExpression" ||
777 statement.type === "VariableDeclarator"
779 statement = statement.parent;
782 return (statement.type === "ExpressionStatement" || statement.type === "VariableDeclaration") && statement.parent.type === "Program";
786 * Counts the number of linebreaks that follow the last non-whitespace character in a string
787 * @param {string} string The string to check
788 * @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character,
789 * or the total number of linebreaks if the string is all whitespace.
791 function countTrailingLinebreaks(string) {
792 const trailingWhitespace = string.match(/\s*$/u)[0];
793 const linebreakMatches = trailingWhitespace.match(astUtils.createGlobalLinebreakMatcher());
795 return linebreakMatches === null ? 0 : linebreakMatches.length;
799 * Check indentation for lists of elements (arrays, objects, function params)
800 * @param {ASTNode[]} elements List of elements that should be offset
801 * @param {Token} startToken The start token of the list that element should be aligned against, e.g. '['
802 * @param {Token} endToken The end token of the list, e.g. ']'
803 * @param {number|string} offset The amount that the elements should be offset
806 function addElementListIndent(elements, startToken, endToken, offset) {
809 * Gets the first token of a given element, including surrounding parentheses.
810 * @param {ASTNode} element A node in the `elements` list
811 * @returns {Token} The first token of this element
813 function getFirstToken(element) {
814 let token = sourceCode.getTokenBefore(element);
816 while (astUtils.isOpeningParenToken(token) && token !== startToken) {
817 token = sourceCode.getTokenBefore(token);
819 return sourceCode.getTokenAfter(token);
822 // Run through all the tokens in the list, and offset them by one indent level (mainly for comments, other things will end up overridden)
823 offsets.setDesiredOffsets(
824 [startToken.range[1], endToken.range[0]],
826 typeof offset === "number" ? offset : 1
828 offsets.setDesiredOffset(endToken, startToken, 0);
830 // If the preference is "first" but there is no first element (e.g. sparse arrays w/ empty first slot), fall back to 1 level.
831 if (offset === "first" && elements.length && !elements[0]) {
834 elements.forEach((element, index) => {
837 // Skip holes in arrays
840 if (offset === "off") {
842 // Ignore the first token of every element if the "off" option is used
843 offsets.ignoreToken(getFirstToken(element));
846 // Offset the following elements correctly relative to the first element
850 if (offset === "first" && tokenInfo.isFirstTokenOfLine(getFirstToken(element))) {
851 offsets.matchOffsetOf(getFirstToken(elements[0]), getFirstToken(element));
853 const previousElement = elements[index - 1];
854 const firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement);
855 const previousElementLastToken = previousElement && sourceCode.getLastToken(previousElement);
859 previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line
861 offsets.setDesiredOffsets(
862 [previousElement.range[1], element.range[1]],
863 firstTokenOfPreviousElement,
872 * Check and decide whether to check for indentation for blockless nodes
873 * Scenarios are for or while statements without braces around them
874 * @param {ASTNode} node node to examine
877 function addBlocklessNodeIndent(node) {
878 if (node.type !== "BlockStatement") {
879 const lastParentToken = sourceCode.getTokenBefore(node, astUtils.isNotOpeningParenToken);
881 let firstBodyToken = sourceCode.getFirstToken(node);
882 let lastBodyToken = sourceCode.getLastToken(node);
885 astUtils.isOpeningParenToken(sourceCode.getTokenBefore(firstBodyToken)) &&
886 astUtils.isClosingParenToken(sourceCode.getTokenAfter(lastBodyToken))
888 firstBodyToken = sourceCode.getTokenBefore(firstBodyToken);
889 lastBodyToken = sourceCode.getTokenAfter(lastBodyToken);
892 offsets.setDesiredOffsets([firstBodyToken.range[0], lastBodyToken.range[1]], lastParentToken, 1);
895 * For blockless nodes with semicolon-first style, don't indent the semicolon.
898 * ; [1, 2, 3].map(foo)
900 const lastToken = sourceCode.getLastToken(node);
902 if (node.type !== "EmptyStatement" && astUtils.isSemicolonToken(lastToken)) {
903 offsets.setDesiredOffset(lastToken, lastParentToken, 0);
909 * Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`)
910 * @param {ASTNode} node A CallExpression or NewExpression node
913 function addFunctionCallIndent(node) {
916 if (node.arguments.length) {
917 openingParen = sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken);
919 openingParen = sourceCode.getLastToken(node, 1);
921 const closingParen = sourceCode.getLastToken(node);
923 parameterParens.add(openingParen);
924 parameterParens.add(closingParen);
925 offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);
927 addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments);
931 * Checks the indentation of parenthesized values, given a list of tokens in a program
932 * @param {Token[]} tokens A list of tokens
935 function addParensIndent(tokens) {
936 const parenStack = [];
937 const parenPairs = [];
939 tokens.forEach(nextToken => {
941 // Accumulate a list of parenthesis pairs
942 if (astUtils.isOpeningParenToken(nextToken)) {
943 parenStack.push(nextToken);
944 } else if (astUtils.isClosingParenToken(nextToken)) {
945 parenPairs.unshift({ left: parenStack.pop(), right: nextToken });
949 parenPairs.forEach(pair => {
950 const leftParen = pair.left;
951 const rightParen = pair.right;
953 // We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments.
954 if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) {
955 const parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen));
957 parenthesizedTokens.forEach(token => {
958 if (!parenthesizedTokens.has(offsets.getFirstDependency(token))) {
959 offsets.setDesiredOffset(token, leftParen, 1);
964 offsets.setDesiredOffset(rightParen, leftParen, 0);
969 * Ignore all tokens within an unknown node whose offset do not depend
970 * on another token's offset within the unknown node
971 * @param {ASTNode} node Unknown Node
974 function ignoreNode(node) {
975 const unknownNodeTokens = new Set(sourceCode.getTokens(node, { includeComments: true }));
977 unknownNodeTokens.forEach(token => {
978 if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) {
979 const firstTokenOfLine = tokenInfo.getFirstTokenOfLine(token);
981 if (token === firstTokenOfLine) {
982 offsets.ignoreToken(token);
984 offsets.setDesiredOffset(token, firstTokenOfLine, 0);
991 * Check whether the given token is on the first line of a statement.
992 * @param {Token} token The token to check.
993 * @param {ASTNode} leafNode The expression node that the token belongs directly.
994 * @returns {boolean} `true` if the token is on the first line of a statement.
996 function isOnFirstLineOfStatement(token, leafNode) {
999 while (node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration")) {
1004 return !node || node.loc.start.line === token.loc.start.line;
1008 * Check whether there are any blank (whitespace-only) lines between
1009 * two tokens on separate lines.
1010 * @param {Token} firstToken The first token.
1011 * @param {Token} secondToken The second token.
1012 * @returns {boolean} `true` if the tokens are on separate lines and
1013 * there exists a blank line between them, `false` otherwise.
1015 function hasBlankLinesBetween(firstToken, secondToken) {
1016 const firstTokenLine = firstToken.loc.end.line;
1017 const secondTokenLine = secondToken.loc.start.line;
1019 if (firstTokenLine === secondTokenLine || firstTokenLine === secondTokenLine - 1) {
1023 for (let line = firstTokenLine + 1; line < secondTokenLine; ++line) {
1024 if (!tokenInfo.firstTokensByLineNumber.has(line)) {
1032 const ignoredNodeFirstTokens = new Set();
1034 const baseOffsetListeners = {
1035 "ArrayExpression, ArrayPattern"(node) {
1036 const openingBracket = sourceCode.getFirstToken(node);
1037 const closingBracket = sourceCode.getTokenAfter(lodash.findLast(node.elements) || openingBracket, astUtils.isClosingBracketToken);
1039 addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression);
1042 "ObjectExpression, ObjectPattern"(node) {
1043 const openingCurly = sourceCode.getFirstToken(node);
1044 const closingCurly = sourceCode.getTokenAfter(
1045 node.properties.length ? node.properties[node.properties.length - 1] : openingCurly,
1046 astUtils.isClosingBraceToken
1049 addElementListIndent(node.properties, openingCurly, closingCurly, options.ObjectExpression);
1052 ArrowFunctionExpression(node) {
1053 const firstToken = sourceCode.getFirstToken(node);
1055 if (astUtils.isOpeningParenToken(firstToken)) {
1056 const openingParen = firstToken;
1057 const closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken);
1059 parameterParens.add(openingParen);
1060 parameterParens.add(closingParen);
1061 addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
1063 addBlocklessNodeIndent(node.body);
1066 AssignmentExpression(node) {
1067 const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
1069 offsets.setDesiredOffsets([operator.range[0], node.range[1]], sourceCode.getLastToken(node.left), 1);
1070 offsets.ignoreToken(operator);
1071 offsets.ignoreToken(sourceCode.getTokenAfter(operator));
1074 "BinaryExpression, LogicalExpression"(node) {
1075 const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
1078 * For backwards compatibility, don't check BinaryExpression indents, e.g.
1083 const tokenAfterOperator = sourceCode.getTokenAfter(operator);
1085 offsets.ignoreToken(operator);
1086 offsets.ignoreToken(tokenAfterOperator);
1087 offsets.setDesiredOffset(tokenAfterOperator, operator, 0);
1090 "BlockStatement, ClassBody"(node) {
1092 let blockIndentLevel;
1094 if (node.parent && isOuterIIFE(node.parent)) {
1095 blockIndentLevel = options.outerIIFEBody;
1096 } else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) {
1097 blockIndentLevel = options.FunctionExpression.body;
1098 } else if (node.parent && node.parent.type === "FunctionDeclaration") {
1099 blockIndentLevel = options.FunctionDeclaration.body;
1101 blockIndentLevel = 1;
1105 * For blocks that aren't lone statements, ensure that the opening curly brace
1106 * is aligned with the parent.
1108 if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) {
1109 offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0);
1111 addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel);
1114 CallExpression: addFunctionCallIndent,
1116 "ClassDeclaration[superClass], ClassExpression[superClass]"(node) {
1117 const classToken = sourceCode.getFirstToken(node);
1118 const extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken);
1120 offsets.setDesiredOffsets([extendsToken.range[0], node.body.range[0]], classToken, 1);
1123 ConditionalExpression(node) {
1124 const firstToken = sourceCode.getFirstToken(node);
1126 // `flatTernaryExpressions` option is for the following style:
1131 if (!options.flatTernaryExpressions ||
1132 !astUtils.isTokenOnSameLine(node.test, node.consequent) ||
1133 isOnFirstLineOfStatement(firstToken, node)
1135 const questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.type === "Punctuator" && token.value === "?");
1136 const colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, token => token.type === "Punctuator" && token.value === ":");
1138 const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken);
1139 const lastConsequentToken = sourceCode.getTokenBefore(colonToken);
1140 const firstAlternateToken = sourceCode.getTokenAfter(colonToken);
1142 offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
1143 offsets.setDesiredOffset(colonToken, firstToken, 1);
1145 offsets.setDesiredOffset(firstConsequentToken, firstToken, 1);
1148 * The alternate and the consequent should usually have the same indentation.
1149 * If they share part of a line, align the alternate against the first token of the consequent.
1150 * This allows the alternate to be indented correctly in cases like this:
1153 * ) : ( // this '(' is aligned with the '(' above, so it's considered to be aligned with `foo`
1154 * baz // as a result, `baz` is offset by 1 rather than 2
1157 if (lastConsequentToken.loc.end.line === firstAlternateToken.loc.start.line) {
1158 offsets.setDesiredOffset(firstAlternateToken, firstConsequentToken, 0);
1162 * If the alternate and consequent do not share part of a line, offset the alternate from the first
1163 * token of the conditional expression. For example:
1167 * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up
1168 * having no expected indentation.
1170 offsets.setDesiredOffset(firstAlternateToken, firstToken, 1);
1175 "DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement": node => addBlocklessNodeIndent(node.body),
1177 ExportNamedDeclaration(node) {
1178 if (node.declaration === null) {
1179 const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
1181 // Indent the specifiers in `export {foo, bar, baz}`
1182 addElementListIndent(node.specifiers, sourceCode.getFirstToken(node, { skip: 1 }), closingCurly, 1);
1186 // Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'`
1187 offsets.setDesiredOffsets([closingCurly.range[1], node.range[1]], sourceCode.getFirstToken(node), 1);
1192 ForStatement(node) {
1193 const forOpeningParen = sourceCode.getFirstToken(node, 1);
1196 offsets.setDesiredOffsets(node.init.range, forOpeningParen, 1);
1199 offsets.setDesiredOffsets(node.test.range, forOpeningParen, 1);
1202 offsets.setDesiredOffsets(node.update.range, forOpeningParen, 1);
1204 addBlocklessNodeIndent(node.body);
1207 "FunctionDeclaration, FunctionExpression"(node) {
1208 const closingParen = sourceCode.getTokenBefore(node.body);
1209 const openingParen = sourceCode.getTokenBefore(node.params.length ? node.params[0] : closingParen);
1211 parameterParens.add(openingParen);
1212 parameterParens.add(closingParen);
1213 addElementListIndent(node.params, openingParen, closingParen, options[node.type].parameters);
1217 addBlocklessNodeIndent(node.consequent);
1218 if (node.alternate && node.alternate.type !== "IfStatement") {
1219 addBlocklessNodeIndent(node.alternate);
1223 ImportDeclaration(node) {
1224 if (node.specifiers.some(specifier => specifier.type === "ImportSpecifier")) {
1225 const openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken);
1226 const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
1228 addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration);
1231 const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from");
1232 const sourceToken = sourceCode.getLastToken(node, token => token.type === "String");
1233 const semiToken = sourceCode.getLastToken(node, token => token.type === "Punctuator" && token.value === ";");
1236 const end = semiToken && semiToken.range[1] === sourceToken.range[1] ? node.range[1] : sourceToken.range[1];
1238 offsets.setDesiredOffsets([fromToken.range[0], end], sourceCode.getFirstToken(node), 1);
1242 ImportExpression(node) {
1243 const openingParen = sourceCode.getFirstToken(node, 1);
1244 const closingParen = sourceCode.getLastToken(node);
1246 parameterParens.add(openingParen);
1247 parameterParens.add(closingParen);
1248 offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);
1250 addElementListIndent([node.source], openingParen, closingParen, options.CallExpression.arguments);
1253 "MemberExpression, JSXMemberExpression, MetaProperty"(node) {
1254 const object = node.type === "MetaProperty" ? node.meta : node.object;
1255 const firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken);
1256 const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken);
1258 const objectParenCount = sourceCode.getTokensBetween(object, node.property, { filter: astUtils.isClosingParenToken }).length;
1259 const firstObjectToken = objectParenCount
1260 ? sourceCode.getTokenBefore(object, { skip: objectParenCount - 1 })
1261 : sourceCode.getFirstToken(object);
1262 const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken);
1263 const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;
1265 if (node.computed) {
1267 // For computed MemberExpressions, match the closing bracket with the opening bracket.
1268 offsets.setDesiredOffset(sourceCode.getLastToken(node), firstNonObjectToken, 0);
1269 offsets.setDesiredOffsets(node.property.range, firstNonObjectToken, 1);
1273 * If the object ends on the same line that the property starts, match against the last token
1274 * of the object, to ensure that the MemberExpression is not indented.
1276 * Otherwise, match against the first token of the object, e.g.
1279 * .baz // <-- offset by 1 from `foo`
1281 const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line
1285 if (typeof options.MemberExpression === "number") {
1287 // Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object.
1288 offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression);
1291 * For computed MemberExpressions, match the first token of the property against the opening bracket.
1292 * Otherwise, match the first token of the property against the object.
1294 offsets.setDesiredOffset(secondNonObjectToken, node.computed ? firstNonObjectToken : offsetBase, options.MemberExpression);
1297 // If the MemberExpression option is off, ignore the dot and the first token of the property.
1298 offsets.ignoreToken(firstNonObjectToken);
1299 offsets.ignoreToken(secondNonObjectToken);
1301 // To ignore the property indentation, ensure that the property tokens depend on the ignored tokens.
1302 offsets.setDesiredOffset(firstNonObjectToken, offsetBase, 0);
1303 offsets.setDesiredOffset(secondNonObjectToken, firstNonObjectToken, 0);
1307 NewExpression(node) {
1309 // Only indent the arguments if the NewExpression has parens (e.g. `new Foo(bar)` or `new Foo()`, but not `new Foo`
1310 if (node.arguments.length > 0 ||
1311 astUtils.isClosingParenToken(sourceCode.getLastToken(node)) &&
1312 astUtils.isOpeningParenToken(sourceCode.getLastToken(node, 1))) {
1313 addFunctionCallIndent(node);
1318 if (!node.shorthand && !node.method && node.kind === "init") {
1319 const colon = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isColonToken);
1321 offsets.ignoreToken(sourceCode.getTokenAfter(colon));
1325 SwitchStatement(node) {
1326 const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken);
1327 const closingCurly = sourceCode.getLastToken(node);
1329 offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, options.SwitchCase);
1331 if (node.cases.length) {
1332 sourceCode.getTokensBetween(
1333 node.cases[node.cases.length - 1],
1335 { includeComments: true, filter: astUtils.isCommentToken }
1336 ).forEach(token => offsets.ignoreToken(token));
1341 if (!(node.consequent.length === 1 && node.consequent[0].type === "BlockStatement")) {
1342 const caseKeyword = sourceCode.getFirstToken(node);
1343 const tokenAfterCurrentCase = sourceCode.getTokenAfter(node);
1345 offsets.setDesiredOffsets([caseKeyword.range[1], tokenAfterCurrentCase.range[0]], caseKeyword, 1);
1349 TemplateLiteral(node) {
1350 node.expressions.forEach((expression, index) => {
1351 const previousQuasi = node.quasis[index];
1352 const nextQuasi = node.quasis[index + 1];
1353 const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line
1354 ? sourceCode.getFirstToken(previousQuasi)
1357 offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1);
1358 offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0);
1362 VariableDeclaration(node) {
1363 let variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, node.kind)
1364 ? options.VariableDeclarator[node.kind]
1365 : DEFAULT_VARIABLE_INDENT;
1367 const firstToken = sourceCode.getFirstToken(node),
1368 lastToken = sourceCode.getLastToken(node);
1370 if (options.VariableDeclarator[node.kind] === "first") {
1371 if (node.declarations.length > 1) {
1372 addElementListIndent(
1381 variableIndent = DEFAULT_VARIABLE_INDENT;
1384 if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) {
1387 * VariableDeclarator indentation is a bit different from other forms of indentation, in that the
1388 * indentation of an opening bracket sometimes won't match that of a closing bracket. For example,
1389 * the following indentations are correct:
1400 * Account for when exiting the AST (after indentations have already been set for the nodes in
1401 * the declaration) by manually increasing the indentation level of the tokens in this declarator
1402 * on the same line as the start of the declaration, provided that there are declarators that
1405 offsets.setDesiredOffsets(node.range, firstToken, variableIndent, true);
1407 offsets.setDesiredOffsets(node.range, firstToken, variableIndent);
1410 if (astUtils.isSemicolonToken(lastToken)) {
1411 offsets.ignoreToken(lastToken);
1415 VariableDeclarator(node) {
1417 const equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken);
1418 const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator);
1420 offsets.ignoreToken(equalOperator);
1421 offsets.ignoreToken(tokenAfterOperator);
1422 offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1);
1423 offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0);
1427 "JSXAttribute[value]"(node) {
1428 const equalsToken = sourceCode.getFirstTokenBetween(node.name, node.value, token => token.type === "Punctuator" && token.value === "=");
1430 offsets.setDesiredOffsets([equalsToken.range[0], node.value.range[1]], sourceCode.getFirstToken(node.name), 1);
1434 if (node.closingElement) {
1435 addElementListIndent(node.children, sourceCode.getFirstToken(node.openingElement), sourceCode.getFirstToken(node.closingElement), 1);
1439 JSXOpeningElement(node) {
1440 const firstToken = sourceCode.getFirstToken(node);
1443 if (node.selfClosing) {
1444 closingToken = sourceCode.getLastToken(node, { skip: 1 });
1445 offsets.setDesiredOffset(sourceCode.getLastToken(node), closingToken, 0);
1447 closingToken = sourceCode.getLastToken(node);
1449 offsets.setDesiredOffsets(node.name.range, sourceCode.getFirstToken(node));
1450 addElementListIndent(node.attributes, firstToken, closingToken, 1);
1453 JSXClosingElement(node) {
1454 const firstToken = sourceCode.getFirstToken(node);
1456 offsets.setDesiredOffsets(node.name.range, firstToken, 1);
1460 const firstOpeningToken = sourceCode.getFirstToken(node.openingFragment);
1461 const firstClosingToken = sourceCode.getFirstToken(node.closingFragment);
1463 addElementListIndent(node.children, firstOpeningToken, firstClosingToken, 1);
1466 JSXOpeningFragment(node) {
1467 const firstToken = sourceCode.getFirstToken(node);
1468 const closingToken = sourceCode.getLastToken(node);
1470 offsets.setDesiredOffsets(node.range, firstToken, 1);
1471 offsets.matchOffsetOf(firstToken, closingToken);
1474 JSXClosingFragment(node) {
1475 const firstToken = sourceCode.getFirstToken(node);
1476 const slashToken = sourceCode.getLastToken(node, { skip: 1 });
1477 const closingToken = sourceCode.getLastToken(node);
1478 const tokenToMatch = astUtils.isTokenOnSameLine(slashToken, closingToken) ? slashToken : closingToken;
1480 offsets.setDesiredOffsets(node.range, firstToken, 1);
1481 offsets.matchOffsetOf(firstToken, tokenToMatch);
1484 JSXExpressionContainer(node) {
1485 const openingCurly = sourceCode.getFirstToken(node);
1486 const closingCurly = sourceCode.getLastToken(node);
1488 offsets.setDesiredOffsets(
1489 [openingCurly.range[1], closingCurly.range[0]],
1495 JSXSpreadAttribute(node) {
1496 const openingCurly = sourceCode.getFirstToken(node);
1497 const closingCurly = sourceCode.getLastToken(node);
1499 offsets.setDesiredOffsets(
1500 [openingCurly.range[1], closingCurly.range[0]],
1507 const firstToken = sourceCode.getFirstToken(node);
1509 // Ensure that the children of every node are indented at least as much as the first token.
1510 if (firstToken && !ignoredNodeFirstTokens.has(firstToken)) {
1511 offsets.setDesiredOffsets(node.range, firstToken, 0);
1516 const listenerCallQueue = [];
1519 * To ignore the indentation of a node:
1520 * 1. Don't call the node's listener when entering it (if it has a listener)
1521 * 2. Don't set any offsets against the first token of the node.
1522 * 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets.
1524 const offsetListeners = lodash.mapValues(
1525 baseOffsetListeners,
1528 * Offset listener calls are deferred until traversal is finished, and are called as
1529 * part of the final `Program:exit` listener. This is necessary because a node might
1530 * be matched by multiple selectors.
1532 * Example: Suppose there is an offset listener for `Identifier`, and the user has
1533 * specified in configuration that `MemberExpression > Identifier` should be ignored.
1534 * Due to selector specificity rules, the `Identifier` listener will get called first. However,
1535 * if a given Identifier node is supposed to be ignored, then the `Identifier` offset listener
1536 * should not have been called at all. Without doing extra selector matching, we don't know
1537 * whether the Identifier matches the `MemberExpression > Identifier` selector until the
1538 * `MemberExpression > Identifier` listener is called.
1540 * To avoid this, the `Identifier` listener isn't called until traversal finishes and all
1541 * ignored nodes are known.
1545 listenerCallQueue.push({ listener, node })
1548 // For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set.
1549 const ignoredNodes = new Set();
1553 * @param {ASTNode} node The node to ignore
1556 function addToIgnoredNodes(node) {
1557 ignoredNodes.add(node);
1558 ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node));
1561 const ignoredNodeListeners = options.ignoredNodes.reduce(
1562 (listeners, ignoredSelector) => Object.assign(listeners, { [ignoredSelector]: addToIgnoredNodes }),
1567 * Join the listeners, and add a listener to verify that all tokens actually have the correct indentation
1570 * Using Object.assign will cause some offset listeners to be overwritten if the same selector also appears
1571 * in `ignoredNodeListeners`. This isn't a problem because all of the matching nodes will be ignored,
1572 * so those listeners wouldn't be called anyway.
1574 return Object.assign(
1576 ignoredNodeListeners,
1580 // If a node's type is nonstandard, we can't tell how its children should be offset, so ignore it.
1581 if (!KNOWN_NODES.has(node.type)) {
1582 addToIgnoredNodes(node);
1587 // If ignoreComments option is enabled, ignore all comment tokens.
1588 if (options.ignoreComments) {
1589 sourceCode.getAllComments()
1590 .forEach(comment => offsets.ignoreToken(comment));
1593 // Invoke the queued offset listeners for the nodes that aren't ignored.
1595 .filter(nodeInfo => !ignoredNodes.has(nodeInfo.node))
1596 .forEach(nodeInfo => nodeInfo.listener(nodeInfo.node));
1598 // Update the offsets for ignored nodes to prevent their child tokens from being reported.
1599 ignoredNodes.forEach(ignoreNode);
1601 addParensIndent(sourceCode.ast.tokens);
1604 * Create a Map from (tokenOrComment) => (precedingToken).
1605 * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly.
1607 const precedingTokens = sourceCode.ast.comments.reduce((commentMap, comment) => {
1608 const tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { includeComments: true });
1610 return commentMap.set(comment, commentMap.has(tokenOrCommentBefore) ? commentMap.get(tokenOrCommentBefore) : tokenOrCommentBefore);
1613 sourceCode.lines.forEach((line, lineIndex) => {
1614 const lineNumber = lineIndex + 1;
1616 if (!tokenInfo.firstTokensByLineNumber.has(lineNumber)) {
1618 // Don't check indentation on blank lines
1622 const firstTokenOfLine = tokenInfo.firstTokensByLineNumber.get(lineNumber);
1624 if (firstTokenOfLine.loc.start.line !== lineNumber) {
1626 // Don't check the indentation of multi-line tokens (e.g. template literals or block comments) twice.
1630 if (astUtils.isCommentToken(firstTokenOfLine)) {
1631 const tokenBefore = precedingTokens.get(firstTokenOfLine);
1632 const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0];
1633 const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine);
1634 const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter);
1637 * If a comment precedes a line that begins with a semicolon token, align to that token, i.e.
1641 * ;(async () => {})()
1643 if (tokenAfter && astUtils.isSemicolonToken(tokenAfter) && !astUtils.isTokenOnSameLine(firstTokenOfLine, tokenAfter)) {
1644 offsets.setDesiredOffset(firstTokenOfLine, tokenAfter, 0);
1647 // If a comment matches the expected indentation of the token immediately before or after, don't report it.
1649 mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
1650 mayAlignWithAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))
1656 // If the token matches the expected indentation, don't report it.
1657 if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) {
1661 // Otherwise, report the token/comment.
1662 report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine));