@macroforge/typescript-plugin 0.1.33 → 0.1.34

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
@@ -1,10 +1,53 @@
1
1
  # @macroforge/typescript-plugin
2
2
 
3
- > **Warning:** This is a work in progress and probably won't work for you. Use at your own risk!
3
+ TypeScript language service plugin that augments classes decorated with @derive to include macro-generated methods.
4
4
 
5
- TypeScript language service plugin for macroforge compile-time macros.
5
+ [![npm version](https://badge.fury.io/js/%40macroforge%2Ftypescript-plugin.svg)](https://www.npmjs.com/package/@macroforge/typescript-plugin)
6
6
 
7
- Part of the [macroforge](https://github.com/rymskip/macroforge-ts) project.
7
+ ## Overview
8
+
9
+ TypeScript Language Service Plugin for Macroforge
10
+
11
+ This plugin integrates Macroforge's compile-time macro expansion with TypeScript's
12
+ Language Service to provide seamless IDE support for macro-decorated classes.
13
+
14
+ ## Architecture Overview
15
+
16
+ The plugin operates by intercepting TypeScript's Language Service methods and
17
+ transforming source code on-the-fly:
18
+
19
+ 1. **Macro Expansion**: When TypeScript requests a file's content via `getScriptSnapshot`,
20
+ this plugin intercepts the call and returns the macro-expanded version instead.
21
+
22
+ 2. **Position Mapping**: Since expanded code has different positions than the original,
23
+ the plugin maintains a {@link PositionMapper} for each file to translate positions
24
+ between original and expanded coordinates.
25
+
26
+ 3. **Virtual .d.ts Files**: For each macro-containing file, the plugin generates a
27
+ companion `.macroforge.d.ts` file containing type declarations for generated methods.
28
+
29
+ ## Supported File Types
30
+
31
+ - `.ts` - TypeScript files
32
+ - `.tsx` - TypeScript JSX files
33
+ - `.svelte` - Svelte components (with `<script lang="ts">`)
34
+
35
+ ## Hook Categories
36
+
37
+ The plugin hooks into three categories of Language Service methods:
38
+
39
+ - **Host-level hooks**: Control what TypeScript "sees" (`getScriptSnapshot`, `fileExists`, etc.)
40
+ - **Diagnostic hooks**: Map error positions back to original source (`getSemanticDiagnostics`)
41
+ - **Navigation hooks**: Handle go-to-definition, references, completions, etc.
42
+
43
+ @example
44
+ ```typescript
45
+ {
46
+ "compilerOptions": {
47
+ "plugins": [{ "name": "@macroforge/typescript-plugin" }]
48
+ }
49
+ }
50
+ ```
8
51
 
9
52
  ## Installation
10
53
 
@@ -12,20 +55,40 @@ Part of the [macroforge](https://github.com/rymskip/macroforge-ts) project.
12
55
  npm install @macroforge/typescript-plugin
13
56
  ```
14
57
 
15
- ## Usage
58
+ ## API
59
+
60
+ ### Functions
16
61
 
17
- Add to your `tsconfig.json`:
62
+ - **`findDeriveAtPosition`** - Finds a macro name within `@derive(...)` decorators at a given cursor position.
63
+ - **`findDecoratorAtPosition`** - Finds a field decorator (like `@serde` or `@debug`) at a given cursor position.
64
+ - **`getMacroHoverInfo`** - const lastCommentEnd = beforeMatch.lastIndexOf("*/
65
+ - **`shouldProcess`** - Determines whether a file should be processed for macro expansion.
66
+ - **`hasMacroDirectives`** - Performs a quick check to determine if a file contains any macro-related directives.
67
+ - **`loadMacroConfig`** - Whether to preserve decorator syntax in the expanded output.
68
+ - **`init`** - Main plugin factory function conforming to the TypeScript Language Service Plugin API.
69
+ - **`create`** - Creates the plugin instance for a TypeScript project.
70
+ - **`processFile`** - Processes a file through macro expansion via the native Rust plugin.
71
+ - **`toPlainDiagnostic`** - Converts a TypeScript diagnostic to a plain object for the native plugin.
72
+ - ... and 1 more
18
73
 
19
- ```json
74
+ ### Types
75
+
76
+ - **`MacroConfig`** - Configuration options loaded from `macroforge.json`.
77
+
78
+ ## Examples
79
+
80
+ ```typescript
20
81
  {
21
- "compilerOptions": {
22
- "plugins": [
23
- { "name": "@macroforge/typescript-plugin" }
24
- ]
25
- }
82
+ "compilerOptions": {
83
+ "plugins": [{ "name": "@macroforge/typescript-plugin" }]
84
+ }
26
85
  }
27
86
  ```
28
87
 
88
+ ## Documentation
89
+
90
+ See the [full documentation](https://macroforge.dev/docs/api/reference/typescript/typescript-plugin) on the Macroforge website.
91
+
29
92
  ## License
30
93
 
31
94
  MIT
package/dist/index.d.ts CHANGED
@@ -1,4 +1,111 @@
1
+ /**
2
+ * @fileoverview TypeScript Language Service Plugin for Macroforge
3
+ *
4
+ * This plugin integrates Macroforge's compile-time macro expansion with TypeScript's
5
+ * Language Service to provide seamless IDE support for macro-decorated classes.
6
+ *
7
+ * ## Architecture Overview
8
+ *
9
+ * The plugin operates by intercepting TypeScript's Language Service methods and
10
+ * transforming source code on-the-fly:
11
+ *
12
+ * 1. **Macro Expansion**: When TypeScript requests a file's content via `getScriptSnapshot`,
13
+ * this plugin intercepts the call and returns the macro-expanded version instead.
14
+ *
15
+ * 2. **Position Mapping**: Since expanded code has different positions than the original,
16
+ * the plugin maintains a {@link PositionMapper} for each file to translate positions
17
+ * between original and expanded coordinates.
18
+ *
19
+ * 3. **Virtual .d.ts Files**: For each macro-containing file, the plugin generates a
20
+ * companion `.macroforge.d.ts` file containing type declarations for generated methods.
21
+ *
22
+ * ## Supported File Types
23
+ *
24
+ * - `.ts` - TypeScript files
25
+ * - `.tsx` - TypeScript JSX files
26
+ * - `.svelte` - Svelte components (with `<script lang="ts">`)
27
+ *
28
+ * ## Hook Categories
29
+ *
30
+ * The plugin hooks into three categories of Language Service methods:
31
+ *
32
+ * - **Host-level hooks**: Control what TypeScript "sees" (`getScriptSnapshot`, `fileExists`, etc.)
33
+ * - **Diagnostic hooks**: Map error positions back to original source (`getSemanticDiagnostics`)
34
+ * - **Navigation hooks**: Handle go-to-definition, references, completions, etc.
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * // tsconfig.json
39
+ * {
40
+ * "compilerOptions": {
41
+ * "plugins": [{ "name": "@macroforge/typescript-plugin" }]
42
+ * }
43
+ * }
44
+ * ```
45
+ *
46
+ * @see {@link init} - The main plugin factory function
47
+ * @see {@link PositionMapper} - Position mapping between original and expanded code
48
+ * @module @macroforge/typescript-plugin
49
+ */
1
50
  import type ts from "typescript/lib/tsserverlibrary";
51
+ /**
52
+ * Main plugin factory function conforming to the TypeScript Language Service Plugin API.
53
+ *
54
+ * This function is called by TypeScript when the plugin is loaded. It receives the
55
+ * TypeScript module reference and returns an object with a `create` function that
56
+ * TypeScript will call to instantiate the plugin for each project.
57
+ *
58
+ * @param modules - Object containing the TypeScript module reference
59
+ * @param modules.typescript - The TypeScript module (`typescript/lib/tsserverlibrary`)
60
+ * @returns An object with a `create` method that TypeScript calls to instantiate the plugin
61
+ *
62
+ * @remarks
63
+ * The plugin follows the standard TypeScript Language Service Plugin pattern:
64
+ * 1. `init()` is called once when the plugin is loaded
65
+ * 2. `create()` is called for each TypeScript project that uses the plugin
66
+ * 3. The returned LanguageService has hooked methods that intercept TypeScript operations
67
+ *
68
+ * ## Plugin Architecture
69
+ *
70
+ * The plugin maintains several internal data structures:
71
+ * - **virtualDtsFiles**: Stores generated `.macroforge.d.ts` type declaration files
72
+ * - **snapshotCache**: Caches expanded file snapshots for stable identity across TS requests
73
+ * - **processingFiles**: Guards against reentrancy during macro expansion
74
+ * - **nativePlugin**: Rust-backed expansion engine (handles actual macro processing)
75
+ *
76
+ * ## Hooked Methods
77
+ *
78
+ * The plugin hooks into ~22 TypeScript Language Service methods to provide seamless
79
+ * IDE support. These fall into three categories:
80
+ *
81
+ * 1. **Host-level hooks** (what TS "sees"):
82
+ * - `getScriptSnapshot` - Returns expanded code instead of original
83
+ * - `getScriptVersion` - Provides versions for virtual .d.ts files
84
+ * - `getScriptFileNames` - Includes virtual .d.ts in project file list
85
+ * - `fileExists` - Resolves virtual .d.ts files
86
+ *
87
+ * 2. **Diagnostic hooks** (error reporting):
88
+ * - `getSemanticDiagnostics` - Maps error positions, adds macro errors
89
+ * - `getSyntacticDiagnostics` - Maps syntax error positions
90
+ *
91
+ * 3. **Navigation hooks** (IDE features):
92
+ * - `getQuickInfoAtPosition` - Hover information
93
+ * - `getCompletionsAtPosition` - IntelliSense completions
94
+ * - `getDefinitionAtPosition` - Go to definition
95
+ * - `findReferences` - Find all references
96
+ * - ... and many more
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * // This is how TypeScript loads the plugin (internal to TS)
101
+ * const plugin = require('@macroforge/typescript-plugin');
102
+ * const { create } = plugin(modules);
103
+ * const languageService = create(pluginCreateInfo);
104
+ * ```
105
+ *
106
+ * @see {@link shouldProcess} - File filtering logic
107
+ * @see {@link processFile} - Main macro expansion entry point
108
+ */
2
109
  declare function init(modules: {
3
110
  typescript: typeof ts;
4
111
  }): {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,gCAAgC,CAAC;AAoOrD,iBAAS,IAAI,CAAC,OAAO,EAAE;IAAE,UAAU,EAAE,OAAO,EAAE,CAAA;CAAE;mBACxB,EAAE,CAAC,MAAM,CAAC,gBAAgB;EAgoDjD;AAED,SAAS,IAAI,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,OAAO,KAAK,EAAE,MAAM,gCAAgC,CAAC;AAmdrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyDG;AACH,iBAAS,IAAI,CAAC,OAAO,EAAE;IAAE,UAAU,EAAE,OAAO,EAAE,CAAA;CAAE;mBAcxB,EAAE,CAAC,MAAM,CAAC,gBAAgB;EA69DjD;AAED,SAAS,IAAI,CAAC"}