eslint-plugin-toml 1.0.4 → 1.1.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.
Files changed (3) hide show
  1. package/lib/index.d.mts +177 -24
  2. package/lib/index.mjs +32 -301
  3. package/package.json +2 -1
package/lib/index.d.mts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { AST, TOMLVersionOption } from "toml-eslint-parser";
2
2
  import { IDirective, TextSourceCodeBase, TraversalStep } from "@eslint/plugin-kit";
3
+ import { CursorWithCountOptionsWithComment, CursorWithCountOptionsWithFilter, CursorWithCountOptionsWithoutFilter, CursorWithSkipOptionsWithComment, CursorWithSkipOptionsWithFilter, CursorWithSkipOptionsWithoutFilter } from "@ota-meshi/ast-token-store";
3
4
  import * as _eslint_core0 from "@eslint/core";
4
5
  import { File, FileProblem, Language, OkParseResult, RuleDefinition, RulesConfig } from "@eslint/core";
5
6
  import { Linter, Scope } from "eslint";
@@ -11,19 +12,6 @@ declare namespace meta_d_exports {
11
12
  declare const name: string;
12
13
  declare const version: string;
13
14
  //#endregion
14
- //#region src/language/token-store.d.ts
15
- type FilterPredicate = (tokenOrComment: TOMLToken) => boolean;
16
- type CursorWithSkipOptions = number | FilterPredicate | {
17
- includeComments?: boolean;
18
- filter?: FilterPredicate;
19
- skip?: number;
20
- };
21
- type CursorWithCountOptions = number | FilterPredicate | {
22
- includeComments?: boolean;
23
- filter?: FilterPredicate;
24
- count?: number;
25
- };
26
- //#endregion
27
15
  //#region src/language/toml-source-code.d.ts
28
16
  /**
29
17
  * TOML-specific syntax element type
@@ -95,21 +83,186 @@ declare class TOMLSourceCode extends TextSourceCodeBase<{
95
83
  problems: FileProblem[];
96
84
  };
97
85
  getNodeByRangeIndex(index: number): AST.TOMLNode | null;
86
+ /**
87
+ * Gets the first token of the given node.
88
+ */
98
89
  getFirstToken(node: TOMLSyntaxElement): AST.Token;
99
- getFirstToken(node: TOMLSyntaxElement, options?: CursorWithSkipOptions): TOMLToken | null;
90
+ /**
91
+ * Gets the first token of the given node with options.
92
+ */
93
+ getFirstToken(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithoutFilter): AST.Token | null;
94
+ /**
95
+ * Gets the first token of the given node with filter options.
96
+ */
97
+ getFirstToken<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithFilter<AST.Token, R>): R | null;
98
+ /**
99
+ * Gets the first token of the given node with comment options.
100
+ */
101
+ getFirstToken<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithComment<AST.Token, AST.Comment, R>): R | null;
102
+ /**
103
+ * Gets the first tokens of the given node.
104
+ */
105
+ getFirstTokens(node: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
106
+ /**
107
+ * Gets the first tokens of the given node with filter options.
108
+ */
109
+ getFirstTokens<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
110
+ /**
111
+ * Gets the first tokens of the given node with comment options.
112
+ */
113
+ getFirstTokens<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
114
+ /**
115
+ * Gets the last token of the given node.
116
+ */
100
117
  getLastToken(node: TOMLSyntaxElement): AST.Token;
101
- getLastToken(node: TOMLSyntaxElement, options?: CursorWithSkipOptions): TOMLToken | null;
102
- getTokenBefore(node: TOMLSyntaxElement): AST.Token | null;
103
- getTokenBefore(node: TOMLSyntaxElement, options?: CursorWithSkipOptions): TOMLToken | null;
104
- getTokensBefore(node: TOMLSyntaxElement, options?: CursorWithCountOptions): TOMLToken[];
105
- getTokenAfter(node: TOMLSyntaxElement): AST.Token | null;
106
- getTokenAfter(node: TOMLSyntaxElement, options?: CursorWithSkipOptions): TOMLToken | null;
107
- getFirstTokenBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options?: CursorWithSkipOptions): TOMLToken | null;
108
- getTokensBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, paddingOrOptions?: number | FilterPredicate | CursorWithCountOptions): TOMLToken[];
109
- getTokens(node: AST.TOMLNode, options?: FilterPredicate | CursorWithCountOptions): TOMLToken[];
118
+ /**
119
+ * Gets the last token of the given node with options.
120
+ */
121
+ getLastToken(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithoutFilter): AST.Token | null;
122
+ /**
123
+ * Gets the last token of the given node with filter options.
124
+ */
125
+ getLastToken<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithFilter<AST.Token, R>): R | null;
126
+ /**
127
+ * Gets the last token of the given node with comment options.
128
+ */
129
+ getLastToken<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithComment<AST.Token, AST.Comment, R>): R | null;
130
+ /**
131
+ * Get the last tokens of the given node.
132
+ */
133
+ getLastTokens(node: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
134
+ /**
135
+ * Get the last tokens of the given node with filter options.
136
+ */
137
+ getLastTokens<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
138
+ /**
139
+ * Get the last tokens of the given node with comment options.
140
+ */
141
+ getLastTokens<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
142
+ /**
143
+ * Gets the token that precedes a given node or token.
144
+ */
145
+ getTokenBefore(node: TOMLSyntaxElement, options?: CursorWithSkipOptionsWithoutFilter): AST.Token | null;
146
+ /**
147
+ * Gets the token that precedes a given node or token with filter options.
148
+ */
149
+ getTokenBefore<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithFilter<AST.Token, R>): R | null;
150
+ /**
151
+ * Gets the token that precedes a given node or token with comment options.
152
+ */
153
+ getTokenBefore<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithComment<AST.Token, AST.Comment, R>): R | null;
154
+ /**
155
+ * Gets the `count` tokens that precedes a given node or token.
156
+ */
157
+ getTokensBefore(node: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
158
+ /**
159
+ * Gets the `count` tokens that precedes a given node or token with filter options.
160
+ */
161
+ getTokensBefore<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
162
+ /**
163
+ * Gets the `count` tokens that precedes a given node or token with comment options.
164
+ */
165
+ getTokensBefore<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
166
+ /**
167
+ * Gets the token that follows a given node or token.
168
+ */
169
+ getTokenAfter(node: TOMLSyntaxElement, options?: CursorWithSkipOptionsWithoutFilter): AST.Token | null;
170
+ /**
171
+ * Gets the token that follows a given node or token with filter options.
172
+ */
173
+ getTokenAfter<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithFilter<AST.Token, R>): R | null;
174
+ /**
175
+ * Gets the token that follows a given node or token with comment options.
176
+ */
177
+ getTokenAfter<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithSkipOptionsWithComment<AST.Token, AST.Comment, R>): R | null;
178
+ /**
179
+ * Gets the `count` tokens that follows a given node or token.
180
+ */
181
+ getTokensAfter(node: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
182
+ /**
183
+ * Gets the `count` tokens that follows a given node or token with filter options.
184
+ */
185
+ getTokensAfter<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
186
+ /**
187
+ * Gets the `count` tokens that follows a given node or token with comment options.
188
+ */
189
+ getTokensAfter<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
190
+ /**
191
+ * Gets the first token between two non-overlapping nodes.
192
+ */
193
+ getFirstTokenBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options?: CursorWithSkipOptionsWithoutFilter): AST.Token | null;
194
+ /**
195
+ * Gets the first token between two non-overlapping nodes with filter options.
196
+ */
197
+ getFirstTokenBetween<R extends AST.Token>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithSkipOptionsWithFilter<AST.Token, R>): R | null;
198
+ /**
199
+ * Gets the first token between two non-overlapping nodes with comment options.
200
+ */
201
+ getFirstTokenBetween<R extends AST.Token | AST.Comment>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithSkipOptionsWithComment<AST.Token, AST.Comment, R>): R | null;
202
+ /**
203
+ * Gets the first tokens between two non-overlapping nodes.
204
+ */
205
+ getFirstTokensBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
206
+ /**
207
+ * Gets the first tokens between two non-overlapping nodes with filter options.
208
+ */
209
+ getFirstTokensBetween<R extends AST.Token>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
210
+ /**
211
+ * Gets the first tokens between two non-overlapping nodes with comment options.
212
+ */
213
+ getFirstTokensBetween<R extends AST.Token | AST.Comment>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
214
+ /**
215
+ * Gets the last token between two non-overlapping nodes.
216
+ */
217
+ getLastTokenBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options?: CursorWithSkipOptionsWithoutFilter): AST.Token | null;
218
+ /**
219
+ * Gets the last token between two non-overlapping nodes with filter options.
220
+ */
221
+ getLastTokenBetween<R extends AST.Token>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithSkipOptionsWithFilter<AST.Token, R>): R | null;
222
+ /**
223
+ * Gets the last token between two non-overlapping nodes with comment options.
224
+ */
225
+ getLastTokenBetween<R extends AST.Token | AST.Comment>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithSkipOptionsWithComment<AST.Token, AST.Comment, R>): R | null;
226
+ /**
227
+ * Gets the last tokens between two non-overlapping nodes.
228
+ */
229
+ getLastTokensBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
230
+ /**
231
+ * Gets the last tokens between two non-overlapping nodes with filter options.
232
+ */
233
+ getLastTokensBetween<R extends AST.Token>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
234
+ /**
235
+ * Gets the last tokens between two non-overlapping nodes with comment options.
236
+ */
237
+ getLastTokensBetween<R extends AST.Token | AST.Comment>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
238
+ /**
239
+ * Gets all tokens that are related to the given node.
240
+ */
241
+ getTokens(node: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
242
+ /**
243
+ * Gets all tokens that are related to the given node with filter options.
244
+ */
245
+ getTokens<R extends AST.Token>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
246
+ /**
247
+ * Gets all tokens that are related to the given node with comment options.
248
+ */
249
+ getTokens<R extends AST.Token | AST.Comment>(node: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
250
+ /**
251
+ * Gets all of the tokens between two non-overlapping nodes.
252
+ */
253
+ getTokensBetween(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options?: CursorWithCountOptionsWithoutFilter): AST.Token[];
254
+ /**
255
+ * Gets all of the tokens between two non-overlapping nodes with filter options.
256
+ */
257
+ getTokensBetween<R extends AST.Token>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithCountOptionsWithFilter<AST.Token, R>): R[];
258
+ /**
259
+ * Gets all of the tokens between two non-overlapping nodes with comment options.
260
+ */
261
+ getTokensBetween<R extends AST.Token | AST.Comment>(left: TOMLSyntaxElement, right: TOMLSyntaxElement, options: CursorWithCountOptionsWithComment<AST.Token, AST.Comment, R>): R[];
262
+ getCommentsInside(nodeOrToken: TOMLSyntaxElement): AST.Comment[];
110
263
  getCommentsBefore(nodeOrToken: TOMLSyntaxElement): AST.Comment[];
111
264
  getCommentsAfter(nodeOrToken: TOMLSyntaxElement): AST.Comment[];
112
- isSpaceBetween(first: TOMLToken, second: TOMLToken): boolean;
265
+ isSpaceBetween(first: AST.Token | AST.Comment, second: AST.Token | AST.Comment): boolean;
113
266
  /**
114
267
  * Compatibility for ESLint's SourceCode API
115
268
  * @deprecated TOML does not have scopes
package/lib/index.mjs CHANGED
@@ -3,6 +3,7 @@ import * as tomlESLintParser from "toml-eslint-parser";
3
3
  import { VisitorKeys, getStaticTOMLValue, parseTOML, traverseNodes } from "toml-eslint-parser";
4
4
  import path from "path";
5
5
  import { CallMethodStep, ConfigCommentParser, Directive, TextSourceCodeBase, VisitNodeStep } from "@eslint/plugin-kit";
6
+ import { TokenStore } from "@ota-meshi/ast-token-store";
6
7
 
7
8
  //#region src/utils/index.ts
8
9
  /**
@@ -3070,7 +3071,7 @@ var standard_default = [...base_default, { rules: {
3070
3071
  //#endregion
3071
3072
  //#region package.json
3072
3073
  var name$1 = "eslint-plugin-toml";
3073
- var version$1 = "1.0.4";
3074
+ var version$1 = "1.1.0";
3074
3075
 
3075
3076
  //#endregion
3076
3077
  //#region src/meta.ts
@@ -3081,285 +3082,6 @@ var meta_exports = /* @__PURE__ */ __exportAll({
3081
3082
  const name = name$1;
3082
3083
  const version = version$1;
3083
3084
 
3084
- //#endregion
3085
- //#region src/language/token-store.ts
3086
- /**
3087
- * Binary search for the index of the first token that is after the given location.
3088
- */
3089
- function search(tokens, location) {
3090
- let minIndex = 0;
3091
- let maxIndex = tokens.length - 1;
3092
- while (minIndex <= maxIndex) {
3093
- const index = Math.floor((minIndex + maxIndex) / 2);
3094
- const tokenStartLocation = tokens[index].range[0];
3095
- if (tokenStartLocation < location) minIndex = index + 1;
3096
- else if (tokenStartLocation > location) maxIndex = index - 1;
3097
- else return index;
3098
- }
3099
- return minIndex;
3100
- }
3101
- /**
3102
- * Get the index of the first token that is after the given location.
3103
- */
3104
- function getFirstIndex(tokens, indexMap, startLoc) {
3105
- const index = indexMap.get(startLoc);
3106
- if (index != null) return index;
3107
- return search(tokens, startLoc);
3108
- }
3109
- /**
3110
- * Get the index of the last token that is before the given location.
3111
- */
3112
- function getLastIndex(tokens, indexMap, endLoc) {
3113
- const index = indexMap.get(endLoc);
3114
- if (index != null) return index - 1;
3115
- return search(tokens, endLoc) - 1;
3116
- }
3117
- /**
3118
- * Normalizes the options for cursor methods.
3119
- */
3120
- function normalizeSkipOptions(options) {
3121
- if (typeof options === "number") return {
3122
- filter: isNotComment,
3123
- skip: options
3124
- };
3125
- if (typeof options === "function") return {
3126
- filter: (n) => {
3127
- if (isComment(n)) return false;
3128
- return options(n);
3129
- },
3130
- skip: 0
3131
- };
3132
- let filter;
3133
- if (options?.includeComments) filter = options?.filter ?? (() => true);
3134
- else if (options?.filter) {
3135
- const baseFilter = options?.filter;
3136
- filter = (token) => {
3137
- if (isComment(token)) return false;
3138
- return baseFilter(token);
3139
- };
3140
- } else filter = isNotComment;
3141
- return {
3142
- filter,
3143
- skip: options?.skip ?? 0
3144
- };
3145
- }
3146
- /**
3147
- * Checks if a token is a comment.
3148
- */
3149
- function isComment(token) {
3150
- return token.type === "Block";
3151
- }
3152
- /**
3153
- * Checks if a token is a not comment.
3154
- */
3155
- function isNotComment(token) {
3156
- return !isComment(token);
3157
- }
3158
- /**
3159
- * Normalizes the options for cursor methods with count.
3160
- */
3161
- function normalizeCountOptions(options) {
3162
- if (typeof options === "number") return {
3163
- filter: isNotComment,
3164
- count: options
3165
- };
3166
- if (typeof options === "function") return {
3167
- filter: (n) => {
3168
- if (isComment(n)) return false;
3169
- return options(n);
3170
- },
3171
- count: 0
3172
- };
3173
- let filter;
3174
- if (options?.includeComments) filter = options?.filter ?? (() => true);
3175
- else if (options?.filter) {
3176
- const baseFilter = options?.filter;
3177
- filter = (token) => {
3178
- if (isComment(token)) return false;
3179
- return baseFilter(token);
3180
- };
3181
- } else filter = isNotComment;
3182
- return {
3183
- filter,
3184
- count: options?.count ?? 0
3185
- };
3186
- }
3187
- var TokenStore = class {
3188
- constructor(params) {
3189
- const tokens = params.ast.tokens || [];
3190
- const comments = params.ast.comments || [];
3191
- this.allTokens = [...tokens, ...comments].sort((a, b) => a.range[0] - b.range[0]);
3192
- this.tokenStartToIndex = /* @__PURE__ */ new Map();
3193
- for (let i = 0; i < this.allTokens.length; i++) this.tokenStartToIndex.set(this.allTokens[i].range[0], i);
3194
- }
3195
- /**
3196
- * Gets the first token of the given node.
3197
- */
3198
- getFirstToken(node, options) {
3199
- const { filter, skip } = normalizeSkipOptions(options);
3200
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, node.range[0]);
3201
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, node.range[1]);
3202
- let skipped = 0;
3203
- for (let i = startIndex; i <= endIndex && i < this.allTokens.length; i++) {
3204
- const token = this.allTokens[i];
3205
- if (filter && !filter(token)) continue;
3206
- if (skipped < skip) {
3207
- skipped++;
3208
- continue;
3209
- }
3210
- return token;
3211
- }
3212
- return null;
3213
- }
3214
- /**
3215
- * Gets the last token of the given node.
3216
- */
3217
- getLastToken(node, options) {
3218
- const { filter, skip } = normalizeSkipOptions(options);
3219
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, node.range[0]);
3220
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, node.range[1]);
3221
- let skipped = 0;
3222
- for (let i = endIndex; i >= startIndex && i >= 0; i--) {
3223
- const token = this.allTokens[i];
3224
- if (filter && !filter(token)) continue;
3225
- if (skipped < skip) {
3226
- skipped++;
3227
- continue;
3228
- }
3229
- return token;
3230
- }
3231
- return null;
3232
- }
3233
- /**
3234
- * Gets the first token between two non-overlapping nodes.
3235
- */
3236
- getFirstTokenBetween(left, right, options) {
3237
- const { filter, skip } = normalizeSkipOptions(options);
3238
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, left.range[1]);
3239
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, right.range[0]);
3240
- let skipped = 0;
3241
- for (let i = startIndex; i <= endIndex && i < this.allTokens.length; i++) {
3242
- const token = this.allTokens[i];
3243
- if (filter && !filter(token)) continue;
3244
- if (skipped < skip) {
3245
- skipped++;
3246
- continue;
3247
- }
3248
- return token;
3249
- }
3250
- return null;
3251
- }
3252
- /**
3253
- * Gets the token that precedes a given node or token.
3254
- */
3255
- getTokenBefore(node, options) {
3256
- const { filter, skip } = normalizeSkipOptions(options);
3257
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, node.range[0]);
3258
- let skipped = 0;
3259
- for (let i = endIndex; i >= 0; i--) {
3260
- const token = this.allTokens[i];
3261
- if (filter && !filter(token)) continue;
3262
- if (skipped < skip) {
3263
- skipped++;
3264
- continue;
3265
- }
3266
- return token;
3267
- }
3268
- return null;
3269
- }
3270
- /**
3271
- * Gets the token that follows a given node or token.
3272
- */
3273
- getTokenAfter(node, options) {
3274
- const { filter, skip } = normalizeSkipOptions(options);
3275
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, node.range[1]);
3276
- let skipped = 0;
3277
- for (let i = startIndex; i < this.allTokens.length; i++) {
3278
- const token = this.allTokens[i];
3279
- if (filter && !filter(token)) continue;
3280
- if (skipped < skip) {
3281
- skipped++;
3282
- continue;
3283
- }
3284
- return token;
3285
- }
3286
- return null;
3287
- }
3288
- /**
3289
- * Gets the `count` tokens that precedes a given node or token.
3290
- */
3291
- getTokensBefore(node, options) {
3292
- const { filter, count } = normalizeCountOptions(options);
3293
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, node.range[0]);
3294
- const result = [];
3295
- for (let i = endIndex; i >= 0; i--) {
3296
- const token = this.allTokens[i];
3297
- if (filter && !filter(token)) continue;
3298
- result.unshift(token);
3299
- if (count > 0 && result.length >= count) break;
3300
- }
3301
- return result;
3302
- }
3303
- /**
3304
- * Gets all tokens that are related to the given node.
3305
- */
3306
- getTokens(node, options) {
3307
- const { filter, count } = normalizeCountOptions(options);
3308
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, node.range[0]);
3309
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, node.range[1]);
3310
- const result = [];
3311
- for (let i = startIndex; i <= endIndex && i < this.allTokens.length; i++) {
3312
- const token = this.allTokens[i];
3313
- if (filter && !filter(token)) continue;
3314
- result.push(token);
3315
- if (count > 0 && result.length >= count) break;
3316
- }
3317
- return result;
3318
- }
3319
- /**
3320
- * Gets all of the tokens between two non-overlapping nodes.
3321
- */
3322
- getTokensBetween(left, right, paddingOrOptions) {
3323
- const { filter, count } = normalizeCountOptions(paddingOrOptions);
3324
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, left.range[1]);
3325
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, right.range[0]);
3326
- const result = [];
3327
- for (let i = startIndex; i <= endIndex && i < this.allTokens.length; i++) {
3328
- const token = this.allTokens[i];
3329
- if (filter && !filter(token)) continue;
3330
- result.push(token);
3331
- if (count > 0 && result.length >= count) break;
3332
- }
3333
- return result;
3334
- }
3335
- /**
3336
- * Gets all comment tokens directly before the given node or token.
3337
- */
3338
- getCommentsBefore(nodeOrToken) {
3339
- const endIndex = getLastIndex(this.allTokens, this.tokenStartToIndex, nodeOrToken.range[0]);
3340
- const result = [];
3341
- for (let i = endIndex; i >= 0; i--) {
3342
- const token = this.allTokens[i];
3343
- if (isComment(token)) result.unshift(token);
3344
- else break;
3345
- }
3346
- return result;
3347
- }
3348
- /**
3349
- * Gets all comment tokens directly after the given node or token.
3350
- */
3351
- getCommentsAfter(nodeOrToken) {
3352
- const startIndex = getFirstIndex(this.allTokens, this.tokenStartToIndex, nodeOrToken.range[1]);
3353
- const result = [];
3354
- for (let i = startIndex; i < this.allTokens.length; i++) {
3355
- const token = this.allTokens[i];
3356
- if (isComment(token)) result.push(token);
3357
- else break;
3358
- }
3359
- return result;
3360
- }
3361
- };
3362
-
3363
3085
  //#endregion
