Skip to content

Commit 34b1384

Browse files
committed
add transformer for emit add property to default export
1 parent 799656a commit 34b1384

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

src/compiler/transformers/declarations.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,43 @@ namespace ts {
10081008
return createVariableStatement(/*modifiers*/ undefined, createVariableDeclarationList([varDecl]));
10091009
});
10101010
const namespaceDecl = createModuleDeclaration(/*decorators*/ undefined, ensureModifiers(input, isPrivate), input.name!, createModuleBlock(declarations), NodeFlags.Namespace);
1011-
return [clean, namespaceDecl];
1011+
1012+
if (!hasModifier(clean, ModifierFlags.ExportDefault)) {
1013+
return [clean, namespaceDecl];
1014+
}
1015+
1016+
const modifierFlags = (getModifierFlags(clean) & ~ModifierFlags.ExportDefault) | ModifierFlags.Ambient;
1017+
const cleanDeclaration = updateFunctionDeclaration(
1018+
clean,
1019+
/*decorators*/ undefined,
1020+
createModifiersFromModifierFlags(modifierFlags),
1021+
/*asteriskToken*/ undefined,
1022+
clean.name,
1023+
clean.typeParameters,
1024+
clean.parameters,
1025+
clean.type,
1026+
/*body*/ undefined
1027+
);
1028+
1029+
const namespaceDeclaration = updateModuleDeclaration(
1030+
namespaceDecl,
1031+
/*decorators*/ undefined,
1032+
createModifiersFromModifierFlags(modifierFlags),
1033+
input.name!,
1034+
createModuleBlock(declarations)
1035+
);
1036+
1037+
const exportDefaultDeclaration = createExportAssignment(
1038+
/*decorators*/ undefined,
1039+
/*modifiers*/undefined,
1040+
/*isExportEquals*/false,
1041+
clean.name!
1042+
);
1043+
1044+
resultHasExternalModuleIndicator = true;
1045+
resultHasScopeMarker = true;
1046+
1047+
return [cleanDeclaration, namespaceDeclaration, exportDefaultDeclaration];
10121048
}
10131049
else {
10141050
return clean;
@@ -1153,6 +1189,8 @@ namespace ts {
11531189
return preserveJsDoc(updateEnumMember(m, m.name, constValue !== undefined ? createLiteral(constValue) : undefined), m);
11541190
}))));
11551191
}
1192+
case SyntaxKind.ExportAssignment:
1193+
return;
11561194
}
11571195
// Anything left unhandled is an error, so this should be unreachable
11581196
return Debug.assertNever(input, `Unhandled top-level node in declaration emit: ${(ts as any).SyntaxKind[(input as any).kind]}`);

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3435,7 +3435,8 @@ namespace ts {
34353435
| ModuleDeclaration
34363436
| TypeAliasDeclaration
34373437
| InterfaceDeclaration
3438-
| EnumDeclaration;
3438+
| EnumDeclaration
3439+
| ExportAssignment;
34393440

34403441
/* @internal */
34413442
export interface SymbolVisibilityResult {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [exportDefaultNamespace.ts]
2+
export default function someFunc() {
3+
return 'hello!';
4+
}
5+
6+
someFunc.someProp = 'yo';
7+
8+
9+
//// [exportDefaultNamespace.js]
10+
"use strict";
11+
exports.__esModule = true;
12+
function someFunc() {
13+
return 'hello!';
14+
}
15+
exports["default"] = someFunc;
16+
someFunc.someProp = 'yo';
17+
18+
19+
//// [exportDefaultNamespace.d.ts]
20+
declare function someFunc(): string;
21+
declare namespace someFunc {
22+
var someProp: string;
23+
}
24+
export default someFunc;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/declarationEmit/exportDefaultNamespace.ts ===
2+
export default function someFunc() {
3+
>someFunc : Symbol(someFunc, Decl(exportDefaultNamespace.ts, 0, 0), Decl(exportDefaultNamespace.ts, 2, 1))
4+
5+
return 'hello!';
6+
}
7+
8+
someFunc.someProp = 'yo';
9+
>someFunc.someProp : Symbol(someFunc.someProp, Decl(exportDefaultNamespace.ts, 2, 1))
10+
>someFunc : Symbol(someFunc, Decl(exportDefaultNamespace.ts, 0, 0), Decl(exportDefaultNamespace.ts, 2, 1))
11+
>someProp : Symbol(someFunc.someProp, Decl(exportDefaultNamespace.ts, 2, 1))
12+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/declarationEmit/exportDefaultNamespace.ts ===
2+
export default function someFunc() {
3+
>someFunc : typeof someFunc
4+
5+
return 'hello!';
6+
>'hello!' : "hello!"
7+
}
8+
9+
someFunc.someProp = 'yo';
10+
>someFunc.someProp = 'yo' : "yo"
11+
>someFunc.someProp : string
12+
>someFunc : typeof someFunc
13+
>someProp : string
14+
>'yo' : "yo"
15+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @declaration: true
2+
3+
export default function someFunc() {
4+
return 'hello!';
5+
}
6+
7+
someFunc.someProp = 'yo';

0 commit comments

Comments
 (0)