boperators 0.1.4 → 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 +7 -1
- package/dist/{esm/core → core}/OverloadInjector.d.ts +3 -3
- package/dist/core/OverloadInjector.js +154 -0
- package/dist/core/SourceMap.d.ts +20 -0
- package/dist/{cjs/core → core}/SourceMap.js +2 -78
- package/dist/core/v3SourceMap.d.ts +21 -0
- package/dist/core/v3SourceMap.js +121 -0
- package/dist/{esm/index.d.ts → index.d.ts} +3 -1
- package/dist/{cjs/index.js → index.js} +4 -2
- package/package.json +14 -12
- package/dist/cjs/core/OverloadInjector.js +0 -136
- package/dist/cjs/lib/index.js +0 -17
- package/dist/esm/core/BopConfig.js +0 -65
- package/dist/esm/core/ErrorManager.js +0 -118
- package/dist/esm/core/OverloadInjector.js +0 -129
- package/dist/esm/core/OverloadStore.js +0 -624
- package/dist/esm/core/SourceMap.d.ts +0 -41
- package/dist/esm/core/SourceMap.js +0 -164
- package/dist/esm/core/helpers/ensureImportedName.js +0 -46
- package/dist/esm/core/helpers/getImportedNameForSymbol.js +0 -60
- package/dist/esm/core/helpers/getModuleSpecifier.js +0 -57
- package/dist/esm/core/helpers/getOperatorStringFromProperty.js +0 -30
- package/dist/esm/core/helpers/resolveExpressionType.js +0 -37
- package/dist/esm/core/helpers/unwrapInitializer.js +0 -15
- package/dist/esm/core/operatorMap.js +0 -92
- package/dist/esm/core/validateExports.js +0 -84
- package/dist/esm/index.js +0 -14
- package/dist/esm/lib/index.d.ts +0 -1
- package/dist/esm/lib/index.js +0 -1
- package/dist/esm/lib/operatorSymbols.js +0 -36
- /package/dist/{esm/core → core}/BopConfig.d.ts +0 -0
- /package/dist/{cjs/core → core}/BopConfig.js +0 -0
- /package/dist/{esm/core → core}/ErrorManager.d.ts +0 -0
- /package/dist/{cjs/core → core}/ErrorManager.js +0 -0
- /package/dist/{esm/core → core}/OverloadStore.d.ts +0 -0
- /package/dist/{cjs/core → core}/OverloadStore.js +0 -0
- /package/dist/{esm/core → core}/helpers/ensureImportedName.d.ts +0 -0
- /package/dist/{cjs/core → core}/helpers/ensureImportedName.js +0 -0
- /package/dist/{esm/core → core}/helpers/getImportedNameForSymbol.d.ts +0 -0
- /package/dist/{cjs/core → core}/helpers/getImportedNameForSymbol.js +0 -0
- /package/dist/{esm/core → core}/helpers/getModuleSpecifier.d.ts +0 -0
- /package/dist/{cjs/core → core}/helpers/getModuleSpecifier.js +0 -0
- /package/dist/{esm/core → core}/helpers/getOperatorStringFromProperty.d.ts +0 -0
- /package/dist/{cjs/core → core}/helpers/getOperatorStringFromProperty.js +0 -0
- /package/dist/{esm/core → core}/helpers/resolveExpressionType.d.ts +0 -0
- /package/dist/{cjs/core → core}/helpers/resolveExpressionType.js +0 -0
- /package/dist/{esm/core → core}/helpers/unwrapInitializer.d.ts +0 -0
- /package/dist/{cjs/core → core}/helpers/unwrapInitializer.js +0 -0
- /package/dist/{esm/core → core}/operatorMap.d.ts +0 -0
- /package/dist/{cjs/core → core}/operatorMap.js +0 -0
- /package/dist/{esm/core → core}/validateExports.d.ts +0 -0
- /package/dist/{cjs/core → core}/validateExports.js +0 -0
- /package/dist/{esm/lib → lib}/operatorSymbols.d.ts +0 -0
- /package/dist/{cjs/lib → lib}/operatorSymbols.js +0 -0
|
@@ -1,164 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,30 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { SyntaxKind } from "ts-morph";
|
|
2
|
-
/**
|
|
3
|
-
* Strips `import("...").` qualification from a type name as returned by
|
|
4
|
-
* ts-morph's `getType().getText()`. Language-server and cross-package contexts
|
|
5
|
-
* produce fully-qualified names like `import("/path/to/file").ClassName`, but
|
|
6
|
-
* overloads are keyed by short class names.
|
|
7
|
-
*/
|
|
8
|
-
export function normalizeTypeName(typeName) {
|
|
9
|
-
return typeName.replace(/import\("[^"]*"\)\./g, "");
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Resolves the effective type name for a node in a binary expression.
|
|
13
|
-
*
|
|
14
|
-
* Handles special cases:
|
|
15
|
-
* - Numeric literals → `"number"`
|
|
16
|
-
* - Boolean literals (not in string context) → `"boolean"`
|
|
17
|
-
* - `"any"` type → falls back to the declared type of the symbol
|
|
18
|
-
* (needed for compound assignments where TS can't infer the result type)
|
|
19
|
-
* - Qualified type names → stripped to short class name via `normalizeTypeName`
|
|
20
|
-
*/
|
|
21
|
-
export function resolveExpressionType(node) {
|
|
22
|
-
var _a;
|
|
23
|
-
let typeName = node.getType().getText();
|
|
24
|
-
if (node.getKind() === SyntaxKind.NumericLiteral) {
|
|
25
|
-
return "number";
|
|
26
|
-
}
|
|
27
|
-
if (node.getKind() !== SyntaxKind.StringLiteral &&
|
|
28
|
-
(typeName === "true" || typeName === "false")) {
|
|
29
|
-
return "boolean";
|
|
30
|
-
}
|
|
31
|
-
if (typeName === "any") {
|
|
32
|
-
const decl = (_a = node.getSymbol()) === null || _a === void 0 ? void 0 : _a.getValueDeclaration();
|
|
33
|
-
if (decl)
|
|
34
|
-
typeName = decl.getType().getText();
|
|
35
|
-
}
|
|
36
|
-
return normalizeTypeName(typeName);
|
|
37
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
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.EXPONENT]: SyntaxKind.AsteriskAsteriskToken,
|
|
14
|
-
[Operator.EXPONENT_EQUALS]: SyntaxKind.AsteriskAsteriskEqualsToken,
|
|
15
|
-
[Operator.DIVIDE]: SyntaxKind.SlashToken,
|
|
16
|
-
[Operator.DIVIDE_EQUALS]: SyntaxKind.SlashEqualsToken,
|
|
17
|
-
[Operator.GREATER_THAN]: SyntaxKind.GreaterThanToken,
|
|
18
|
-
[Operator.GREATER_THAN_EQUAL_TO]: SyntaxKind.GreaterThanEqualsToken,
|
|
19
|
-
[Operator.LESS_THAN]: SyntaxKind.LessThanToken,
|
|
20
|
-
[Operator.LESS_THAN_EQUAL_TO]: SyntaxKind.LessThanEqualsToken,
|
|
21
|
-
[Operator.MODULO]: SyntaxKind.PercentToken,
|
|
22
|
-
[Operator.MODULO_EQUALS]: SyntaxKind.PercentEqualsToken,
|
|
23
|
-
[Operator.EQUALS]: SyntaxKind.EqualsEqualsToken,
|
|
24
|
-
[Operator.STRICT_EQUALS]: SyntaxKind.EqualsEqualsEqualsToken,
|
|
25
|
-
[Operator.NOT_EQUALS]: SyntaxKind.ExclamationEqualsToken,
|
|
26
|
-
[Operator.STRICT_NOT_EQUALS]: SyntaxKind.ExclamationEqualsEqualsToken,
|
|
27
|
-
[Operator.AND]: SyntaxKind.AmpersandAmpersandToken,
|
|
28
|
-
[Operator.AND_EQUALS]: SyntaxKind.AmpersandAmpersandEqualsToken,
|
|
29
|
-
[Operator.OR]: SyntaxKind.BarBarToken,
|
|
30
|
-
[Operator.OR_EQUALS]: SyntaxKind.BarBarEqualsToken,
|
|
31
|
-
[Operator.NULLISH]: SyntaxKind.QuestionQuestionToken,
|
|
32
|
-
};
|
|
33
|
-
export const operatorSyntaxKinds = Object.values(operatorMap);
|
|
34
|
-
export const isOperatorSyntaxKind = (syntaxKind) => operatorSyntaxKinds.includes(syntaxKind);
|
|
35
|
-
/**
|
|
36
|
-
* Set of which operators whose overloads should be instance operators
|
|
37
|
-
* i.e. operate on the LHS object.
|
|
38
|
-
* These should return void.
|
|
39
|
-
*/
|
|
40
|
-
export const instanceOperators = new Set([
|
|
41
|
-
operatorMap[Operator.PLUS_EQUALS],
|
|
42
|
-
operatorMap[Operator.MINUS_EQUALS],
|
|
43
|
-
operatorMap[Operator.MULTIPLY_EQUALS],
|
|
44
|
-
operatorMap[Operator.EXPONENT_EQUALS],
|
|
45
|
-
operatorMap[Operator.DIVIDE_EQUALS],
|
|
46
|
-
operatorMap[Operator.MODULO_EQUALS],
|
|
47
|
-
operatorMap[Operator.AND_EQUALS],
|
|
48
|
-
operatorMap[Operator.OR_EQUALS],
|
|
49
|
-
]);
|
|
50
|
-
/**
|
|
51
|
-
* Set of operators where the expected return type of their overload is a boolean.
|
|
52
|
-
*/
|
|
53
|
-
export const comparisonOperators = new Set([
|
|
54
|
-
operatorMap[Operator.GREATER_THAN],
|
|
55
|
-
operatorMap[Operator.GREATER_THAN_EQUAL_TO],
|
|
56
|
-
operatorMap[Operator.LESS_THAN],
|
|
57
|
-
operatorMap[Operator.LESS_THAN_EQUAL_TO],
|
|
58
|
-
operatorMap[Operator.EQUALS],
|
|
59
|
-
operatorMap[Operator.STRICT_EQUALS],
|
|
60
|
-
operatorMap[Operator.NOT_EQUALS],
|
|
61
|
-
operatorMap[Operator.STRICT_NOT_EQUALS],
|
|
62
|
-
]);
|
|
63
|
-
/**
|
|
64
|
-
* Maps prefix unary operator strings to their corresponding TypeScript syntax kind.
|
|
65
|
-
* Operators like `-` and `+` share SyntaxKind tokens with their binary counterparts;
|
|
66
|
-
* disambiguation happens at the AST node level (PrefixUnaryExpression vs BinaryExpression).
|
|
67
|
-
*/
|
|
68
|
-
export const prefixUnaryOperatorMap = {
|
|
69
|
-
[Operator.MINUS]: SyntaxKind.MinusToken,
|
|
70
|
-
[Operator.PLUS]: SyntaxKind.PlusToken,
|
|
71
|
-
[Operator.NOT]: SyntaxKind.ExclamationToken,
|
|
72
|
-
[Operator.BITWISE_NOT]: SyntaxKind.TildeToken,
|
|
73
|
-
};
|
|
74
|
-
export const prefixUnaryOperatorSyntaxKinds = Object.values(prefixUnaryOperatorMap);
|
|
75
|
-
export const isPrefixUnaryOperatorSyntaxKind = (syntaxKind) => prefixUnaryOperatorSyntaxKinds.includes(syntaxKind);
|
|
76
|
-
/**
|
|
77
|
-
* Maps postfix unary operator strings to their corresponding TypeScript syntax kind.
|
|
78
|
-
*/
|
|
79
|
-
export const postfixUnaryOperatorMap = {
|
|
80
|
-
[Operator.INCREMENT]: SyntaxKind.PlusPlusToken,
|
|
81
|
-
[Operator.DECREMENT]: SyntaxKind.MinusMinusToken,
|
|
82
|
-
};
|
|
83
|
-
export const postfixUnaryOperatorSyntaxKinds = Object.values(postfixUnaryOperatorMap);
|
|
84
|
-
export const isPostfixUnaryOperatorSyntaxKind = (syntaxKind) => postfixUnaryOperatorSyntaxKinds.includes(syntaxKind);
|
|
85
|
-
/**
|
|
86
|
-
* Set of operator strings that can appear as prefix unary overloads.
|
|
87
|
-
*/
|
|
88
|
-
export const prefixUnaryOperatorStrings = new Set(Object.keys(prefixUnaryOperatorMap));
|
|
89
|
-
/**
|
|
90
|
-
* Set of operator strings that can appear as postfix unary overloads.
|
|
91
|
-
*/
|
|
92
|
-
export const postfixUnaryOperatorStrings = new Set(Object.keys(postfixUnaryOperatorMap));
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import { Node } from "ts-morph";
|
|
2
|
-
/**
|
|
3
|
-
* Validate that all classes with registered operator overloads are exported
|
|
4
|
-
* and (optionally) reachable from a package entry point.
|
|
5
|
-
*
|
|
6
|
-
* @param project The ts-morph project containing the source files.
|
|
7
|
-
* @param overloadStore Populated OverloadStore for the project.
|
|
8
|
-
* @param projectDir Absolute path to the project root — used to filter out
|
|
9
|
-
* overloads sourced from library dependencies.
|
|
10
|
-
* @param entryPoint Absolute path to the source entry point (e.g. `src/index.ts`).
|
|
11
|
-
* When provided, a second pass checks that every overload
|
|
12
|
-
* class is transitively reachable from this file via its
|
|
13
|
-
* export graph. Omit to run the file-level check only.
|
|
14
|
-
*/
|
|
15
|
-
export function validateExports({ project, overloadStore, projectDir, entryPoint, }) {
|
|
16
|
-
var _a;
|
|
17
|
-
const violations = [];
|
|
18
|
-
const normalizedDir = projectDir.replace(/\\/g, "/").replace(/\/$/, "");
|
|
19
|
-
// Deduplicate: collect unique (className, classFilePath) pairs whose
|
|
20
|
-
// source lives inside this project (skip .d.ts from dependencies).
|
|
21
|
-
const seen = new Set();
|
|
22
|
-
const classes = [];
|
|
23
|
-
for (const overload of overloadStore.getAllOverloads()) {
|
|
24
|
-
const normalized = overload.classFilePath.replace(/\\/g, "/");
|
|
25
|
-
const inProject = normalized.startsWith(`${normalizedDir}/`) ||
|
|
26
|
-
normalized === normalizedDir;
|
|
27
|
-
if (!inProject)
|
|
28
|
-
continue;
|
|
29
|
-
if (normalized.endsWith(".d.ts"))
|
|
30
|
-
continue;
|
|
31
|
-
const key = `${overload.className}::${normalized}`;
|
|
32
|
-
if (!seen.has(key)) {
|
|
33
|
-
seen.add(key);
|
|
34
|
-
classes.push({
|
|
35
|
-
className: overload.className,
|
|
36
|
-
classFilePath: overload.classFilePath,
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
// Level 2 setup: resolve the set of class declarations reachable from the
|
|
41
|
-
// entry point by walking the full export graph (follows re-exports).
|
|
42
|
-
let entryExportedClassDecls;
|
|
43
|
-
if (entryPoint) {
|
|
44
|
-
const entryFile = (_a = project.getSourceFile(entryPoint)) !== null && _a !== void 0 ? _a : project.addSourceFileAtPath(entryPoint);
|
|
45
|
-
entryExportedClassDecls = new Set();
|
|
46
|
-
for (const decls of entryFile.getExportedDeclarations().values()) {
|
|
47
|
-
for (const decl of decls) {
|
|
48
|
-
if (Node.isClassDeclaration(decl)) {
|
|
49
|
-
entryExportedClassDecls.add(decl);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
// Check each class.
|
|
55
|
-
for (const { className, classFilePath } of classes) {
|
|
56
|
-
const sourceFile = project.getSourceFile(classFilePath);
|
|
57
|
-
if (!sourceFile)
|
|
58
|
-
continue;
|
|
59
|
-
const classDecl = sourceFile.getClass(className);
|
|
60
|
-
if (!classDecl)
|
|
61
|
-
continue;
|
|
62
|
-
// Level 1: is the class exported from its own source file?
|
|
63
|
-
if (!classDecl.isExported()) {
|
|
64
|
-
violations.push({
|
|
65
|
-
className,
|
|
66
|
-
classFilePath,
|
|
67
|
-
reason: "not-exported-from-file",
|
|
68
|
-
});
|
|
69
|
-
// If not exported from file it cannot be in the entry point graph
|
|
70
|
-
// either — no need to run the deep check for this class.
|
|
71
|
-
continue;
|
|
72
|
-
}
|
|
73
|
-
// Level 2: is the class reachable from the entry point?
|
|
74
|
-
if (entryExportedClassDecls !== undefined &&
|
|
75
|
-
!entryExportedClassDecls.has(classDecl)) {
|
|
76
|
-
violations.push({
|
|
77
|
-
className,
|
|
78
|
-
classFilePath,
|
|
79
|
-
reason: "not-reachable-from-entry",
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return { violations };
|
|
84
|
-
}
|
package/dist/esm/index.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// Core transformation pipeline
|
|
2
|
-
export { ConsoleLogger, loadConfig } from "./core/BopConfig";
|
|
3
|
-
export { ErrorDescription, ErrorManager } from "./core/ErrorManager";
|
|
4
|
-
export { getOperatorStringFromProperty } from "./core/helpers/getOperatorStringFromProperty";
|
|
5
|
-
export { resolveExpressionType } from "./core/helpers/resolveExpressionType";
|
|
6
|
-
export { unwrapInitializer } from "./core/helpers/unwrapInitializer";
|
|
7
|
-
export { OverloadInjector } from "./core/OverloadInjector";
|
|
8
|
-
export { OverloadStore } from "./core/OverloadStore";
|
|
9
|
-
export { isOperatorSyntaxKind, isPostfixUnaryOperatorSyntaxKind, isPrefixUnaryOperatorSyntaxKind, } from "./core/operatorMap";
|
|
10
|
-
export { SourceMap } from "./core/SourceMap";
|
|
11
|
-
export { validateExports } from "./core/validateExports";
|
|
12
|
-
// Operator definitions
|
|
13
|
-
export { Operator, operatorSymbols } from "./lib/operatorSymbols";
|
|
14
|
-
export { Node, Project, SyntaxKind } from "ts-morph";
|
package/dist/esm/lib/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./operatorSymbols";
|
package/dist/esm/lib/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./operatorSymbols";
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Designed for internal use to ensure consistency.
|
|
3
|
-
*/
|
|
4
|
-
export var Operator;
|
|
5
|
-
(function (Operator) {
|
|
6
|
-
Operator["PLUS"] = "+";
|
|
7
|
-
Operator["PLUS_EQUALS"] = "+=";
|
|
8
|
-
Operator["MINUS"] = "-";
|
|
9
|
-
Operator["MINUS_EQUALS"] = "-=";
|
|
10
|
-
Operator["MULTIPLY"] = "*";
|
|
11
|
-
Operator["MULTIPLY_EQUALS"] = "*=";
|
|
12
|
-
Operator["EXPONENT"] = "**";
|
|
13
|
-
Operator["EXPONENT_EQUALS"] = "**=";
|
|
14
|
-
Operator["DIVIDE"] = "/";
|
|
15
|
-
Operator["DIVIDE_EQUALS"] = "/=";
|
|
16
|
-
Operator["GREATER_THAN"] = ">";
|
|
17
|
-
Operator["GREATER_THAN_EQUAL_TO"] = ">=";
|
|
18
|
-
Operator["LESS_THAN"] = "<";
|
|
19
|
-
Operator["LESS_THAN_EQUAL_TO"] = "<=";
|
|
20
|
-
Operator["MODULO"] = "%";
|
|
21
|
-
Operator["MODULO_EQUALS"] = "%=";
|
|
22
|
-
Operator["EQUALS"] = "==";
|
|
23
|
-
Operator["STRICT_EQUALS"] = "===";
|
|
24
|
-
Operator["NOT_EQUALS"] = "!=";
|
|
25
|
-
Operator["STRICT_NOT_EQUALS"] = "!==";
|
|
26
|
-
Operator["AND"] = "&&";
|
|
27
|
-
Operator["AND_EQUALS"] = "&&=";
|
|
28
|
-
Operator["OR"] = "||";
|
|
29
|
-
Operator["OR_EQUALS"] = "||=";
|
|
30
|
-
Operator["NULLISH"] = "??";
|
|
31
|
-
Operator["NOT"] = "!";
|
|
32
|
-
Operator["BITWISE_NOT"] = "~";
|
|
33
|
-
Operator["INCREMENT"] = "++";
|
|
34
|
-
Operator["DECREMENT"] = "--";
|
|
35
|
-
})(Operator || (Operator = {}));
|
|
36
|
-
export const operatorSymbols = Object.values(Operator);
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|