oxlint-plugin-eslint 1.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +36 -0
- package/common/ast-utils.cjs +5843 -0
- package/common/chunk.cjs +9 -0
- package/common/eslint-utils.cjs +1633 -0
- package/common/fix-tracker.cjs +89 -0
- package/common/ignore.cjs +123 -0
- package/common/keywords.cjs +15 -0
- package/common/regexpp.cjs +1720 -0
- package/common/regular-expressions.cjs +40 -0
- package/common/string-utils.cjs +39 -0
- package/index.js +7477 -0
- package/package.json +34 -0
- package/rules/accessor-pairs.cjs +233 -0
- package/rules/array-bracket-newline.cjs +172 -0
- package/rules/array-bracket-spacing.cjs +184 -0
- package/rules/array-callback-return.cjs +262 -0
- package/rules/array-element-newline.cjs +178 -0
- package/rules/arrow-body-style.cjs +135 -0
- package/rules/arrow-parens.cjs +130 -0
- package/rules/arrow-spacing.cjs +129 -0
- package/rules/block-scoped-var.cjs +101 -0
- package/rules/block-spacing.cjs +125 -0
- package/rules/brace-style.cjs +141 -0
- package/rules/callback-return.cjs +117 -0
- package/rules/camelcase.cjs +194 -0
- package/rules/capitalized-comments.cjs +168 -0
- package/rules/class-methods-use-this.cjs +155 -0
- package/rules/comma-dangle.cjs +274 -0
- package/rules/comma-spacing.cjs +111 -0
- package/rules/comma-style.cjs +180 -0
- package/rules/complexity.cjs +100 -0
- package/rules/computed-property-spacing.cjs +155 -0
- package/rules/consistent-return.cjs +113 -0
- package/rules/consistent-this.cjs +109 -0
- package/rules/constructor-super.cjs +207 -0
- package/rules/curly.cjs +173 -0
- package/rules/default-case-last.cjs +36 -0
- package/rules/default-case.cjs +50 -0
- package/rules/default-param-last.cjs +61 -0
- package/rules/dot-location.cjs +87 -0
- package/rules/dot-notation.cjs +77 -0
- package/rules/eol-last.cjs +91 -0
- package/rules/eqeqeq.cjs +127 -0
- package/rules/for-direction.cjs +94 -0
- package/rules/func-call-spacing.cjs +127 -0
- package/rules/func-name-matching.cjs +138 -0
- package/rules/func-names.cjs +121 -0
- package/rules/func-style.cjs +103 -0
- package/rules/function-call-argument-newline.cjs +109 -0
- package/rules/function-paren-newline.cjs +173 -0
- package/rules/generator-star-spacing.cjs +174 -0
- package/rules/getter-return.cjs +129 -0
- package/rules/global-require.cjs +84 -0
- package/rules/grouped-accessor-pairs.cjs +146 -0
- package/rules/guard-for-in.cjs +39 -0
- package/rules/handle-callback-err.cjs +86 -0
- package/rules/id-blacklist.cjs +135 -0
- package/rules/id-denylist.cjs +126 -0
- package/rules/id-length.cjs +119 -0
- package/rules/id-match.cjs +139 -0
- package/rules/implicit-arrow-linebreak.cjs +76 -0
- package/rules/indent-legacy.cjs +520 -0
- package/rules/indent.cjs +842 -0
- package/rules/init-declarations.cjs +88 -0
- package/rules/jsx-quotes.cjs +84 -0
- package/rules/key-spacing.cjs +399 -0
- package/rules/keyword-spacing.cjs +464 -0
- package/rules/line-comment-position.cjs +75 -0
- package/rules/linebreak-style.cjs +89 -0
- package/rules/lines-around-comment.cjs +290 -0
- package/rules/lines-around-directive.cjs +129 -0
- package/rules/lines-between-class-members.cjs +212 -0
- package/rules/logical-assignment-operators.cjs +305 -0
- package/rules/max-classes-per-file.cjs +61 -0
- package/rules/max-depth.cjs +117 -0
- package/rules/max-len.cjs +256 -0
- package/rules/max-lines-per-function.cjs +116 -0
- package/rules/max-lines.cjs +109 -0
- package/rules/max-nested-callbacks.cjs +78 -0
- package/rules/max-params.cjs +78 -0
- package/rules/max-statements-per-line.cjs +147 -0
- package/rules/max-statements.cjs +119 -0
- package/rules/multiline-comment-style.cjs +319 -0
- package/rules/multiline-ternary.cjs +106 -0
- package/rules/new-cap.cjs +139 -0
- package/rules/new-parens.cjs +70 -0
- package/rules/newline-after-var.cjs +159 -0
- package/rules/newline-before-return.cjs +128 -0
- package/rules/newline-per-chained-call.cjs +100 -0
- package/rules/no-alert.cjs +84 -0
- package/rules/no-array-constructor.cjs +91 -0
- package/rules/no-async-promise-executor.cjs +33 -0
- package/rules/no-await-in-loop.cjs +75 -0
- package/rules/no-bitwise.cjs +106 -0
- package/rules/no-buffer-constructor.cjs +51 -0
- package/rules/no-caller.cjs +34 -0
- package/rules/no-case-declarations.cjs +56 -0
- package/rules/no-catch-shadow.cjs +57 -0
- package/rules/no-class-assign.cjs +55 -0
- package/rules/no-compare-neg-zero.cjs +52 -0
- package/rules/no-cond-assign.cjs +104 -0
- package/rules/no-confusing-arrow.cjs +87 -0
- package/rules/no-console.cjs +138 -0
- package/rules/no-const-assign.cjs +50 -0
- package/rules/no-constant-binary-expression.cjs +284 -0
- package/rules/no-constant-condition.cjs +119 -0
- package/rules/no-constructor-return.cjs +43 -0
- package/rules/no-continue.cjs +33 -0
- package/rules/no-control-regex.cjs +74 -0
- package/rules/no-debugger.cjs +33 -0
- package/rules/no-delete-var.cjs +32 -0
- package/rules/no-div-regex.cjs +39 -0
- package/rules/no-dupe-args.cjs +61 -0
- package/rules/no-dupe-class-members.cjs +77 -0
- package/rules/no-dupe-else-if.cjs +74 -0
- package/rules/no-dupe-keys.cjs +88 -0
- package/rules/no-duplicate-case.cjs +49 -0
- package/rules/no-duplicate-imports.cjs +166 -0
- package/rules/no-else-return.cjs +182 -0
- package/rules/no-empty-character-class.cjs +47 -0
- package/rules/no-empty-function.cjs +141 -0
- package/rules/no-empty-pattern.cjs +50 -0
- package/rules/no-empty-static-block.cjs +50 -0
- package/rules/no-empty.cjs +78 -0
- package/rules/no-eq-null.cjs +34 -0
- package/rules/no-eval.cjs +165 -0
- package/rules/no-ex-assign.cjs +45 -0
- package/rules/no-extend-native.cjs +103 -0
- package/rules/no-extra-bind.cjs +127 -0
- package/rules/no-extra-boolean-cast.cjs +159 -0
- package/rules/no-extra-label.cjs +116 -0
- package/rules/no-extra-parens.cjs +665 -0
- package/rules/no-extra-semi.cjs +103 -0
- package/rules/no-fallthrough.cjs +136 -0
- package/rules/no-floating-decimal.cjs +66 -0
- package/rules/no-func-assign.cjs +63 -0
- package/rules/no-global-assign.cjs +62 -0
- package/rules/no-implicit-coercion.cjs +219 -0
- package/rules/no-implicit-globals.cjs +77 -0
- package/rules/no-implied-eval.cjs +87 -0
- package/rules/no-import-assign.cjs +123 -0
- package/rules/no-inline-comments.cjs +57 -0
- package/rules/no-inner-declarations.cjs +82 -0
- package/rules/no-invalid-regexp.cjs +130 -0
- package/rules/no-invalid-this.cjs +90 -0
- package/rules/no-irregular-whitespace.cjs +157 -0
- package/rules/no-iterator.cjs +34 -0
- package/rules/no-label-var.cjs +46 -0
- package/rules/no-labels.cjs +106 -0
- package/rules/no-lone-blocks.cjs +75 -0
- package/rules/no-lonely-if.cjs +42 -0
- package/rules/no-loop-func.cjs +143 -0
- package/rules/no-loss-of-precision.cjs +160 -0
- package/rules/no-magic-numbers.cjs +222 -0
- package/rules/no-misleading-character-class.cjs +500 -0
- package/rules/no-mixed-operators.cjs +197 -0
- package/rules/no-mixed-requires.cjs +117 -0
- package/rules/no-mixed-spaces-and-tabs.cjs +87 -0
- package/rules/no-multi-assign.cjs +38 -0
- package/rules/no-multi-spaces.cjs +95 -0
- package/rules/no-multi-str.cjs +44 -0
- package/rules/no-multiple-empty-lines.cjs +112 -0
- package/rules/no-native-reassign.cjs +72 -0
- package/rules/no-negated-condition.cjs +77 -0
- package/rules/no-negated-in-lhs.cjs +43 -0
- package/rules/no-nested-ternary.cjs +33 -0
- package/rules/no-new-func.cjs +50 -0
- package/rules/no-new-native-nonconstructor.cjs +42 -0
- package/rules/no-new-object.cjs +47 -0
- package/rules/no-new-require.cjs +50 -0
- package/rules/no-new-symbol.cjs +48 -0
- package/rules/no-new-wrappers.cjs +44 -0
- package/rules/no-new.cjs +33 -0
- package/rules/no-nonoctal-decimal-escape.cjs +80 -0
- package/rules/no-obj-calls.cjs +64 -0
- package/rules/no-object-constructor.cjs +69 -0
- package/rules/no-octal-escape.cjs +35 -0
- package/rules/no-octal.cjs +32 -0
- package/rules/no-param-reassign.cjs +135 -0
- package/rules/no-path-concat.cjs +52 -0
- package/rules/no-plusplus.cjs +64 -0
- package/rules/no-process-env.cjs +51 -0
- package/rules/no-process-exit.cjs +50 -0
- package/rules/no-promise-executor-return.cjs +131 -0
- package/rules/no-proto.cjs +34 -0
- package/rules/no-prototype-builtins.cjs +85 -0
- package/rules/no-redeclare.cjs +103 -0
- package/rules/no-regex-spaces.cjs +115 -0
- package/rules/no-restricted-exports.cjs +117 -0
- package/rules/no-restricted-globals.cjs +147 -0
- package/rules/no-restricted-imports.cjs +421 -0
- package/rules/no-restricted-modules.cjs +144 -0
- package/rules/no-restricted-properties.cjs +123 -0
- package/rules/no-restricted-syntax.cjs +49 -0
- package/rules/no-return-assign.cjs +45 -0
- package/rules/no-return-await.cjs +88 -0
- package/rules/no-script-url.cjs +49 -0
- package/rules/no-self-assign.cjs +88 -0
- package/rules/no-self-compare.cjs +53 -0
- package/rules/no-sequences.cjs +81 -0
- package/rules/no-setter-return.cjs +110 -0
- package/rules/no-shadow-restricted-names.cjs +60 -0
- package/rules/no-shadow.cjs +302 -0
- package/rules/no-spaced-func.cjs +69 -0
- package/rules/no-sparse-arrays.cjs +40 -0
- package/rules/no-sync.cjs +58 -0
- package/rules/no-tabs.cjs +73 -0
- package/rules/no-template-curly-in-string.cjs +33 -0
- package/rules/no-ternary.cjs +33 -0
- package/rules/no-this-before-super.cjs +177 -0
- package/rules/no-throw-literal.cjs +40 -0
- package/rules/no-trailing-spaces.cjs +123 -0
- package/rules/no-unassigned-vars.cjs +54 -0
- package/rules/no-undef-init.cjs +46 -0
- package/rules/no-undef.cjs +51 -0
- package/rules/no-undefined.cjs +58 -0
- package/rules/no-underscore-dangle.cjs +211 -0
- package/rules/no-unexpected-multiline.cjs +76 -0
- package/rules/no-unmodified-loop-condition.cjs +849 -0
- package/rules/no-unneeded-ternary.cjs +112 -0
- package/rules/no-unreachable-loop.cjs +128 -0
- package/rules/no-unreachable.cjs +195 -0
- package/rules/no-unsafe-finally.cjs +67 -0
- package/rules/no-unsafe-negation.cjs +86 -0
- package/rules/no-unsafe-optional-chaining.cjs +164 -0
- package/rules/no-unused-expressions.cjs +163 -0
- package/rules/no-unused-labels.cjs +97 -0
- package/rules/no-unused-private-class-members.cjs +69 -0
- package/rules/no-unused-vars.cjs +639 -0
- package/rules/no-use-before-define.cjs +245 -0
- package/rules/no-useless-assignment.cjs +250 -0
- package/rules/no-useless-backreference.cjs +137 -0
- package/rules/no-useless-call.cjs +57 -0
- package/rules/no-useless-catch.cjs +38 -0
- package/rules/no-useless-computed-key.cjs +106 -0
- package/rules/no-useless-concat.cjs +78 -0
- package/rules/no-useless-constructor.cjs +152 -0
- package/rules/no-useless-escape.cjs +164 -0
- package/rules/no-useless-rename.cjs +94 -0
- package/rules/no-useless-return.cjs +229 -0
- package/rules/no-var.cjs +235 -0
- package/rules/no-void.cjs +39 -0
- package/rules/no-warning-comments.cjs +112 -0
- package/rules/no-whitespace-before-property.cjs +72 -0
- package/rules/no-with.cjs +32 -0
- package/rules/nonblock-statement-body-position.cjs +107 -0
- package/rules/object-curly-newline.cjs +194 -0
- package/rules/object-curly-spacing.cjs +225 -0
- package/rules/object-property-newline.cjs +81 -0
- package/rules/object-shorthand.cjs +284 -0
- package/rules/one-var-declaration-per-line.cjs +75 -0
- package/rules/one-var.cjs +378 -0
- package/rules/operator-assignment.cjs +146 -0
- package/rules/operator-linebreak.cjs +150 -0
- package/rules/padded-blocks.cjs +192 -0
- package/rules/padding-line-between-statements.cjs +377 -0
- package/rules/prefer-arrow-callback.cjs +185 -0
- package/rules/prefer-const.cjs +240 -0
- package/rules/prefer-destructuring.cjs +188 -0
- package/rules/prefer-exponentiation-operator.cjs +100 -0
- package/rules/prefer-named-capture-group.cjs +111 -0
- package/rules/prefer-numeric-literals.cjs +75 -0
- package/rules/prefer-object-has-own.cjs +54 -0
- package/rules/prefer-object-spread.cjs +170 -0
- package/rules/prefer-promise-reject-errors.cjs +62 -0
- package/rules/prefer-reflect.cjs +100 -0
- package/rules/prefer-regex-literals.cjs +256 -0
- package/rules/prefer-rest-params.cjs +80 -0
- package/rules/prefer-spread.cjs +57 -0
- package/rules/prefer-template.cjs +147 -0
- package/rules/preserve-caught-error.cjs +196 -0
- package/rules/quote-props.cjs +224 -0
- package/rules/quotes.cjs +187 -0
- package/rules/radix.cjs +124 -0
- package/rules/require-atomic-updates.cjs +197 -0
- package/rules/require-await.cjs +90 -0
- package/rules/require-unicode-regexp.cjs +101 -0
- package/rules/require-yield.cjs +56 -0
- package/rules/rest-spread-spacing.cjs +99 -0
- package/rules/semi-spacing.cjs +215 -0
- package/rules/semi-style.cjs +116 -0
- package/rules/semi.cjs +239 -0
- package/rules/sort-imports.cjs +140 -0
- package/rules/sort-keys.cjs +165 -0
- package/rules/sort-vars.cjs +50 -0
- package/rules/space-before-blocks.cjs +132 -0
- package/rules/space-before-function-paren.cjs +126 -0
- package/rules/space-in-parens.cjs +175 -0
- package/rules/space-infix-ops.cjs +133 -0
- package/rules/space-unary-ops.cjs +237 -0
- package/rules/spaced-comment.cjs +233 -0
- package/rules/strict.cjs +206 -0
- package/rules/switch-colon-spacing.cjs +114 -0
- package/rules/symbol-description.cjs +49 -0
- package/rules/template-curly-spacing.cjs +112 -0
- package/rules/template-tag-spacing.cjs +82 -0
- package/rules/unicode-bom.cjs +52 -0
- package/rules/use-isnan.cjs +144 -0
- package/rules/valid-typeof.cjs +104 -0
- package/rules/vars-on-top.cjs +94 -0
- package/rules/wrap-iife.cjs +131 -0
- package/rules/wrap-regex.cjs +57 -0
- package/rules/yield-star-spacing.cjs +115 -0
- package/rules/yoda.cjs +185 -0
package/rules/indent.cjs
ADDED
|
@@ -0,0 +1,842 @@
|
|
|
1
|
+
const require_chunk = require("../common/chunk.cjs"), require_ast_utils$1 = require("../common/ast-utils.cjs");
|
|
2
|
+
//#region ../../node_modules/.pnpm/eslint@9.39.3_jiti@2.6.1/node_modules/eslint/lib/rules/indent.js
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview This rule sets a specific indentation style and width for your code
|
|
5
|
+
*
|
|
6
|
+
* @author Teddy Katz
|
|
7
|
+
* @author Vitaly Puzrin
|
|
8
|
+
* @author Gyandeep Singh
|
|
9
|
+
* @deprecated in ESLint v8.53.0
|
|
10
|
+
*/
|
|
11
|
+
var require_indent = /* @__PURE__ */ require_chunk.t(((exports, module) => {
|
|
12
|
+
let astUtils = require_ast_utils$1.t(), KNOWN_NODES = new Set(/* @__PURE__ */ "AssignmentExpression.AssignmentPattern.ArrayExpression.ArrayPattern.ArrowFunctionExpression.AwaitExpression.BlockStatement.BinaryExpression.BreakStatement.CallExpression.CatchClause.ChainExpression.ClassBody.ClassDeclaration.ClassExpression.ConditionalExpression.ContinueStatement.DoWhileStatement.DebuggerStatement.EmptyStatement.ExperimentalRestProperty.ExperimentalSpreadProperty.ExpressionStatement.ForStatement.ForInStatement.ForOfStatement.FunctionDeclaration.FunctionExpression.Identifier.IfStatement.Literal.LabeledStatement.LogicalExpression.MemberExpression.MetaProperty.MethodDefinition.NewExpression.ObjectExpression.ObjectPattern.PrivateIdentifier.Program.Property.PropertyDefinition.RestElement.ReturnStatement.SequenceExpression.SpreadElement.StaticBlock.Super.SwitchCase.SwitchStatement.TaggedTemplateExpression.TemplateElement.TemplateLiteral.ThisExpression.ThrowStatement.TryStatement.UnaryExpression.UpdateExpression.VariableDeclaration.VariableDeclarator.WhileStatement.WithStatement.YieldExpression.JSXFragment.JSXOpeningFragment.JSXClosingFragment.JSXIdentifier.JSXNamespacedName.JSXMemberExpression.JSXEmptyExpression.JSXExpressionContainer.JSXElement.JSXClosingElement.JSXOpeningElement.JSXAttribute.JSXSpreadAttribute.JSXText.ExportDefaultDeclaration.ExportNamedDeclaration.ExportAllDeclaration.ExportSpecifier.ImportDeclaration.ImportSpecifier.ImportDefaultSpecifier.ImportNamespaceSpecifier.ImportExpression".split("."));
|
|
13
|
+
/**
|
|
14
|
+
* A mutable map that stores (key, value) pairs. The keys are numeric indices, and must be unique.
|
|
15
|
+
* This is intended to be a generic wrapper around a map with non-negative integer keys, so that the underlying implementation
|
|
16
|
+
* can easily be swapped out.
|
|
17
|
+
*/
|
|
18
|
+
var IndexMap = class {
|
|
19
|
+
/**
|
|
20
|
+
* Creates an empty map
|
|
21
|
+
* @param {number} maxKey The maximum key
|
|
22
|
+
*/
|
|
23
|
+
constructor(maxKey) {
|
|
24
|
+
this._values = Array(maxKey + 1);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Inserts an entry into the map.
|
|
28
|
+
* @param {number} key The entry's key
|
|
29
|
+
* @param {any} value The entry's value
|
|
30
|
+
* @returns {void}
|
|
31
|
+
*/
|
|
32
|
+
insert(key, value) {
|
|
33
|
+
this._values[key] = value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Finds the value of the entry with the largest key less than or equal to the provided key
|
|
37
|
+
* @param {number} key The provided key
|
|
38
|
+
* @returns {*|undefined} The value of the found entry, or undefined if no such entry exists.
|
|
39
|
+
*/
|
|
40
|
+
findLastNotAfter(key) {
|
|
41
|
+
let values = this._values;
|
|
42
|
+
for (let index = key; index >= 0; index--) {
|
|
43
|
+
let value = values[index];
|
|
44
|
+
if (value) return value;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Deletes all of the keys in the interval [start, end)
|
|
49
|
+
* @param {number} start The start of the range
|
|
50
|
+
* @param {number} end The end of the range
|
|
51
|
+
* @returns {void}
|
|
52
|
+
*/
|
|
53
|
+
deleteRange(start, end) {
|
|
54
|
+
this._values.fill(void 0, start, end);
|
|
55
|
+
}
|
|
56
|
+
}, TokenInfo = class {
|
|
57
|
+
/**
|
|
58
|
+
* @param {SourceCode} sourceCode A SourceCode object
|
|
59
|
+
*/
|
|
60
|
+
constructor(sourceCode) {
|
|
61
|
+
this.sourceCode = sourceCode, this.firstTokensByLineNumber = /* @__PURE__ */ new Map();
|
|
62
|
+
let tokens = sourceCode.tokensAndComments;
|
|
63
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
64
|
+
let token = tokens[i];
|
|
65
|
+
this.firstTokensByLineNumber.has(token.loc.start.line) || this.firstTokensByLineNumber.set(token.loc.start.line, token), !this.firstTokensByLineNumber.has(token.loc.end.line) && sourceCode.text.slice(token.range[1] - token.loc.end.column, token.range[1]).trim() && this.firstTokensByLineNumber.set(token.loc.end.line, token);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Gets the first token on a given token's line
|
|
70
|
+
* @param {Token|ASTNode} token a node or token
|
|
71
|
+
* @returns {Token} The first token on the given line
|
|
72
|
+
*/
|
|
73
|
+
getFirstTokenOfLine(token) {
|
|
74
|
+
return this.firstTokensByLineNumber.get(token.loc.start.line);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Determines whether a token is the first token in its line
|
|
78
|
+
* @param {Token} token The token
|
|
79
|
+
* @returns {boolean} `true` if the token is the first on its line
|
|
80
|
+
*/
|
|
81
|
+
isFirstTokenOfLine(token) {
|
|
82
|
+
return this.getFirstTokenOfLine(token) === token;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the actual indent of a token
|
|
86
|
+
* @param {Token} token Token to examine. This should be the first token on its line.
|
|
87
|
+
* @returns {string} The indentation characters that precede the token
|
|
88
|
+
*/
|
|
89
|
+
getTokenIndent(token) {
|
|
90
|
+
return this.sourceCode.text.slice(token.range[0] - token.loc.start.column, token.range[0]);
|
|
91
|
+
}
|
|
92
|
+
}, OffsetStorage = class {
|
|
93
|
+
/**
|
|
94
|
+
* @param {TokenInfo} tokenInfo a TokenInfo instance
|
|
95
|
+
* @param {number} indentSize The desired size of each indentation level
|
|
96
|
+
* @param {string} indentType The indentation character
|
|
97
|
+
* @param {number} maxIndex The maximum end index of any token
|
|
98
|
+
*/
|
|
99
|
+
constructor(tokenInfo, indentSize, indentType, maxIndex) {
|
|
100
|
+
this._tokenInfo = tokenInfo, this._indentSize = indentSize, this._indentType = indentType, this._indexMap = new IndexMap(maxIndex), this._indexMap.insert(0, {
|
|
101
|
+
offset: 0,
|
|
102
|
+
from: null,
|
|
103
|
+
force: !1
|
|
104
|
+
}), this._lockedFirstTokens = /* @__PURE__ */ new WeakMap(), this._desiredIndentCache = /* @__PURE__ */ new WeakMap(), this._ignoredTokens = /* @__PURE__ */ new WeakSet();
|
|
105
|
+
}
|
|
106
|
+
_getOffsetDescriptor(token) {
|
|
107
|
+
return this._indexMap.findLastNotAfter(token.range[0]);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Sets the offset column of token B to match the offset column of token A.
|
|
111
|
+
* - **WARNING**: This matches a *column*, even if baseToken is not the first token on its line. In
|
|
112
|
+
* most cases, `setDesiredOffset` should be used instead.
|
|
113
|
+
* @param {Token} baseToken The first token
|
|
114
|
+
* @param {Token} offsetToken The second token, whose offset should be matched to the first token
|
|
115
|
+
* @returns {void}
|
|
116
|
+
*/
|
|
117
|
+
matchOffsetOf(baseToken, offsetToken) {
|
|
118
|
+
this._lockedFirstTokens.set(offsetToken, baseToken);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Sets the desired offset of a token.
|
|
122
|
+
*
|
|
123
|
+
* This uses a line-based offset collapsing behavior to handle tokens on the same line.
|
|
124
|
+
* For example, consider the following two cases:
|
|
125
|
+
*
|
|
126
|
+
* (
|
|
127
|
+
* [
|
|
128
|
+
* bar
|
|
129
|
+
* ]
|
|
130
|
+
* )
|
|
131
|
+
*
|
|
132
|
+
* ([
|
|
133
|
+
* bar
|
|
134
|
+
* ])
|
|
135
|
+
*
|
|
136
|
+
* Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from
|
|
137
|
+
* the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is
|
|
138
|
+
* the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces)
|
|
139
|
+
* from the start of its line.
|
|
140
|
+
*
|
|
141
|
+
* However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level
|
|
142
|
+
* between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the
|
|
143
|
+
* `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented
|
|
144
|
+
* by 1 indent level from the start of the line.
|
|
145
|
+
*
|
|
146
|
+
* This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node,
|
|
147
|
+
* without needing to check which lines those tokens are on.
|
|
148
|
+
*
|
|
149
|
+
* Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive
|
|
150
|
+
* behavior can occur. For example, consider the following cases:
|
|
151
|
+
*
|
|
152
|
+
* foo(
|
|
153
|
+
* ).
|
|
154
|
+
* bar(
|
|
155
|
+
* baz
|
|
156
|
+
* )
|
|
157
|
+
*
|
|
158
|
+
* foo(
|
|
159
|
+
* ).bar(
|
|
160
|
+
* baz
|
|
161
|
+
* )
|
|
162
|
+
*
|
|
163
|
+
* Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz`
|
|
164
|
+
* should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz`
|
|
165
|
+
* being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no
|
|
166
|
+
* collapsing would occur).
|
|
167
|
+
*
|
|
168
|
+
* Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and
|
|
169
|
+
* offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed
|
|
170
|
+
* in the second case.
|
|
171
|
+
* @param {Token} token The token
|
|
172
|
+
* @param {Token} fromToken The token that `token` should be offset from
|
|
173
|
+
* @param {number} offset The desired indent level
|
|
174
|
+
* @returns {void}
|
|
175
|
+
*/
|
|
176
|
+
setDesiredOffset(token, fromToken, offset) {
|
|
177
|
+
return this.setDesiredOffsets(token.range, fromToken, offset);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Sets the desired offset of all tokens in a range
|
|
181
|
+
* It's common for node listeners in this file to need to apply the same offset to a large, contiguous range of tokens.
|
|
182
|
+
* Moreover, the offset of any given token is usually updated multiple times (roughly once for each node that contains
|
|
183
|
+
* it). This means that the offset of each token is updated O(AST depth) times.
|
|
184
|
+
* It would not be performant to store and update the offsets for each token independently, because the rule would end
|
|
185
|
+
* up having a time complexity of O(number of tokens * AST depth), which is quite slow for large files.
|
|
186
|
+
*
|
|
187
|
+
* Instead, the offset tree is represented as a collection of contiguous offset ranges in a file. For example, the following
|
|
188
|
+
* list could represent the state of the offset tree at a given point:
|
|
189
|
+
*
|
|
190
|
+
* - Tokens starting in the interval [0, 15) are aligned with the beginning of the file
|
|
191
|
+
* - Tokens starting in the interval [15, 30) are offset by 1 indent level from the `bar` token
|
|
192
|
+
* - Tokens starting in the interval [30, 43) are offset by 1 indent level from the `foo` token
|
|
193
|
+
* - Tokens starting in the interval [43, 820) are offset by 2 indent levels from the `bar` token
|
|
194
|
+
* - Tokens starting in the interval [820, ∞) are offset by 1 indent level from the `baz` token
|
|
195
|
+
*
|
|
196
|
+
* The `setDesiredOffsets` methods inserts ranges like the ones above. The third line above would be inserted by using:
|
|
197
|
+
* `setDesiredOffsets([30, 43], fooToken, 1);`
|
|
198
|
+
* @param {[number, number]} range A [start, end] pair. All tokens with range[0] <= token.start < range[1] will have the offset applied.
|
|
199
|
+
* @param {Token} fromToken The token that this is offset from
|
|
200
|
+
* @param {number} offset The desired indent level
|
|
201
|
+
* @param {boolean} force `true` if this offset should not use the normal collapsing behavior. This should almost always be false.
|
|
202
|
+
* @returns {void}
|
|
203
|
+
*/
|
|
204
|
+
setDesiredOffsets(range, fromToken, offset, force) {
|
|
205
|
+
let descriptorToInsert = {
|
|
206
|
+
offset,
|
|
207
|
+
from: fromToken,
|
|
208
|
+
force
|
|
209
|
+
}, descriptorAfterRange = this._indexMap.findLastNotAfter(range[1]), fromTokenIsInRange = fromToken && fromToken.range[0] >= range[0] && fromToken.range[1] <= range[1], fromTokenDescriptor = fromTokenIsInRange && this._getOffsetDescriptor(fromToken);
|
|
210
|
+
this._indexMap.deleteRange(range[0] + 1, range[1]), this._indexMap.insert(range[0], descriptorToInsert), fromTokenIsInRange && (this._indexMap.insert(fromToken.range[0], fromTokenDescriptor), this._indexMap.insert(fromToken.range[1], descriptorToInsert)), this._indexMap.insert(range[1], descriptorAfterRange);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Gets the desired indent of a token
|
|
214
|
+
* @param {Token} token The token
|
|
215
|
+
* @returns {string} The desired indent of the token
|
|
216
|
+
*/
|
|
217
|
+
getDesiredIndent(token) {
|
|
218
|
+
if (!this._desiredIndentCache.has(token)) if (this._ignoredTokens.has(token)) this._desiredIndentCache.set(token, this._tokenInfo.getTokenIndent(token));
|
|
219
|
+
else if (this._lockedFirstTokens.has(token)) {
|
|
220
|
+
let firstToken = this._lockedFirstTokens.get(token);
|
|
221
|
+
this._desiredIndentCache.set(token, this.getDesiredIndent(this._tokenInfo.getFirstTokenOfLine(firstToken)) + this._indentType.repeat(firstToken.loc.start.column - this._tokenInfo.getFirstTokenOfLine(firstToken).loc.start.column));
|
|
222
|
+
} else {
|
|
223
|
+
let offsetInfo = this._getOffsetDescriptor(token), offset = offsetInfo.from && offsetInfo.from.loc.start.line === token.loc.start.line && !/^\s*?\n/u.test(token.value) && !offsetInfo.force ? 0 : offsetInfo.offset * this._indentSize;
|
|
224
|
+
this._desiredIndentCache.set(token, (offsetInfo.from ? this.getDesiredIndent(offsetInfo.from) : "") + this._indentType.repeat(offset));
|
|
225
|
+
}
|
|
226
|
+
return this._desiredIndentCache.get(token);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Ignores a token, preventing it from being reported.
|
|
230
|
+
* @param {Token} token The token
|
|
231
|
+
* @returns {void}
|
|
232
|
+
*/
|
|
233
|
+
ignoreToken(token) {
|
|
234
|
+
this._tokenInfo.isFirstTokenOfLine(token) && this._ignoredTokens.add(token);
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Gets the first token that the given token's indentation is dependent on
|
|
238
|
+
* @param {Token} token The token
|
|
239
|
+
* @returns {Token} The token that the given token depends on, or `null` if the given token is at the top level
|
|
240
|
+
*/
|
|
241
|
+
getFirstDependency(token) {
|
|
242
|
+
return this._getOffsetDescriptor(token).from;
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
let ELEMENT_LIST_SCHEMA = { oneOf: [{
|
|
246
|
+
type: "integer",
|
|
247
|
+
minimum: 0
|
|
248
|
+
}, { enum: ["first", "off"] }] };
|
|
249
|
+
/** @type {import('../types').Rule.RuleModule} */
|
|
250
|
+
module.exports = {
|
|
251
|
+
meta: {
|
|
252
|
+
deprecated: {
|
|
253
|
+
message: "Formatting rules are being moved out of ESLint core.",
|
|
254
|
+
url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
|
|
255
|
+
deprecatedSince: "8.53.0",
|
|
256
|
+
availableUntil: "11.0.0",
|
|
257
|
+
replacedBy: [{
|
|
258
|
+
message: "ESLint Stylistic now maintains deprecated stylistic core rules.",
|
|
259
|
+
url: "https://eslint.style/guide/migration",
|
|
260
|
+
plugin: {
|
|
261
|
+
name: "@stylistic/eslint-plugin",
|
|
262
|
+
url: "https://eslint.style"
|
|
263
|
+
},
|
|
264
|
+
rule: {
|
|
265
|
+
name: "indent",
|
|
266
|
+
url: "https://eslint.style/rules/indent"
|
|
267
|
+
}
|
|
268
|
+
}]
|
|
269
|
+
},
|
|
270
|
+
type: "layout",
|
|
271
|
+
docs: {
|
|
272
|
+
description: "Enforce consistent indentation",
|
|
273
|
+
recommended: !1,
|
|
274
|
+
url: "https://eslint.org/docs/latest/rules/indent"
|
|
275
|
+
},
|
|
276
|
+
fixable: "whitespace",
|
|
277
|
+
schema: [{ oneOf: [{ enum: ["tab"] }, {
|
|
278
|
+
type: "integer",
|
|
279
|
+
minimum: 0
|
|
280
|
+
}] }, {
|
|
281
|
+
type: "object",
|
|
282
|
+
properties: {
|
|
283
|
+
SwitchCase: {
|
|
284
|
+
type: "integer",
|
|
285
|
+
minimum: 0,
|
|
286
|
+
default: 0
|
|
287
|
+
},
|
|
288
|
+
VariableDeclarator: { oneOf: [ELEMENT_LIST_SCHEMA, {
|
|
289
|
+
type: "object",
|
|
290
|
+
properties: {
|
|
291
|
+
var: ELEMENT_LIST_SCHEMA,
|
|
292
|
+
let: ELEMENT_LIST_SCHEMA,
|
|
293
|
+
const: ELEMENT_LIST_SCHEMA
|
|
294
|
+
},
|
|
295
|
+
additionalProperties: !1
|
|
296
|
+
}] },
|
|
297
|
+
outerIIFEBody: { oneOf: [{
|
|
298
|
+
type: "integer",
|
|
299
|
+
minimum: 0
|
|
300
|
+
}, { enum: ["off"] }] },
|
|
301
|
+
MemberExpression: { oneOf: [{
|
|
302
|
+
type: "integer",
|
|
303
|
+
minimum: 0
|
|
304
|
+
}, { enum: ["off"] }] },
|
|
305
|
+
FunctionDeclaration: {
|
|
306
|
+
type: "object",
|
|
307
|
+
properties: {
|
|
308
|
+
parameters: ELEMENT_LIST_SCHEMA,
|
|
309
|
+
body: {
|
|
310
|
+
type: "integer",
|
|
311
|
+
minimum: 0
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
additionalProperties: !1
|
|
315
|
+
},
|
|
316
|
+
FunctionExpression: {
|
|
317
|
+
type: "object",
|
|
318
|
+
properties: {
|
|
319
|
+
parameters: ELEMENT_LIST_SCHEMA,
|
|
320
|
+
body: {
|
|
321
|
+
type: "integer",
|
|
322
|
+
minimum: 0
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
additionalProperties: !1
|
|
326
|
+
},
|
|
327
|
+
StaticBlock: {
|
|
328
|
+
type: "object",
|
|
329
|
+
properties: { body: {
|
|
330
|
+
type: "integer",
|
|
331
|
+
minimum: 0
|
|
332
|
+
} },
|
|
333
|
+
additionalProperties: !1
|
|
334
|
+
},
|
|
335
|
+
CallExpression: {
|
|
336
|
+
type: "object",
|
|
337
|
+
properties: { arguments: ELEMENT_LIST_SCHEMA },
|
|
338
|
+
additionalProperties: !1
|
|
339
|
+
},
|
|
340
|
+
ArrayExpression: ELEMENT_LIST_SCHEMA,
|
|
341
|
+
ObjectExpression: ELEMENT_LIST_SCHEMA,
|
|
342
|
+
ImportDeclaration: ELEMENT_LIST_SCHEMA,
|
|
343
|
+
flatTernaryExpressions: {
|
|
344
|
+
type: "boolean",
|
|
345
|
+
default: !1
|
|
346
|
+
},
|
|
347
|
+
offsetTernaryExpressions: {
|
|
348
|
+
type: "boolean",
|
|
349
|
+
default: !1
|
|
350
|
+
},
|
|
351
|
+
ignoredNodes: {
|
|
352
|
+
type: "array",
|
|
353
|
+
items: {
|
|
354
|
+
type: "string",
|
|
355
|
+
not: { pattern: ":exit$" }
|
|
356
|
+
}
|
|
357
|
+
},
|
|
358
|
+
ignoreComments: {
|
|
359
|
+
type: "boolean",
|
|
360
|
+
default: !1
|
|
361
|
+
}
|
|
362
|
+
},
|
|
363
|
+
additionalProperties: !1
|
|
364
|
+
}],
|
|
365
|
+
messages: { wrongIndentation: "Expected indentation of {{expected}} but found {{actual}}." }
|
|
366
|
+
},
|
|
367
|
+
create(context) {
|
|
368
|
+
let indentType = "space", indentSize = 4, options = {
|
|
369
|
+
SwitchCase: 0,
|
|
370
|
+
VariableDeclarator: {
|
|
371
|
+
var: 1,
|
|
372
|
+
let: 1,
|
|
373
|
+
const: 1
|
|
374
|
+
},
|
|
375
|
+
outerIIFEBody: 1,
|
|
376
|
+
FunctionDeclaration: {
|
|
377
|
+
parameters: 1,
|
|
378
|
+
body: 1
|
|
379
|
+
},
|
|
380
|
+
FunctionExpression: {
|
|
381
|
+
parameters: 1,
|
|
382
|
+
body: 1
|
|
383
|
+
},
|
|
384
|
+
StaticBlock: { body: 1 },
|
|
385
|
+
CallExpression: { arguments: 1 },
|
|
386
|
+
MemberExpression: 1,
|
|
387
|
+
ArrayExpression: 1,
|
|
388
|
+
ObjectExpression: 1,
|
|
389
|
+
ImportDeclaration: 1,
|
|
390
|
+
flatTernaryExpressions: !1,
|
|
391
|
+
ignoredNodes: [],
|
|
392
|
+
ignoreComments: !1
|
|
393
|
+
};
|
|
394
|
+
context.options.length && (context.options[0] === "tab" ? (indentSize = 1, indentType = "tab") : (indentSize = context.options[0], indentType = "space"), context.options[1] && (Object.assign(options, context.options[1]), (typeof options.VariableDeclarator == "number" || options.VariableDeclarator === "first") && (options.VariableDeclarator = {
|
|
395
|
+
var: options.VariableDeclarator,
|
|
396
|
+
let: options.VariableDeclarator,
|
|
397
|
+
const: options.VariableDeclarator
|
|
398
|
+
})));
|
|
399
|
+
let sourceCode = context.sourceCode, tokenInfo = new TokenInfo(sourceCode), offsets = new OffsetStorage(tokenInfo, indentSize, indentType === "space" ? " " : " ", sourceCode.text.length), parameterParens = /* @__PURE__ */ new WeakSet();
|
|
400
|
+
/**
|
|
401
|
+
* Creates an error message for a line, given the expected/actual indentation.
|
|
402
|
+
* @param {number} expectedAmount The expected amount of indentation characters for this line
|
|
403
|
+
* @param {number} actualSpaces The actual number of indentation spaces that were found on this line
|
|
404
|
+
* @param {number} actualTabs The actual number of indentation tabs that were found on this line
|
|
405
|
+
* @returns {string} An error message for this line
|
|
406
|
+
*/
|
|
407
|
+
function createErrorMessageData(expectedAmount, actualSpaces, actualTabs) {
|
|
408
|
+
let expectedStatement = `${expectedAmount} ${indentType}${expectedAmount === 1 ? "" : "s"}`, foundSpacesWord = `space${actualSpaces === 1 ? "" : "s"}`, foundTabsWord = `tab${actualTabs === 1 ? "" : "s"}`, foundStatement;
|
|
409
|
+
return foundStatement = actualSpaces > 0 ? indentType === "space" ? actualSpaces : `${actualSpaces} ${foundSpacesWord}` : actualTabs > 0 ? indentType === "tab" ? actualTabs : `${actualTabs} ${foundTabsWord}` : "0", {
|
|
410
|
+
expected: expectedStatement,
|
|
411
|
+
actual: foundStatement
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Reports a given indent violation
|
|
416
|
+
* @param {Token} token Token violating the indent rule
|
|
417
|
+
* @param {string} neededIndent Expected indentation string
|
|
418
|
+
* @returns {void}
|
|
419
|
+
*/
|
|
420
|
+
function report(token, neededIndent) {
|
|
421
|
+
let actualIndent = Array.from(tokenInfo.getTokenIndent(token)), numSpaces = actualIndent.filter((char) => char === " ").length, numTabs = actualIndent.filter((char) => char === " ").length;
|
|
422
|
+
context.report({
|
|
423
|
+
node: token,
|
|
424
|
+
messageId: "wrongIndentation",
|
|
425
|
+
data: createErrorMessageData(neededIndent.length, numSpaces, numTabs),
|
|
426
|
+
loc: {
|
|
427
|
+
start: {
|
|
428
|
+
line: token.loc.start.line,
|
|
429
|
+
column: 0
|
|
430
|
+
},
|
|
431
|
+
end: {
|
|
432
|
+
line: token.loc.start.line,
|
|
433
|
+
column: token.loc.start.column
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
fix(fixer) {
|
|
437
|
+
let range = [token.range[0] - token.loc.start.column, token.range[0]], newText = neededIndent;
|
|
438
|
+
return fixer.replaceTextRange(range, newText);
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Checks if a token's indentation is correct
|
|
444
|
+
* @param {Token} token Token to examine
|
|
445
|
+
* @param {string} desiredIndent Desired indentation of the string
|
|
446
|
+
* @returns {boolean} `true` if the token's indentation is correct
|
|
447
|
+
*/
|
|
448
|
+
function validateTokenIndent(token, desiredIndent) {
|
|
449
|
+
let indentation = tokenInfo.getTokenIndent(token);
|
|
450
|
+
return indentation === desiredIndent || indentation.includes(" ") && indentation.includes(" ");
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Check to see if the node is a file level IIFE
|
|
454
|
+
* @param {ASTNode} node The function node to check.
|
|
455
|
+
* @returns {boolean} True if the node is the outer IIFE
|
|
456
|
+
*/
|
|
457
|
+
function isOuterIIFE(node) {
|
|
458
|
+
if (!node.parent || node.parent.type !== "CallExpression" || node.parent.callee !== node) return !1;
|
|
459
|
+
let statement = node.parent && node.parent.parent;
|
|
460
|
+
for (; statement.type === "UnaryExpression" && [
|
|
461
|
+
"!",
|
|
462
|
+
"~",
|
|
463
|
+
"+",
|
|
464
|
+
"-"
|
|
465
|
+
].includes(statement.operator) || statement.type === "AssignmentExpression" || statement.type === "LogicalExpression" || statement.type === "SequenceExpression" || statement.type === "VariableDeclarator";) statement = statement.parent;
|
|
466
|
+
return (statement.type === "ExpressionStatement" || statement.type === "VariableDeclaration") && statement.parent.type === "Program";
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Counts the number of linebreaks that follow the last non-whitespace character in a string
|
|
470
|
+
* @param {string} string The string to check
|
|
471
|
+
* @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character,
|
|
472
|
+
* or the total number of linebreaks if the string is all whitespace.
|
|
473
|
+
*/
|
|
474
|
+
function countTrailingLinebreaks(string) {
|
|
475
|
+
let linebreakMatches = string.match(/\s*$/u)[0].match(astUtils.createGlobalLinebreakMatcher());
|
|
476
|
+
return linebreakMatches === null ? 0 : linebreakMatches.length;
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Check indentation for lists of elements (arrays, objects, function params)
|
|
480
|
+
* @param {ASTNode[]} elements List of elements that should be offset
|
|
481
|
+
* @param {Token} startToken The start token of the list that element should be aligned against, e.g. '['
|
|
482
|
+
* @param {Token} endToken The end token of the list, e.g. ']'
|
|
483
|
+
* @param {number|string} offset The amount that the elements should be offset
|
|
484
|
+
* @returns {void}
|
|
485
|
+
*/
|
|
486
|
+
function addElementListIndent(elements, startToken, endToken, offset) {
|
|
487
|
+
/**
|
|
488
|
+
* Gets the first token of a given element, including surrounding parentheses.
|
|
489
|
+
* @param {ASTNode} element A node in the `elements` list
|
|
490
|
+
* @returns {Token} The first token of this element
|
|
491
|
+
*/
|
|
492
|
+
function getFirstToken(element) {
|
|
493
|
+
let token = sourceCode.getTokenBefore(element);
|
|
494
|
+
for (; astUtils.isOpeningParenToken(token) && token !== startToken;) token = sourceCode.getTokenBefore(token);
|
|
495
|
+
return sourceCode.getTokenAfter(token);
|
|
496
|
+
}
|
|
497
|
+
offsets.setDesiredOffsets([startToken.range[1], endToken.range[0]], startToken, typeof offset == "number" ? offset : 1), offsets.setDesiredOffset(endToken, startToken, 0), !(offset === "first" && elements.length && !elements[0]) && elements.forEach((element, index) => {
|
|
498
|
+
if (element && (offset === "off" && offsets.ignoreToken(getFirstToken(element)), index !== 0)) if (offset === "first" && tokenInfo.isFirstTokenOfLine(getFirstToken(element))) offsets.matchOffsetOf(getFirstToken(elements[0]), getFirstToken(element));
|
|
499
|
+
else {
|
|
500
|
+
let previousElement = elements[index - 1], firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement), previousElementLastToken = previousElement && sourceCode.getLastToken(previousElement);
|
|
501
|
+
previousElement && previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line && offsets.setDesiredOffsets([previousElement.range[1], element.range[1]], firstTokenOfPreviousElement, 0);
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Check and decide whether to check for indentation for blockless nodes
|
|
507
|
+
* Scenarios are for or while statements without braces around them
|
|
508
|
+
* @param {ASTNode} node node to examine
|
|
509
|
+
* @returns {void}
|
|
510
|
+
*/
|
|
511
|
+
function addBlocklessNodeIndent(node) {
|
|
512
|
+
if (node.type !== "BlockStatement") {
|
|
513
|
+
let lastParentToken = sourceCode.getTokenBefore(node, astUtils.isNotOpeningParenToken), firstBodyToken = sourceCode.getFirstToken(node), lastBodyToken = sourceCode.getLastToken(node);
|
|
514
|
+
for (; astUtils.isOpeningParenToken(sourceCode.getTokenBefore(firstBodyToken)) && astUtils.isClosingParenToken(sourceCode.getTokenAfter(lastBodyToken));) firstBodyToken = sourceCode.getTokenBefore(firstBodyToken), lastBodyToken = sourceCode.getTokenAfter(lastBodyToken);
|
|
515
|
+
offsets.setDesiredOffsets([firstBodyToken.range[0], lastBodyToken.range[1]], lastParentToken, 1);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
/**
|
|
519
|
+
* Checks the indentation for nodes that are like function calls (`CallExpression` and `NewExpression`)
|
|
520
|
+
* @param {ASTNode} node A CallExpression or NewExpression node
|
|
521
|
+
* @returns {void}
|
|
522
|
+
*/
|
|
523
|
+
function addFunctionCallIndent(node) {
|
|
524
|
+
let openingParen;
|
|
525
|
+
openingParen = node.arguments.length ? sourceCode.getFirstTokenBetween(node.callee, node.arguments[0], astUtils.isOpeningParenToken) : sourceCode.getLastToken(node, 1);
|
|
526
|
+
let closingParen = sourceCode.getLastToken(node);
|
|
527
|
+
if (parameterParens.add(openingParen), parameterParens.add(closingParen), node.optional) {
|
|
528
|
+
let dotToken = sourceCode.getTokenAfter(node.callee, astUtils.isQuestionDotToken), calleeParenCount = sourceCode.getTokensBetween(node.callee, dotToken, { filter: astUtils.isClosingParenToken }).length, firstTokenOfCallee = calleeParenCount ? sourceCode.getTokenBefore(node.callee, { skip: calleeParenCount - 1 }) : sourceCode.getFirstToken(node.callee), lastTokenOfCallee = sourceCode.getTokenBefore(dotToken), offsetBase = lastTokenOfCallee.loc.end.line === openingParen.loc.start.line ? lastTokenOfCallee : firstTokenOfCallee;
|
|
529
|
+
offsets.setDesiredOffset(dotToken, offsetBase, 1);
|
|
530
|
+
}
|
|
531
|
+
let offsetAfterToken = node.callee.type === "TaggedTemplateExpression" ? sourceCode.getFirstToken(node.callee.quasi) : openingParen, offsetToken = sourceCode.getTokenBefore(offsetAfterToken);
|
|
532
|
+
offsets.setDesiredOffset(openingParen, offsetToken, 0), addElementListIndent(node.arguments, openingParen, closingParen, options.CallExpression.arguments);
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* Checks the indentation of parenthesized values, given a list of tokens in a program
|
|
536
|
+
* @param {Token[]} tokens A list of tokens
|
|
537
|
+
* @returns {void}
|
|
538
|
+
*/
|
|
539
|
+
function addParensIndent(tokens) {
|
|
540
|
+
let parenStack = [], parenPairs = [];
|
|
541
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
542
|
+
let nextToken = tokens[i];
|
|
543
|
+
astUtils.isOpeningParenToken(nextToken) ? parenStack.push(nextToken) : astUtils.isClosingParenToken(nextToken) && parenPairs.push({
|
|
544
|
+
left: parenStack.pop(),
|
|
545
|
+
right: nextToken
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
for (let i = parenPairs.length - 1; i >= 0; i--) {
|
|
549
|
+
let leftParen = parenPairs[i].left, rightParen = parenPairs[i].right;
|
|
550
|
+
if (!parameterParens.has(leftParen) && !parameterParens.has(rightParen)) {
|
|
551
|
+
let parenthesizedTokens = new Set(sourceCode.getTokensBetween(leftParen, rightParen));
|
|
552
|
+
parenthesizedTokens.forEach((token) => {
|
|
553
|
+
parenthesizedTokens.has(offsets.getFirstDependency(token)) || offsets.setDesiredOffset(token, leftParen, 1);
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
offsets.setDesiredOffset(rightParen, leftParen, 0);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Ignore all tokens within an unknown node whose offset do not depend
|
|
561
|
+
* on another token's offset within the unknown node
|
|
562
|
+
* @param {ASTNode} node Unknown Node
|
|
563
|
+
* @returns {void}
|
|
564
|
+
*/
|
|
565
|
+
function ignoreNode(node) {
|
|
566
|
+
let unknownNodeTokens = new Set(sourceCode.getTokens(node, { includeComments: !0 }));
|
|
567
|
+
unknownNodeTokens.forEach((token) => {
|
|
568
|
+
if (!unknownNodeTokens.has(offsets.getFirstDependency(token))) {
|
|
569
|
+
let firstTokenOfLine = tokenInfo.getFirstTokenOfLine(token);
|
|
570
|
+
token === firstTokenOfLine ? offsets.ignoreToken(token) : offsets.setDesiredOffset(token, firstTokenOfLine, 0);
|
|
571
|
+
}
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
/**
|
|
575
|
+
* Check whether the given token is on the first line of a statement.
|
|
576
|
+
* @param {Token} token The token to check.
|
|
577
|
+
* @param {ASTNode} leafNode The expression node that the token belongs directly.
|
|
578
|
+
* @returns {boolean} `true` if the token is on the first line of a statement.
|
|
579
|
+
*/
|
|
580
|
+
function isOnFirstLineOfStatement(token, leafNode) {
|
|
581
|
+
let node = leafNode;
|
|
582
|
+
for (; node.parent && !node.parent.type.endsWith("Statement") && !node.parent.type.endsWith("Declaration");) node = node.parent;
|
|
583
|
+
return node = node.parent, !node || node.loc.start.line === token.loc.start.line;
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Check whether there are any blank (whitespace-only) lines between
|
|
587
|
+
* two tokens on separate lines.
|
|
588
|
+
* @param {Token} firstToken The first token.
|
|
589
|
+
* @param {Token} secondToken The second token.
|
|
590
|
+
* @returns {boolean} `true` if the tokens are on separate lines and
|
|
591
|
+
* there exists a blank line between them, `false` otherwise.
|
|
592
|
+
*/
|
|
593
|
+
function hasBlankLinesBetween(firstToken, secondToken) {
|
|
594
|
+
let firstTokenLine = firstToken.loc.end.line, secondTokenLine = secondToken.loc.start.line;
|
|
595
|
+
if (firstTokenLine === secondTokenLine || firstTokenLine === secondTokenLine - 1) return !1;
|
|
596
|
+
for (let line = firstTokenLine + 1; line < secondTokenLine; ++line) if (!tokenInfo.firstTokensByLineNumber.has(line)) return !0;
|
|
597
|
+
return !1;
|
|
598
|
+
}
|
|
599
|
+
let ignoredNodeFirstTokens = /* @__PURE__ */ new Set(), baseOffsetListeners = {
|
|
600
|
+
"ArrayExpression, ArrayPattern"(node) {
|
|
601
|
+
let openingBracket = sourceCode.getFirstToken(node), closingBracket = sourceCode.getTokenAfter([...node.elements].reverse().find((_) => _) || openingBracket, astUtils.isClosingBracketToken);
|
|
602
|
+
addElementListIndent(node.elements, openingBracket, closingBracket, options.ArrayExpression);
|
|
603
|
+
},
|
|
604
|
+
"ObjectExpression, ObjectPattern"(node) {
|
|
605
|
+
let openingCurly = sourceCode.getFirstToken(node), closingCurly = sourceCode.getTokenAfter(node.properties.length ? node.properties.at(-1) : openingCurly, astUtils.isClosingBraceToken);
|
|
606
|
+
addElementListIndent(node.properties, openingCurly, closingCurly, options.ObjectExpression);
|
|
607
|
+
},
|
|
608
|
+
ArrowFunctionExpression(node) {
|
|
609
|
+
let maybeOpeningParen = sourceCode.getFirstToken(node, { skip: node.async ? 1 : 0 });
|
|
610
|
+
if (astUtils.isOpeningParenToken(maybeOpeningParen)) {
|
|
611
|
+
let openingParen = maybeOpeningParen, closingParen = sourceCode.getTokenBefore(node.body, astUtils.isClosingParenToken);
|
|
612
|
+
parameterParens.add(openingParen), parameterParens.add(closingParen), addElementListIndent(node.params, openingParen, closingParen, options.FunctionExpression.parameters);
|
|
613
|
+
}
|
|
614
|
+
addBlocklessNodeIndent(node.body);
|
|
615
|
+
},
|
|
616
|
+
AssignmentExpression(node) {
|
|
617
|
+
let operator = sourceCode.getFirstTokenBetween(node.left, node.right, (token) => token.value === node.operator);
|
|
618
|
+
offsets.setDesiredOffsets([operator.range[0], node.range[1]], sourceCode.getLastToken(node.left), 1), offsets.ignoreToken(operator), offsets.ignoreToken(sourceCode.getTokenAfter(operator));
|
|
619
|
+
},
|
|
620
|
+
"BinaryExpression, LogicalExpression"(node) {
|
|
621
|
+
let operator = sourceCode.getFirstTokenBetween(node.left, node.right, (token) => token.value === node.operator), tokenAfterOperator = sourceCode.getTokenAfter(operator);
|
|
622
|
+
offsets.ignoreToken(operator), offsets.ignoreToken(tokenAfterOperator), offsets.setDesiredOffset(tokenAfterOperator, operator, 0);
|
|
623
|
+
},
|
|
624
|
+
"BlockStatement, ClassBody"(node) {
|
|
625
|
+
let blockIndentLevel;
|
|
626
|
+
blockIndentLevel = node.parent && isOuterIIFE(node.parent) ? options.outerIIFEBody : node.parent && (node.parent.type === "FunctionExpression" || node.parent.type === "ArrowFunctionExpression") ? options.FunctionExpression.body : node.parent && node.parent.type === "FunctionDeclaration" ? options.FunctionDeclaration.body : 1, astUtils.STATEMENT_LIST_PARENTS.has(node.parent.type) || offsets.setDesiredOffset(sourceCode.getFirstToken(node), sourceCode.getFirstToken(node.parent), 0), addElementListIndent(node.body, sourceCode.getFirstToken(node), sourceCode.getLastToken(node), blockIndentLevel);
|
|
627
|
+
},
|
|
628
|
+
CallExpression: addFunctionCallIndent,
|
|
629
|
+
"ClassDeclaration[superClass], ClassExpression[superClass]"(node) {
|
|
630
|
+
let classToken = sourceCode.getFirstToken(node), extendsToken = sourceCode.getTokenBefore(node.superClass, astUtils.isNotOpeningParenToken);
|
|
631
|
+
offsets.setDesiredOffsets([extendsToken.range[0], node.body.range[0]], classToken, 1);
|
|
632
|
+
},
|
|
633
|
+
ConditionalExpression(node) {
|
|
634
|
+
let firstToken = sourceCode.getFirstToken(node);
|
|
635
|
+
if (!options.flatTernaryExpressions || !astUtils.isTokenOnSameLine(node.test, node.consequent) || isOnFirstLineOfStatement(firstToken, node)) {
|
|
636
|
+
let questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, (token) => token.type === "Punctuator" && token.value === "?"), colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, (token) => token.type === "Punctuator" && token.value === ":"), firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken), lastConsequentToken = sourceCode.getTokenBefore(colonToken), firstAlternateToken = sourceCode.getTokenAfter(colonToken);
|
|
637
|
+
offsets.setDesiredOffset(questionMarkToken, firstToken, 1), offsets.setDesiredOffset(colonToken, firstToken, 1), offsets.setDesiredOffset(firstConsequentToken, firstToken, firstConsequentToken.type === "Punctuator" && options.offsetTernaryExpressions ? 2 : 1), lastConsequentToken.loc.end.line === firstAlternateToken.loc.start.line ? offsets.setDesiredOffset(firstAlternateToken, firstConsequentToken, 0) : offsets.setDesiredOffset(firstAlternateToken, firstToken, firstAlternateToken.type === "Punctuator" && options.offsetTernaryExpressions ? 2 : 1);
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
"DoWhileStatement, WhileStatement, ForInStatement, ForOfStatement, WithStatement": (node) => addBlocklessNodeIndent(node.body),
|
|
641
|
+
ExportNamedDeclaration(node) {
|
|
642
|
+
if (node.declaration === null) {
|
|
643
|
+
let closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
|
|
644
|
+
addElementListIndent(node.specifiers, sourceCode.getFirstToken(node, { skip: 1 }), closingCurly, 1), node.source && offsets.setDesiredOffsets([closingCurly.range[1], node.range[1]], sourceCode.getFirstToken(node), 1);
|
|
645
|
+
}
|
|
646
|
+
},
|
|
647
|
+
ForStatement(node) {
|
|
648
|
+
let forOpeningParen = sourceCode.getFirstToken(node, 1);
|
|
649
|
+
node.init && offsets.setDesiredOffsets(node.init.range, forOpeningParen, 1), node.test && offsets.setDesiredOffsets(node.test.range, forOpeningParen, 1), node.update && offsets.setDesiredOffsets(node.update.range, forOpeningParen, 1), addBlocklessNodeIndent(node.body);
|
|
650
|
+
},
|
|
651
|
+
"FunctionDeclaration, FunctionExpression"(node) {
|
|
652
|
+
let closingParen = sourceCode.getTokenBefore(node.body), openingParen = sourceCode.getTokenBefore(node.params.length ? node.params[0] : closingParen);
|
|
653
|
+
parameterParens.add(openingParen), parameterParens.add(closingParen), addElementListIndent(node.params, openingParen, closingParen, options[node.type].parameters);
|
|
654
|
+
},
|
|
655
|
+
IfStatement(node) {
|
|
656
|
+
addBlocklessNodeIndent(node.consequent), node.alternate && addBlocklessNodeIndent(node.alternate);
|
|
657
|
+
},
|
|
658
|
+
":matches(DoWhileStatement, ForStatement, ForInStatement, ForOfStatement, IfStatement, WhileStatement, WithStatement):exit"(node) {
|
|
659
|
+
let nodesToCheck;
|
|
660
|
+
node.type === "IfStatement" ? (nodesToCheck = [node.consequent], node.alternate && nodesToCheck.push(node.alternate)) : nodesToCheck = [node.body];
|
|
661
|
+
for (let nodeToCheck of nodesToCheck) {
|
|
662
|
+
let lastToken = sourceCode.getLastToken(nodeToCheck);
|
|
663
|
+
if (astUtils.isSemicolonToken(lastToken)) {
|
|
664
|
+
let tokenBeforeLast = sourceCode.getTokenBefore(lastToken), tokenAfterLast = sourceCode.getTokenAfter(lastToken);
|
|
665
|
+
!astUtils.isTokenOnSameLine(tokenBeforeLast, lastToken) && tokenAfterLast && astUtils.isTokenOnSameLine(lastToken, tokenAfterLast) && offsets.setDesiredOffset(lastToken, sourceCode.getFirstToken(node), 0);
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
},
|
|
669
|
+
ImportDeclaration(node) {
|
|
670
|
+
if (node.specifiers.some((specifier) => specifier.type === "ImportSpecifier")) {
|
|
671
|
+
let openingCurly = sourceCode.getFirstToken(node, astUtils.isOpeningBraceToken), closingCurly = sourceCode.getLastToken(node, astUtils.isClosingBraceToken);
|
|
672
|
+
addElementListIndent(node.specifiers.filter((specifier) => specifier.type === "ImportSpecifier"), openingCurly, closingCurly, options.ImportDeclaration);
|
|
673
|
+
}
|
|
674
|
+
let fromToken = sourceCode.getLastToken(node, (token) => token.type === "Identifier" && token.value === "from"), sourceToken = sourceCode.getLastToken(node, (token) => token.type === "String"), semiToken = sourceCode.getLastToken(node, (token) => token.type === "Punctuator" && token.value === ";");
|
|
675
|
+
if (fromToken) {
|
|
676
|
+
let end = semiToken && semiToken.range[1] === sourceToken.range[1] ? node.range[1] : sourceToken.range[1];
|
|
677
|
+
offsets.setDesiredOffsets([fromToken.range[0], end], sourceCode.getFirstToken(node), 1);
|
|
678
|
+
}
|
|
679
|
+
},
|
|
680
|
+
ImportExpression(node) {
|
|
681
|
+
let openingParen = sourceCode.getFirstToken(node, 1), closingParen = sourceCode.getLastToken(node);
|
|
682
|
+
parameterParens.add(openingParen), parameterParens.add(closingParen), offsets.setDesiredOffset(openingParen, sourceCode.getTokenBefore(openingParen), 0), addElementListIndent([node.source], openingParen, closingParen, options.CallExpression.arguments);
|
|
683
|
+
},
|
|
684
|
+
"MemberExpression, JSXMemberExpression, MetaProperty"(node) {
|
|
685
|
+
let object = node.type === "MetaProperty" ? node.meta : node.object, firstNonObjectToken = sourceCode.getFirstTokenBetween(object, node.property, astUtils.isNotClosingParenToken), secondNonObjectToken = sourceCode.getTokenAfter(firstNonObjectToken), objectParenCount = sourceCode.getTokensBetween(object, node.property, { filter: astUtils.isClosingParenToken }).length, firstObjectToken = objectParenCount ? sourceCode.getTokenBefore(object, { skip: objectParenCount - 1 }) : sourceCode.getFirstToken(object), lastObjectToken = sourceCode.getTokenBefore(firstNonObjectToken), firstPropertyToken = node.computed ? firstNonObjectToken : secondNonObjectToken;
|
|
686
|
+
node.computed && (offsets.setDesiredOffset(sourceCode.getLastToken(node), firstNonObjectToken, 0), offsets.setDesiredOffsets(node.property.range, firstNonObjectToken, 1));
|
|
687
|
+
let offsetBase = lastObjectToken.loc.end.line === firstPropertyToken.loc.start.line ? lastObjectToken : firstObjectToken;
|
|
688
|
+
typeof options.MemberExpression == "number" ? (offsets.setDesiredOffset(firstNonObjectToken, offsetBase, options.MemberExpression), offsets.setDesiredOffset(secondNonObjectToken, node.computed ? firstNonObjectToken : offsetBase, options.MemberExpression)) : (offsets.ignoreToken(firstNonObjectToken), offsets.ignoreToken(secondNonObjectToken), offsets.setDesiredOffset(firstNonObjectToken, offsetBase, 0), offsets.setDesiredOffset(secondNonObjectToken, firstNonObjectToken, 0));
|
|
689
|
+
},
|
|
690
|
+
NewExpression(node) {
|
|
691
|
+
(node.arguments.length > 0 || astUtils.isClosingParenToken(sourceCode.getLastToken(node)) && astUtils.isOpeningParenToken(sourceCode.getLastToken(node, 1))) && addFunctionCallIndent(node);
|
|
692
|
+
},
|
|
693
|
+
Property(node) {
|
|
694
|
+
if (!node.shorthand && !node.method && node.kind === "init") {
|
|
695
|
+
let colon = sourceCode.getFirstTokenBetween(node.key, node.value, astUtils.isColonToken);
|
|
696
|
+
offsets.ignoreToken(sourceCode.getTokenAfter(colon));
|
|
697
|
+
}
|
|
698
|
+
},
|
|
699
|
+
PropertyDefinition(node) {
|
|
700
|
+
let firstToken = sourceCode.getFirstToken(node), maybeSemicolonToken = sourceCode.getLastToken(node), keyLastToken;
|
|
701
|
+
if (node.computed) {
|
|
702
|
+
let bracketTokenL = sourceCode.getTokenBefore(node.key, astUtils.isOpeningBracketToken), bracketTokenR = keyLastToken = sourceCode.getTokenAfter(node.key, astUtils.isClosingBracketToken), keyRange = [bracketTokenL.range[1], bracketTokenR.range[0]];
|
|
703
|
+
bracketTokenL !== firstToken && offsets.setDesiredOffset(bracketTokenL, firstToken, 0), offsets.setDesiredOffsets(keyRange, bracketTokenL, 1), offsets.setDesiredOffset(bracketTokenR, bracketTokenL, 0);
|
|
704
|
+
} else {
|
|
705
|
+
let idToken = keyLastToken = sourceCode.getFirstToken(node.key);
|
|
706
|
+
idToken !== firstToken && offsets.setDesiredOffset(idToken, firstToken, 1);
|
|
707
|
+
}
|
|
708
|
+
if (node.value) {
|
|
709
|
+
let eqToken = sourceCode.getTokenBefore(node.value, astUtils.isEqToken), valueToken = sourceCode.getTokenAfter(eqToken);
|
|
710
|
+
offsets.setDesiredOffset(eqToken, keyLastToken, 1), offsets.setDesiredOffset(valueToken, eqToken, 1), astUtils.isSemicolonToken(maybeSemicolonToken) && offsets.setDesiredOffset(maybeSemicolonToken, eqToken, 1);
|
|
711
|
+
} else astUtils.isSemicolonToken(maybeSemicolonToken) && offsets.setDesiredOffset(maybeSemicolonToken, keyLastToken, 1);
|
|
712
|
+
},
|
|
713
|
+
StaticBlock(node) {
|
|
714
|
+
let openingCurly = sourceCode.getFirstToken(node, { skip: 1 }), closingCurly = sourceCode.getLastToken(node);
|
|
715
|
+
addElementListIndent(node.body, openingCurly, closingCurly, options.StaticBlock.body);
|
|
716
|
+
},
|
|
717
|
+
SwitchStatement(node) {
|
|
718
|
+
let openingCurly = sourceCode.getTokenAfter(node.discriminant, astUtils.isOpeningBraceToken), closingCurly = sourceCode.getLastToken(node);
|
|
719
|
+
offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, options.SwitchCase), node.cases.length && sourceCode.getTokensBetween(node.cases.at(-1), closingCurly, {
|
|
720
|
+
includeComments: !0,
|
|
721
|
+
filter: astUtils.isCommentToken
|
|
722
|
+
}).forEach((token) => offsets.ignoreToken(token));
|
|
723
|
+
},
|
|
724
|
+
SwitchCase(node) {
|
|
725
|
+
if (!(node.consequent.length === 1 && node.consequent[0].type === "BlockStatement")) {
|
|
726
|
+
let caseKeyword = sourceCode.getFirstToken(node), tokenAfterCurrentCase = sourceCode.getTokenAfter(node);
|
|
727
|
+
offsets.setDesiredOffsets([caseKeyword.range[1], tokenAfterCurrentCase.range[0]], caseKeyword, 1);
|
|
728
|
+
}
|
|
729
|
+
},
|
|
730
|
+
TemplateLiteral(node) {
|
|
731
|
+
node.expressions.forEach((expression, index) => {
|
|
732
|
+
let previousQuasi = node.quasis[index], nextQuasi = node.quasis[index + 1], tokenToAlignFrom = previousQuasi.loc.start.line === previousQuasi.loc.end.line ? sourceCode.getFirstToken(previousQuasi) : null;
|
|
733
|
+
offsets.setDesiredOffsets([previousQuasi.range[1], nextQuasi.range[0]], tokenToAlignFrom, 1), offsets.setDesiredOffset(sourceCode.getFirstToken(nextQuasi), tokenToAlignFrom, 0);
|
|
734
|
+
});
|
|
735
|
+
},
|
|
736
|
+
VariableDeclaration(node) {
|
|
737
|
+
let variableIndent = Object.hasOwn(options.VariableDeclarator, node.kind) ? options.VariableDeclarator[node.kind] : 1, firstToken = sourceCode.getFirstToken(node), lastToken = sourceCode.getLastToken(node);
|
|
738
|
+
if (options.VariableDeclarator[node.kind] === "first") {
|
|
739
|
+
if (node.declarations.length > 1) {
|
|
740
|
+
addElementListIndent(node.declarations, firstToken, lastToken, "first");
|
|
741
|
+
return;
|
|
742
|
+
}
|
|
743
|
+
variableIndent = 1;
|
|
744
|
+
}
|
|
745
|
+
node.declarations.at(-1).loc.start.line > node.loc.start.line ? offsets.setDesiredOffsets(node.range, firstToken, variableIndent, !0) : offsets.setDesiredOffsets(node.range, firstToken, variableIndent), astUtils.isSemicolonToken(lastToken) && offsets.ignoreToken(lastToken);
|
|
746
|
+
},
|
|
747
|
+
VariableDeclarator(node) {
|
|
748
|
+
if (node.init) {
|
|
749
|
+
let equalOperator = sourceCode.getTokenBefore(node.init, astUtils.isNotOpeningParenToken), tokenAfterOperator = sourceCode.getTokenAfter(equalOperator);
|
|
750
|
+
offsets.ignoreToken(equalOperator), offsets.ignoreToken(tokenAfterOperator), offsets.setDesiredOffsets([tokenAfterOperator.range[0], node.range[1]], equalOperator, 1), offsets.setDesiredOffset(equalOperator, sourceCode.getLastToken(node.id), 0);
|
|
751
|
+
}
|
|
752
|
+
},
|
|
753
|
+
"JSXAttribute[value]"(node) {
|
|
754
|
+
let equalsToken = sourceCode.getFirstTokenBetween(node.name, node.value, (token) => token.type === "Punctuator" && token.value === "=");
|
|
755
|
+
offsets.setDesiredOffsets([equalsToken.range[0], node.value.range[1]], sourceCode.getFirstToken(node.name), 1);
|
|
756
|
+
},
|
|
757
|
+
JSXElement(node) {
|
|
758
|
+
node.closingElement && addElementListIndent(node.children, sourceCode.getFirstToken(node.openingElement), sourceCode.getFirstToken(node.closingElement), 1);
|
|
759
|
+
},
|
|
760
|
+
JSXOpeningElement(node) {
|
|
761
|
+
let firstToken = sourceCode.getFirstToken(node), closingToken;
|
|
762
|
+
node.selfClosing ? (closingToken = sourceCode.getLastToken(node, { skip: 1 }), offsets.setDesiredOffset(sourceCode.getLastToken(node), closingToken, 0)) : closingToken = sourceCode.getLastToken(node), offsets.setDesiredOffsets(node.name.range, sourceCode.getFirstToken(node)), addElementListIndent(node.attributes, firstToken, closingToken, 1);
|
|
763
|
+
},
|
|
764
|
+
JSXClosingElement(node) {
|
|
765
|
+
let firstToken = sourceCode.getFirstToken(node);
|
|
766
|
+
offsets.setDesiredOffsets(node.name.range, firstToken, 1);
|
|
767
|
+
},
|
|
768
|
+
JSXFragment(node) {
|
|
769
|
+
let firstOpeningToken = sourceCode.getFirstToken(node.openingFragment), firstClosingToken = sourceCode.getFirstToken(node.closingFragment);
|
|
770
|
+
addElementListIndent(node.children, firstOpeningToken, firstClosingToken, 1);
|
|
771
|
+
},
|
|
772
|
+
JSXOpeningFragment(node) {
|
|
773
|
+
let firstToken = sourceCode.getFirstToken(node), closingToken = sourceCode.getLastToken(node);
|
|
774
|
+
offsets.setDesiredOffsets(node.range, firstToken, 1), offsets.matchOffsetOf(firstToken, closingToken);
|
|
775
|
+
},
|
|
776
|
+
JSXClosingFragment(node) {
|
|
777
|
+
let firstToken = sourceCode.getFirstToken(node), slashToken = sourceCode.getLastToken(node, { skip: 1 }), closingToken = sourceCode.getLastToken(node), tokenToMatch = astUtils.isTokenOnSameLine(slashToken, closingToken) ? slashToken : closingToken;
|
|
778
|
+
offsets.setDesiredOffsets(node.range, firstToken, 1), offsets.matchOffsetOf(firstToken, tokenToMatch);
|
|
779
|
+
},
|
|
780
|
+
JSXExpressionContainer(node) {
|
|
781
|
+
let openingCurly = sourceCode.getFirstToken(node), closingCurly = sourceCode.getLastToken(node);
|
|
782
|
+
offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, 1);
|
|
783
|
+
},
|
|
784
|
+
JSXSpreadAttribute(node) {
|
|
785
|
+
let openingCurly = sourceCode.getFirstToken(node), closingCurly = sourceCode.getLastToken(node);
|
|
786
|
+
offsets.setDesiredOffsets([openingCurly.range[1], closingCurly.range[0]], openingCurly, 1);
|
|
787
|
+
},
|
|
788
|
+
"*"(node) {
|
|
789
|
+
let firstToken = sourceCode.getFirstToken(node);
|
|
790
|
+
firstToken && !ignoredNodeFirstTokens.has(firstToken) && offsets.setDesiredOffsets(node.range, firstToken, 0);
|
|
791
|
+
}
|
|
792
|
+
}, listenerCallQueue = [], offsetListeners = {};
|
|
793
|
+
for (let [selector, listener] of Object.entries(baseOffsetListeners)) offsetListeners[selector] = (node) => listenerCallQueue.push({
|
|
794
|
+
listener,
|
|
795
|
+
node
|
|
796
|
+
});
|
|
797
|
+
let ignoredNodes = /* @__PURE__ */ new Set();
|
|
798
|
+
/**
|
|
799
|
+
* Ignores a node
|
|
800
|
+
* @param {ASTNode} node The node to ignore
|
|
801
|
+
* @returns {void}
|
|
802
|
+
*/
|
|
803
|
+
function addToIgnoredNodes(node) {
|
|
804
|
+
ignoredNodes.add(node), ignoredNodeFirstTokens.add(sourceCode.getFirstToken(node));
|
|
805
|
+
}
|
|
806
|
+
let ignoredNodeListeners = options.ignoredNodes.reduce((listeners, ignoredSelector) => Object.assign(listeners, { [ignoredSelector]: addToIgnoredNodes }), {});
|
|
807
|
+
return Object.assign(offsetListeners, ignoredNodeListeners, {
|
|
808
|
+
"*:exit"(node) {
|
|
809
|
+
KNOWN_NODES.has(node.type) || addToIgnoredNodes(node);
|
|
810
|
+
},
|
|
811
|
+
"Program:exit"() {
|
|
812
|
+
options.ignoreComments && sourceCode.getAllComments().forEach((comment) => offsets.ignoreToken(comment));
|
|
813
|
+
for (let i = 0; i < listenerCallQueue.length; i++) {
|
|
814
|
+
let nodeInfo = listenerCallQueue[i];
|
|
815
|
+
ignoredNodes.has(nodeInfo.node) || nodeInfo.listener(nodeInfo.node);
|
|
816
|
+
}
|
|
817
|
+
ignoredNodes.forEach(ignoreNode), addParensIndent(sourceCode.ast.tokens);
|
|
818
|
+
let precedingTokens = /* @__PURE__ */ new WeakMap();
|
|
819
|
+
for (let i = 0; i < sourceCode.ast.comments.length; i++) {
|
|
820
|
+
let comment = sourceCode.ast.comments[i], tokenOrCommentBefore = sourceCode.getTokenBefore(comment, { includeComments: !0 }), hasToken = precedingTokens.has(tokenOrCommentBefore) ? precedingTokens.get(tokenOrCommentBefore) : tokenOrCommentBefore;
|
|
821
|
+
precedingTokens.set(comment, hasToken);
|
|
822
|
+
}
|
|
823
|
+
for (let i = 1; i < sourceCode.lines.length + 1; i++) {
|
|
824
|
+
if (!tokenInfo.firstTokensByLineNumber.has(i)) continue;
|
|
825
|
+
let firstTokenOfLine = tokenInfo.firstTokensByLineNumber.get(i);
|
|
826
|
+
if (firstTokenOfLine.loc.start.line === i) {
|
|
827
|
+
if (astUtils.isCommentToken(firstTokenOfLine)) {
|
|
828
|
+
let tokenBefore = precedingTokens.get(firstTokenOfLine), tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0], mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine), mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter);
|
|
829
|
+
if (tokenAfter && astUtils.isSemicolonToken(tokenAfter) && !astUtils.isTokenOnSameLine(firstTokenOfLine, tokenAfter) && offsets.setDesiredOffset(firstTokenOfLine, tokenAfter, 0), mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) || mayAlignWithAfter && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))) continue;
|
|
830
|
+
}
|
|
831
|
+
validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine)) || report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine));
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
};
|
|
838
|
+
}));
|
|
839
|
+
//#endregion
|
|
840
|
+
//#region src-js/generated/plugin-eslint/rules/indent.cjs
|
|
841
|
+
module.exports = require_indent().create;
|
|
842
|
+
//#endregion
|