Skip to content

Commit

Permalink
emit publish of exported values in assignment expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
vladima committed Apr 11, 2015
1 parent 83fcca8 commit 7102b1d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11803,6 +11803,18 @@ module ts {
return !!resolveName(location, name, SymbolFlags.Value, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined);
}

function getReferencedValueDeclaration(reference: Identifier): Declaration {
Debug.assert(!nodeIsSynthesized(reference));
let symbol =
getNodeLinks(reference).resolvedSymbol ||
resolveName(reference, reference.text, SymbolFlags.Value | SymbolFlags.Alias, /*nodeNotFoundMessage*/ undefined, /*nameArg*/ undefined);

if (!symbol) {
return undefined;
}
return symbol.flags & SymbolFlags.Export ? symbol.exportSymbol.valueDeclaration : symbol.valueDeclaration
}

function getBlockScopedVariableId(n: Identifier): number {
Debug.assert(!nodeIsSynthesized(n));

Expand Down Expand Up @@ -11861,6 +11873,7 @@ module ts {
resolvesToSomeValue,
collectLinkedAliases,
getBlockScopedVariableId,
getReferencedValueDeclaration,
serializeTypeOfNode,
serializeParameterTypesOfNode,
serializeReturnTypeOfNode,
Expand Down
21 changes: 21 additions & 0 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1930,12 +1930,33 @@ var __param = this.__param || function(index, decorator) { return function (targ
emitDestructuring(node, node.parent.kind === SyntaxKind.ExpressionStatement);
}
else {
let emitPublishOfExportedValue = false;

if (currentFileIsEmittedAsSystemModule() &&
node.left.kind === SyntaxKind.Identifier &&
node.operatorToken.kind >= SyntaxKind.FirstAssignment &&
node.operatorToken.kind <= SyntaxKind.LastAssignment) {

let referencedDecl = resolver.getReferencedValueDeclaration(<Identifier>node.left);
if (referencedDecl && (getCombinedNodeFlags(referencedDecl) & NodeFlags.Export) && isSourceFileLevelDeclaration(referencedDecl)) {
emitPublishOfExportedValue = true;
}
}

if (emitPublishOfExportedValue) {
write(`${exportFunctionForFile}("`);
emitNodeWithoutSourceMap(node.left);
write(`", `);
}
emit(node.left);
let indentedBeforeOperator = indentIfOnDifferentLines(node, node.left, node.operatorToken, node.operatorToken.kind !== SyntaxKind.CommaToken ? " " : undefined);
write(tokenToString(node.operatorToken.kind));
let indentedAfterOperator = indentIfOnDifferentLines(node, node.operatorToken, node.right, " ");
emit(node.right);
decreaseIndentIf(indentedBeforeOperator, indentedAfterOperator);
if (emitPublishOfExportedValue) {
write(")");
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,7 @@ module ts {
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number;
resolvesToSomeValue(location: Node, name: string): boolean;
getBlockScopedVariableId(node: Identifier): number;
getReferencedValueDeclaration(reference: Identifier): Node;
serializeTypeOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): string | string[];
serializeParameterTypesOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): (string | string[])[];
serializeReturnTypeOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): string | string[];
Expand Down

1 comment on commit 7102b1d

@CyrusNajmabadi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.