@ota-meshi/ast-token-store 0.0.0 → 0.2.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/README.md CHANGED
@@ -86,10 +86,18 @@ All methods accept options for filtering, skipping, counting, and including comm
86
86
 
87
87
  | Method | Description |
88
88
  | ----------------------------------- | ------------------------------------------------------------ |
89
+ | `getAllComments()` | Gets all comment tokens |
90
+ | `getCommentsInside(nodeOrToken)` | Gets all comment tokens inside the given node/token |
89
91
  | `getCommentsBefore(nodeOrToken)` | Gets all comment tokens directly before the given node/token |
90
92
  | `getCommentsAfter(nodeOrToken)` | Gets all comment tokens directly after the given node/token |
91
93
  | `commentsExistBetween(left, right)` | Checks if any comments exist between two nodes |
92
94
 
95
+ #### Spacing Methods
96
+
97
+ | Method | Description |
98
+ | ----------------------------- | --------------------------------------------------------------- |
99
+ | `isSpaceBetween(left, right)` | Checks if there is whitespace between two non-overlapping nodes |
100
+
93
101
  ### Options
94
102
 
95
103
  Single token methods (`getFirstToken`, `getLastToken`, etc.) accept skip options:
package/lib/index.d.mts CHANGED
@@ -46,6 +46,10 @@ declare class TokenStore<Node extends SyntaxElement, Token extends SyntaxElement
46
46
  * Gets all tokens, including comments.
47
47
  */
48
48
  getAllTokens(): (Token | Comment)[];
49
+ /**
50
+ * Gets all comments.
51
+ */
52
+ getAllComments(): Comment[];
49
53
  /**
50
54
  * Gets the first token of the given node.
51
55
  */
@@ -53,7 +57,7 @@ declare class TokenStore<Node extends SyntaxElement, Token extends SyntaxElement
53
57
  /**
54
58
  * Gets the first token of the given node with simple options.
55
59
  */
56
- getFirstToken(node: Node | Token | Comment, options: CursorWithSkipOptionsWithoutFilter): Token | null;
60
+ getFirstToken(node: Node | Token | Comment, options?: CursorWithSkipOptionsWithoutFilter): Token | null;
57
61
  /**
58
62
  * Gets the first token of the given node with options.
59
63
  */
@@ -81,7 +85,7 @@ declare class TokenStore<Node extends SyntaxElement, Token extends SyntaxElement
81
85
  /**
82
86
  * Gets the last token of the given node with options.
83
87
  */
84
- getLastToken(node: Node | Token | Comment, options: CursorWithSkipOptionsWithoutFilter): Token | null;
88
+ getLastToken(node: Node | Token | Comment, options?: CursorWithSkipOptionsWithoutFilter): Token | null;
85
89
  /**
86
90
  * Gets the last token of the given node with options.
87
91
  */
@@ -222,6 +226,10 @@ declare class TokenStore<Node extends SyntaxElement, Token extends SyntaxElement
222
226
  * Gets all of the tokens between two non-overlapping nodes with comment options.
223
227
  */
224
228
  getTokensBetween<R extends Token | Comment>(left: Node | Token | Comment, right: Node | Token | Comment, options: CursorWithCountOptionsWithComment<Token, Comment, R>): R[];
229
+ /**
230
+ * Gets all comment tokens inside the given node or token.
231
+ */
232
+ getCommentsInside(nodeOrToken: Node | Token | Comment): Comment[];
225
233
  /**
226
234
  * Gets all comment tokens directly before the given node or token.
227
235
  */
@@ -234,6 +242,10 @@ declare class TokenStore<Node extends SyntaxElement, Token extends SyntaxElement
234
242
  * Checks if there are any comment tokens between two non-overlapping nodes.
235
243
  */
236
244
  commentsExistBetween(left: Node | Token | Comment, right: Node | Token | Comment): boolean;
245
+ /**
246
+ * Checks if there is whitespace between two non-overlapping nodes.
247
+ */
248
+ isSpaceBetween(left: Node | Token | Comment, right: Node | Token | Comment): boolean;
237
249
  }
238
250
  //#endregion
239
251
  //#region src/index.d.ts
package/lib/index.mjs CHANGED
@@ -124,7 +124,8 @@ var TokenStore = class {
124
124
  ctx: {
125
125
  isComment: params.isComment,
126
126
  isNotComment: (token) => !params.isComment(token)
127
- }
127
+ },
128
+ cacheAllComments: null
128
129
  };
129
130
  }
