Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tbosch opened this issue Aug 3, 2017 · 1 comment
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@tbosch
Copy link

tbosch commented Aug 3, 2017

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

@DanielRosenwasser
Copy link
Member

Fix is up at #17631.

@DanielRosenwasser DanielRosenwasser modified the milestones: TypeScript 2.5, TypeScript 2.5.1 Aug 5, 2017
@mhegazy mhegazy modified the milestones: TypeScript 2.5.1, TypeScript 2.5 Aug 16, 2017
@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Aug 16, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants