Skip to content

Commit c29786f

Browse files
committed
any types and LICENSE
1 parent edfa435 commit c29786f

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

packages/deparser/config/deparser-versions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"types": "index.d.ts",
3636
"description": "PostgreSQL AST Deparser",
3737
"scripts": {
38-
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
38+
"copy": "copyfiles -f ../../../../LICENSE README.md package.json dist",
3939
"clean": "rimraf dist",
4040
"prepare": "npm run build",
4141
"build": "npm run clean && tsc && tsc -p tsconfig.esm.json && npm run copy"

packages/deparser/scripts/strip-direct-transformer-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const OUTPUT_DIR = 'versions/direct';
1919
// Types to strip and replace with 'any'
2020
const TYPES_TO_STRIP = [
2121
'PG13', 'PG14', 'PG15', 'PG16', 'PG17',
22-
'V13Types', 'V14Types', 'V15Types', 'V16Types', 'V17Types',
2322
'TransformerContext'
2423
];
2524

packages/deparser/scripts/strip-transformer-types.ts

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,17 @@ const OUTPUT_DIR = 'versions';
1919
// Types to strip and replace with 'any'
2020
const TYPES_TO_STRIP = [
2121
'PG13', 'PG14', 'PG15', 'PG16', 'PG17',
22-
'V13Types', 'V14Types', 'V15Types', 'V16Types', 'V17Types',
2322
'TransformerContext'
2423
];
2524

25+
// Check if a type text contains any of the types to strip
26+
function shouldStripType(typeText: string): boolean {
27+
return TYPES_TO_STRIP.some(type => {
28+
// Check for exact match or namespace usage (e.g., PG13.Something)
29+
return typeText === type || typeText.startsWith(type + '.') || typeText.includes('.' + type);
30+
});
31+
}
32+
2633
function stripTypes(sourceFile: ts.SourceFile): string {
2734
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
2835

@@ -60,8 +67,8 @@ function stripTypes(sourceFile: ts.SourceFile): string {
6067

6168
// Replace parameter type annotations
6269
if (ts.isParameter(node) && node.type) {
63-
const typeText = node.type.getText();
64-
if (TYPES_TO_STRIP.some(type => typeText.includes(type))) {
70+
const typeText = node.type.getText(sourceFile);
71+
if (shouldStripType(typeText)) {
6572
return ts.factory.updateParameterDeclaration(
6673
node,
6774
node.modifiers,
@@ -77,7 +84,7 @@ function stripTypes(sourceFile: ts.SourceFile): string {
7784
// Replace variable type annotations
7885
if (ts.isVariableDeclaration(node) && node.type) {
7986
const typeText = node.type.getText();
80-
if (TYPES_TO_STRIP.some(type => typeText.includes(type))) {
87+
if (shouldStripType(typeText)) {
8188
return ts.factory.updateVariableDeclaration(
8289
node,
8390
node.name,
@@ -91,7 +98,7 @@ function stripTypes(sourceFile: ts.SourceFile): string {
9198
// Replace return type annotations
9299
if ((ts.isMethodDeclaration(node) || ts.isFunctionDeclaration(node) || ts.isArrowFunction(node)) && node.type) {
93100
const typeText = node.type.getText();
94-
if (TYPES_TO_STRIP.some(type => typeText.includes(type))) {
101+
if (shouldStripType(typeText)) {
95102
if (ts.isMethodDeclaration(node)) {
96103
return ts.factory.updateMethodDeclaration(
97104
node,
@@ -131,16 +138,20 @@ function stripTypes(sourceFile: ts.SourceFile): string {
131138

132139
// Replace type assertions
133140
if (ts.isAsExpression(node)) {
134-
const typeText = node.type.getText();
135-
if (TYPES_TO_STRIP.some(type => typeText.includes(type))) {
136-
return node.expression; // Remove the assertion entirely
141+
const typeText = node.type.getText(sourceFile);
142+
if (shouldStripType(typeText)) {
143+
// Replace with 'as any' instead of removing entirely
144+
return ts.factory.createAsExpression(
145+
node.expression,
146+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword)
147+
);
137148
}
138149
}
139150

140151
// Handle property declarations with types
141152
if (ts.isPropertyDeclaration(node) && node.type) {
142153
const typeText = node.type.getText();
143-
if (TYPES_TO_STRIP.some(type => typeText.includes(type))) {
154+
if (shouldStripType(typeText)) {
144155
return ts.factory.updatePropertyDeclaration(
145156
node,
146157
node.modifiers,
@@ -155,15 +166,15 @@ function stripTypes(sourceFile: ts.SourceFile): string {
155166
// Handle interface declarations - remove them entirely
156167
if (ts.isInterfaceDeclaration(node)) {
157168
const name = node.name.getText();
158-
if (TYPES_TO_STRIP.some(type => name.includes(type))) {
169+
if (shouldStripType(name)) {
159170
return undefined;
160171
}
161172
}
162173

163174
// Handle type alias declarations - remove them entirely
164175
if (ts.isTypeAliasDeclaration(node)) {
165176
const name = node.name.getText();
166-
if (TYPES_TO_STRIP.some(type => name.includes(type))) {
177+
if (shouldStripType(name)) {
167178
return undefined;
168179
}
169180
}
@@ -183,9 +194,24 @@ function stripTypes(sourceFile: ts.SourceFile): string {
183194
* DO NOT EDIT - Generated by strip-transformer-types.ts
184195
*/
185196
197+
// @ts-nocheck
186198
`;
187199

188-
return headerComment + printer.printFile(transformedSourceFile);
200+
let output = headerComment + printer.printFile(transformedSourceFile);
201+
202+
// Post-process to catch any remaining type references
203+
// This is a fallback for cases where the AST visitor might miss some nodes
204+
205+
// Replace type assertions (e.g., "as PG14.ParseResult")
206+
output = output.replace(/\bas\s+PG\d+\.[A-Za-z_][A-Za-z0-9_]*\b/g, 'as any');
207+
208+
// Replace parameter types (e.g., "node: PG13.ParseResult")
209+
output = output.replace(/:\s*PG\d+\.[A-Za-z_][A-Za-z0-9_]*\b/g, ': any');
210+
211+
// Replace TransformerContext type
212+
output = output.replace(/:\s*TransformerContext\b/g, ': any');
213+
214+
return output;
189215
}
190216

191217
function processFile(filePath: string): void {

0 commit comments

Comments
 (0)