|
| 1 | +import * as babel from '@babel/core'; |
| 2 | +import type * as BabelTypes from '@babel/types'; |
| 3 | +import { promises as fs } from 'fs'; |
| 4 | +import path from 'path'; |
| 5 | +import { signatures } from '../'; |
| 6 | + |
| 7 | +function applyAsyncRewriterChanges() { |
| 8 | + return ({ |
| 9 | + types: t, |
| 10 | + }: { |
| 11 | + types: typeof BabelTypes; |
| 12 | + }): babel.PluginObj<{ |
| 13 | + processedMethods: [string, string, BabelTypes.ClassBody][]; |
| 14 | + }> => { |
| 15 | + return { |
| 16 | + pre() { |
| 17 | + this.processedMethods = []; |
| 18 | + }, |
| 19 | + post() { |
| 20 | + for (const className of Object.keys(signatures)) { |
| 21 | + for (const methodName of Object.keys( |
| 22 | + signatures[className].attributes ?? {} |
| 23 | + )) { |
| 24 | + if ( |
| 25 | + signatures[className].attributes?.[methodName].returnsPromise && |
| 26 | + !signatures[className].attributes?.[methodName].inherited |
| 27 | + ) { |
| 28 | + if ( |
| 29 | + !this.processedMethods.find( |
| 30 | + ([cls, method]) => cls === className && method === methodName |
| 31 | + ) |
| 32 | + ) { |
| 33 | + console.error( |
| 34 | + `Expected to find and transpile type for @returnsPromise-annotated method ${className}.${methodName}` |
| 35 | + ); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + }, |
| 41 | + visitor: { |
| 42 | + TSDeclareMethod(path) { |
| 43 | + if ('isMongoshAsyncRewrittenMethod' in path.node) return; |
| 44 | + |
| 45 | + if (path.parent.type !== 'ClassBody') return; |
| 46 | + if (path.parentPath.parent.type !== 'ClassDeclaration') return; |
| 47 | + const classId = path.parentPath.parent.id; |
| 48 | + if (classId?.type !== 'Identifier') return; |
| 49 | + const className = classId.name; |
| 50 | + if (path.node.key.type !== 'Identifier') return; |
| 51 | + const methodName = path.node.key.name; |
| 52 | + |
| 53 | + if ( |
| 54 | + this.processedMethods.find( |
| 55 | + ([cls, method, classBody]) => |
| 56 | + cls === className && |
| 57 | + method === methodName && |
| 58 | + classBody !== path.parent |
| 59 | + ) |
| 60 | + ) { |
| 61 | + throw new Error(`Duplicate method: ${className}.${methodName}`); |
| 62 | + } |
| 63 | + this.processedMethods.push([className, methodName, path.parent]); |
| 64 | + |
| 65 | + if (!signatures[className]?.attributes?.[methodName]?.returnsPromise) |
| 66 | + return; |
| 67 | + |
| 68 | + const { returnType } = path.node; |
| 69 | + if (returnType?.type !== 'TSTypeAnnotation') return; |
| 70 | + if (returnType.typeAnnotation.type !== 'TSTypeReference') return; |
| 71 | + if (returnType.typeAnnotation.typeName.type !== 'Identifier') return; |
| 72 | + if (returnType.typeAnnotation.typeName.name !== 'Promise') return; |
| 73 | + if (!returnType.typeAnnotation.typeParameters?.params.length) return; |
| 74 | + path.replaceWith({ |
| 75 | + ...path.node, |
| 76 | + returnType: { |
| 77 | + ...returnType, |
| 78 | + typeAnnotation: |
| 79 | + returnType.typeAnnotation.typeParameters.params[0], |
| 80 | + }, |
| 81 | + isMongoshAsyncRewrittenMethod: true, |
| 82 | + }); |
| 83 | + }, |
| 84 | + }, |
| 85 | + }; |
| 86 | + }; |
| 87 | +} |
| 88 | + |
| 89 | +async function main() { |
| 90 | + const apiRaw = await fs.readFile( |
| 91 | + path.resolve(__dirname, '..', 'lib', 'api-raw.d.ts'), |
| 92 | + 'utf8' |
| 93 | + ); |
| 94 | + const result = babel.transformSync(apiRaw, { |
| 95 | + code: true, |
| 96 | + ast: false, |
| 97 | + configFile: false, |
| 98 | + babelrc: false, |
| 99 | + browserslistConfigFile: false, |
| 100 | + compact: false, |
| 101 | + sourceType: 'module', |
| 102 | + plugins: [applyAsyncRewriterChanges()], |
| 103 | + parserOpts: { |
| 104 | + plugins: ['typescript'], |
| 105 | + }, |
| 106 | + }); |
| 107 | + let code = result?.code ?? ''; |
| 108 | + code += ` |
| 109 | +// REPLACEME |
| 110 | +type MongodbServerSchema = { |
| 111 | + admin: {}, |
| 112 | + config: {}, |
| 113 | + test: { |
| 114 | + test: { |
| 115 | + schema: { |
| 116 | + _id: ObjectId; |
| 117 | + foo: number; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +// REPLACEME |
| 123 | +
|
| 124 | +declare global { |
| 125 | + // second argument optional |
| 126 | + var db: Database<MongodbServerSchema, MongodbServerSchema['test']>; |
| 127 | +
|
| 128 | + var use: (collection: StringKey<MongodbServerSchema>) => void; |
| 129 | +} |
| 130 | +`; |
| 131 | + await fs.writeFile( |
| 132 | + path.resolve(__dirname, '..', 'lib', 'api-processed.d.ts'), |
| 133 | + code |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +main().catch((err) => |
| 138 | + process.nextTick(() => { |
| 139 | + throw err; |
| 140 | + }) |
| 141 | +); |
0 commit comments