(_options, program)
| 1 | const ts = require('typescript'); |
| 2 | |
| 3 | function before(_options, program) { |
| 4 | const compilerOptions = program?.getCompilerOptions?.() ?? {}; |
| 5 | |
| 6 | return (context) => { |
| 7 | const factory = context.factory ?? ts.factory; |
| 8 | |
| 9 | return (sourceFile) => { |
| 10 | let mutated = false; |
| 11 | |
| 12 | // Always preserve original top-level import declarations in final output |
| 13 | const originalImportStatements = sourceFile.statements.filter((s) => ts.isImportDeclaration(s)); |
| 14 | |
| 15 | const transformStatements = (statements, isTopLevel) => { |
| 16 | let changed = false; |
| 17 | const result = []; |
| 18 | |
| 19 | for (const statement of statements) { |
| 20 | if (ts.isClassDeclaration(statement) && hasNativeClassDecorator(statement)) { |
| 21 | mutated = true; |
| 22 | changed = true; |
| 23 | result.push(...emitDownleveledClass(statement)); |
| 24 | continue; |
| 25 | } |
| 26 | |
| 27 | if (ts.isExpressionStatement(statement)) { |
| 28 | const updated = removeNativeClassFromDecorate(factory, statement); |
| 29 | if (updated === undefined) { |
| 30 | mutated = true; |
| 31 | changed = true; |
| 32 | continue; |
| 33 | } |
| 34 | const visited = ts.visitEachChild(updated, visitNode, context); |
| 35 | if (updated !== statement || visited !== statement) { |
| 36 | mutated = true; |
| 37 | changed = true; |
| 38 | } |
| 39 | result.push(visited); |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | if (isTopLevel && ts.isImportDeclaration(statement)) { |
| 44 | // Safety: Do not alter import declarations here. Earlier versions attempted to |
| 45 | // remove `NativeClass` from named imports, but this proved too risky and could |
| 46 | // accidentally elide unrelated imports in certain TS versions/configs. We keep |
| 47 | // imports intact and rely on bundlers/TS to tree-shake if needed. |
| 48 | result.push(statement); |
| 49 | continue; |
| 50 | } |
| 51 | |
| 52 | const visited = ts.visitEachChild(statement, visitNode, context); |
| 53 | if (visited !== statement) { |
| 54 | mutated = true; |
| 55 | changed = true; |
| 56 | } |
| 57 | result.push(visited); |
| 58 | } |
| 59 | |
| 60 | return [changed ? factory.createNodeArray(result) : statements, changed]; |
no test coverage detected