Skip to content

Commit 84dc99b

Browse files
committed
Moved AMD/CJS/UMD transform to end
1 parent bb8a3c4 commit 84dc99b

File tree

350 files changed

+1647
-1296
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

350 files changed

+1647
-1296
lines changed

src/compiler/binder.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,9 +2566,9 @@ namespace ts {
25662566

25672567
// A class with a parameter property assignment, property initializer, or decorator is
25682568
// TypeScript syntax.
2569-
// An exported declaration may be TypeScript syntax.
2569+
// An exported declaration may be TypeScript syntax, but is handled by the visitor
2570+
// for a namespace declaration.
25702571
if ((subtreeFlags & TransformFlags.TypeScriptClassSyntaxMask)
2571-
|| (modifierFlags & ModifierFlags.Export)
25722572
|| node.typeParameters) {
25732573
transformFlags |= TransformFlags.AssertTypeScript;
25742574
}
@@ -2727,11 +2727,6 @@ namespace ts {
27272727
else {
27282728
transformFlags = subtreeFlags | TransformFlags.ContainsHoistedDeclarationOrCompletion;
27292729

2730-
// If a FunctionDeclaration is exported, then it is either ES6 or TypeScript syntax.
2731-
if (modifierFlags & ModifierFlags.Export) {
2732-
transformFlags |= TransformFlags.AssertTypeScript | TransformFlags.AssertES2015;
2733-
}
2734-
27352730
// TypeScript-specific modifiers, type parameters, and type annotations are TypeScript
27362731
// syntax.
27372732
if (modifierFlags & ModifierFlags.TypeScriptModifier
@@ -2873,11 +2868,6 @@ namespace ts {
28732868
else {
28742869
transformFlags = subtreeFlags;
28752870

2876-
// If a VariableStatement is exported, then it is either ES6 or TypeScript syntax.
2877-
if (modifierFlags & ModifierFlags.Export) {
2878-
transformFlags |= TransformFlags.AssertES2015 | TransformFlags.AssertTypeScript;
2879-
}
2880-
28812871
if (declarationListTransformFlags & TransformFlags.ContainsBindingPattern) {
28822872
transformFlags |= TransformFlags.AssertES2015;
28832873
}
@@ -2994,12 +2984,6 @@ namespace ts {
29942984
transformFlags |= TransformFlags.AssertJsx;
29952985
break;
29962986

2997-
case SyntaxKind.ExportKeyword:
2998-
// This node is both ES6 and TypeScript syntax.
2999-
transformFlags |= TransformFlags.AssertES2015 | TransformFlags.AssertTypeScript;
3000-
break;
3001-
3002-
case SyntaxKind.DefaultKeyword:
30032987
case SyntaxKind.NoSubstitutionTemplateLiteral:
30042988
case SyntaxKind.TemplateHead:
30052989
case SyntaxKind.TemplateMiddle:
@@ -3008,6 +2992,7 @@ namespace ts {
30082992
case SyntaxKind.TaggedTemplateExpression:
30092993
case SyntaxKind.ShorthandPropertyAssignment:
30102994
case SyntaxKind.ForOfStatement:
2995+
case SyntaxKind.StaticKeyword:
30112996
// These nodes are ES6 syntax.
30122997
transformFlags |= TransformFlags.AssertES2015;
30132998
break;

src/compiler/core.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,13 @@ namespace ts {
485485
return result;
486486
}
487487

488+
export function append<T>(to: T[] | undefined, value: T | undefined): T[] | undefined {
489+
if (value === undefined) return to;
490+
if (to === undefined) to = [];
491+
to.push(value);
492+
return to;
493+
}
494+
488495
export function addRange<T>(to: T[], from: T[]): void {
489496
if (to && from) {
490497
for (const v of from) {
@@ -830,7 +837,7 @@ namespace ts {
830837
* Adds the value to an array of values associated with the key, and returns the array.
831838
* Creates the array if it does not already exist.
832839
*/
833-
export function multiMapAdd<V>(map: Map<V[]>, key: string, value: V): V[] {
840+
export function multiMapAdd<V>(map: Map<V[]>, key: string | number, value: V): V[] {
834841
const values = map[key];
835842
if (values) {
836843
values.push(value);

src/compiler/factory.ts

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

src/compiler/transformer.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ namespace ts {
111111
const transformers: Transformer[] = [];
112112

113113
transformers.push(transformTypeScript);
114-
transformers.push(moduleTransformerMap[moduleKind] || moduleTransformerMap[ModuleKind.None]);
114+
115+
if (moduleKind === ModuleKind.System) {
116+
transformers.push(moduleTransformerMap[moduleKind] || moduleTransformerMap[ModuleKind.None]);
117+
}
115118

116119
if (jsx === JsxEmit.React) {
117120
transformers.push(transformJsx);
@@ -130,6 +133,10 @@ namespace ts {
130133
transformers.push(transformGenerators);
131134
}
132135

136+
if (moduleKind !== ModuleKind.System) {
137+
transformers.push(moduleTransformerMap[moduleKind] || moduleTransformerMap[ModuleKind.None]);
138+
}
139+
133140
if (languageVersion < ScriptTarget.ES5) {
134141
transformers.push(transformES5);
135142
}

0 commit comments

Comments
 (0)