@boperators/plugin-tsc 0.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.
- package/README.md +86 -0
- package/dist/index.js +80 -0
- package/license.txt +8 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# @boperators/plugin-tsc
|
|
2
|
+
|
|
3
|
+
A [ts-patch](https://github.com/package/ts-patch) plugin that runs [boperators](https://www.npmjs.com/package/boperators) transformations during `tsc` compilation.
|
|
4
|
+
|
|
5
|
+
This plugin operates as a **Program Transformer** — it transforms your operator expressions before TypeScript type-checks the code, so the compiler sees valid function calls rather than unsupported operator usage on class types.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install -D boperators ts-patch @boperators/plugin-tsc
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Configuration
|
|
14
|
+
|
|
15
|
+
Add the plugin to your `tsconfig.json`:
|
|
16
|
+
|
|
17
|
+
```json
|
|
18
|
+
{
|
|
19
|
+
"compilerOptions": {
|
|
20
|
+
"plugins": [
|
|
21
|
+
{
|
|
22
|
+
"transform": "@boperators/plugin-tsc",
|
|
23
|
+
"transformProgram": true
|
|
24
|
+
}
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### Options
|
|
31
|
+
|
|
32
|
+
| Option | Type | Default | Description |
|
|
33
|
+
|--------|------|---------|-------------|
|
|
34
|
+
| `transform` | `string` | — | Must be `"@boperators/plugin-tsc"` |
|
|
35
|
+
| `transformProgram` | `boolean` | — | Must be `true` |
|
|
36
|
+
| `errorOnWarning` | `boolean` | `false` | Treat conflicting overload warnings as errors |
|
|
37
|
+
|
|
38
|
+
## Usage
|
|
39
|
+
|
|
40
|
+
Compile with `tspc` (ts-patch's compiler wrapper) instead of `tsc`:
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
npx tspc
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Alternatively, patch your local TypeScript installation so `tsc` itself runs plugins:
|
|
47
|
+
|
|
48
|
+
```sh
|
|
49
|
+
npx ts-patch install
|
|
50
|
+
npx tsc
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To make the patch persist across installs, add a `prepare` script:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{
|
|
57
|
+
"scripts": {
|
|
58
|
+
"prepare": "ts-patch install -s"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## How It Works
|
|
64
|
+
|
|
65
|
+
The plugin runs as a ts-patch Program Transformer, which executes during `ts.createProgram()` — before type-checking:
|
|
66
|
+
|
|
67
|
+
1. Creates a [ts-morph](https://ts-morph.com) Project from your tsconfig
|
|
68
|
+
2. Scans all source files for operator overload definitions
|
|
69
|
+
3. Transforms expressions in project files (e.g. `v1 + v2` becomes `Vector3["+"][0](v1, v2)`)
|
|
70
|
+
4. Returns a new TypeScript Program with the transformed source text
|
|
71
|
+
|
|
72
|
+
TypeScript then type-checks and emits the transformed code, which contains only valid function calls.
|
|
73
|
+
|
|
74
|
+
If transformation fails for any reason, the plugin logs the error and falls back to the original program (where operator expressions will surface as normal TypeScript type errors).
|
|
75
|
+
|
|
76
|
+
## Comparison with Other Approaches
|
|
77
|
+
|
|
78
|
+
| Approach | When it runs | Use case |
|
|
79
|
+
|----------|-------------|----------|
|
|
80
|
+
| **`@boperators/cli`** | Before compilation | Batch transform to disk, then compile normally |
|
|
81
|
+
| **`@boperators/plugin-tsc`** | During compilation | Seamless `tsc` integration, no intermediate files |
|
|
82
|
+
| **`@boperators/plugin-bun`** | At runtime | Bun-only, transforms on module load |
|
|
83
|
+
|
|
84
|
+
## License
|
|
85
|
+
|
|
86
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
6
|
+
const boperators_1 = require("boperators");
|
|
7
|
+
function normalizePath(p) {
|
|
8
|
+
return p.replace(/\\/g, "/");
|
|
9
|
+
}
|
|
10
|
+
const transformer = (program, host, config, extras) => {
|
|
11
|
+
const tsInstance = extras.ts;
|
|
12
|
+
const compilerOptions = program.getCompilerOptions();
|
|
13
|
+
const configFilePath = compilerOptions
|
|
14
|
+
.configFilePath;
|
|
15
|
+
const bopConfig = (0, boperators_1.loadConfig)({
|
|
16
|
+
searchDir: configFilePath ? node_path_1.default.dirname(configFilePath) : undefined,
|
|
17
|
+
overrides: { errorOnWarning: config.errorOnWarning },
|
|
18
|
+
});
|
|
19
|
+
if (!configFilePath) {
|
|
20
|
+
bopConfig.logger.warn("No tsconfig path found; skipping transformation.");
|
|
21
|
+
return program;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
// 1. Create ts-morph project from the same tsconfig
|
|
25
|
+
const tsMorphProject = new boperators_1.Project({
|
|
26
|
+
tsConfigFilePath: configFilePath,
|
|
27
|
+
});
|
|
28
|
+
const errorManager = new boperators_1.ErrorManager(bopConfig);
|
|
29
|
+
const overloadStore = new boperators_1.OverloadStore(tsMorphProject, errorManager, bopConfig.logger);
|
|
30
|
+
const overloadInjector = new boperators_1.OverloadInjector(tsMorphProject, overloadStore, bopConfig.logger);
|
|
31
|
+
// 2. Parse ALL files for overload definitions (including dependencies)
|
|
32
|
+
const allFiles = tsMorphProject.getSourceFiles();
|
|
33
|
+
for (const file of allFiles) {
|
|
34
|
+
overloadStore.addOverloadsFromFile(file);
|
|
35
|
+
}
|
|
36
|
+
errorManager.throwIfErrorsElseLogWarnings();
|
|
37
|
+
// 3. Filter to project files only (same logic as CLI)
|
|
38
|
+
const projectDir = `${normalizePath(node_path_1.default.dirname(configFilePath))}/`;
|
|
39
|
+
const projectFiles = allFiles.filter((file) => {
|
|
40
|
+
const filePath = file.getFilePath();
|
|
41
|
+
if (filePath.endsWith(".d.ts"))
|
|
42
|
+
return false;
|
|
43
|
+
if (filePath.includes("/node_modules/"))
|
|
44
|
+
return false;
|
|
45
|
+
return filePath.startsWith(projectDir);
|
|
46
|
+
});
|
|
47
|
+
// 4. Transform project files and collect results
|
|
48
|
+
const transformedTexts = new Map();
|
|
49
|
+
for (const file of projectFiles) {
|
|
50
|
+
const originalText = file.getFullText();
|
|
51
|
+
const result = overloadInjector.overloadFile(file);
|
|
52
|
+
if (result.text !== originalText) {
|
|
53
|
+
transformedTexts.set(file.getFilePath(), result.text);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
errorManager.throwIfErrorsElseLogWarnings();
|
|
57
|
+
// 5. Short-circuit if nothing changed
|
|
58
|
+
if (transformedTexts.size === 0) {
|
|
59
|
+
return program;
|
|
60
|
+
}
|
|
61
|
+
// 6. Create proxy CompilerHost that returns transformed source files
|
|
62
|
+
const originalHost = host !== null && host !== void 0 ? host : tsInstance.createCompilerHost(compilerOptions);
|
|
63
|
+
const originalGetSourceFile = originalHost.getSourceFile.bind(originalHost);
|
|
64
|
+
const proxyHost = Object.assign(Object.assign({}, originalHost), { getSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile) {
|
|
65
|
+
const normalized = normalizePath(fileName);
|
|
66
|
+
const transformedText = transformedTexts.get(normalized);
|
|
67
|
+
if (transformedText !== undefined) {
|
|
68
|
+
return tsInstance.createSourceFile(fileName, transformedText, languageVersionOrOptions);
|
|
69
|
+
}
|
|
70
|
+
return originalGetSourceFile(fileName, languageVersionOrOptions, onError, shouldCreateNewSourceFile);
|
|
71
|
+
} });
|
|
72
|
+
// 7. Create and return new program with transformed files
|
|
73
|
+
return tsInstance.createProgram(program.getRootFileNames(), compilerOptions, proxyHost);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
bopConfig.logger.error(`Transformation failed: ${error}`);
|
|
77
|
+
return program;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
module.exports = transformer;
|
package/license.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2025 Dief Bell
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@boperators/plugin-tsc",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "ts-patch plugin for boperators - transforms operator overloads during tsc compilation.",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/DiefBell/boperators",
|
|
9
|
+
"directory": "plugins/tsc"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/DiefBell/boperators/tree/main/plugins/tsc",
|
|
12
|
+
"type": "commonjs",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"boperators",
|
|
16
|
+
"typescript",
|
|
17
|
+
"operator",
|
|
18
|
+
"overload",
|
|
19
|
+
"operator-overloading",
|
|
20
|
+
"ts-patch",
|
|
21
|
+
"tsc",
|
|
22
|
+
"transform"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsc",
|
|
26
|
+
"watch": "tsc -w",
|
|
27
|
+
"prepublish": "bun run build"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"package.json",
|
|
31
|
+
"README.md",
|
|
32
|
+
"license.txt",
|
|
33
|
+
"dist"
|
|
34
|
+
],
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"boperators": "0.1.0",
|
|
37
|
+
"typescript": ">=5.0.0 <5.10.0"
|
|
38
|
+
}
|
|
39
|
+
}
|