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",
39 "ConditionalExpression",
44 "ExperimentalRestProperty",
45 "ExperimentalSpreadProperty",
46 "ExpressionStatement",
50 "FunctionDeclaration",
72 "TaggedTemplateExpression",
80 "VariableDeclaration",
90 "JSXMemberExpression",
92 "JSXExpressionContainer",
99 "ExportDefaultDeclaration",
100 "ExportNamedDeclaration",
101 "ExportAllDeclaration",
105 "ImportDefaultSpecifier",
106 "ImportNamespaceSpecifier",
111 * General rule strategy:
112 * 1. An OffsetStorage instance stores a map of desired offsets, where each token has a specified offset from another
113 * specified token or to the first column.
114 * 2. As the AST is traversed, modify the desired offsets of tokens accordingly. For example, when entering a
115 * BlockStatement, offset all of the tokens in the BlockStatement by 1 indent level from the opening curly
116 * brace of the BlockStatement.
117 * 3. After traversing the AST, calculate the expected indentation levels of every token according to the
118 * OffsetStorage container.
119 * 4. For each line, compare the expected indentation of the first token to the actual indentation in the file,
120 * and report the token if the two values are not equal.
125 * A mutable balanced binary search tree that stores (key, value) pairs. The keys are numeric, and must be unique.
126 * This is intended to be a generic wrapper around a balanced binary search tree library, so that the underlying implementation
127 * can easily be swapped out.
129 class BinarySearchTree {
132 * Creates an empty tree
135 this._rbTree = createTree();
139 * Inserts an entry into the tree.
140 * @param {number} key The entry's key
141 * @param {*} value The entry's value
145 const iterator = this._rbTree.find(key);
147 if (iterator.valid) {
148 this._rbTree = iterator.update(value);
150 this._rbTree = this._rbTree.insert(key, value);
155 * Finds the entry with the largest key less than or equal to the provided key
156 * @param {number} key The provided key
157 * @returns {{key: number, value: *}|null} The found entry, or null if no such entry exists.
160 const iterator = this._rbTree.le(key);
162 return iterator && { key: iterator.key, value: iterator.value };
166 * Deletes all of the keys in the interval [start, end)
167 * @param {number} start The start of the range
168 * @param {number} end The end of the range
171 deleteRange(start, end) {
173 // Exit without traversing the tree if the range has zero size.
177 const iterator = this._rbTree.ge(start);
179 while (iterator.valid && iterator.key < end) {
180 this._rbTree = this._rbTree.remove(iterator.key);
187 * A helper class to get token-based info related to indentation
191 // eslint-disable-next-line jsdoc/require-description
193 * @param {SourceCode} sourceCode A SourceCode object
195 constructor(sourceCode) {
196 this.sourceCode = sourceCode;
197 this.firstTokensByLineNumber = sourceCode.tokensAndComments.reduce((map, token) => {
198 if (!map.has(token.loc.start.line)) {
199 map.set(token.loc.start.line, token);
201 if (!map.has(token.loc.end.line) && sourceCode.text.slice(token.range[1] - token.loc.end.column, token.range[1]).trim()) {
202 map.set(token.loc.end.line, token);
209 * Gets the first token on a given token's line
210 * @param {Token|ASTNode} token a node or token
211 * @returns {Token} The first token on the given line
213 getFirstTokenOfLine(token) {
214 return this.firstTokensByLineNumber.get(token.loc.start.line);
218 * Determines whether a token is the first token in its line
219 * @param {Token} token The token
220 * @returns {boolean} `true` if the token is the first on its line
222 isFirstTokenOfLine(token) {
223 return this.getFirstTokenOfLine(token) === token;
227 * Get the actual indent of a token
228 * @param {Token} token Token to examine. This should be the first token on its line.
229 * @returns {string} The indentation characters that precede the token
231 getTokenIndent(token) {
232 return this.sourceCode.text.slice(token.range[0] - token.loc.start.column, token.range[0]);
237 * A class to store information on desired offsets of tokens from each other
239 class OffsetStorage {
241 // eslint-disable-next-line jsdoc/require-description
243 * @param {TokenInfo} tokenInfo a TokenInfo instance
244 * @param {number} indentSize The desired size of each indentation level
245 * @param {string} indentType The indentation character
247 constructor(tokenInfo, indentSize, indentType) {
248 this._tokenInfo = tokenInfo;
249 this._indentSize = indentSize;
250 this._indentType = indentType;
252 this._tree = new BinarySearchTree();
253 this._tree.insert(0, { offset: 0, from: null, force: false });
255 this._lockedFirstTokens = new WeakMap();
256 this._desiredIndentCache = new WeakMap();
257 this._ignoredTokens = new WeakSet();
260 _getOffsetDescriptor(token) {
261 return this._tree.findLe(token.range[0]).value;
265 * Sets the offset column of token B to match the offset column of token A.
266 * **WARNING**: This matches a *column*, even if baseToken is not the first token on its line. In
267 * most cases, `setDesiredOffset` should be used instead.
268 * @param {Token} baseToken The first token
269 * @param {Token} offsetToken The second token, whose offset should be matched to the first token
272 matchOffsetOf(baseToken, offsetToken) {
275 * lockedFirstTokens is a map from a token whose indentation is controlled by the "first" option to
276 * the token that it depends on. For example, with the `ArrayExpression: first` option, the first
277 * token of each element in the array after the first will be mapped to the first token of the first
278 * element. The desired indentation of each of these tokens is computed based on the desired indentation
279 * of the "first" element, rather than through the normal offset mechanism.
281 this._lockedFirstTokens.set(offsetToken, baseToken);
285 * Sets the desired offset of a token.
287 * This uses a line-based offset collapsing behavior to handle tokens on the same line.
288 * For example, consider the following two cases:
300 * Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from
301 * the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is
302 * the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces)
303 * from the start of its line.
305 * However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level
306 * between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the
307 * `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented
308 * by 1 indent level from the start of the line.
310 * This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node,
311 * without needing to check which lines those tokens are on.
313 * Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive
314 * behavior can occur. For example, consider the following cases:
327 * Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz`
328 * should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz`
329 * being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no
330 * collapsing would occur).
332 * Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and
333 * offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed
334 * in the second case.
335 * @param {Token} token The token
336 * @param {Token} fromToken The token that `token` should be offset from
337 * @param {number} offset The desired indent level
340 setDesiredOffset(token, fromToken, offset) {
341 return this.setDesiredOffsets(token.range, fromToken, offset);
345 * Sets the desired offset of all tokens in a range
346 * It's common for node listeners in this file to need to apply the same offset to a large, contiguous range of tokens.
347 * Moreover, the offset of any given token is usually updated multiple times (roughly once for each node that contains
348 * it). This means that the offset of each token is updated O(AST depth) times.
349 * It would not be performant to store and update the offsets for each token independently, because the rule would end
350 * up having a time complexity of O(number of tokens * AST depth), which is quite slow for large files.
352 * Instead, the offset tree is represented as a collection of contiguous offset ranges in a file. For example, the following
353 * list could represent the state of the offset tree at a given point:
355 * * Tokens starting in the interval [0, 15) are aligned with the beginning of the file
356 * * Tokens starting in the interval [15, 30) are offset by 1 indent level from the `bar` token
357 * * Tokens starting in the interval [30, 43) are offset by 1 indent level from the `foo` token
358 * * Tokens starting in the interval [43, 820) are offset by 2 indent levels from the `bar` token
359 * * Tokens starting in the interval [820, ∞) are offset by 1 indent level from the `baz` token
361 * The `setDesiredOffsets` methods inserts ranges like the ones above. The third line above would be inserted by using:
362 * `setDesiredOffsets([30, 43], fooToken, 1);`
363 * @param {[number, number]} range A [start, end] pair. All tokens with range[0] <= token.start < range[1] will have the offset applied.
364 * @param {Token} fromToken The token that this is offset from
365 * @param {number} offset The desired indent level
366 * @param {boolean} force `true` if this offset should not use the normal collapsing behavior. This should almost always be false.
369 setDesiredOffsets(range, fromToken, offset, force) {
372 * Offset ranges are stored as a collection of nodes, where each node maps a numeric key to an offset
373 * descriptor. The tree for the example above would have the following nodes:
375 * * key: 0, value: { offset: 0, from: null }
376 * * key: 15, value: { offset: 1, from: barToken }
377 * * key: 30, value: { offset: 1, from: fooToken }
378 * * key: 43, value: { offset: 2, from: barToken }
379 * * key: 820, value: { offset: 1, from: bazToken }
381 * To find the offset descriptor for any given token, one needs to find the node with the largest key
382 * which is <= token.start. To make this operation fast, the nodes are stored in a balanced binary
383 * search tree indexed by key.
386 const descriptorToInsert = { offset, from: fromToken, force };
388 const descriptorAfterRange = this._tree.findLe(range[1]).value;
390 const fromTokenIsInRange = fromToken && fromToken.range[0] >= range[0] && fromToken.range[1] <= range[1];
391 const fromTokenDescriptor = fromTokenIsInRange && this._getOffsetDescriptor(fromToken);
393 // First, remove any existing nodes in the range from the tree.
394 this._tree.deleteRange(range[0] + 1, range[1]);
396 // Insert a new node into the tree for this range
397 this._tree.insert(range[0], descriptorToInsert);
400 * To avoid circular offset dependencies, keep the `fromToken` token mapped to whatever it was mapped to previously,
401 * even if it's in the current range.
403 if (fromTokenIsInRange) {
404 this._tree.insert(fromToken.range[0], fromTokenDescriptor);
405 this._tree.insert(fromToken.range[1], descriptorToInsert);
409 * To avoid modifying the offset of tokens after the range, insert another node to keep the offset of the following
410 * tokens the same as it was before.
412 this._tree.insert(range[1], descriptorAfterRange);
416 * Gets the desired indent of a token
417 * @param {Token} token The token
418 * @returns {string} The desired indent of the token
420 getDesiredIndent(token) {
421 if (!this._desiredIndentCache.has(token)) {
423 if (this._ignoredTokens.has(token)) {
426 * If the token is ignored, use the actual indent of the token as the desired indent.
427 * This ensures that no errors are reported for this token.
429 this._desiredIndentCache.set(
431 this._tokenInfo.getTokenIndent(token)
433 } else if (this._lockedFirstTokens.has(token)) {
434 const firstToken = this._lockedFirstTokens.get(token);
436 this._desiredIndentCache.set(
439 // (indentation for the first element's line)
440 this.getDesiredIndent(this._tokenInfo.getFirstTokenOfLine(firstToken)) +
442 // (space between the start of the first element's line and the first element)
443 this._indentType.repeat(firstToken.loc.start.column - this._tokenInfo.getFirstTokenOfLine(firstToken).loc.start.column)
446 const offsetInfo = this._getOffsetDescriptor(token);
449 offsetInfo.from.loc.start.line === token.loc.start.line &&
450 !/^\s*?\n/u.test(token.value) &&
452 ) ? 0 : offsetInfo.offset * this._indentSize;
454 this._desiredIndentCache.set(
456 (offsetInfo.from ? this.getDesiredIndent(offsetInfo.from) : "") + this._indentType.repeat(offset)
460 return this._desiredIndentCache.get(token);
464 * Ignores a token, preventing it from being reported.
465 * @param {Token} token The token
469 if (this._tokenInfo.isFirstTokenOfLine(token)) {
470 this._ignoredTokens.add(token);
475 * Gets the first token that the given token's indentation is dependent on
476 * @param {Token} token The token
477 * @returns {Token} The token that the given token depends on, or `null` if the given token is at the top level
479 getFirstDependency(token) {
480 return this._getOffsetDescriptor(token).from;
484 const ELEMENT_LIST_SCHEMA = {
491 enum: ["first", "off"]
501 description: "enforce consistent indentation",
502 category: "Stylistic Issues",
504 url: "https://eslint.org/docs/rules/indent"
507 fixable: "whitespace",
529 VariableDeclarator: {
535 var: ELEMENT_LIST_SCHEMA,
536 let: ELEMENT_LIST_SCHEMA,
537 const: ELEMENT_LIST_SCHEMA
539 additionalProperties: false
565 FunctionDeclaration: {
568 parameters: ELEMENT_LIST_SCHEMA,
574 additionalProperties: false
576 FunctionExpression: {
579 parameters: ELEMENT_LIST_SCHEMA,
585 additionalProperties: false
590 arguments: ELEMENT_LIST_SCHEMA
592 additionalProperties: false
594 ArrayExpression: ELEMENT_LIST_SCHEMA,
595 ObjectExpression: ELEMENT_LIST_SCHEMA,
596 ImportDeclaration: ELEMENT_LIST_SCHEMA,
597 flatTernaryExpressions: {
601 offsetTernaryExpressions: {
619 additionalProperties: false
623 wrongIndentation: "Expected indentation of {{expected}} but found {{actual}}."
628 const DEFAULT_VARIABLE_INDENT = 1;
629 const DEFAULT_PARAMETER_INDENT = 1;
630 const DEFAULT_FUNCTION_BODY_INDENT = 1;
632 let indentType = "space";
636 VariableDeclarator: {
637 var: DEFAULT_VARIABLE_INDENT,
638 let: DEFAULT_VARIABLE_INDENT,
639 const: DEFAULT_VARIABLE_INDENT
642 FunctionDeclaration: {
643 parameters: DEFAULT_PARAMETER_INDENT,
644 body: DEFAULT_FUNCTION_BODY_INDENT
646 FunctionExpression: {
647 parameters: DEFAULT_PARAMETER_INDENT,
648 body: DEFAULT_FUNCTION_BODY_INDENT
651 arguments: DEFAULT_PARAMETER_INDENT
656 ImportDeclaration: 1,
657 flatTernaryExpressions: false,
659 ignoreComments: false
662 if (context.options.length) {
663 if (context.options[0] === "tab") {
667 indentSize = context.options[0];
668 indentType = "space";
671 if (context.options[1]) {
672 Object.assign(options, context.options[1]);
674 if (typeof options.VariableDeclarator === "number" || options.VariableDeclarator === "first") {
675 options.VariableDeclarator = {
676 var: options.VariableDeclarator,
677 let: options.VariableDeclarator,
678 const: options.VariableDeclarator
684 const sourceCode = context.getSourceCode();
685 const tokenInfo = new TokenInfo(sourceCode);
686 const offsets = new OffsetStorage(tokenInfo, indentSize, indentType === "space" ? " " : "\t");
687 const parameterParens = new WeakSet();
690 * Creates an error message for a line, given the expected/actual indentation.
691 * @param {int} expectedAmount The expected amount of indentation characters for this line
692 * @param {int} actualSpaces The actual number of indentation spaces that were found on this line
693 * @param {int} actualTabs The actual number of indentation tabs that were found on this line
694 * @returns {string} An error message for this line
696 function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) {
697 const expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`; // e.g. "2 tabs"
698 const foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`; // e.g. "space"
699 const foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`; // e.g. "tabs"
702 if (actualSpaces > 0) {
705 * Abbreviate the message if the expected indentation is also spaces.
706 * e.g. 'Expected 4 spaces but found 2' rather than 'Expected 4 spaces but found 2 spaces'
708 foundStatement = indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}`;
709 } else if (actualTabs > 0) {
710 foundStatement = indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}`;
712 foundStatement = "0";
715 expected: expectedStatement,
716 actual: foundStatement
721 * Reports a given indent violation
722 * @param {Token} token Token violating the indent rule
723 * @param {string} neededIndent Expected indentation string
726 function report(token, neededIndent) {
727 const actualIndent = Array.from(tokenInfo.getTokenIndent(token));
728 const numSpaces = actualIndent.filter(char => char === " ").length;
729 const numTabs = actualIndent.filter(char => char === "\t").length;
733 messageId: "wrongIndentation",
734 data: createErrorMessageData(neededIndent.length, numSpaces, numTabs),
736 start: { line: token.loc.start.line, column: 0 },
737 end: { line: token.loc.start.line, column: token.loc.start.column }
740 const range = [token.range[0] - token.loc.start.column, token.range[0]];
741 const newText = neededIndent;
743 return fixer.replaceTextRange(range, newText);
749 * Checks if a token's indentation is correct
750 * @param {Token} token Token to examine
751 * @param {string} desiredIndent Desired indentation of the string
752 * @returns {boolean} `true` if the token's indentation is correct
754 function validateTokenIndent(token, desiredIndent) {
755 const indentation = tokenInfo.getTokenIndent(token);
757 return indentation === desiredIndent ||
759 // To avoid conflicts with no-mixed-spaces-and-tabs, don't report mixed spaces and tabs.
760 indentation.includes(" ") && indentation.includes("\t");
764 * Check to see if the node is a file level IIFE
765 * @param {ASTNode} node The function node to check.
766 * @returns {boolean} True if the node is the outer IIFE
768 function isOuterIIFE(node) {
771 * Verify that the node is an IIFE
773 if (!node.parent || node.parent.type !== "CallExpression" || node.parent.callee !== node) {
778 * Navigate legal ancestors to determine whether this IIFE is outer.
779 * A "legal ancestor" is an expression or statement that causes the function to get executed immediately.
780 * For example, `!(function(){})()` is an outer IIFE even though it is preceded by a ! operator.
782 let statement = node.parent && node.parent.parent;
785 statement.type === "UnaryExpression" && ["!", "~", "+", "-"].indexOf(statement.operator) > -1 ||
786 statement.type === "AssignmentExpression" ||
787 statement.type === "LogicalExpression" ||
788 statement.type === "SequenceExpression" ||
789 statement.type === "VariableDeclarator"
791 statement = statement.parent;
794 return (statement.type === "ExpressionStatement" || statement.type === "VariableDeclaration") && statement.parent.type === "Program";
798 * Counts the number of linebreaks that follow the last non-whitespace character in a string
799 * @param {string} string The string to check
800 * @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character,
801 * or the total number of linebreaks if the string is all whitespace.
803 function countTrailingLinebreaks(string) {
804 const trailingWhitespace = string.match(/\s*$/u)[0];
805 const linebreakMatches = trailingWhitespace.match(astUtils.createGlobalLinebreakMatcher());
807 return linebreakMatches === null ? 0 : linebreakMatches.length;
811 * Check indentation for lists of elements (arrays, objects, function params)
812 * @param {ASTNode[]} elements List of elements that should be offset
813 * @param {Token} startToken The start token of the list that element should be aligned against, e.g. '['
814 * @param {Token} endToken The end token of the list, e.g. ']'
815 * @param {number|string} offset The amount that the elements should be offset
818 function addElementListIndent(elements, startToken, endToken, offset) {
821 * Gets the first token of a given element, including surrounding parentheses.
822 * @param {ASTNode} element A node in the `elements` list
823 * @returns {Token} The first token of this element
825 function getFirstToken(element) {
826 let token = sourceCode.getTokenBefore(element);
828 while (astUtils.isOpeningParenToken(token) && token !== startToken) {
829 token = sourceCode.getTokenBefore(token);
831 return sourceCode.getTokenAfter(token);
834 // Run through all the tokens in the list, and offset them by one indent level (mainly for comments, other things will end up overridden)
835 offsets.setDesiredOffsets(
836 [startToken.range[1], endToken.range[0]],
838 typeof offset === "number" ? offset : 1
840 offsets.setDesiredOffset(endToken, startToken, 0);
842 // If the preference is "first" but there is no first element (e.g. sparse arrays w/ empty first slot), fall back to 1 level.
843 if (offset === "first" && elements.length && !elements[0]) {
846 elements.forEach((element, index) => {
849 // Skip holes in arrays
852 if (offset === "off") {
854 // Ignore the first token of every element if the "off" option is used
855 offsets.ignoreToken(getFirstToken(element));
858 // Offset the following elements correctly relative to the first element
862 if (offset === "first" && tokenInfo.isFirstTokenOfLine(getFirstToken(element))) {
863 offsets.matchOffsetOf(getFirstToken(elements[0]), getFirstToken(element));
865 const previousElement = elements[index - 1];
866 const firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement);
867 const previousElementLastToken = previousElement && sourceCode.getLastToken(previousElement);
871 previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line
873 offsets.setDesiredOffsets(
874 [previousElement.range[1], element.range[1]],
875 firstTokenOfPreviousElement,
884 * Check and decide whether to check for indentation for blockless nodes
885 * Scenarios are for or while statements without braces around them
886 * @param {ASTNode} node node to examine
889 function addBlocklessNodeIndent(node) {
890 if (node.type !== "BlockStatement") {
891 const lastParentToken = sourceCode.getTokenBefore(node, astUtils.isNotOpeningParenToken);
893 let firstBodyToken = sourceCode.getFirstToken(node);
894 let lastBodyToken = sourceCode.getLastToken(node);
897 astUtils.isOpeningParenToken(sourceCode.getTokenBefore(firstBodyToken)) &&
898 astUtils.isClosingParenToken(sourceCode.getTokenAfter(lastBodyToken))
900 firstBodyToken = sourceCode.getTokenBefore(firstBodyToken);
901 lastBodyToken = sourceCode.getTokenAfter(lastBodyToken);
904 offsets.setDesiredOffsets([firstBodyToken.range[0], lastBodyToken.range[1]], lastParentToken, 1);
907 * For blockless nodes with semicolon-first style, don't indent the semicolon.
910 * ; [1, 2, 3].map(foo)
912 const lastToken = sourceCode.getLastToken(node);
914 if (node.type !== "EmptyStatement" && astUtils.isSemicolonToken(lastToken)) {
915 offsets.setDesiredOffset(lastToken, lastParentToken, 0);
921 * Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`)
922 * @param {ASTNode} node A CallExpression or NewExpression node
925 function addFunctionCallIndent(node) {
928 if (node.arguments.length) {
929 openingParen = sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken);
931 openingParen = sourceCode.getLastToken(node, 1);
933 const closingParen = sourceCode.getLastToken(node);
935 parameterParens.add(openingParen);
936 parameterParens.add(closingParen);
939 * If `?.` token exists, set desired offset for that.
940 * This logic is copied from `MemberExpression`'s.
943 const dotToken = sourceCode.getTokenAfter(node.callee, astUtils.isQuestionDotToken);
944 const calleeParenCount = sourceCode.getTokensBetween(node.callee, dotToken, { filter: astUtils.isClosingParenToken }).length;
945 const firstTokenOfCallee = calleeParenCount
946 ? sourceCode.getTokenBefore(node.callee, { skip: calleeParenCount - 1 })
947 : sourceCode.getFirstToken(node.callee);
948 const lastTokenOfCallee = sourceCode.getTokenBefore(dotToken);
949 const offsetBase = lastTokenOfCallee.loc.end.line === openingParen.loc.start.line
951 : firstTokenOfCallee;
953 offsets.setDesiredOffset(dotToken, offsetBase, 1);
956 const offsetAfterToken = node.callee.type === "TaggedTemplateExpression" ? sourceCode.getFirstToken(node.callee.quasi) : openingParen;
957 const offsetToken = sourceCode.getTokenBefore(offsetAfterToken);
959 offsets.setDesiredOffset(openingParen, offsetToken, 0);
961 addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments);
965 * Checks the indentation of parenthesized values, given a list of tokens in a program
966 * @param {Token[]} tokens A list of tokens
969 function addParensIndent(tokens) {
970 const parenStack = [];
971 const parenPairs = [];
973 tokens.forEach(nextToken => {
975 // Accumulate a list of parenthesis pairs
976 if (astUtils.isOpeningParenToken(nextToken)) {
977 parenStack.push(nextToken);
978 } else if (astUtils.isClosingParenToken(nextToken)) {
979 parenPairs.unshift({ left: parenStack.pop(), right: nextToken });
983 parenPairs.forEach(pair => {
984 const leftParen = pair.left;
985 const rightParen = pair.right;
987 // We only want to handle parens around expressions, so exclude parentheses that are in function parameters and function call arguments.
988 if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) {
989 const parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen));
991 parenthesizedTokens.forEach(token => {
992 if (!parenthesizedTokens.has(offsets.getFirstDependency(token))) {
993 offsets.setDesiredOffset(token, leftParen, 1);
998 offsets.setDesiredOffset(rightParen, leftParen, 0);
1003 * Ignore all tokens within an unknown node whose offset do not depend
1004 * on another token's offset within the unknown node
1005 * @param {ASTNode} node Unknown Node
1008 function ignoreNode(node) {
1009 const unknownNodeTokens = new Set(sourceCode.getTokens(node, { includeComments: true }));
1011 unknownNodeTokens.forEach(token => {
1012 if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) {
1013 const firstTokenOfLine = tokenInfo.getFirstTokenOfLine(token);
1015 if (token === firstTokenOfLine) {
1016 offsets.ignoreToken(token);
1018 offsets.setDesiredOffset(token, firstTokenOfLine, 0);
1025 * Check whether the given token is on the first line of a statement.
1026 * @param {Token} token The token to check.
1027 * @param {ASTNode} leafNode The expression node that the token belongs directly.
1028 * @returns {boolean} `true` if the token is on the first line of a statement.
1030 function isOnFirstLineOfStatement(token, leafNode) {
1031 let node = leafNode;
1033 while (node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration")) {
1038 return !node || node.loc.start.line === token.loc.start.line;
1042 * Check whether there are any blank (whitespace-only) lines between
1043 * two tokens on separate lines.
1044 * @param {Token} firstToken The first token.
1045 * @param {Token} secondToken The second token.
1046 * @returns {boolean} `true` if the tokens are on separate lines and
1047 * there exists a blank line between them, `false` otherwise.
1049 function hasBlankLinesBetween(firstToken, secondToken) {
1050 const firstTokenLine = firstToken.loc.end.line;
1051 const secondTokenLine = secondToken.loc.start.line;
1053 if (firstTokenLine === secondTokenLine || firstTokenLine === secondTokenLine - 1) {
1057 for (let line = firstTokenLine + 1; line < secondTokenLine; ++line) {
1058 if (!tokenInfo.firstTokensByLineNumber.has(line)) {
1066 const ignoredNodeFirstTokens = new Set();
1068 const baseOffsetListeners = {
1069 "ArrayExpression, ArrayPattern"(node) {
1070 const openingBracket = sourceCode.getFirstToken(node);
1071 const closingBracket = sourceCode.getTokenAfter(lodash.findLast(node.elements) || openingBracket, astUtils.isClosingBracketToken);
1073 addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression);
1076 "ObjectExpression, ObjectPattern"(node) {
1077 const openingCurly = sourceCode.getFirstToken(node);
1078 const closingCurly = sourceCode.getTokenAfter(
1079 node.properties.length ? node.properties[node.properties.length - 1] : openingCurly,
1080 astUtils.isClosingBraceToken
1083 addElementListIndent(node.properties, openingCurly, closingCurly, options.ObjectExpression);
1086 ArrowFunctionExpression(node) {
1087 const maybeOpeningParen = sourceCode.getFirstToken(node, { skip: node.async ? 1 : 0 });
1089 if (astUtils.isOpeningParenToken(maybeOpeningParen)) {
1090 const openingParen = maybeOpeningParen;
1091 const closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken);
1093 parameterParens.add(openingParen);
1094 parameterParens.add(closingParen);
1095 addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
1098 addBlocklessNodeIndent(node.body);
1101 AssignmentExpression(node) {
1102 const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
1104 offsets.setDesiredOffsets([operator.range[0], node.range[1]], sourceCode.getLastToken(node.left), 1);
1105 offsets.ignoreToken(operator);
1106 offsets.ignoreToken(sourceCode.getTokenAfter(operator));
1109 "BinaryExpression, LogicalExpression"(node) {
1110 const operator = sourceCode.getFirstTokenBetween(node.left, node.right, token => token.value === node.operator);
1113 * For backwards compatibility, don't check BinaryExpression indents, e.g.
1118 const tokenAfterOperator = sourceCode.getTokenAfter(operator);
1120 offsets.ignoreToken(operator);
1121 offsets.ignoreToken(tokenAfterOperator);
1122 offsets.setDesiredOffset(tokenAfterOperator, operator, 0);
1125 "BlockStatement, ClassBody"(node) {
1126 let blockIndentLevel;
1128 if (node.parent && isOuterIIFE(node.parent)) {
1129 blockIndentLevel = options.outerIIFEBody;
1130 } else if (node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression")) {
1131 blockIndentLevel = options.FunctionExpression.body;
1132 } else if (node.parent && node.parent.type === "FunctionDeclaration") {
1133 blockIndentLevel = options.FunctionDeclaration.body;
1135 blockIndentLevel = 1;
1139 * For blocks that aren't lone statements, ensure that the opening curly brace
1140 * is aligned with the parent.
1142 if (!astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type)) {
1143 offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0);
1146 addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel);
1149 CallExpression: addFunctionCallIndent,
1151 "ClassDeclaration[superClass], ClassExpression[superClass]"(node) {
1152 const classToken = sourceCode.getFirstToken(node);
1153 const extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken);
1155 offsets.setDesiredOffsets([extendsToken.range[0], node.body.range[0]], classToken, 1);
1158 ConditionalExpression(node) {
1159 const firstToken = sourceCode.getFirstToken(node);
1161 // `flatTernaryExpressions` option is for the following style:
1166 if (!options.flatTernaryExpressions ||
1167 !astUtils.isTokenOnSameLine(node.test, node.consequent) ||
1168 isOnFirstLineOfStatement(firstToken, node)
1170 const questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.type === "Punctuator" && token.value === "?");
1171 const colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, token => token.type === "Punctuator" && token.value === ":");
1173 const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken);
1174 const lastConsequentToken = sourceCode.getTokenBefore(colonToken);
1175 const firstAlternateToken = sourceCode.getTokenAfter(colonToken);
1177 offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
1178 offsets.setDesiredOffset(colonToken, firstToken, 1);
1180 offsets.setDesiredOffset(firstConsequentToken, firstToken,
1181 firstConsequentToken.type === "Punctuator" &&
1182 options.offsetTernaryExpressions ? 2 : 1);
1185 * The alternate and the consequent should usually have the same indentation.
1186 * If they share part of a line, align the alternate against the first token of the consequent.
1187 * This allows the alternate to be indented correctly in cases like this:
1190 * ) : ( // this '(' is aligned with the '(' above, so it's considered to be aligned with `foo`
1191 * baz // as a result, `baz` is offset by 1 rather than 2
1194 if (lastConsequentToken.loc.end.line === firstAlternateToken.loc.start.line) {
1195 offsets.setDesiredOffset(firstAlternateToken, firstConsequentToken, 0);
1199 * If the alternate and consequent do not share part of a line, offset the alternate from the first
1200 * token of the conditional expression. For example:
1204 * If `baz` were aligned with `bar` rather than being offset by 1 from `foo`, `baz` would end up
1205 * having no expected indentation.
1207 offsets.setDesiredOffset(firstAlternateToken, firstToken,
1208 firstAlternateToken.type === "Punctuator" &&
1209 options.offsetTernaryExpressions ? 2 : 1);
1214 "DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement": node => addBlocklessNodeIndent(node.body),
1216 ExportNamedDeclaration(node) {
1217 if (node.declaration === null) {
1218 const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
1220 // Indent the specifiers in `export {foo, bar, baz}`
1221 addElementListIndent(node.specifiers, sourceCode.getFirstToken(node, { skip: 1 }), closingCurly, 1);
1225 // Indent everything after and including the `from` token in `export {foo, bar, baz} from 'qux'`
1226 offsets.setDesiredOffsets([closingCurly.range[1], node.range[1]], sourceCode.getFirstToken(node), 1);
1231 ForStatement(node) {
1232 const forOpeningParen = sourceCode.getFirstToken(node, 1);
1235 offsets.setDesiredOffsets(node.init.range, forOpeningParen, 1);
1238 offsets.setDesiredOffsets(node.test.range, forOpeningParen, 1);
1241 offsets.setDesiredOffsets(node.update.range, forOpeningParen, 1);
1243 addBlocklessNodeIndent(node.body);
1246 "FunctionDeclaration, FunctionExpression"(node) {
1247 const closingParen = sourceCode.getTokenBefore(node.body);
1248 const openingParen = sourceCode.getTokenBefore(node.params.length ? node.params[0] : closingParen);
1250 parameterParens.add(openingParen);
1251 parameterParens.add(closingParen);
1252 addElementListIndent(node.params, openingParen, closingParen, options[node.type].parameters);
1256 addBlocklessNodeIndent(node.consequent);
1257 if (node.alternate && node.alternate.type !== "IfStatement") {
1258 addBlocklessNodeIndent(node.alternate);
1262 ImportDeclaration(node) {
1263 if (node.specifiers.some(specifier => specifier.type === "ImportSpecifier")) {
1264 const openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken);
1265 const closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
1267 addElementListIndent(node.specifiers.filter(specifier => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration);
1270 const fromToken = sourceCode.getLastToken(node, token => token.type === "Identifier" && token.value === "from");
1271 const sourceToken = sourceCode.getLastToken(node, token => token.type === "String");
1272 const semiToken = sourceCode.getLastToken(node, token => token.type === "Punctuator" && token.value === ";");
1275 const end = semiToken && semiToken.range[1] === sourceToken.range[1] ? node.range[1] : sourceToken.range[1];
1277 offsets.setDesiredOffsets([fromToken.range[0], end], sourceCode.getFirstToken(node), 1);
1281 ImportExpression(node) {
1282 const openingParen = sourceCode.getFirstToken(node, 1);
1283 const closingParen = sourceCode.getLastToken(node);
1285 parameterParens.add(openingParen);
1286 parameterParens.add(closingParen);
1287 offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0);
1289 addElementListIndent([node.source], openingParen, closingParen, options.CallExpression.arguments);
1292 "MemberExpression, JSXMemberExpression, MetaProperty"(node) {
1293 const object = node.type === "MetaProperty" ? node.meta : node.object;
1294 const firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken);
1295 const secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken);
1297 const objectParenCount = sourceCode.getTokensBetween(object, node.property, { filter: astUtils.isClosingParenToken }).length;
1298 const firstObjectToken = objectParenCount
1299 ? sourceCode.getTokenBefore(object, { skip: objectParenCount - 1 })
1300 : sourceCode.getFirstToken(object);
1301 const lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken);
1302 const firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;
1304 if (node.computed) {
1306 // For computed MemberExpressions, match the closing bracket with the opening bracket.
1307 offsets.setDesiredOffset(sourceCode.getLastToken(node), firstNonObjectToken, 0);
1308 offsets.setDesiredOffsets(node.property.range, firstNonObjectToken, 1);
1312 * If the object ends on the same line that the property starts, match against the last token
1313 * of the object, to ensure that the MemberExpression is not indented.
1315 * Otherwise, match against the first token of the object, e.g.
1318 * .baz // <-- offset by 1 from `foo`
1320 const offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line
1324 if (typeof options.MemberExpression === "number") {
1326 // Match the dot (for non-computed properties) or the opening bracket (for computed properties) against the object.
1327 offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression);
1330 * For computed MemberExpressions, match the first token of the property against the opening bracket.
1331 * Otherwise, match the first token of the property against the object.
1333 offsets.setDesiredOffset(secondNonObjectToken, node.computed ? firstNonObjectToken : offsetBase, options.MemberExpression);
1336 // If the MemberExpression option is off, ignore the dot and the first token of the property.
1337 offsets.ignoreToken(firstNonObjectToken);
1338 offsets.ignoreToken(secondNonObjectToken);
1340 // To ignore the property indentation, ensure that the property tokens depend on the ignored tokens.
1341 offsets.setDesiredOffset(firstNonObjectToken, offsetBase, 0);
1342 offsets.setDesiredOffset(secondNonObjectToken, firstNonObjectToken, 0);
1346 NewExpression(node) {
1348 // Only indent the arguments if the NewExpression has parens (e.g. `new Foo(bar)` or `new Foo()`, but not `new Foo`
1349 if (node.arguments.length > 0 ||
1350 astUtils.isClosingParenToken(sourceCode.getLastToken(node)) &&
1351 astUtils.isOpeningParenToken(sourceCode.getLastToken(node, 1))) {
1352 addFunctionCallIndent(node);
1357 if (!node.shorthand && !node.method && node.kind === "init") {
1358 const colon = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isColonToken);
1360 offsets.ignoreToken(sourceCode.getTokenAfter(colon));
1364 SwitchStatement(node) {
1365 const openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken);
1366 const closingCurly = sourceCode.getLastToken(node);
1368 offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, options.SwitchCase);
1370 if (node.cases.length) {
1371 sourceCode.getTokensBetween(
1372 node.cases[node.cases.length - 1],
1374 { includeComments: true, filter: astUtils.isCommentToken }
1375 ).forEach(token => offsets.ignoreToken(token));
1380 if (!(node.consequent.length === 1 && node.consequent[0].type === "BlockStatement")) {
1381 const caseKeyword = sourceCode.getFirstToken(node);
1382 const tokenAfterCurrentCase = sourceCode.getTokenAfter(node);
1384 offsets.setDesiredOffsets([caseKeyword.range[1], tokenAfterCurrentCase.range[0]], caseKeyword, 1);
1388 TemplateLiteral(node) {
1389 node.expressions.forEach((expression, index) => {
1390 const previousQuasi = node.quasis[index];
1391 const nextQuasi = node.quasis[index + 1];
1392 const tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line
1393 ? sourceCode.getFirstToken(previousQuasi)
1396 offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1);
1397 offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0);
1401 VariableDeclaration(node) {
1402 let variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, node.kind)
1403 ? options.VariableDeclarator[node.kind]
1404 : DEFAULT_VARIABLE_INDENT;
1406 const firstToken = sourceCode.getFirstToken(node),
1407 lastToken = sourceCode.getLastToken(node);
1409 if (options.VariableDeclarator[node.kind] === "first") {
1410 if (node.declarations.length > 1) {
1411 addElementListIndent(
1420 variableIndent = DEFAULT_VARIABLE_INDENT;
1423 if (node.declarations[node.declarations.length - 1].loc.start.line > node.loc.start.line) {
1426 * VariableDeclarator indentation is a bit different from other forms of indentation, in that the
1427 * indentation of an opening bracket sometimes won't match that of a closing bracket. For example,
1428 * the following indentations are correct:
1439 * Account for when exiting the AST (after indentations have already been set for the nodes in
1440 * the declaration) by manually increasing the indentation level of the tokens in this declarator
1441 * on the same line as the start of the declaration, provided that there are declarators that
1444 offsets.setDesiredOffsets(node.range, firstToken, variableIndent, true);
1446 offsets.setDesiredOffsets(node.range, firstToken, variableIndent);
1449 if (astUtils.isSemicolonToken(lastToken)) {
1450 offsets.ignoreToken(lastToken);
1454 VariableDeclarator(node) {
1456 const equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken);
1457 const tokenAfterOperator = sourceCode.getTokenAfter(equalOperator);
1459 offsets.ignoreToken(equalOperator);
1460 offsets.ignoreToken(tokenAfterOperator);
1461 offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1);
1462 offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0);
1466 "JSXAttribute[value]"(node) {
1467 const equalsToken = sourceCode.getFirstTokenBetween(node.name, node.value, token => token.type === "Punctuator" && token.value === "=");
1469 offsets.setDesiredOffsets([equalsToken.range[0], node.value.range[1]], sourceCode.getFirstToken(node.name), 1);
1473 if (node.closingElement) {
1474 addElementListIndent(node.children, sourceCode.getFirstToken(node.openingElement), sourceCode.getFirstToken(node.closingElement), 1);
1478 JSXOpeningElement(node) {
1479 const firstToken = sourceCode.getFirstToken(node);
1482 if (node.selfClosing) {
1483 closingToken = sourceCode.getLastToken(node, { skip: 1 });
1484 offsets.setDesiredOffset(sourceCode.getLastToken(node), closingToken, 0);
1486 closingToken = sourceCode.getLastToken(node);
1488 offsets.setDesiredOffsets(node.name.range, sourceCode.getFirstToken(node));
1489 addElementListIndent(node.attributes, firstToken, closingToken, 1);
1492 JSXClosingElement(node) {
1493 const firstToken = sourceCode.getFirstToken(node);
1495 offsets.setDesiredOffsets(node.name.range, firstToken, 1);
1499 const firstOpeningToken = sourceCode.getFirstToken(node.openingFragment);
1500 const firstClosingToken = sourceCode.getFirstToken(node.closingFragment);
1502 addElementListIndent(node.children, firstOpeningToken, firstClosingToken, 1);
1505 JSXOpeningFragment(node) {
1506 const firstToken = sourceCode.getFirstToken(node);
1507 const closingToken = sourceCode.getLastToken(node);
1509 offsets.setDesiredOffsets(node.range, firstToken, 1);
1510 offsets.matchOffsetOf(firstToken, closingToken);
1513 JSXClosingFragment(node) {
1514 const firstToken = sourceCode.getFirstToken(node);
1515 const slashToken = sourceCode.getLastToken(node, { skip: 1 });
1516 const closingToken = sourceCode.getLastToken(node);
1517 const tokenToMatch = astUtils.isTokenOnSameLine(slashToken, closingToken) ? slashToken : closingToken;
1519 offsets.setDesiredOffsets(node.range, firstToken, 1);
1520 offsets.matchOffsetOf(firstToken, tokenToMatch);
1523 JSXExpressionContainer(node) {
1524 const openingCurly = sourceCode.getFirstToken(node);
1525 const closingCurly = sourceCode.getLastToken(node);
1527 offsets.setDesiredOffsets(
1528 [openingCurly.range[1], closingCurly.range[0]],
1534 JSXSpreadAttribute(node) {
1535 const openingCurly = sourceCode.getFirstToken(node);
1536 const closingCurly = sourceCode.getLastToken(node);
1538 offsets.setDesiredOffsets(
1539 [openingCurly.range[1], closingCurly.range[0]],
1546 const firstToken = sourceCode.getFirstToken(node);
1548 // Ensure that the children of every node are indented at least as much as the first token.
1549 if (firstToken && !ignoredNodeFirstTokens.has(firstToken)) {
1550 offsets.setDesiredOffsets(node.range, firstToken, 0);
1555 const listenerCallQueue = [];
1558 * To ignore the indentation of a node:
1559 * 1. Don't call the node's listener when entering it (if it has a listener)
1560 * 2. Don't set any offsets against the first token of the node.
1561 * 3. Call `ignoreNode` on the node sometime after exiting it and before validating offsets.
1563 const offsetListeners = lodash.mapValues(
1564 baseOffsetListeners,
1567 * Offset listener calls are deferred until traversal is finished, and are called as
1568 * part of the final `Program:exit` listener. This is necessary because a node might
1569 * be matched by multiple selectors.
1571 * Example: Suppose there is an offset listener for `Identifier`, and the user has
1572 * specified in configuration that `MemberExpression > Identifier` should be ignored.
1573 * Due to selector specificity rules, the `Identifier` listener will get called first. However,
1574 * if a given Identifier node is supposed to be ignored, then the `Identifier` offset listener
1575 * should not have been called at all. Without doing extra selector matching, we don't know
1576 * whether the Identifier matches the `MemberExpression > Identifier` selector until the
1577 * `MemberExpression > Identifier` listener is called.
1579 * To avoid this, the `Identifier` listener isn't called until traversal finishes and all
1580 * ignored nodes are known.
1584 listenerCallQueue.push({ listener, node })
1587 // For each ignored node selector, set up a listener to collect it into the `ignoredNodes` set.
1588 const ignoredNodes = new Set();
1592 * @param {ASTNode} node The node to ignore
1595 function addToIgnoredNodes(node) {
1596 ignoredNodes.add(node);
1597 ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node));
1600 const ignoredNodeListeners = options.ignoredNodes.reduce(
1601 (listeners, ignoredSelector) => Object.assign(listeners, { [ignoredSelector]: addToIgnoredNodes }),
1606 * Join the listeners, and add a listener to verify that all tokens actually have the correct indentation
1609 * Using Object.assign will cause some offset listeners to be overwritten if the same selector also appears
1610 * in `ignoredNodeListeners`. This isn't a problem because all of the matching nodes will be ignored,
1611 * so those listeners wouldn't be called anyway.
1613 return Object.assign(
1615 ignoredNodeListeners,
1619 // If a node's type is nonstandard, we can't tell how its children should be offset, so ignore it.
1620 if (!KNOWN_NODES.has(node.type)) {
1621 addToIgnoredNodes(node);
1626 // If ignoreComments option is enabled, ignore all comment tokens.
1627 if (options.ignoreComments) {
1628 sourceCode.getAllComments()
1629 .forEach(comment => offsets.ignoreToken(comment));
1632 // Invoke the queued offset listeners for the nodes that aren't ignored.
1634 .filter(nodeInfo => !ignoredNodes.has(nodeInfo.node))
1635 .forEach(nodeInfo => nodeInfo.listener(nodeInfo.node));
1637 // Update the offsets for ignored nodes to prevent their child tokens from being reported.
1638 ignoredNodes.forEach(ignoreNode);
1640 addParensIndent(sourceCode.ast.tokens);
1643 * Create a Map from (tokenOrComment) => (precedingToken).
1644 * This is necessary because sourceCode.getTokenBefore does not handle a comment as an argument correctly.
1646 const precedingTokens = sourceCode.ast.comments.reduce((commentMap, comment) => {
1647 const tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { includeComments: true });
1649 return commentMap.set(comment, commentMap.has(tokenOrCommentBefore) ? commentMap.get(tokenOrCommentBefore) : tokenOrCommentBefore);
1652 sourceCode.lines.forEach((line, lineIndex) => {
1653 const lineNumber = lineIndex + 1;
1655 if (!tokenInfo.firstTokensByLineNumber.has(lineNumber)) {
1657 // Don't check indentation on blank lines
1661 const firstTokenOfLine = tokenInfo.firstTokensByLineNumber.get(lineNumber);
1663 if (firstTokenOfLine.loc.start.line !== lineNumber) {
1665 // Don't check the indentation of multi-line tokens (e.g. template literals or block comments) twice.
1669 if (astUtils.isCommentToken(firstTokenOfLine)) {
1670 const tokenBefore = precedingTokens.get(firstTokenOfLine);
1671 const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0];
1672 const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine);
1673 const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter);
1676 * If a comment precedes a line that begins with a semicolon token, align to that token, i.e.
1680 * ;(async () => {})()
1682 if (tokenAfter && astUtils.isSemicolonToken(tokenAfter) && !astUtils.isTokenOnSameLine(firstTokenOfLine, tokenAfter)) {
1683 offsets.setDesiredOffset(firstTokenOfLine, tokenAfter, 0);
1686 // If a comment matches the expected indentation of the token immediately before or after, don't report it.
1688 mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
1689 mayAlignWithAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))
1695 // If the token matches the expected indentation, don't report it.
1696 if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) {
1700 // Otherwise, report the token/comment.
1701 report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine));