Skip to content

Synthesized ModuleDeclarations are missing the top level variable declaration in the emit #17596

Closed
@tbosch

Description

@tbosch

TypeScript Version: 2.4.2

Context
Needed by Angular / Tsickle for converting TypeScript types into closure type comments.

Our workaround (see transformer_util.ts):

  • copy the ts.Node.Symbol from the original node over to the synthetic node (note that this is an @internal field)

Code

Plunker is here.

case.ts

module Reflect {
  const x = 1;
}

transformer.ts:

function forceSyntheticModuleDeclaration(context: ts.TransformationContext) {
  return (sourceFile: ts.SourceFile): ts.SourceFile => {
    return visitNode(sourceFile);

    function visitNode<T extends ts.Node>(node: T) {
      if (node.kind === ts.SyntaxKind.ModuleBlock) {
        const md = node as ts.ModuleBlock;
        return ts.updateModuleBlock(md, ts.setTextRange(ts.createNodeArray([...md.statements]));
      }
      return ts.visitEachChild(node, visitNode, context); 
    }
  };
}

Expected behavior:
A top level variable statement should be emitted:

var Reflect;
(function (Reflect) {
    var x = 1;
})(Reflect || (Reflect = {}));

Actual behavior:
No top level variable statement is emitted:

(function (Reflect) {
    var x = 1;
})(Reflect || (Reflect = {}));

/cc @mprobst @evmar @alexeagle

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions