Skip to content

Commit

Permalink
emit publish of exported values in prefix/postfix unary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
vladima committed Apr 11, 2015
1 parent 7102b1d commit 7bced68
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
46 changes: 41 additions & 5 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,23 @@ var __param = this.__param || function(index, decorator) { return function (targ
emit(node.expression);
}

function shouldEmitPublishOfExportedValue(node: PrefixUnaryExpression | PostfixUnaryExpression): boolean {
if (!currentFileIsEmittedAsSystemModule() || node.operand.kind !== SyntaxKind.Identifier) {
return false;
}

return isExportedSourceLevelDeclaration(resolver.getReferencedValueDeclaration(<Identifier>node.operand));
}

function emitPrefixUnaryExpression(node: PrefixUnaryExpression) {
const emitPublishOfExportedValue = shouldEmitPublishOfExportedValue(node);

if (emitPublishOfExportedValue) {
write(`${exportFunctionForFile}("`);
emitNodeWithoutSourceMap(node.operand);
write(`", `);
}

write(tokenToString(node.operator));
// In some cases, we need to emit a space between the operator and the operand. One obvious case
// is when the operator is an identifier, like delete or typeof. We also need to do this for plus
Expand All @@ -1917,11 +1933,35 @@ var __param = this.__param || function(index, decorator) { return function (targ
}
}
emit(node.operand);

if (emitPublishOfExportedValue) {
write(")");
}
}

function emitPostfixUnaryExpression(node: PostfixUnaryExpression) {
const emitPublishOfExportedValue = shouldEmitPublishOfExportedValue(node);
if (emitPublishOfExportedValue) {
write(`${exportFunctionForFile}("`);
emitNodeWithoutSourceMap(node.operand);
write(`", `);
}

emit(node.operand);
write(tokenToString(node.operator));

if (emitPublishOfExportedValue) {
write("), ");

This comment has been minimized.

Copy link
@CyrusNajmabadi

CyrusNajmabadi Apr 21, 2015

Contributor

Isn't a more appropriate approach to store the value first into a temp, then pass the ++'d value to the exporter. Then return the original temp.

This comment has been minimized.

Copy link
@vladima

vladima Apr 22, 2015

Author Contributor

Just a personal preference: I'd like to avoid introducing new temps until it is really necessary - IMO code with temps\comma expressions looks messier.

emitNodeWithoutSourceMap(node.operand);
write(node.operator === SyntaxKind.PlusPlusToken ? " - 1" : " + 1");
}
}

function isExportedSourceLevelDeclaration(decl: Declaration): boolean {
if (!decl) {
return false;
}
return (getCombinedNodeFlags(decl) & NodeFlags.Export) && isSourceFileLevelDeclaration(decl);
}

function emitBinaryExpression(node: BinaryExpression) {
Expand All @@ -1936,11 +1976,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
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;
}
emitPublishOfExportedValue = isExportedSourceLevelDeclaration(resolver.getReferencedValueDeclaration(<Identifier>node.left));
}

if (emitPublishOfExportedValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,7 +1256,7 @@ module ts {
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number;
resolvesToSomeValue(location: Node, name: string): boolean;
getBlockScopedVariableId(node: Identifier): number;
getReferencedValueDeclaration(reference: Identifier): Node;
getReferencedValueDeclaration(reference: Identifier): Declaration;
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

0 comments on commit 7bced68

Please sign in to comment.