@@ -1461,6 +1461,17 @@ namespace ts {
14611461 return node ;
14621462 }
14631463
1464+ /**
1465+ * Creates a synthetic element to act as a placeholder for the end of an emitted declaration in
1466+ * order to properly emit exports.
1467+ */
1468+ export function createEndOfDeclarationMarker ( original : Node ) {
1469+ const node = < EndOfDeclarationMarker > createNode ( SyntaxKind . EndOfDeclarationMarker ) ;
1470+ node . emitNode = { } ;
1471+ node . original = original ;
1472+ return node ;
1473+ }
1474+
14641475 /**
14651476 * Creates a synthetic expression to act as a placeholder for a not-emitted expression in
14661477 * order to preserve comments or sourcemap positions.
@@ -1637,6 +1648,18 @@ namespace ts {
16371648 ) ;
16381649 }
16391650
1651+ export function createExportDefault ( expression : Expression ) {
1652+ return createExportAssignment ( /*decorators*/ undefined , /*modifiers*/ undefined , /*isExportEquals*/ false , expression ) ;
1653+ }
1654+
1655+ export function createExternalModuleExport ( exportName : Identifier ) {
1656+ return createExportDeclaration ( /*decorators*/ undefined , /*modifiers*/ undefined , createNamedExports ( [ createExportSpecifier ( exportName ) ] ) ) ;
1657+ }
1658+
1659+ export function createLetStatement ( name : Identifier , initializer : Expression , location ?: TextRange ) {
1660+ return createVariableStatement ( /*modifiers*/ undefined , createLetDeclarationList ( [ createVariableDeclaration ( name , /*type*/ undefined , initializer ) ] ) , location ) ;
1661+ }
1662+
16401663 export function createLetDeclarationList ( declarations : VariableDeclaration [ ] , location ?: TextRange ) {
16411664 return createVariableDeclarationList ( declarations , location , NodeFlags . Let ) ;
16421665 }
@@ -2165,6 +2188,132 @@ namespace ts {
21652188 ) ;
21662189 }
21672190
2191+ /**
2192+ * Gets the local name of a declaration. This is primarily used for declarations that can be
2193+ * referred to by name in the declaration's immediate scope (classes, enums, namespaces). A
2194+ * local name will *never* be prefixed with an module or namespace export modifier like
2195+ * "exports." when emitted as an expression.
2196+ *
2197+ * @param node The declaration.
2198+ * @param allowComments A value indicating whether comments may be emitted for the name.
2199+ * @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2200+ */
2201+ export function getLocalName ( node : Declaration , allowComments ?: boolean , allowSourceMaps ?: boolean ) {
2202+ return getName ( node , allowComments , allowSourceMaps , EmitFlags . LocalName ) ;
2203+ }
2204+
2205+ /**
2206+ * Gets whether an identifier should only be referred to by its local name.
2207+ */
2208+ export function isLocalName ( node : Identifier ) {
2209+ return ( getEmitFlags ( node ) & EmitFlags . ExportBindingName ) === EmitFlags . LocalName ;
2210+ }
2211+
2212+ /**
2213+ * Gets the export name of a declaration. This is primarily used for declarations that can be
2214+ * referred to by name in the declaration's immediate scope (classes, enums, namespaces). An
2215+ * export name will *always* be prefixed with an module or namespace export modifier like
2216+ * `"exports."` when emitted as an expression if the name points to an exported symbol.
2217+ *
2218+ * @param node The declaration.
2219+ * @param allowComments A value indicating whether comments may be emitted for the name.
2220+ * @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2221+ */
2222+ export function getExportName ( node : Declaration , allowComments ?: boolean , allowSourceMaps ?: boolean ) : Identifier {
2223+ return getName ( node , allowComments , allowSourceMaps , EmitFlags . ExportName ) ;
2224+ }
2225+
2226+ /**
2227+ * Gets whether an identifier should only be referred to by its export representation if the
2228+ * name points to an exported symbol.
2229+ */
2230+ export function isExportName ( node : Identifier ) {
2231+ return ( getEmitFlags ( node ) & EmitFlags . ExportBindingName ) === EmitFlags . ExportName ;
2232+ }
2233+
2234+ /**
2235+ * Gets the export binding name of a declaration for use in the left-hand side of assignment
2236+ * expressions. This is primarily used for declarations that can be referred to by name in the
2237+ * declaration's immediate scope (classes, enums, namespaces). If the declaration is exported
2238+ * and the name is the target of an assignment expression, its export binding name should be
2239+ * substituted with an expression that assigns *both* the local *and* export names of the
2240+ * declaration. If an export binding name appears in any other position it should be treated
2241+ * as a local name.
2242+ *
2243+ * @param node The declaration.
2244+ * @param allowComments A value indicating whether comments may be emitted for the name.
2245+ * @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2246+ */
2247+ export function getExportBindingName ( node : Declaration , allowComments ?: boolean , allowSourceMaps ?: boolean ) : Identifier {
2248+ return getName ( node , allowComments , allowSourceMaps , EmitFlags . ExportBindingName ) ;
2249+ }
2250+
2251+ /**
2252+ * Gets whether an identifier should be treated as both an export name and a local name when
2253+ * it is the target of an assignment expression.
2254+ */
2255+ export function isExportBindingName ( node : Identifier ) {
2256+ return ( getEmitFlags ( node ) & EmitFlags . ExportBindingName ) === EmitFlags . ExportBindingName ;
2257+ }
2258+
2259+ /**
2260+ * Gets the name of a declaration for use in declarations.
2261+ *
2262+ * @param node The declaration.
2263+ * @param allowComments A value indicating whether comments may be emitted for the name.
2264+ * @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2265+ */
2266+ export function getDeclarationName ( node : Declaration , allowComments ?: boolean , allowSourceMaps ?: boolean ) {
2267+ return getName ( node , allowComments , allowSourceMaps ) ;
2268+ }
2269+
2270+ function getName ( node : Declaration , allowComments ?: boolean , allowSourceMaps ?: boolean , emitFlags ?: EmitFlags ) {
2271+ if ( node . name && isIdentifier ( node . name ) && ! isGeneratedIdentifier ( node . name ) ) {
2272+ const name = getMutableClone ( node . name ) ;
2273+ emitFlags |= getEmitFlags ( node . name ) ;
2274+ if ( ! allowSourceMaps ) emitFlags |= EmitFlags . NoSourceMap ;
2275+ if ( ! allowComments ) emitFlags |= EmitFlags . NoComments ;
2276+ if ( emitFlags ) setEmitFlags ( name , emitFlags ) ;
2277+ return name ;
2278+ }
2279+ return getGeneratedNameForNode ( node ) ;
2280+ }
2281+
2282+ /**
2283+ * Gets the exported name of a declaration for use in expressions.
2284+ *
2285+ * An exported name will *always* be prefixed with an module or namespace export modifier like
2286+ * "exports." if the name points to an exported symbol.
2287+ *
2288+ * @param ns The namespace identifier.
2289+ * @param node The declaration.
2290+ * @param allowComments A value indicating whether comments may be emitted for the name.
2291+ * @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2292+ */
2293+ export function getExternalModuleOrNamespaceExportName ( ns : Identifier | undefined , node : Declaration , allowComments ?: boolean , allowSourceMaps ?: boolean ) : Identifier | PropertyAccessExpression {
2294+ if ( ns && hasModifier ( node , ModifierFlags . Export ) ) {
2295+ return getNamespaceMemberName ( ns , getName ( node ) , allowComments , allowSourceMaps ) ;
2296+ }
2297+ return getExportName ( node , allowComments , allowSourceMaps ) ;
2298+ }
2299+
2300+ /**
2301+ * Gets a namespace-qualified name for use in expressions.
2302+ *
2303+ * @param ns The namespace identifier.
2304+ * @param name The name.
2305+ * @param allowComments A value indicating whether comments may be emitted for the name.
2306+ * @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2307+ */
2308+ export function getNamespaceMemberName ( ns : Identifier , name : Identifier , allowComments ?: boolean , allowSourceMaps ?: boolean ) : PropertyAccessExpression {
2309+ const qualifiedName = createPropertyAccess ( ns , nodeIsSynthesized ( name ) ? name : getSynthesizedClone ( name ) , /*location*/ name ) ;
2310+ let emitFlags : EmitFlags ;
2311+ if ( ! allowSourceMaps ) emitFlags |= EmitFlags . NoSourceMap ;
2312+ if ( ! allowComments ) emitFlags |= EmitFlags . NoComments ;
2313+ if ( emitFlags ) setEmitFlags ( qualifiedName , emitFlags ) ;
2314+ return qualifiedName ;
2315+ }
2316+
21682317 // Utilities
21692318
21702319 function isUseStrictPrologue ( node : ExpressionStatement ) : boolean {
0 commit comments