3364
3086
  //#region src/language/toml-source-code.ts
3365
3087
  /**
@@ -3389,7 +3111,10 @@ var TOMLSourceCode = class extends TextSourceCodeBase {
3389
3111
  this.hasBOM = Boolean(config.hasBOM);
3390
3112
  this.parserServices = config.parserServices;
3391
3113
  this.visitorKeys = config.visitorKeys || {};
3392
- this.tokenStore = new TokenStore({ ast: this.ast });
3114
+ this.tokenStore = new TokenStore({
3115
+ tokens: [...config.ast.tokens, ...config.ast.comments],
3116
+ isComment: (token) => token.type === "Block"
3117
+ });
3393
3118
  }
3394
3119
  traverse() {
3395
3120
  if (this.#steps != null) return this.#steps;
@@ -3432,7 +3157,7 @@ var TOMLSourceCode = class extends TextSourceCodeBase {
3432
3157
  return this.lines;
3433
3158
  }
3434
3159
  getAllComments() {
3435
- return this.ast.comments;
3160
+ return this.tokenStore.getAllComments();
3436
3161
  }
3437
3162
  /**
3438
3163
  * Returns an array of all inline configuration nodes found in the source code.
@@ -3532,9 +3257,15 @@ var TOMLSourceCode = class extends TextSourceCodeBase {
3532
3257
  getFirstToken(node, options) {
3533
3258
  return this.tokenStore.getFirstToken(node, options);
3534
3259
  }
3260
+ getFirstTokens(node, options) {
3261
+ return this.tokenStore.getFirstTokens(node, options);
3262
+ }
3535
3263
  getLastToken(node, options) {
3536
3264
  return this.tokenStore.getLastToken(node, options);
3537
3265
  }
3266
+ getLastTokens(node, options) {
3267
+ return this.tokenStore.getLastTokens(node, options);
3268
+ }
3538
3269
  getTokenBefore(node, options) {
3539
3270
  return this.tokenStore.getTokenBefore(node, options);
3540
3271
  }
@@ -3544,15 +3275,30 @@ var TOMLSourceCode = class extends TextSourceCodeBase {
3544
3275
  getTokenAfter(node, options) {
3545
3276
  return this.tokenStore.getTokenAfter(node, options);
3546
3277
  }
3278
+ getTokensAfter(node, options) {
3279
+ return this.tokenStore.getTokensAfter(node, options);
3280
+ }
3547
3281
  getFirstTokenBetween(left, right, options) {
3548
3282
  return this.tokenStore.getFirstTokenBetween(left, right, options);
3549
3283
  }
3550
- getTokensBetween(left, right, paddingOrOptions) {
3551
- return this.tokenStore.getTokensBetween(left, right, paddingOrOptions);
3284
+ getFirstTokensBetween(left, right, options) {
3285
+ return this.tokenStore.getFirstTokensBetween(left, right, options);
3286
+ }
3287
+ getLastTokenBetween(left, right, options) {
3288
+ return this.tokenStore.getLastTokenBetween(left, right, options);
3289
+ }
3290
+ getLastTokensBetween(left, right, options) {
3291
+ return this.tokenStore.getLastTokensBetween(left, right, options);
3552
3292
  }
3553
3293
  getTokens(node, options) {
3554
3294
  return this.tokenStore.getTokens(node, options);
3555
3295
  }
3296
+ getTokensBetween(left, right, options) {
3297
+ return this.tokenStore.getTokensBetween(left, right, options);
3298
+ }
3299
+ getCommentsInside(nodeOrToken) {
3300
+ return this.tokenStore.getCommentsInside(nodeOrToken);
3301
+ }
3556
3302
  getCommentsBefore(nodeOrToken) {
3557
3303
  return this.tokenStore.getCommentsBefore(nodeOrToken);
3558
3304
  }
@@ -3560,17 +3306,8 @@ var TOMLSourceCode = class extends TextSourceCodeBase {
3560
3306
  return this.tokenStore.getCommentsAfter(nodeOrToken);
3561
3307
  }
3562
3308
  isSpaceBetween(first, second) {
3563
- if (nodesOrTokensOverlap(first, second)) return false;
3564
- const [startingNodeOrToken, endingNodeOrToken] = first.range[1] <= second.range[0] ? [first, second] : [second, first];
3565
- const firstToken = this.getLastToken(startingNodeOrToken) || startingNodeOrToken;
3566
- const finalToken = this.getFirstToken(endingNodeOrToken) || endingNodeOrToken;
3567
- let currentToken = firstToken;
3568
- while (currentToken !== finalToken) {
3569
- const nextToken = this.getTokenAfter(currentToken, { includeComments: true });
3570
- if (currentToken.range[1] !== nextToken.range[0]) return true;
3571
- currentToken = nextToken;
3572
- }
3573
- return false;
3309
+ const [left, right] = first.range[1] <= second.range[0] ? [first, second] : [second, first];
3310
+ return this.tokenStore.isSpaceBetween(left, right);
3574
3311
  }
3575
3312
  /**
3576
3313
  * Compatibility for ESLint's SourceCode API
@@ -3622,12 +3359,6 @@ function isNode(value) {
3622
3359
  return typeof value === "object" && value !== null && typeof value.type === "string" && Array.isArray(value.range) && Boolean(value.loc) && typeof value.loc === "object";
3623
3360
  }
3624
3361
  /**
3625
- * Determines whether two nodes or tokens overlap.
3626
- */
3627
- function nodesOrTokensOverlap(first, second) {
3628
- return first.range[0] < second.range[1] && second.range[0] < first.range[1];
3629
- }
3630
- /**
3631
3362
  * Creates a fake global scope for TOML files.
3632
3363
  * @deprecated TOML does not have scopes
3633
3364
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-toml",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "This ESLint plugin provides linting rules for TOML.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -66,6 +66,7 @@
66
66
  "dependencies": {
67
67
  "@eslint/core": "^1.0.1",
68
68
  "@eslint/plugin-kit": "^0.6.0",
69
+ "@ota-meshi/ast-token-store": "^0.2.0",
69
70
  "debug": "^4.1.1",
70
71
  "toml-eslint-parser": "^1.0.1"
71
72
  },