Skip to content

Commit b66a32f

Browse files
committed
Merge branch 'csnover-fix-2036'
2 parents 4e78464 + ded0e39 commit b66a32f

Some content is hidden

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

60 files changed

+1457
-51
lines changed

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ module ts {
5050
shortName: "m",
5151
type: {
5252
"commonjs": ModuleKind.CommonJS,
53-
"amd": ModuleKind.AMD
53+
"amd": ModuleKind.AMD,
54+
"umd": ModuleKind.UMD
5455
},
55-
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_or_amd,
56+
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_or_umd,
5657
paramType: Diagnostics.KIND,
57-
error: Diagnostics.Argument_for_module_option_must_be_commonjs_or_amd
58+
error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_or_umd
5859
},
5960
{
6061
name: "noEmit",
@@ -150,7 +151,7 @@ module ts {
150151
type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5, "es6": ScriptTarget.ES6 },
151152
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES6_experimental,
152153
paramType: Diagnostics.VERSION,
153-
error: Diagnostics.Argument_for_target_option_must_be_es3_es5_or_es6
154+
error: Diagnostics.Argument_for_target_option_must_be_ES3_ES5_or_ES6
154155
},
155156
{
156157
name: "version",

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ module ts {
161161
Line_terminator_not_permitted_before_arrow: { code: 1200, category: DiagnosticCategory.Error, key: "Line terminator not permitted before arrow." },
162162
Import_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_or_import_d_from_mod_instead: { code: 1202, category: DiagnosticCategory.Error, key: "Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"' or 'import d from \"mod\"' instead." },
163163
Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead: { code: 1203, category: DiagnosticCategory.Error, key: "Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead." },
164-
Cannot_compile_external_modules_into_amd_or_commonjs_when_targeting_es6_or_higher: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile external modules into amd or commonjs when targeting es6 or higher." },
164+
Cannot_compile_external_modules_into_amd_commonjs_or_umd_when_targeting_ES6_or_higher: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile external modules into 'amd', 'commonjs' or 'umd' when targeting 'ES6' or higher." },
165165
Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher: { code: 1205, category: DiagnosticCategory.Error, key: "Decorators are only available when targeting ECMAScript 5 and higher." },
166166
Decorators_are_not_valid_here: { code: 1206, category: DiagnosticCategory.Error, key: "Decorators are not valid here." },
167167
Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name: { code: 1207, category: DiagnosticCategory.Error, key: "Decorators cannot be applied to multiple get/set accessors of the same name." },
@@ -463,7 +463,7 @@ module ts {
463463
Do_not_emit_comments_to_output: { code: 6009, category: DiagnosticCategory.Message, key: "Do not emit comments to output." },
464464
Do_not_emit_outputs: { code: 6010, category: DiagnosticCategory.Message, key: "Do not emit outputs." },
465465
Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES6_experimental: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES6' (experimental)" },
466-
Specify_module_code_generation_Colon_commonjs_or_amd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs' or 'amd'" },
466+
Specify_module_code_generation_Colon_commonjs_amd_or_umd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs', 'amd', or 'umd'." },
467467
Print_this_message: { code: 6017, category: DiagnosticCategory.Message, key: "Print this message." },
468468
Print_the_compiler_s_version: { code: 6019, category: DiagnosticCategory.Message, key: "Print the compiler's version." },
469469
Compile_the_project_in_the_given_directory: { code: 6020, category: DiagnosticCategory.Message, key: "Compile the project in the given directory." },
@@ -484,8 +484,8 @@ module ts {
484484
Generates_corresponding_map_file: { code: 6043, category: DiagnosticCategory.Message, key: "Generates corresponding '.map' file." },
485485
Compiler_option_0_expects_an_argument: { code: 6044, category: DiagnosticCategory.Error, key: "Compiler option '{0}' expects an argument." },
486486
Unterminated_quoted_string_in_response_file_0: { code: 6045, category: DiagnosticCategory.Error, key: "Unterminated quoted string in response file '{0}'." },
487-
Argument_for_module_option_must_be_commonjs_or_amd: { code: 6046, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
488-
Argument_for_target_option_must_be_es3_es5_or_es6: { code: 6047, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3', 'es5', or 'es6'." },
487+
Argument_for_module_option_must_be_commonjs_amd_or_umd: { code: 6046, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs', 'amd', or 'umd'." },
488+
Argument_for_target_option_must_be_ES3_ES5_or_ES6: { code: 6047, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'ES3', 'ES5', or 'ES6'." },
489489
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: { code: 6048, category: DiagnosticCategory.Error, key: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'." },
490490
Unsupported_locale_0: { code: 6049, category: DiagnosticCategory.Error, key: "Unsupported locale '{0}'." },
491491
Unable_to_open_file_0: { code: 6050, category: DiagnosticCategory.Error, key: "Unable to open file '{0}'." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@
631631
"category": "Error",
632632
"code": 1203
633633
},
634-
"Cannot compile external modules into amd or commonjs when targeting es6 or higher.": {
634+
"Cannot compile external modules into 'amd', 'commonjs' or 'umd' when targeting 'ES6' or higher.": {
635635
"category": "Error",
636636
"code": 1204
637637
},
@@ -1840,7 +1840,7 @@
18401840
"category": "Message",
18411841
"code": 6015
18421842
},
1843-
"Specify module code generation: 'commonjs' or 'amd'": {
1843+
"Specify module code generation: 'commonjs', 'amd', or 'umd'.": {
18441844
"category": "Message",
18451845
"code": 6016
18461846
},
@@ -1924,11 +1924,11 @@
19241924
"category": "Error",
19251925
"code": 6045
19261926
},
1927-
"Argument for '--module' option must be 'commonjs' or 'amd'.": {
1927+
"Argument for '--module' option must be 'commonjs', 'amd', or 'umd'.": {
19281928
"category": "Error",
19291929
"code": 6046
19301930
},
1931-
"Argument for '--target' option must be 'es3', 'es5', or 'es6'.": {
1931+
"Argument for '--target' option must be 'ES3', 'ES5', or 'ES6'.": {
19321932
"category": "Error",
19331933
"code": 6047
19341934
},

src/compiler/emitter.ts

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4645,27 +4645,25 @@ var __param = this.__param || function(index, decorator) { return function (targ
46454645
}
46464646
}
46474647

4648-
function emitAMDModule(node: SourceFile, startIndex: number) {
4649-
collectExternalModuleInfo(node);
4650-
4648+
function emitAMDDependencies(node: SourceFile, includeNonAmdDependencies: boolean) {
46514649
// An AMD define function has the following shape:
46524650
// define(id?, dependencies?, factory);
46534651
//
46544652
// This has the shape of
46554653
// define(name, ["module1", "module2"], function (module1Alias) {
4656-
// The location of the alias in the parameter list in the factory function needs to
4654+
// The location of the alias in the parameter list in the factory function needs to
46574655
// match the position of the module name in the dependency list.
46584656
//
4659-
// To ensure this is true in cases of modules with no aliases, e.g.:
4660-
// `import "module"` or `<amd-dependency path= "a.css" />`
4657+
// To ensure this is true in cases of modules with no aliases, e.g.:
4658+
// `import "module"` or `<amd-dependency path= "a.css" />`
46614659
// we need to add modules without alias names to the end of the dependencies list
4662-
4663-
let aliasedModuleNames: string[] = []; // names of modules with corresponding parameter in the
4660+
4661+
let aliasedModuleNames: string[] = []; // names of modules with corresponding parameter in the
46644662
// factory function.
46654663
let unaliasedModuleNames: string[] = []; // names of modules with no corresponding parameters in
46664664
// factory function.
4667-
let importAliasNames: string[] = []; // names of the parameters in the factory function; these
4668-
// paramters need to match the indexes of the corresponding
4665+
let importAliasNames: string[] = []; // names of the parameters in the factory function; these
4666+
// parameters need to match the indexes of the corresponding
46694667
// module names in aliasedModuleNames.
46704668

46714669
// Fill in amd-dependency tags
@@ -4687,7 +4685,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
46874685
externalModuleName = getLiteralText(<LiteralExpression>moduleName);
46884686
}
46894687

4690-
// Find the name of the module alais, if there is one
4688+
// Find the name of the module alias, if there is one
46914689
let importAliasName: string;
46924690
let namespaceDeclaration = getNamespaceDeclarationNode(importNode);
46934691
if (namespaceDeclaration && !isDefaultImport(importNode)) {
@@ -4697,20 +4695,15 @@ var __param = this.__param || function(index, decorator) { return function (targ
46974695
importAliasName = getGeneratedNameForNode(<ImportDeclaration | ExportDeclaration>importNode);
46984696
}
46994697

4700-
if (importAliasName) {
4698+
if (includeNonAmdDependencies && importAliasName) {
47014699
aliasedModuleNames.push(externalModuleName);
47024700
importAliasNames.push(importAliasName);
47034701
}
47044702
else {
47054703
unaliasedModuleNames.push(externalModuleName);
47064704
}
47074705
}
4708-
4709-
writeLine();
4710-
write("define(");
4711-
if (node.amdModuleName) {
4712-
write("\"" + node.amdModuleName + "\", ");
4713-
}
4706+
47144707
write("[\"require\", \"exports\"");
47154708
if (aliasedModuleNames.length) {
47164709
write(", ");
@@ -4725,6 +4718,17 @@ var __param = this.__param || function(index, decorator) { return function (targ
47254718
write(", ");
47264719
write(importAliasNames.join(", "));
47274720
}
4721+
}
4722+
4723+
function emitAMDModule(node: SourceFile, startIndex: number) {
4724+
collectExternalModuleInfo(node);
4725+
4726+
writeLine();
4727+
write("define(");
4728+
if (node.amdModuleName) {
4729+
write("\"" + node.amdModuleName + "\", ");
4730+
}
4731+
emitAMDDependencies(node, /*includeNonAmdDependencies*/ true);
47284732
write(") {");
47294733
increaseIndent();
47304734
emitExportStarHelper();
@@ -4746,6 +4750,31 @@ var __param = this.__param || function(index, decorator) { return function (targ
47464750
emitExportEquals(/*emitAsReturn*/ false);
47474751
}
47484752

4753+
function emitUMDModule(node: SourceFile, startIndex: number) {
4754+
collectExternalModuleInfo(node);
4755+
4756+
// Module is detected first to support Browserify users that load into a browser with an AMD loader
4757+
writeLines(`(function (deps, factory) {
4758+
if (typeof module === 'object' && typeof module.exports === 'object') {
4759+
var v = factory(require, exports); if (v !== undefined) module.exports = v;
4760+
}
4761+
else if (typeof define === 'function' && define.amd) {
4762+
define(deps, factory);
4763+
}
4764+
})(`);
4765+
emitAMDDependencies(node, false);
4766+
write(") {");
4767+
increaseIndent();
4768+
emitExportStarHelper();
4769+
emitCaptureThisForNodeIfNecessary(node);
4770+
emitLinesStartingAt(node.statements, startIndex);
4771+
emitTempDeclarations(/*newLine*/ true);
4772+
emitExportEquals(/*emitAsReturn*/ true);
4773+
decreaseIndent();
4774+
writeLine();
4775+
write("});");
4776+
}
4777+
47494778
function emitES6Module(node: SourceFile, startIndex: number) {
47504779
externalImports = undefined;
47514780
exportSpecifiers = undefined;
@@ -4830,6 +4859,9 @@ var __param = this.__param || function(index, decorator) { return function (targ
48304859
else if (compilerOptions.module === ModuleKind.AMD) {
48314860
emitAMDModule(node, startIndex);
48324861
}
4862+
else if (compilerOptions.module === ModuleKind.UMD) {
4863+
emitUMDModule(node, startIndex);
4864+
}
48334865
else {
48344866
emitCommonJSModule(node, startIndex);
48354867
}

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ module ts {
567567

568568
// Cannot specify module gen target when in es6 or above
569569
if (options.module && languageVersion >= ScriptTarget.ES6) {
570-
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_external_modules_into_amd_or_commonjs_when_targeting_es6_or_higher));
570+
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_external_modules_into_amd_commonjs_or_umd_when_targeting_ES6_or_higher));
571571
}
572572

573573
// there has to be common source directory if user specified --outdir || --sourceRoot

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,7 @@ module ts {
16741674
None = 0,
16751675
CommonJS = 1,
16761676
AMD = 2,
1677+
UMD = 3,
16771678
}
16781679

16791680
export interface LineAndCharacter {

src/harness/harness.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,8 @@ module Harness {
957957
if (typeof setting.value === 'string') {
958958
if (setting.value.toLowerCase() === 'amd') {
959959
options.module = ts.ModuleKind.AMD;
960+
} else if (setting.value.toLowerCase() === 'umd') {
961+
options.module = ts.ModuleKind.UMD;
960962
} else if (setting.value.toLowerCase() === 'commonjs') {
961963
options.module = ts.ModuleKind.CommonJS;
962964
} else if (setting.value.toLowerCase() === 'unspecified') {

tests/baselines/reference/constDeclarations-access5.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error TS1204: Cannot compile external modules into amd or commonjs when targeting es6 or higher.
1+
error TS1204: Cannot compile external modules into 'amd', 'commonjs' or 'umd' when targeting 'ES6' or higher.
22
tests/cases/compiler/constDeclarations_access_2.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from "mod"', 'import {a} from "mod"' or 'import d from "mod"' instead.
33
tests/cases/compiler/constDeclarations_access_2.ts(4,1): error TS2450: Left-hand side of assignment expression cannot be a constant.
44
tests/cases/compiler/constDeclarations_access_2.ts(5,1): error TS2450: Left-hand side of assignment expression cannot be a constant.
@@ -20,7 +20,7 @@ tests/cases/compiler/constDeclarations_access_2.ts(22,3): error TS2449: The oper
2020
tests/cases/compiler/constDeclarations_access_2.ts(24,1): error TS2450: Left-hand side of assignment expression cannot be a constant.
2121

2222

23-
!!! error TS1204: Cannot compile external modules into amd or commonjs when targeting es6 or higher.
23+
!!! error TS1204: Cannot compile external modules into 'amd', 'commonjs' or 'umd' when targeting 'ES6' or higher.
2424
==== tests/cases/compiler/constDeclarations_access_2.ts (19 errors) ====
2525
///<reference path='constDeclarations_access_1.ts'/>
2626
import m = require('constDeclarations_access_1');

tests/baselines/reference/es5-umd.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//// [es5-umd.ts]
2+
3+
class A
4+
{
5+
constructor ()
6+
{
7+
8+
}
9+
10+
public B()
11+
{
12+
return 42;
13+
}
14+
}
15+
16+
17+
//// [es5-umd.js]
18+
var A = (function () {
19+
function A() {
20+
}
21+
A.prototype.B = function () {
22+
return 42;
23+
};
24+
return A;
25+
})();
26+
//# sourceMappingURL=es5-umd.js.map
27+
28+
//// [es5-umd.d.ts]
29+
declare class A {
30+
constructor();
31+
B(): number;
32+
}

tests/baselines/reference/es5-umd.js.map

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)