(body: Statement[], ms: MagicString)
| 63 | } |
| 64 | |
| 65 | function rewriteBody(body: Statement[], ms: MagicString): void { |
| 66 | let open: { id: string; lastStmtEnd: number } | null = null; |
| 67 | |
| 68 | for (const stmt of body) { |
| 69 | if (isElementStyleHelperUsing(stmt)) { |
| 70 | if (open) { |
| 71 | ms.appendLeft(open.lastStmtEnd, ` } finally { ${open.id}?.[Symbol.dispose]?.(); }`); |
| 72 | } |
| 73 | |
| 74 | const idNode = stmt.declarations[0].id; |
| 75 | if (idNode.type !== 'Identifier') { |
| 76 | // destructured `using` is not supported. |
| 77 | open = null; |
| 78 | continue; |
| 79 | } |
| 80 | const name = (idNode as IdentifierName).name; |
| 81 | |
| 82 | // `using` is 5 chars; replace with `const` |
| 83 | ms.update(stmt.start, stmt.start + 5, 'const'); |
| 84 | ms.appendLeft(stmt.end, ' try {'); |
| 85 | |
| 86 | open = { id: name, lastStmtEnd: stmt.end }; |
| 87 | } else if (open) { |
| 88 | open.lastStmtEnd = stmt.end; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (open) { |
| 93 | ms.appendLeft(open.lastStmtEnd, ` } finally { ${open.id}?.[Symbol.dispose]?.(); }`); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | function isElementStyleHelperUsing( |
| 98 | stmt: Statement |
no test coverage detected