boperators 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 +202 -0
- package/dist/cjs/core/BopConfig.js +73 -0
- package/dist/cjs/core/ErrorManager.js +126 -0
- package/dist/cjs/core/OverloadInjector.js +136 -0
- package/dist/cjs/core/OverloadStore.js +622 -0
- package/dist/cjs/core/SourceMap.js +168 -0
- package/dist/cjs/core/helpers/ensureImportedName.js +50 -0
- package/dist/cjs/core/helpers/getImportedNameForSymbol.js +64 -0
- package/dist/cjs/core/helpers/getModuleSpecifier.js +61 -0
- package/dist/cjs/core/helpers/getOperatorStringFromProperty.js +33 -0
- package/dist/cjs/core/helpers/resolveExpressionType.js +30 -0
- package/dist/cjs/core/helpers/unwrapInitializer.js +18 -0
- package/dist/cjs/core/operatorMap.js +95 -0
- package/dist/cjs/core/validateExports.js +87 -0
- package/dist/cjs/index.js +36 -0
- package/dist/cjs/lib/index.js +17 -0
- package/dist/cjs/lib/operatorSymbols.js +37 -0
- package/dist/esm/core/BopConfig.d.ts +34 -0
- package/dist/esm/core/BopConfig.js +65 -0
- package/dist/esm/core/ErrorManager.d.ts +90 -0
- package/dist/esm/core/ErrorManager.js +118 -0
- package/dist/esm/core/OverloadInjector.d.ts +33 -0
- package/dist/esm/core/OverloadInjector.js +129 -0
- package/dist/esm/core/OverloadStore.d.ts +114 -0
- package/dist/esm/core/OverloadStore.js +618 -0
- package/dist/esm/core/SourceMap.d.ts +41 -0
- package/dist/esm/core/SourceMap.js +164 -0
- package/dist/esm/core/helpers/ensureImportedName.d.ts +11 -0
- package/dist/esm/core/helpers/ensureImportedName.js +46 -0
- package/dist/esm/core/helpers/getImportedNameForSymbol.d.ts +8 -0
- package/dist/esm/core/helpers/getImportedNameForSymbol.js +60 -0
- package/dist/esm/core/helpers/getModuleSpecifier.d.ts +2 -0
- package/dist/esm/core/helpers/getModuleSpecifier.js +57 -0
- package/dist/esm/core/helpers/getOperatorStringFromProperty.d.ts +13 -0
- package/dist/esm/core/helpers/getOperatorStringFromProperty.js +30 -0
- package/dist/esm/core/helpers/resolveExpressionType.d.ts +11 -0
- package/dist/esm/core/helpers/resolveExpressionType.js +27 -0
- package/dist/esm/core/helpers/unwrapInitializer.d.ts +9 -0
- package/dist/esm/core/helpers/unwrapInitializer.js +15 -0
- package/dist/esm/core/operatorMap.d.ts +77 -0
- package/dist/esm/core/operatorMap.js +89 -0
- package/dist/esm/core/validateExports.d.ts +30 -0
- package/dist/esm/core/validateExports.js +84 -0
- package/dist/esm/index.d.ts +19 -0
- package/dist/esm/index.js +14 -0
- package/dist/esm/lib/index.d.ts +1 -0
- package/dist/esm/lib/index.js +1 -0
- package/dist/esm/lib/operatorSymbols.d.ts +33 -0
- package/dist/esm/lib/operatorSymbols.js +34 -0
- package/license.txt +8 -0
- package/package.json +54 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bidirectional position mapping between original and transformed source text.
|
|
3
|
+
*
|
|
4
|
+
* Computes a list of edit records by diffing the two texts, then provides
|
|
5
|
+
* O(edits) position and span mapping in both directions.
|
|
6
|
+
*/
|
|
7
|
+
export class SourceMap {
|
|
8
|
+
constructor(original, transformed) {
|
|
9
|
+
this.edits = computeEdits(original, transformed);
|
|
10
|
+
}
|
|
11
|
+
/** Returns true if no edits were detected (original === transformed). */
|
|
12
|
+
get isEmpty() {
|
|
13
|
+
return this.edits.length === 0;
|
|
14
|
+
}
|
|
15
|
+
/** Map a position from original source to transformed source. */
|
|
16
|
+
originalToTransformed(pos) {
|
|
17
|
+
let delta = 0;
|
|
18
|
+
for (const edit of this.edits) {
|
|
19
|
+
if (pos < edit.origStart) {
|
|
20
|
+
// Before this edit — just apply accumulated delta
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
if (pos < edit.origEnd) {
|
|
24
|
+
// Inside an edited region — map to start of the transformed replacement
|
|
25
|
+
return edit.transStart;
|
|
26
|
+
}
|
|
27
|
+
// Past this edit — accumulate the delta
|
|
28
|
+
delta = edit.transEnd - edit.origEnd;
|
|
29
|
+
}
|
|
30
|
+
return pos + delta;
|
|
31
|
+
}
|
|
32
|
+
/** Map a position from transformed source to original source. */
|
|
33
|
+
transformedToOriginal(pos) {
|
|
34
|
+
let delta = 0;
|
|
35
|
+
for (const edit of this.edits) {
|
|
36
|
+
if (pos < edit.transStart) {
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (pos < edit.transEnd) {
|
|
40
|
+
// Inside a transformed region — map to start of the original span
|
|
41
|
+
return edit.origStart;
|
|
42
|
+
}
|
|
43
|
+
delta = edit.origEnd - edit.transEnd;
|
|
44
|
+
}
|
|
45
|
+
return pos + delta;
|
|
46
|
+
}
|
|
47
|
+
/** Map a text span { start, length } from transformed positions to original positions. */
|
|
48
|
+
remapSpan(span) {
|
|
49
|
+
const origStart = this.transformedToOriginal(span.start);
|
|
50
|
+
const origEnd = this.transformedToOriginal(span.start + span.length);
|
|
51
|
+
return { start: origStart, length: origEnd - origStart };
|
|
52
|
+
}
|
|
53
|
+
/** Check if an original-source position falls inside an edited region. */
|
|
54
|
+
isInsideEdit(originalPos) {
|
|
55
|
+
for (const edit of this.edits) {
|
|
56
|
+
if (originalPos < edit.origStart)
|
|
57
|
+
return false;
|
|
58
|
+
if (originalPos < edit.origEnd)
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* For a position inside an edited region (in original coords),
|
|
65
|
+
* return the EditRecord it falls in, or undefined.
|
|
66
|
+
*/
|
|
67
|
+
getEditAt(originalPos) {
|
|
68
|
+
for (const edit of this.edits) {
|
|
69
|
+
if (originalPos < edit.origStart)
|
|
70
|
+
return undefined;
|
|
71
|
+
if (originalPos < edit.origEnd)
|
|
72
|
+
return edit;
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Compute edit records by scanning both texts for mismatches.
|
|
79
|
+
*
|
|
80
|
+
* Uses character-level comparison with anchor-based convergence:
|
|
81
|
+
* identical characters are skipped, mismatches start an edit region,
|
|
82
|
+
* and convergence is found by searching for a matching anchor substring
|
|
83
|
+
* in the remaining text.
|
|
84
|
+
*/
|
|
85
|
+
function computeEdits(original, transformed) {
|
|
86
|
+
if (original === transformed)
|
|
87
|
+
return [];
|
|
88
|
+
const edits = [];
|
|
89
|
+
let i = 0; // index in original
|
|
90
|
+
let j = 0; // index in transformed
|
|
91
|
+
while (i < original.length && j < transformed.length) {
|
|
92
|
+
// Skip matching characters
|
|
93
|
+
if (original[i] === transformed[j]) {
|
|
94
|
+
i++;
|
|
95
|
+
j++;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
// Mismatch — start of an edit
|
|
99
|
+
const origEditStart = i;
|
|
100
|
+
const transEditStart = j;
|
|
101
|
+
// Find where the texts converge again.
|
|
102
|
+
// Search for an anchor: a substring from `original` that also
|
|
103
|
+
// appears at the corresponding position in `transformed`.
|
|
104
|
+
const ANCHOR_LEN = 8;
|
|
105
|
+
let found = false;
|
|
106
|
+
// Scan ahead in original from the mismatch point
|
|
107
|
+
for (let oi = origEditStart + 1; oi <= original.length - ANCHOR_LEN; oi++) {
|
|
108
|
+
const anchor = original.substring(oi, oi + ANCHOR_LEN);
|
|
109
|
+
const transPos = transformed.indexOf(anchor, transEditStart);
|
|
110
|
+
if (transPos >= 0) {
|
|
111
|
+
// Verify the anchor actually converges by checking a few more chars
|
|
112
|
+
let valid = true;
|
|
113
|
+
const verifyLen = Math.min(ANCHOR_LEN * 2, original.length - oi, transformed.length - transPos);
|
|
114
|
+
for (let k = ANCHOR_LEN; k < verifyLen; k++) {
|
|
115
|
+
if (original[oi + k] !== transformed[transPos + k]) {
|
|
116
|
+
valid = false;
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (valid) {
|
|
121
|
+
edits.push({
|
|
122
|
+
origStart: origEditStart,
|
|
123
|
+
origEnd: oi,
|
|
124
|
+
transStart: transEditStart,
|
|
125
|
+
transEnd: transPos,
|
|
126
|
+
});
|
|
127
|
+
i = oi;
|
|
128
|
+
j = transPos;
|
|
129
|
+
found = true;
|
|
130
|
+
break;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (!found) {
|
|
135
|
+
// No convergence — remaining text is all part of the edit.
|
|
136
|
+
// Use common suffix to tighten the bounds.
|
|
137
|
+
let suffixLen = 0;
|
|
138
|
+
while (suffixLen < original.length - origEditStart &&
|
|
139
|
+
suffixLen < transformed.length - transEditStart &&
|
|
140
|
+
original[original.length - 1 - suffixLen] ===
|
|
141
|
+
transformed[transformed.length - 1 - suffixLen]) {
|
|
142
|
+
suffixLen++;
|
|
143
|
+
}
|
|
144
|
+
edits.push({
|
|
145
|
+
origStart: origEditStart,
|
|
146
|
+
origEnd: original.length - suffixLen,
|
|
147
|
+
transStart: transEditStart,
|
|
148
|
+
transEnd: transformed.length - suffixLen,
|
|
149
|
+
});
|
|
150
|
+
i = original.length;
|
|
151
|
+
j = transformed.length;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
// Handle remaining text at the end
|
|
155
|
+
if (i < original.length || j < transformed.length) {
|
|
156
|
+
edits.push({
|
|
157
|
+
origStart: i,
|
|
158
|
+
origEnd: original.length,
|
|
159
|
+
transStart: j,
|
|
160
|
+
transEnd: transformed.length,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
return edits;
|
|
164
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Symbol as AstSymbol, type SourceFile } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Ensures that the specified symbol is imported into the source file.
|
|
4
|
+
* If it is not already imported, adds it.
|
|
5
|
+
*
|
|
6
|
+
* @param sourceFile The SourceFile to modify.
|
|
7
|
+
* @param symbol The AstSymbol representing the class to import.
|
|
8
|
+
* @param moduleSpecifier The module from which the class should be imported.
|
|
9
|
+
* @returns The imported identifier name to use in the source file.
|
|
10
|
+
*/
|
|
11
|
+
export declare const ensureImportedName: (sourceFile: SourceFile, symbol: AstSymbol, moduleSpecifier: string) => string;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { SyntaxKind, } from "ts-morph";
|
|
2
|
+
import { getImportedNameForSymbol } from "./getImportedNameForSymbol";
|
|
3
|
+
/**
|
|
4
|
+
* Ensures that the specified symbol is imported into the source file.
|
|
5
|
+
* If it is not already imported, adds it.
|
|
6
|
+
*
|
|
7
|
+
* @param sourceFile The SourceFile to modify.
|
|
8
|
+
* @param symbol The AstSymbol representing the class to import.
|
|
9
|
+
* @param moduleSpecifier The module from which the class should be imported.
|
|
10
|
+
* @returns The imported identifier name to use in the source file.
|
|
11
|
+
*/
|
|
12
|
+
export const ensureImportedName = (sourceFile, symbol, moduleSpecifier) => {
|
|
13
|
+
// Attempt to get the imported name if it already exists
|
|
14
|
+
const existingName = getImportedNameForSymbol(sourceFile, symbol);
|
|
15
|
+
if (existingName)
|
|
16
|
+
return existingName; // Return the already-imported name
|
|
17
|
+
// Generate a unique name for the new import
|
|
18
|
+
const symbolName = symbol.getName();
|
|
19
|
+
let newImportName = symbolName;
|
|
20
|
+
// Ensure the name doesn't clash with existing identifiers in the source file
|
|
21
|
+
const existingIdentifiers = sourceFile.getDescendantsOfKind(SyntaxKind.Identifier);
|
|
22
|
+
const existingNames = new Set(existingIdentifiers.map((id) => id.getText()));
|
|
23
|
+
while (existingNames.has(newImportName)) {
|
|
24
|
+
newImportName = `${symbolName}_${Math.random().toString(36).substr(2, 5)}`;
|
|
25
|
+
}
|
|
26
|
+
// Check if the module is already imported
|
|
27
|
+
const existingImport = sourceFile
|
|
28
|
+
.getImportDeclarations()
|
|
29
|
+
.find((importDecl) => importDecl.getModuleSpecifierValue() === moduleSpecifier);
|
|
30
|
+
if (existingImport) {
|
|
31
|
+
// Add the new import to the named imports
|
|
32
|
+
existingImport.addNamedImport(newImportName === symbolName
|
|
33
|
+
? symbolName
|
|
34
|
+
: { name: symbolName, alias: newImportName });
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
// Add a new import declaration
|
|
38
|
+
sourceFile.addImportDeclaration({
|
|
39
|
+
moduleSpecifier,
|
|
40
|
+
namedImports: newImportName === symbolName
|
|
41
|
+
? [symbolName]
|
|
42
|
+
: [{ name: symbolName, alias: newImportName }],
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
return newImportName;
|
|
46
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Symbol as AstSymbol, SourceFile } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Checks if the given symbol is already imported in the SourceFile.
|
|
4
|
+
* @param sourceFile The SourceFile to search for imports.
|
|
5
|
+
* @param symbol The AstSymbol to match against imports.
|
|
6
|
+
* @returns The imported identifier name if found, otherwise undefined.
|
|
7
|
+
*/
|
|
8
|
+
export declare const getImportedNameForSymbol: (sourceFile: SourceFile, symbol: AstSymbol) => string | undefined;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if the given symbol is already imported in the SourceFile.
|
|
3
|
+
* @param sourceFile The SourceFile to search for imports.
|
|
4
|
+
* @param symbol The AstSymbol to match against imports.
|
|
5
|
+
* @returns The imported identifier name if found, otherwise undefined.
|
|
6
|
+
*/
|
|
7
|
+
export const getImportedNameForSymbol = (sourceFile, symbol) => {
|
|
8
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
9
|
+
const aliasedSymbol = (_a = symbol.getAliasedSymbol()) !== null && _a !== void 0 ? _a : symbol;
|
|
10
|
+
// Helper function to compare symbols by their declarations
|
|
11
|
+
const symbolsAreEquivalent = (a, b) => {
|
|
12
|
+
if (!a || !b)
|
|
13
|
+
return false;
|
|
14
|
+
const aDecls = a.getDeclarations();
|
|
15
|
+
const bDecls = b.getDeclarations();
|
|
16
|
+
if (aDecls.length !== bDecls.length)
|
|
17
|
+
return false;
|
|
18
|
+
return aDecls.every((aDecl, i) => aDecl.getSourceFile() === bDecls[i].getSourceFile() &&
|
|
19
|
+
aDecl.getStart() === bDecls[i].getStart());
|
|
20
|
+
};
|
|
21
|
+
// Get all import declarations in the source file
|
|
22
|
+
const importDeclarations = sourceFile.getImportDeclarations();
|
|
23
|
+
for (const importDecl of importDeclarations) {
|
|
24
|
+
// Named imports
|
|
25
|
+
const namedImports = importDecl.getNamedImports();
|
|
26
|
+
for (const namedImport of namedImports) {
|
|
27
|
+
const importedSymbol = namedImport.getSymbol();
|
|
28
|
+
if (symbolsAreEquivalent(importedSymbol === null || importedSymbol === void 0 ? void 0 : importedSymbol.getAliasedSymbol(), aliasedSymbol)) {
|
|
29
|
+
return ((_c = (_b = namedImport.getAliasNode()) === null || _b === void 0 ? void 0 : _b.getText()) !== null && _c !== void 0 ? _c : namedImport.getNameNode().getText());
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
// Default import
|
|
33
|
+
const defaultImport = importDecl.getDefaultImport();
|
|
34
|
+
if (defaultImport) {
|
|
35
|
+
const defaultSymbol = (_d = defaultImport.getSymbol()) === null || _d === void 0 ? void 0 : _d.getAliasedSymbol();
|
|
36
|
+
if (symbolsAreEquivalent(defaultSymbol, aliasedSymbol)) {
|
|
37
|
+
// Return the default import name
|
|
38
|
+
return defaultImport.getText();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Check namespace imports
|
|
42
|
+
const namespaceImport = importDecl.getNamespaceImport();
|
|
43
|
+
if (namespaceImport) {
|
|
44
|
+
const namespaceSymbol = namespaceImport.getSymbol();
|
|
45
|
+
if (namespaceSymbol) {
|
|
46
|
+
// Look for the symbol under this namespace
|
|
47
|
+
const exports = (_f = (_e = namespaceSymbol.getAliasedSymbol()) === null || _e === void 0 ? void 0 : _e.getExports()) !== null && _f !== void 0 ? _f : [];
|
|
48
|
+
for (const exportedSymbol of exports) {
|
|
49
|
+
const resolvedExportedSymbol = (_g = exportedSymbol.getAliasedSymbol()) !== null && _g !== void 0 ? _g : exportedSymbol;
|
|
50
|
+
if (symbolsAreEquivalent(resolvedExportedSymbol, aliasedSymbol)) {
|
|
51
|
+
// Return namespace-qualified name (e.g., v3.Vector3)
|
|
52
|
+
return `${namespaceImport.getText()}.${exportedSymbol.getName()}`;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// No matching import found
|
|
59
|
+
return undefined;
|
|
60
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { posix as path } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Walk up from a file path to find the nearest `package.json` with a `name`
|
|
5
|
+
* field. Returns the package name and the directory containing it.
|
|
6
|
+
*/
|
|
7
|
+
function findPackageInfo(filePath) {
|
|
8
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
9
|
+
let dir = path.dirname(normalized);
|
|
10
|
+
while (true) {
|
|
11
|
+
const pkgJsonPath = `${dir}/package.json`;
|
|
12
|
+
if (existsSync(pkgJsonPath)) {
|
|
13
|
+
try {
|
|
14
|
+
const content = readFileSync(pkgJsonPath, "utf-8");
|
|
15
|
+
const pkg = JSON.parse(content);
|
|
16
|
+
if (typeof pkg.name === "string") {
|
|
17
|
+
return { name: pkg.name, dir };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (_a) {
|
|
21
|
+
// Malformed package.json, continue searching up
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const parent = path.dirname(dir);
|
|
25
|
+
if (parent === dir)
|
|
26
|
+
break; // filesystem root
|
|
27
|
+
dir = parent;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Checks if `fromFile` already imports from `pkgName` (or a subpath of it)
|
|
33
|
+
* and returns that specifier if so. Otherwise returns `pkgName` bare.
|
|
34
|
+
*/
|
|
35
|
+
function reuseExistingImportOrReturn(fromFile, pkgName) {
|
|
36
|
+
for (const decl of fromFile.getImportDeclarations()) {
|
|
37
|
+
const specifier = decl.getModuleSpecifierValue();
|
|
38
|
+
if (specifier === pkgName || specifier.startsWith(`${pkgName}/`)) {
|
|
39
|
+
return specifier;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return pkgName;
|
|
43
|
+
}
|
|
44
|
+
export const getModuleSpecifier = (fromFile, toFile) => {
|
|
45
|
+
const toPath = toFile.getFilePath();
|
|
46
|
+
const fromPath = fromFile.getFilePath();
|
|
47
|
+
// Check if source and target belong to different packages
|
|
48
|
+
const toPkg = findPackageInfo(toPath);
|
|
49
|
+
const fromPkg = findPackageInfo(fromPath);
|
|
50
|
+
if (toPkg && fromPkg && toPkg.dir !== fromPkg.dir) {
|
|
51
|
+
return reuseExistingImportOrReturn(fromFile, toPkg.name);
|
|
52
|
+
}
|
|
53
|
+
// Same package (or couldn't determine) — use a relative path
|
|
54
|
+
const fromDir = path.dirname(fromPath);
|
|
55
|
+
const relativePath = path.relative(fromDir, toPath);
|
|
56
|
+
return relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
57
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type PropertyDeclaration } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the operator string from a class property declaration, if it
|
|
4
|
+
* represents an operator overload.
|
|
5
|
+
*
|
|
6
|
+
* Handles three property name styles:
|
|
7
|
+
* - `["+"]` — ComputedPropertyName with a StringLiteral expression
|
|
8
|
+
* - `[Operator.PLUS]` — ComputedPropertyName with an enum member expression
|
|
9
|
+
* - `"+"` — StringLiteral property name
|
|
10
|
+
*
|
|
11
|
+
* Returns `undefined` if the property name doesn't resolve to an operator string.
|
|
12
|
+
*/
|
|
13
|
+
export declare function getOperatorStringFromProperty(property: PropertyDeclaration): string | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Extracts the operator string from a class property declaration, if it
|
|
4
|
+
* represents an operator overload.
|
|
5
|
+
*
|
|
6
|
+
* Handles three property name styles:
|
|
7
|
+
* - `["+"]` — ComputedPropertyName with a StringLiteral expression
|
|
8
|
+
* - `[Operator.PLUS]` — ComputedPropertyName with an enum member expression
|
|
9
|
+
* - `"+"` — StringLiteral property name
|
|
10
|
+
*
|
|
11
|
+
* Returns `undefined` if the property name doesn't resolve to an operator string.
|
|
12
|
+
*/
|
|
13
|
+
export function getOperatorStringFromProperty(property) {
|
|
14
|
+
const nameNode = property.getNameNode();
|
|
15
|
+
if (nameNode.isKind(SyntaxKind.ComputedPropertyName)) {
|
|
16
|
+
const expression = nameNode.getExpression();
|
|
17
|
+
if (expression.isKind(SyntaxKind.StringLiteral)) {
|
|
18
|
+
return expression.getLiteralValue();
|
|
19
|
+
}
|
|
20
|
+
// Handle Operator.PLUS style (enum member access)
|
|
21
|
+
const literalValue = expression.getType().getLiteralValue();
|
|
22
|
+
if (typeof literalValue === "string") {
|
|
23
|
+
return literalValue;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
else if (nameNode.isKind(SyntaxKind.StringLiteral)) {
|
|
27
|
+
return nameNode.getLiteralValue();
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Node } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the effective type name for a node in a binary expression.
|
|
4
|
+
*
|
|
5
|
+
* Handles special cases:
|
|
6
|
+
* - Numeric literals → `"number"`
|
|
7
|
+
* - Boolean literals (not in string context) → `"boolean"`
|
|
8
|
+
* - `"any"` type → falls back to the declared type of the symbol
|
|
9
|
+
* (needed for compound assignments where TS can't infer the result type)
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveExpressionType(node: Node): string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the effective type name for a node in a binary expression.
|
|
4
|
+
*
|
|
5
|
+
* Handles special cases:
|
|
6
|
+
* - Numeric literals → `"number"`
|
|
7
|
+
* - Boolean literals (not in string context) → `"boolean"`
|
|
8
|
+
* - `"any"` type → falls back to the declared type of the symbol
|
|
9
|
+
* (needed for compound assignments where TS can't infer the result type)
|
|
10
|
+
*/
|
|
11
|
+
export function resolveExpressionType(node) {
|
|
12
|
+
var _a;
|
|
13
|
+
let typeName = node.getType().getText();
|
|
14
|
+
if (node.getKind() === SyntaxKind.NumericLiteral) {
|
|
15
|
+
return "number";
|
|
16
|
+
}
|
|
17
|
+
if (node.getKind() !== SyntaxKind.StringLiteral &&
|
|
18
|
+
(typeName === "true" || typeName === "false")) {
|
|
19
|
+
return "boolean";
|
|
20
|
+
}
|
|
21
|
+
if (typeName === "any") {
|
|
22
|
+
const decl = (_a = node.getSymbol()) === null || _a === void 0 ? void 0 : _a.getValueDeclaration();
|
|
23
|
+
if (decl)
|
|
24
|
+
typeName = decl.getType().getText();
|
|
25
|
+
}
|
|
26
|
+
return typeName;
|
|
27
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Expression } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Unwraps `as const` and `satisfies` type assertions from an initializer
|
|
4
|
+
* expression, returning the underlying expression.
|
|
5
|
+
*
|
|
6
|
+
* For example, given `[fn1, fn2] as const`, this returns the
|
|
7
|
+
* `[fn1, fn2]` ArrayLiteralExpression.
|
|
8
|
+
*/
|
|
9
|
+
export declare function unwrapInitializer(initializer: Expression | undefined): Expression | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Node } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Unwraps `as const` and `satisfies` type assertions from an initializer
|
|
4
|
+
* expression, returning the underlying expression.
|
|
5
|
+
*
|
|
6
|
+
* For example, given `[fn1, fn2] as const`, this returns the
|
|
7
|
+
* `[fn1, fn2]` ArrayLiteralExpression.
|
|
8
|
+
*/
|
|
9
|
+
export function unwrapInitializer(initializer) {
|
|
10
|
+
if (initializer && Node.isAsExpression(initializer))
|
|
11
|
+
initializer = initializer.getExpression();
|
|
12
|
+
if (initializer && Node.isSatisfiesExpression(initializer))
|
|
13
|
+
initializer = initializer.getExpression();
|
|
14
|
+
return initializer;
|
|
15
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Maps operator string values to their corresponding TypeScript syntax kind.
|
|
4
|
+
*/
|
|
5
|
+
export declare const operatorMap: {
|
|
6
|
+
readonly "+": SyntaxKind.PlusToken;
|
|
7
|
+
readonly "+=": SyntaxKind.PlusEqualsToken;
|
|
8
|
+
readonly "-": SyntaxKind.MinusToken;
|
|
9
|
+
readonly "-=": SyntaxKind.MinusEqualsToken;
|
|
10
|
+
readonly "*": SyntaxKind.AsteriskToken;
|
|
11
|
+
readonly "*=": SyntaxKind.AsteriskEqualsToken;
|
|
12
|
+
readonly "/": SyntaxKind.SlashToken;
|
|
13
|
+
readonly "/=": SyntaxKind.SlashEqualsToken;
|
|
14
|
+
readonly ">": SyntaxKind.GreaterThanToken;
|
|
15
|
+
readonly ">=": SyntaxKind.GreaterThanEqualsToken;
|
|
16
|
+
readonly "<": SyntaxKind.LessThanToken;
|
|
17
|
+
readonly "<=": SyntaxKind.LessThanEqualsToken;
|
|
18
|
+
readonly "%": SyntaxKind.PercentToken;
|
|
19
|
+
readonly "%=": SyntaxKind.PercentEqualsToken;
|
|
20
|
+
readonly "==": SyntaxKind.EqualsEqualsToken;
|
|
21
|
+
readonly "===": SyntaxKind.EqualsEqualsEqualsToken;
|
|
22
|
+
readonly "!=": SyntaxKind.ExclamationEqualsToken;
|
|
23
|
+
readonly "!==": SyntaxKind.ExclamationEqualsEqualsToken;
|
|
24
|
+
readonly "&&": SyntaxKind.AmpersandAmpersandToken;
|
|
25
|
+
readonly "&&=": SyntaxKind.AmpersandAmpersandEqualsToken;
|
|
26
|
+
readonly "||": SyntaxKind.BarBarToken;
|
|
27
|
+
readonly "||=": SyntaxKind.BarBarEqualsToken;
|
|
28
|
+
readonly "??": SyntaxKind.QuestionQuestionToken;
|
|
29
|
+
};
|
|
30
|
+
export type OperatorString = keyof typeof operatorMap;
|
|
31
|
+
export declare const operatorSyntaxKinds: (SyntaxKind.LessThanToken | SyntaxKind.GreaterThanToken | SyntaxKind.LessThanEqualsToken | SyntaxKind.GreaterThanEqualsToken | SyntaxKind.EqualsEqualsToken | SyntaxKind.ExclamationEqualsToken | SyntaxKind.EqualsEqualsEqualsToken | SyntaxKind.ExclamationEqualsEqualsToken | SyntaxKind.PlusToken | SyntaxKind.MinusToken | SyntaxKind.AsteriskToken | SyntaxKind.SlashToken | SyntaxKind.PercentToken | SyntaxKind.AmpersandAmpersandToken | SyntaxKind.BarBarToken | SyntaxKind.QuestionQuestionToken | SyntaxKind.PlusEqualsToken | SyntaxKind.MinusEqualsToken | SyntaxKind.AsteriskEqualsToken | SyntaxKind.SlashEqualsToken | SyntaxKind.PercentEqualsToken | SyntaxKind.BarBarEqualsToken | SyntaxKind.AmpersandAmpersandEqualsToken)[];
|
|
32
|
+
export type OperatorSyntaxKind = (typeof operatorMap)[OperatorString];
|
|
33
|
+
export declare const isOperatorSyntaxKind: (syntaxKind: SyntaxKind) => syntaxKind is OperatorSyntaxKind;
|
|
34
|
+
/**
|
|
35
|
+
* Set of which operators whose overloads should be instance operators
|
|
36
|
+
* i.e. operate on the LHS object.
|
|
37
|
+
* These should return void.
|
|
38
|
+
*/
|
|
39
|
+
export declare const instanceOperators: Set<OperatorSyntaxKind>;
|
|
40
|
+
/**
|
|
41
|
+
* Set of operators where the expected return type of their overload is a boolean.
|
|
42
|
+
*/
|
|
43
|
+
export declare const comparisonOperators: Set<OperatorSyntaxKind>;
|
|
44
|
+
/**
|
|
45
|
+
* Maps prefix unary operator strings to their corresponding TypeScript syntax kind.
|
|
46
|
+
* Operators like `-` and `+` share SyntaxKind tokens with their binary counterparts;
|
|
47
|
+
* disambiguation happens at the AST node level (PrefixUnaryExpression vs BinaryExpression).
|
|
48
|
+
*/
|
|
49
|
+
export declare const prefixUnaryOperatorMap: {
|
|
50
|
+
readonly "-": SyntaxKind.MinusToken;
|
|
51
|
+
readonly "+": SyntaxKind.PlusToken;
|
|
52
|
+
readonly "!": SyntaxKind.ExclamationToken;
|
|
53
|
+
readonly "~": SyntaxKind.TildeToken;
|
|
54
|
+
};
|
|
55
|
+
export type PrefixUnaryOperatorString = keyof typeof prefixUnaryOperatorMap;
|
|
56
|
+
export declare const prefixUnaryOperatorSyntaxKinds: (SyntaxKind.PlusToken | SyntaxKind.MinusToken | SyntaxKind.ExclamationToken | SyntaxKind.TildeToken)[];
|
|
57
|
+
export type PrefixUnaryOperatorSyntaxKind = (typeof prefixUnaryOperatorMap)[PrefixUnaryOperatorString];
|
|
58
|
+
export declare const isPrefixUnaryOperatorSyntaxKind: (syntaxKind: SyntaxKind) => syntaxKind is PrefixUnaryOperatorSyntaxKind;
|
|
59
|
+
/**
|
|
60
|
+
* Maps postfix unary operator strings to their corresponding TypeScript syntax kind.
|
|
61
|
+
*/
|
|
62
|
+
export declare const postfixUnaryOperatorMap: {
|
|
63
|
+
readonly "++": SyntaxKind.PlusPlusToken;
|
|
64
|
+
readonly "--": SyntaxKind.MinusMinusToken;
|
|
65
|
+
};
|
|
66
|
+
export type PostfixUnaryOperatorString = keyof typeof postfixUnaryOperatorMap;
|
|
67
|
+
export declare const postfixUnaryOperatorSyntaxKinds: (SyntaxKind.PlusPlusToken | SyntaxKind.MinusMinusToken)[];
|
|
68
|
+
export type PostfixUnaryOperatorSyntaxKind = (typeof postfixUnaryOperatorMap)[PostfixUnaryOperatorString];
|
|
69
|
+
export declare const isPostfixUnaryOperatorSyntaxKind: (syntaxKind: SyntaxKind) => syntaxKind is PostfixUnaryOperatorSyntaxKind;
|
|
70
|
+
/**
|
|
71
|
+
* Set of operator strings that can appear as prefix unary overloads.
|
|
72
|
+
*/
|
|
73
|
+
export declare const prefixUnaryOperatorStrings: Set<string>;
|
|
74
|
+
/**
|
|
75
|
+
* Set of operator strings that can appear as postfix unary overloads.
|
|
76
|
+
*/
|
|
77
|
+
export declare const postfixUnaryOperatorStrings: Set<string>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
import { Operator } from "../lib/operatorSymbols";
|
|
3
|
+
/**
|
|
4
|
+
* Maps operator string values to their corresponding TypeScript syntax kind.
|
|
5
|
+
*/
|
|
6
|
+
export const operatorMap = {
|
|
7
|
+
[Operator.PLUS]: SyntaxKind.PlusToken,
|
|
8
|
+
[Operator.PLUS_EQUALS]: SyntaxKind.PlusEqualsToken,
|
|
9
|
+
[Operator.MINUS]: SyntaxKind.MinusToken,
|
|
10
|
+
[Operator.MINUS_EQUALS]: SyntaxKind.MinusEqualsToken,
|
|
11
|
+
[Operator.MULTIPLY]: SyntaxKind.AsteriskToken,
|
|
12
|
+
[Operator.MULTIPLY_EQUALS]: SyntaxKind.AsteriskEqualsToken,
|
|
13
|
+
[Operator.DIVIDE]: SyntaxKind.SlashToken,
|
|
14
|
+
[Operator.DIVIDE_EQUALS]: SyntaxKind.SlashEqualsToken,
|
|
15
|
+
[Operator.GREATER_THAN]: SyntaxKind.GreaterThanToken,
|
|
16
|
+
[Operator.GREATER_THAN_EQUAL_TO]: SyntaxKind.GreaterThanEqualsToken,
|
|
17
|
+
[Operator.LESS_THAN]: SyntaxKind.LessThanToken,
|
|
18
|
+
[Operator.LESS_THAN_EQUAL_TO]: SyntaxKind.LessThanEqualsToken,
|
|
19
|
+
[Operator.MODULO]: SyntaxKind.PercentToken,
|
|
20
|
+
[Operator.MODULO_EQUALS]: SyntaxKind.PercentEqualsToken,
|
|
21
|
+
[Operator.EQUALS]: SyntaxKind.EqualsEqualsToken,
|
|
22
|
+
[Operator.STRICT_EQUALS]: SyntaxKind.EqualsEqualsEqualsToken,
|
|
23
|
+
[Operator.NOT_EQUALS]: SyntaxKind.ExclamationEqualsToken,
|
|
24
|
+
[Operator.STRICT_NOT_EQUALS]: SyntaxKind.ExclamationEqualsEqualsToken,
|
|
25
|
+
[Operator.AND]: SyntaxKind.AmpersandAmpersandToken,
|
|
26
|
+
[Operator.AND_EQUALS]: SyntaxKind.AmpersandAmpersandEqualsToken,
|
|
27
|
+
[Operator.OR]: SyntaxKind.BarBarToken,
|
|
28
|
+
[Operator.OR_EQUALS]: SyntaxKind.BarBarEqualsToken,
|
|
29
|
+
[Operator.NULLISH]: SyntaxKind.QuestionQuestionToken,
|
|
30
|
+
};
|
|
31
|
+
export const operatorSyntaxKinds = Object.values(operatorMap);
|
|
32
|
+
export const isOperatorSyntaxKind = (syntaxKind) => operatorSyntaxKinds.includes(syntaxKind);
|
|
33
|
+
/**
|
|
34
|
+
* Set of which operators whose overloads should be instance operators
|
|
35
|
+
* i.e. operate on the LHS object.
|
|
36
|
+
* These should return void.
|
|
37
|
+
*/
|
|
38
|
+
export const instanceOperators = new Set([
|
|
39
|
+
operatorMap[Operator.PLUS_EQUALS],
|
|
40
|
+
operatorMap[Operator.MINUS_EQUALS],
|
|
41
|
+
operatorMap[Operator.MULTIPLY_EQUALS],
|
|
42
|
+
operatorMap[Operator.DIVIDE_EQUALS],
|
|
43
|
+
operatorMap[Operator.MODULO_EQUALS],
|
|
44
|
+
operatorMap[Operator.AND_EQUALS],
|
|
45
|
+
operatorMap[Operator.OR_EQUALS],
|
|
46
|
+
]);
|
|
47
|
+
/**
|
|
48
|
+
* Set of operators where the expected return type of their overload is a boolean.
|
|
49
|
+
*/
|
|
50
|
+
export const comparisonOperators = new Set([
|
|
51
|
+
operatorMap[Operator.GREATER_THAN],
|
|
52
|
+
operatorMap[Operator.GREATER_THAN_EQUAL_TO],
|
|
53
|
+
operatorMap[Operator.LESS_THAN],
|
|
54
|
+
operatorMap[Operator.LESS_THAN_EQUAL_TO],
|
|
55
|
+
operatorMap[Operator.EQUALS],
|
|
56
|
+
operatorMap[Operator.STRICT_EQUALS],
|
|
57
|
+
operatorMap[Operator.NOT_EQUALS],
|
|
58
|
+
operatorMap[Operator.STRICT_NOT_EQUALS],
|
|
59
|
+
]);
|
|
60
|
+
/**
|
|
61
|
+
* Maps prefix unary operator strings to their corresponding TypeScript syntax kind.
|
|
62
|
+
* Operators like `-` and `+` share SyntaxKind tokens with their binary counterparts;
|
|
63
|
+
* disambiguation happens at the AST node level (PrefixUnaryExpression vs BinaryExpression).
|
|
64
|
+
*/
|
|
65
|
+
export const prefixUnaryOperatorMap = {
|
|
66
|
+
[Operator.MINUS]: SyntaxKind.MinusToken,
|
|
67
|
+
[Operator.PLUS]: SyntaxKind.PlusToken,
|
|
68
|
+
[Operator.NOT]: SyntaxKind.ExclamationToken,
|
|
69
|
+
[Operator.BITWISE_NOT]: SyntaxKind.TildeToken,
|
|
70
|
+
};
|
|
71
|
+
export const prefixUnaryOperatorSyntaxKinds = Object.values(prefixUnaryOperatorMap);
|
|
72
|
+
export const isPrefixUnaryOperatorSyntaxKind = (syntaxKind) => prefixUnaryOperatorSyntaxKinds.includes(syntaxKind);
|
|
73
|
+
/**
|
|
74
|
+
* Maps postfix unary operator strings to their corresponding TypeScript syntax kind.
|
|
75
|
+
*/
|
|
76
|
+
export const postfixUnaryOperatorMap = {
|
|
77
|
+
[Operator.INCREMENT]: SyntaxKind.PlusPlusToken,
|
|
78
|
+
[Operator.DECREMENT]: SyntaxKind.MinusMinusToken,
|
|
79
|
+
};
|
|
80
|
+
export const postfixUnaryOperatorSyntaxKinds = Object.values(postfixUnaryOperatorMap);
|
|
81
|
+
export const isPostfixUnaryOperatorSyntaxKind = (syntaxKind) => postfixUnaryOperatorSyntaxKinds.includes(syntaxKind);
|
|
82
|
+
/**
|
|
83
|
+
* Set of operator strings that can appear as prefix unary overloads.
|
|
84
|
+
*/
|
|
85
|
+
export const prefixUnaryOperatorStrings = new Set(Object.keys(prefixUnaryOperatorMap));
|
|
86
|
+
/**
|
|
87
|
+
* Set of operator strings that can appear as postfix unary overloads.
|
|
88
|
+
*/
|
|
89
|
+
export const postfixUnaryOperatorStrings = new Set(Object.keys(postfixUnaryOperatorMap));
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Project as TsMorphProject } from "ts-morph";
|
|
2
|
+
import type { OverloadStore } from "./OverloadStore";
|
|
3
|
+
export type ExportViolationReason = "not-exported-from-file" | "not-reachable-from-entry";
|
|
4
|
+
export type ExportViolation = {
|
|
5
|
+
className: string;
|
|
6
|
+
classFilePath: string;
|
|
7
|
+
reason: ExportViolationReason;
|
|
8
|
+
};
|
|
9
|
+
export type ValidateExportsResult = {
|
|
10
|
+
violations: ExportViolation[];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Validate that all classes with registered operator overloads are exported
|
|
14
|
+
* and (optionally) reachable from a package entry point.
|
|
15
|
+
*
|
|
16
|
+
* @param project The ts-morph project containing the source files.
|
|
17
|
+
* @param overloadStore Populated OverloadStore for the project.
|
|
18
|
+
* @param projectDir Absolute path to the project root — used to filter out
|
|
19
|
+
* overloads sourced from library dependencies.
|
|
20
|
+
* @param entryPoint Absolute path to the source entry point (e.g. `src/index.ts`).
|
|
21
|
+
* When provided, a second pass checks that every overload
|
|
22
|
+
* class is transitively reachable from this file via its
|
|
23
|
+
* export graph. Omit to run the file-level check only.
|
|
24
|
+
*/
|
|
25
|
+
export declare function validateExports({ project, overloadStore, projectDir, entryPoint, }: {
|
|
26
|
+
project: TsMorphProject;
|
|
27
|
+
overloadStore: OverloadStore;
|
|
28
|
+
projectDir: string;
|
|
29
|
+
entryPoint?: string;
|
|
30
|
+
}): ValidateExportsResult;
|