Skip to content
File

Blob: types/src/transforms/onmessage-declaration.ts

typescript36 lines
1// Copyright (c) 2026 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5import ts from "typescript";
6 
7export function createAddOnMessageDeclarationTransformer(): ts.TransformerFactory<ts.SourceFile> {
8 return (context) => {
9 return (sourceFile) => {
10 // Create the new variable declaration
11 const onMessageDeclaration = context.factory.createVariableStatement(
12 [context.factory.createModifier(ts.SyntaxKind.DeclareKeyword)],
13 context.factory.createVariableDeclarationList(
14 [
15 context.factory.createVariableDeclaration(
16 "onmessage",
17 undefined,
18 context.factory.createKeywordTypeNode(ts.SyntaxKind.NeverKeyword)
19 ),
20 ],
21 ts.NodeFlags.None
22 )
23 );
24 
25 // Prepend the new declaration to the source file
26 const updatedStatements = ts.factory.createNodeArray([
27 onMessageDeclaration,
28 ...sourceFile.statements,
29 ]);
30 
31 // Return the updated source file
32 return ts.factory.updateSourceFile(sourceFile, updatedStatements);
33 };
34 };
35}