Skip to content

Commit 2b70196

Browse files
committed
Merge pull request #1049 from Microsoft/const_enums
Convert majority of enums in compiler to const enums
2 parents 26b2211 + 9051bc9 commit 2b70196

19 files changed

+11904
-12991
lines changed

Jakefile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function concatenateFiles(destinationFile, sourceFiles) {
133133
fs.renameSync(temp, destinationFile);
134134
}
135135

136-
var useDebugMode = false;
136+
var useDebugMode = true;
137137
var generateDeclarations = false;
138138
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
139139
var compilerFilename = "tsc.js";
@@ -148,15 +148,16 @@ var compilerFilename = "tsc.js";
148148
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile) {
149149
file(outFile, prereqs, function() {
150150
var dir = useBuiltCompiler ? builtLocalDirectory : LKGDirectory;
151-
var options = "-removeComments --module commonjs -noImplicitAny "; //" -propagateEnumConstants "
151+
var options = "-removeComments --module commonjs -noImplicitAny ";
152152
if (generateDeclarations) {
153153
options += "--declaration ";
154154
}
155-
156-
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
155+
157156
if (useDebugMode) {
158-
cmd = cmd + " " + path.join(harnessDirectory, "external/es5compat.ts") + " " + path.join(harnessDirectory, "external/json2.ts") + " ";
157+
options += "--preserveConstEnums ";
159158
}
159+
160+
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
160161
cmd = cmd + sources.join(" ") + (!noOutFile ? " -out " + outFile : "");
161162
if (useDebugMode) {
162163
cmd = cmd + " -sourcemap -mapRoot file:///" + path.resolve(path.dirname(outFile));
@@ -258,12 +259,11 @@ task("local", ["generate-diagnostics", "lib", tscFile, servicesFile]);
258259

259260

260261
// Local target to build the compiler and services
261-
desc("Emit debug mode files with sourcemaps");
262-
task("debug", function() {
263-
useDebugMode = true;
262+
desc("Sets release mode flag");
263+
task("release", function() {
264+
useDebugMode = false;
264265
});
265266

266-
267267
// Set the default task to "local"
268268
task("default", ["local"]);
269269

@@ -312,7 +312,7 @@ task("generate-spec", [specMd])
312312

313313
// Makes a new LKG. This target does not build anything, but errors if not all the outputs are present in the built/local directory
314314
desc("Makes a new LKG out of the built js files");
315-
task("LKG", libraryTargets, function() {
315+
task("LKG", ["clean", "release", "local"].concat(libraryTargets), function() {
316316
var expectedFiles = [tscFile, servicesFile].concat(libraryTargets);
317317
var missingFiles = expectedFiles.filter(function (f) {
318318
return !fs.existsSync(f);

bin/tsc.js

Lines changed: 2897 additions & 2982 deletions
Large diffs are not rendered by default.

bin/typescriptServices.js

Lines changed: 8946 additions & 9952 deletions
Large diffs are not rendered by default.

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module ts {
77

8-
export enum ModuleInstanceState {
8+
export const enum ModuleInstanceState {
99
NonInstantiated = 0,
1010
Instantiated = 1,
1111
ConstEnumOnly = 2

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,7 @@ module ts {
16061606
return true;
16071607

16081608
default:
1609-
Debug.fail("isDeclarationVisible unknown: SyntaxKind: " + SyntaxKind[node.kind]);
1609+
Debug.fail("isDeclarationVisible unknown: SyntaxKind: " + node.kind);
16101610
}
16111611
}
16121612

src/compiler/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ts {
99
// x | y is False if both x and y are False.
1010
// x | y is Maybe if either x or y is Maybe, but neither x or y is True.
1111
// x | y is True if either x or y is True.
12-
export enum Ternary {
12+
export const enum Ternary {
1313
False = 0,
1414
Maybe = 1,
1515
True = -1
@@ -19,7 +19,7 @@ module ts {
1919
[index: string]: T;
2020
}
2121

22-
export enum Comparison {
22+
export const enum Comparison {
2323
LessThan = -1,
2424
EqualTo = 0,
2525
GreaterThan = 1
@@ -637,7 +637,7 @@ module ts {
637637
getSignatureConstructor: () => <any>Signature
638638
}
639639

640-
export enum AssertionLevel {
640+
export const enum AssertionLevel {
641641
None = 0,
642642
Normal = 1,
643643
Aggressive = 2,

src/compiler/emitter.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,8 @@ module ts {
18981898

18991899
function emitEnumDeclaration(node: EnumDeclaration) {
19001900
// const enums are completely erased during compilation.
1901-
if (isConstEnumDeclaration(node) && !compilerOptions.preserveConstEnums) {
1901+
var isConstEnum = isConstEnumDeclaration(node);
1902+
if (isConstEnum && !compilerOptions.preserveConstEnums) {
19021903
return;
19031904
}
19041905
emitLeadingComments(node);
@@ -1918,7 +1919,7 @@ module ts {
19181919
write(") {");
19191920
increaseIndent();
19201921
scopeEmitStart(node);
1921-
emitEnumMemberDeclarations();
1922+
emitEnumMemberDeclarations(isConstEnum);
19221923
decreaseIndent();
19231924
writeLine();
19241925
emitToken(SyntaxKind.CloseBraceToken, node.members.end);
@@ -1941,7 +1942,7 @@ module ts {
19411942
}
19421943
emitTrailingComments(node);
19431944

1944-
function emitEnumMemberDeclarations() {
1945+
function emitEnumMemberDeclarations(isConstEnum: boolean) {
19451946
forEach(node.members, member => {
19461947
writeLine();
19471948
emitLeadingComments(member);
@@ -1952,7 +1953,7 @@ module ts {
19521953
write("[");
19531954
emitQuotedIdentifier(member.name);
19541955
write("] = ");
1955-
if (member.initializer) {
1956+
if (member.initializer && !isConstEnum) {
19561957
emit(member.initializer);
19571958
}
19581959
else {
@@ -2829,7 +2830,7 @@ module ts {
28292830
break;
28302831

28312832
default:
2832-
Debug.fail("This is unknown parent for type parameter: " + SyntaxKind[node.parent.kind]);
2833+
Debug.fail("This is unknown parent for type parameter: " + node.parent.kind);
28332834
}
28342835

28352836
return {
@@ -3225,7 +3226,7 @@ module ts {
32253226
break;
32263227

32273228
default:
3228-
Debug.fail("This is unknown kind for signature: " + SyntaxKind[node.kind]);
3229+
Debug.fail("This is unknown kind for signature: " + node.kind);
32293230
}
32303231

32313232
return {
@@ -3310,7 +3311,7 @@ module ts {
33103311
break;
33113312

33123313
default:
3313-
Debug.fail("This is unknown parent for parameter: " + SyntaxKind[node.parent.kind]);
3314+
Debug.fail("This is unknown parent for parameter: " + node.parent.kind);
33143315
}
33153316

33163317
return {

src/compiler/parser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ module ts {
666666
return undefined;
667667
}
668668

669-
enum ParsingContext {
669+
const enum ParsingContext {
670670
SourceElements, // Elements in source file
671671
ModuleElements, // Elements in module declaration
672672
BlockStatements, // Statements in block
@@ -687,7 +687,7 @@ module ts {
687687
Count // Number of parsing contexts
688688
}
689689

690-
enum Tristate {
690+
const enum Tristate {
691691
False,
692692
True,
693693
Unknown
@@ -715,13 +715,13 @@ module ts {
715715
}
716716
};
717717

718-
enum LookAheadMode {
718+
const enum LookAheadMode {
719719
NotLookingAhead,
720720
NoErrorYet,
721721
Error
722722
}
723723

724-
enum ModifierContext {
724+
const enum ModifierContext {
725725
SourceElements, // Top level elements in a source file
726726
ModuleElements, // Elements in module declaration
727727
ClassMembers, // Members in class declaration
@@ -730,7 +730,7 @@ module ts {
730730

731731
// Tracks whether we nested (directly or indirectly) in a certain control block.
732732
// Used for validating break and continue statements.
733-
enum ControlBlockContext {
733+
const enum ControlBlockContext {
734734
NotNested,
735735
Nested,
736736
CrossingFunctionBoundary
@@ -2734,7 +2734,7 @@ module ts {
27342734
currentKind = SetAccesor;
27352735
}
27362736
else {
2737-
Debug.fail("Unexpected syntax kind:" + SyntaxKind[p.kind]);
2737+
Debug.fail("Unexpected syntax kind:" + p.kind);
27382738
}
27392739

27402740
if (!hasProperty(seen, p.name.text)) {

src/compiler/types.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module ts {
99
}
1010

1111
// token > SyntaxKind.Identifer => token is a keyword
12-
export enum SyntaxKind {
12+
export const enum SyntaxKind {
1313
Unknown,
1414
EndOfFileToken,
1515
SingleLineCommentTrivia,
@@ -249,7 +249,7 @@ module ts {
249249
LastTemplateToken = TemplateTail
250250
}
251251

252-
export enum NodeFlags {
252+
export const enum NodeFlags {
253253
Export = 0x00000001, // Declarations
254254
Ambient = 0x00000002, // Declarations
255255
QuestionMark = 0x00000004, // Parameter/Property/Method
@@ -722,7 +722,7 @@ module ts {
722722
trackSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): void;
723723
}
724724

725-
export enum TypeFormatFlags {
725+
export const enum TypeFormatFlags {
726726
None = 0x00000000,
727727
WriteArrayAsGenericType = 0x00000001, // Write Array<T> instead T[]
728728
UseTypeOfFunction = 0x00000002, // Write typeof instead of function type literal
@@ -733,7 +733,7 @@ module ts {
733733
InElementType = 0x00000040, // Writing an array or union element type
734734
}
735735

736-
export enum SymbolFormatFlags {
736+
export const enum SymbolFormatFlags {
737737
None = 0x00000000,
738738
WriteTypeParametersOrArguments = 0x00000001, // Write symbols's type argument if it is instantiated symbol
739739
// eg. class C<T> { p: T } <-- Show p as C<T>.p here
@@ -744,7 +744,7 @@ module ts {
744744
// When this flag is specified m.c will be used to refer to the class instead of alias symbol x
745745
}
746746

747-
export enum SymbolAccessibility {
747+
export const enum SymbolAccessibility {
748748
Accessible,
749749
NotAccessible,
750750
CannotBeNamed
@@ -778,7 +778,7 @@ module ts {
778778
hasEarlyErrors(sourceFile?: SourceFile): boolean;
779779
}
780780

781-
export enum SymbolFlags {
781+
export const enum SymbolFlags {
782782
FunctionScopedVariable = 0x00000001, // Variable (var) or parameter
783783
BlockScopedVariable = 0x00000002, // A block-scoped variable (let or const)
784784
Property = 0x00000004, // Property or enum member
@@ -890,7 +890,7 @@ module ts {
890890
[index: string]: Symbol;
891891
}
892892

893-
export enum NodeCheckFlags {
893+
export const enum NodeCheckFlags {
894894
TypeChecked = 0x00000001, // Node has been type checked
895895
LexicalThis = 0x00000002, // Lexical 'this' reference
896896
CaptureThis = 0x00000004, // Lexical 'this' used in body
@@ -915,7 +915,7 @@ module ts {
915915
assignmentChecks?: Map<boolean>; // Cache of assignment checks
916916
}
917917

918-
export enum TypeFlags {
918+
export const enum TypeFlags {
919919
Any = 0x00000001,
920920
String = 0x00000002,
921921
Number = 0x00000004,
@@ -1012,7 +1012,7 @@ module ts {
10121012
mapper?: TypeMapper; // Instantiation mapper
10131013
}
10141014

1015-
export enum SignatureKind {
1015+
export const enum SignatureKind {
10161016
Call,
10171017
Construct,
10181018
}
@@ -1032,7 +1032,7 @@ module ts {
10321032
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
10331033
}
10341034

1035-
export enum IndexKind {
1035+
export const enum IndexKind {
10361036
String,
10371037
Number,
10381038
}
@@ -1112,7 +1112,7 @@ module ts {
11121112
[option: string]: string | number | boolean;
11131113
}
11141114

1115-
export enum ModuleKind {
1115+
export const enum ModuleKind {
11161116
None,
11171117
CommonJS,
11181118
AMD,
@@ -1127,7 +1127,7 @@ module ts {
11271127
}
11281128

11291129

1130-
export enum ScriptTarget {
1130+
export const enum ScriptTarget {
11311131
ES3,
11321132
ES5,
11331133
ES6,
@@ -1149,7 +1149,7 @@ module ts {
11491149
error?: DiagnosticMessage; // The error given when the argument does not fit a customized 'type'.
11501150
}
11511151

1152-
export enum CharacterCodes {
1152+
export const enum CharacterCodes {
11531153
nullCharacter = 0,
11541154
maxAsciiCharacter = 0x7F,
11551155

src/harness/compilerRunner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// <reference path='typeWriter.ts' />
44
/// <reference path='syntacticCleaner.ts' />
55

6-
enum CompilerTestType {
6+
const enum CompilerTestType {
77
Conformance,
88
Regressions,
99
Test262

src/harness/fourslash.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@ module FourSlash {
23682368
};
23692369
}
23702370

2371-
enum State {
2371+
const enum State {
23722372
none,
23732373
inSlashStarMarker,
23742374
inObjectMarker

src/harness/harness.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module Utils {
3030
var global = <any>Function("return this").call(null);
3131

3232
// Setup some globals based on the current environment
33-
export enum ExecutionEnvironment {
33+
export const enum ExecutionEnvironment {
3434
Node,
3535
Browser,
3636
CScript

src/harness/typeWriter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
interface TypeWriterResult {
22
line: number;
33
column: number;
4-
syntaxKind: string;
4+
syntaxKind: number;
55
sourceText: string;
66
type: string;
77
}
@@ -84,7 +84,7 @@ class TypeWriterWalker {
8484
this.results.push({
8585
line: lineAndCharacter.line - 1,
8686
column: lineAndCharacter.character,
87-
syntaxKind: ts.SyntaxKind[node.kind],
87+
syntaxKind: node.kind,
8888
sourceText: sourceText,
8989
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.WriteOwnNameForAnyLike)
9090
});

0 commit comments

Comments
 (0)