Skip to content

Commit 7bced68

Browse files
committed
emit publish of exported values in prefix/postfix unary expressions
1 parent 7102b1d commit 7bced68

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

src/compiler/emitter.ts

+41-5
Original file line numberDiff line numberDiff line change
@@ -1893,7 +1893,23 @@ var __param = this.__param || function(index, decorator) { return function (targ
18931893
emit(node.expression);
18941894
}
18951895

1896+
function shouldEmitPublishOfExportedValue(node: PrefixUnaryExpression | PostfixUnaryExpression): boolean {
1897+
if (!currentFileIsEmittedAsSystemModule() || node.operand.kind !== SyntaxKind.Identifier) {
1898+
return false;
1899+
}
1900+
1901+
return isExportedSourceLevelDeclaration(resolver.getReferencedValueDeclaration(<Identifier>node.operand));
1902+
}
1903+
18961904
function emitPrefixUnaryExpression(node: PrefixUnaryExpression) {
1905+
const emitPublishOfExportedValue = shouldEmitPublishOfExportedValue(node);
1906+
1907+
if (emitPublishOfExportedValue) {
1908+
write(`${exportFunctionForFile}("`);
1909+
emitNodeWithoutSourceMap(node.operand);
1910+
write(`", `);
1911+
}
1912+
18971913
write(tokenToString(node.operator));
18981914
// In some cases, we need to emit a space between the operator and the operand. One obvious case
18991915
// is when the operator is an identifier, like delete or typeof. We also need to do this for plus
@@ -1917,11 +1933,35 @@ var __param = this.__param || function(index, decorator) { return function (targ
19171933
}
19181934
}
19191935
emit(node.operand);
1936+
1937+
if (emitPublishOfExportedValue) {
1938+
write(")");
1939+
}
19201940
}
19211941

19221942
function emitPostfixUnaryExpression(node: PostfixUnaryExpression) {
1943+
const emitPublishOfExportedValue = shouldEmitPublishOfExportedValue(node);
1944+
if (emitPublishOfExportedValue) {
1945+
write(`${exportFunctionForFile}("`);
1946+
emitNodeWithoutSourceMap(node.operand);
1947+
write(`", `);
1948+
}
1949+
19231950
emit(node.operand);
19241951
write(tokenToString(node.operator));
1952+
1953+
if (emitPublishOfExportedValue) {
1954+
write("), ");
1955+
emitNodeWithoutSourceMap(node.operand);
1956+
write(node.operator === SyntaxKind.PlusPlusToken ? " - 1" : " + 1");
1957+
}
1958+
}
1959+
1960+
function isExportedSourceLevelDeclaration(decl: Declaration): boolean {
1961+
if (!decl) {
1962+
return false;
1963+
}
1964+
return (getCombinedNodeFlags(decl) & NodeFlags.Export) && isSourceFileLevelDeclaration(decl);
19251965
}
19261966

19271967
function emitBinaryExpression(node: BinaryExpression) {
@@ -1936,11 +1976,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
19361976
node.left.kind === SyntaxKind.Identifier &&
19371977
node.operatorToken.kind >= SyntaxKind.FirstAssignment &&
19381978
node.operatorToken.kind <= SyntaxKind.LastAssignment) {
1939-
1940-
let referencedDecl = resolver.getReferencedValueDeclaration(<Identifier>node.left);
1941-
if (referencedDecl && (getCombinedNodeFlags(referencedDecl) & NodeFlags.Export) && isSourceFileLevelDeclaration(referencedDecl)) {
1942-
emitPublishOfExportedValue = true;
1943-
}
1979+
emitPublishOfExportedValue = isExportedSourceLevelDeclaration(resolver.getReferencedValueDeclaration(<Identifier>node.left));
19441980
}
19451981

19461982
if (emitPublishOfExportedValue) {

src/compiler/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ module ts {
12561256
getConstantValue(node: EnumMember | PropertyAccessExpression | ElementAccessExpression): number;
12571257
resolvesToSomeValue(location: Node, name: string): boolean;
12581258
getBlockScopedVariableId(node: Identifier): number;
1259-
getReferencedValueDeclaration(reference: Identifier): Node;
1259+
getReferencedValueDeclaration(reference: Identifier): Declaration;
12601260
serializeTypeOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): string | string[];
12611261
serializeParameterTypesOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): (string | string[])[];
12621262
serializeReturnTypeOfNode(node: Node, getGeneratedNameForNode: (Node: Node) => string): string | string[];

0 commit comments

Comments
 (0)