130
131
  /**
@@ -133,6 +134,17 @@ var TokenStore = class {
133
134
  getAllTokens() {
134
135
  return this[PRIVATE].allTokens;
135
136
  }
137
+ /**
138
+ * Gets all comments.
139
+ */
140
+ getAllComments() {
141
+ const { ctx, allTokens, cacheAllComments } = this[PRIVATE];
142
+ if (cacheAllComments) return cacheAllComments;
143
+ const result = [];
144
+ for (const token of allTokens) if (ctx.isComment(token)) result.push(token);
145
+ this[PRIVATE].cacheAllComments = result;
146
+ return result;
147
+ }
136
148
  getFirstToken(node, options) {
137
149
  const { ctx, allTokens, tokenStartToIndex } = this[PRIVATE];
138
150
  const { filter, skip } = normalizeSkipOptions(options, ctx);
@@ -359,6 +371,20 @@ var TokenStore = class {
359
371
  return result;
360
372
  }
361
373
  /**
374
+ * Gets all comment tokens inside the given node or token.
375
+ */
376
+ getCommentsInside(nodeOrToken) {
377
+ const { ctx, allTokens, tokenStartToIndex } = this[PRIVATE];
378
+ const startIndex = getFirstIndex(allTokens, tokenStartToIndex, nodeOrToken.range[0]);
379
+ const endIndex = getLastIndex(allTokens, tokenStartToIndex, nodeOrToken.range[1]);
380
+ const result = [];
381
+ for (let i = startIndex; i <= endIndex && i < allTokens.length; i++) {
382
+ const token = allTokens[i];
383
+ if (ctx.isComment(token)) result.push(token);
384
+ }
385
+ return result;
386
+ }
387
+ /**
362
388
  * Gets all comment tokens directly before the given node or token.
363
389
  */
364
390
  getCommentsBefore(nodeOrToken) {
@@ -399,12 +425,28 @@ var TokenStore = class {
399
425
  }
400
426
  return false;
401
427
  }
428
+ /**
429
+ * Checks if there is whitespace between two non-overlapping nodes.
430
+ */
431
+ isSpaceBetween(left, right) {
432
+ if (left.range[1] >= right.range[0]) return false;
433
+ const { allTokens, tokenStartToIndex } = this[PRIVATE];
434
+ const startIndex = getFirstIndex(allTokens, tokenStartToIndex, left.range[1]);
435
+ const endIndex = getLastIndex(allTokens, tokenStartToIndex, right.range[0]);
436
+ let prev = left;
437
+ for (let i = startIndex; i <= endIndex && i < allTokens.length; i++) {
438
+ const token = allTokens[i];
439
+ if (prev.range[1] < token.range[0]) return true;
440
+ prev = token;
441
+ }
442
+ return prev.range[1] < right.range[0];
443
+ }
402
444
  };
403
445
 
404
446
  //#endregion
405
447
  //#region package.json
406
448
  var name$1 = "@ota-meshi/ast-token-store";
407
- var version$1 = "0.0.0";
449
+ var version$1 = "0.2.0";
408
450
 
409
451
  //#endregion
410
452
  //#region src/meta.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ota-meshi/ast-token-store",
3
- "version": "0.0.0",
3
+ "version": "0.2.0",
4
4
  "description": "A class library that provides an API similar to ESLint's token store",
5
5
  "type": "module",
6
6
  "exports": {
@@ -22,15 +22,16 @@
22
22
  "lint": "npm run lint:js && npm run lint:ts",
23
23
  "lint:js": "eslint .",
24
24
  "lint:ts": "npm run tsc",
25
- "tsc": "tsc --project tsconfig.build.json",
25
+ "tsc": "tsc",
26
26
  "eslint-fix": "eslint . --fix",
27
27
  "test": "npm run mocha -- \"tests/src/**/*.ts\" --reporter=dot --timeout=60000",
28
28
  "test:debug": "node --experimental-strip-types --experimental-transform-types ./node_modules/mocha/bin/mocha.js \"tests/src/**/*.ts\" --reporter=dot --timeout=60000",
29
29
  "test:cover": "c8 --reporter=lcov --reporter=text npm run test:debug",
30
30
  "ts": "node --import=tsx",
31
31
  "mocha": "npm run ts -- ./node_modules/mocha/bin/mocha.js",
32
- "generate:version": "env-cmd -e version -- npm run update && npm run lint -- --fix",
33
- "changeset:version": "env-cmd -e version -- changeset version && npm run generate:version && git add --all",
32
+ "update": "node --experimental-strip-types --experimental-transform-types ./tools/update.ts",
33
+ "generate:version": "npm run update && npm run lint -- --fix",
34
+ "changeset:version": "changeset version && npm run generate:version && git add --all",
34
35
  "changeset:publish": "npm run build && changeset publish"
35
36
  },
36
37
  "repository": {
@@ -73,7 +74,6 @@
73
74
  "@types/semver": "^7.5.8",
74
75
  "assert": "^2.1.0",
75
76
  "c8": "^10.1.3",
76
- "env-cmd": "^11.0.0",
77
77
  "eslint": "^9.34.0",
78
78
  "eslint-compat-utils": "^0.6.4",
79
79
  "eslint-config-prettier": "^10.1.1",