|
| 1 | +import { ESLint } from 'eslint'; |
| 2 | +import { readFileSync, writeFileSync } from 'fs'; |
| 3 | +import * as ts from 'typescript'; |
| 4 | + |
| 5 | +const source = ts.createSourceFile('mongodb.d.ts', readFileSync('mongodb.d.ts', 'utf-8')); |
| 6 | +writeFileSync('mongodb-next.d.ts', readFileSync('mongodb.d.ts', 'utf-8')); |
| 7 | + |
| 8 | +const transformer: ts.TransformerFactory<ts.SourceFile> = _ => { |
| 9 | + function isAsyncDisposeImplementation(node: ts.MethodDeclaration): boolean { |
| 10 | + if ( |
| 11 | + ts.isComputedPropertyName(node.name) && |
| 12 | + ts.isPropertyAccessExpression(node.name.expression) |
| 13 | + ) { |
| 14 | + const expression = node.name.expression.expression; |
| 15 | + const name = node.name.expression.name; |
| 16 | + |
| 17 | + const found = |
| 18 | + ts.isIdentifier(expression) && |
| 19 | + expression.text === 'Symbol' && |
| 20 | + ts.isIdentifier(name) && |
| 21 | + name.text === 'asyncDispose'; |
| 22 | + |
| 23 | + return found; |
| 24 | + } |
| 25 | + return false; |
| 26 | + } |
| 27 | + function cleanClass(node: ts.ClassDeclaration): ts.ClassDeclaration { |
| 28 | + // filter out all implementations |
| 29 | + const members = node.members.filter(node => |
| 30 | + ts.isMethodDeclaration(node) ? !isAsyncDisposeImplementation(node) : true |
| 31 | + ); |
| 32 | + |
| 33 | + const heratigeClauses = node.heritageClauses |
| 34 | + ?.map(node => |
| 35 | + ts.factory.updateHeritageClause( |
| 36 | + node, |
| 37 | + node.types.filter( |
| 38 | + type => |
| 39 | + !( |
| 40 | + ts.isExpressionWithTypeArguments(type) && |
| 41 | + ts.isIdentifier(type.expression) && |
| 42 | + type.expression.text === 'AsyncDisposable' |
| 43 | + ) |
| 44 | + ) |
| 45 | + ) |
| 46 | + ) |
| 47 | + .filter(node => node.types.length > 0); |
| 48 | + |
| 49 | + return ts.factory.updateClassDeclaration( |
| 50 | + node, |
| 51 | + node.modifiers, |
| 52 | + node.name, |
| 53 | + node.typeParameters, |
| 54 | + heratigeClauses, |
| 55 | + members |
| 56 | + ); |
| 57 | + } |
| 58 | + return (node: ts.SourceFile) => { |
| 59 | + const children = node.statements.map(node => { |
| 60 | + if (ts.isClassDeclaration(node)) { |
| 61 | + return cleanClass(node); |
| 62 | + } |
| 63 | + return node; |
| 64 | + }); |
| 65 | + return ts.factory.updateSourceFile(node, children); |
| 66 | + }; |
| 67 | +}; |
| 68 | +const transformed = ts.transform(source, [transformer]).transformed[0]; |
| 69 | +const printer = ts.createPrinter({ |
| 70 | + removeComments: false |
| 71 | +}); |
| 72 | + |
| 73 | +writeFileSync('mongodb.d.ts', printer.printFile(transformed)); |
| 74 | + |
| 75 | +async function lint() { |
| 76 | + const linter = new ESLint({ fix: true }); |
| 77 | + const linted = await linter.lintFiles(['mongodb.d.ts', 'mongodb-next.d.ts']); |
| 78 | + await ESLint.outputFixes(linted); |
| 79 | +} |
| 80 | + |
| 81 | +lint(); |
0 commit comments