@ontrails/source 1.0.0-beta.45 → 1.0.0-beta.46

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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @ontrails/source
2
2
 
3
+ ## 1.0.0-beta.46
4
+
5
+ ### Minor Changes
6
+
7
+ - [`54d259b`](https://github.com/outfitter-dev/trails/commit/54d259be81fb6c41d85be48a6cb2100c746a7126): Expose parser-native comment spans from `parseWithDiagnostics` so source-aware
8
+ tooling can distinguish exact JavaScript and TypeScript comment trivia without
9
+ reimplementing a lexer.
10
+
11
+ Use the shared spans in Warden's public-example rule while keeping leading
12
+ comment ownership fail-closed across JavaScript line terminators.
13
+
3
14
  ## 1.0.0-beta.45
4
15
 
5
16
  ## 1.0.0-beta.44
package/README.md CHANGED
@@ -9,7 +9,8 @@ Shared source-code machinery for Trails packages and repo tooling.
9
9
  `@ontrails/source` owns reusable source-code mechanics:
10
10
 
11
11
  - AST node guards and accessors for the OXC node shapes Trails tooling uses.
12
- - `parse` and `parseWithDiagnostics` wrappers over `oxc-parser`.
12
+ - `parse` and `parseWithDiagnostics` wrappers over `oxc-parser`, including
13
+ parser-native comment spans for tools that must distinguish source trivia.
13
14
  - `walk`, parent-aware walking, and scope-aware walking over `oxc-walker`.
14
15
  - Source locations, source edits, literal extraction, and generic Trails syntax recognition.
15
16
  - Generic trail/entity discovery helpers such as `findTrailDefinitions`, `findImplementationBodies`, `findEntityDefinitions`, and `isImplementationCall`.
@@ -44,6 +45,21 @@ const ast = parse(
44
45
  const trailIds = ast ? findTrailDefinitions(ast).map((trail) => trail.id) : [];
45
46
  ```
46
47
 
48
+ Inspect exact comment spans without rebuilding a JavaScript or TypeScript lexer:
49
+
50
+ ```ts
51
+ import { parseWithDiagnostics } from '@ontrails/source';
52
+
53
+ const sourceCode = '/** Describe a trail. */\nexport const show = 1;\n';
54
+ const parsed = parseWithDiagnostics('example.ts', sourceCode);
55
+ const comments = parsed.comments.map((comment) => ({
56
+ ...comment,
57
+ source: sourceCode.slice(comment.start, comment.end),
58
+ }));
59
+ ```
60
+
61
+ Comment spans are returned only when the parser reports no diagnostics. Tools must treat an empty comment inventory on a recovered parse as unknown rather than as proof that the source contains no comments.
62
+
47
63
  Walk source with parent context:
48
64
 
49
65
  ```ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ontrails/source",
3
- "version": "1.0.0-beta.45",
3
+ "version": "1.0.0-beta.46",
4
4
  "description": "Shared source-code AST parsing, walking, location, edit, literal, and Trails syntax helpers.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/nodes.ts CHANGED
@@ -245,8 +245,19 @@ export interface AstParseDiagnostic {
245
245
  readonly severity: string;
246
246
  }
247
247
 
248
+ /** Parser-native source comment span. Offsets include the comment delimiters. */
249
+ export interface SourceComment {
250
+ readonly end: number;
251
+ readonly start: number;
252
+ readonly type: 'Block' | 'Line';
253
+ /** Comment text without the line or block delimiters. */
254
+ readonly value: string;
255
+ }
256
+
248
257
  export interface AstParseResult {
249
258
  readonly ast: AstNode | null;
259
+ /** Exact parser-native comments for a diagnostic-free parse; empty on errors. */
260
+ readonly comments: readonly SourceComment[];
250
261
  readonly diagnostics: readonly AstParseDiagnostic[];
251
262
  }
252
263
 
package/src/parse.ts CHANGED
@@ -25,22 +25,25 @@ export const parseWithDiagnostics = (
25
25
  ): AstParseResult => {
26
26
  try {
27
27
  const result = parseSync(filePath, sourceCode, { sourceType: 'module' });
28
+ const diagnostics = result.errors.map((error) => ({
29
+ helpMessage: error.helpMessage,
30
+ labels: error.labels.map((label) => ({
31
+ end: label.end,
32
+ message: label.message,
33
+ start: label.start,
34
+ })),
35
+ message: error.message,
36
+ severity: error.severity,
37
+ }));
28
38
  return {
29
39
  ast: result.program as unknown as AstNode,
30
- diagnostics: result.errors.map((error) => ({
31
- helpMessage: error.helpMessage,
32
- labels: error.labels.map((label) => ({
33
- end: label.end,
34
- message: label.message,
35
- start: label.start,
36
- })),
37
- message: error.message,
38
- severity: error.severity,
39
- })),
40
+ comments: diagnostics.length === 0 ? result.comments : [],
41
+ diagnostics,
40
42
  };
41
43
  } catch (error) {
42
44
  return {
43
45
  ast: null,
46
+ comments: [],
44
47
  diagnostics: [
45
48
  {
46
49
  helpMessage: null,