Merge pull request #1049 from Microsoft/const_enums

Convert majority of enums in compiler to const enums
This commit is contained in:
Vladimir Matveev 2014-11-04 12:28:40 -08:00
commit 2b701963ee
19 changed files with 10938 additions and 12025 deletions

View File

@ -133,7 +133,7 @@ function concatenateFiles(destinationFile, sourceFiles) {
fs.renameSync(temp, destinationFile);
}
var useDebugMode = false;
var useDebugMode = true;
var generateDeclarations = false;
var host = (process.env.host || process.env.TYPESCRIPT_HOST || "node");
var compilerFilename = "tsc.js";
@ -148,15 +148,16 @@ var compilerFilename = "tsc.js";
function compileFile(outFile, sources, prereqs, prefixes, useBuiltCompiler, noOutFile) {
file(outFile, prereqs, function() {
var dir = useBuiltCompiler ? builtLocalDirectory : LKGDirectory;
var options = "-removeComments --module commonjs -noImplicitAny "; //" -propagateEnumConstants "
var options = "-removeComments --module commonjs -noImplicitAny ";
if (generateDeclarations) {
options += "--declaration ";
}
if (useDebugMode) {
options += "--preserveConstEnums ";
}
var cmd = host + " " + dir + compilerFilename + " " + options + " ";
if (useDebugMode) {
cmd = cmd + " " + path.join(harnessDirectory, "external/es5compat.ts") + " " + path.join(harnessDirectory, "external/json2.ts") + " ";
}
cmd = cmd + sources.join(" ") + (!noOutFile ? " -out " + outFile : "");
if (useDebugMode) {
cmd = cmd + " -sourcemap -mapRoot file:///" + path.resolve(path.dirname(outFile));
@ -258,12 +259,11 @@ task("local", ["generate-diagnostics", "lib", tscFile, servicesFile]);
// Local target to build the compiler and services
desc("Emit debug mode files with sourcemaps");
task("debug", function() {
useDebugMode = true;
desc("Sets release mode flag");
task("release", function() {
useDebugMode = false;
});
// Set the default task to "local"
task("default", ["local"]);
@ -312,7 +312,7 @@ task("generate-spec", [specMd])
// Makes a new LKG. This target does not build anything, but errors if not all the outputs are present in the built/local directory
desc("Makes a new LKG out of the built js files");
task("LKG", libraryTargets, function() {
task("LKG", ["clean", "release", "local"].concat(libraryTargets), function() {
var expectedFiles = [tscFile, servicesFile].concat(libraryTargets);
var missingFiles = expectedFiles.filter(function (f) {
return !fs.existsSync(f);

5879
bin/tsc.js

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@ -5,7 +5,7 @@
module ts {
export enum ModuleInstanceState {
export const enum ModuleInstanceState {
NonInstantiated = 0,
Instantiated = 1,
ConstEnumOnly = 2

View File

@ -1606,7 +1606,7 @@ module ts {
return true;
default:
Debug.fail("isDeclarationVisible unknown: SyntaxKind: " + SyntaxKind[node.kind]);
Debug.fail("isDeclarationVisible unknown: SyntaxKind: " + node.kind);
}
}

View File

@ -9,7 +9,7 @@ module ts {
// x | y is False if both x and y are False.
// x | y is Maybe if either x or y is Maybe, but neither x or y is True.
// x | y is True if either x or y is True.
export enum Ternary {
export const enum Ternary {
False = 0,
Maybe = 1,
True = -1
@ -19,7 +19,7 @@ module ts {
[index: string]: T;
}
export enum Comparison {
export const enum Comparison {
LessThan = -1,
EqualTo = 0,
GreaterThan = 1
@ -637,7 +637,7 @@ module ts {
getSignatureConstructor: () => <any>Signature
}
export enum AssertionLevel {
export const enum AssertionLevel {
None = 0,
Normal = 1,
Aggressive = 2,

View File

@ -1898,7 +1898,8 @@ module ts {
function emitEnumDeclaration(node: EnumDeclaration) {
// const enums are completely erased during compilation.
if (isConstEnumDeclaration(node) && !compilerOptions.preserveConstEnums) {
var isConstEnum = isConstEnumDeclaration(node);
if (isConstEnum && !compilerOptions.preserveConstEnums) {
return;
}
emitLeadingComments(node);
@ -1918,7 +1919,7 @@ module ts {
write(") {");
increaseIndent();
scopeEmitStart(node);
emitEnumMemberDeclarations();
emitEnumMemberDeclarations(isConstEnum);
decreaseIndent();
writeLine();
emitToken(SyntaxKind.CloseBraceToken, node.members.end);
@ -1941,7 +1942,7 @@ module ts {
}
emitTrailingComments(node);
function emitEnumMemberDeclarations() {
function emitEnumMemberDeclarations(isConstEnum: boolean) {
forEach(node.members, member => {
writeLine();
emitLeadingComments(member);
@ -1952,7 +1953,7 @@ module ts {
write("[");
emitQuotedIdentifier(member.name);
write("] = ");
if (member.initializer) {
if (member.initializer && !isConstEnum) {
emit(member.initializer);
}
else {
@ -2829,7 +2830,7 @@ module ts {
break;
default:
Debug.fail("This is unknown parent for type parameter: " + SyntaxKind[node.parent.kind]);
Debug.fail("This is unknown parent for type parameter: " + node.parent.kind);
}
return {
@ -3225,7 +3226,7 @@ module ts {
break;
default:
Debug.fail("This is unknown kind for signature: " + SyntaxKind[node.kind]);
Debug.fail("This is unknown kind for signature: " + node.kind);
}
return {
@ -3310,7 +3311,7 @@ module ts {
break;
default:
Debug.fail("This is unknown parent for parameter: " + SyntaxKind[node.parent.kind]);
Debug.fail("This is unknown parent for parameter: " + node.parent.kind);
}
return {

View File

@ -666,7 +666,7 @@ module ts {
return undefined;
}
enum ParsingContext {
const enum ParsingContext {
SourceElements, // Elements in source file
ModuleElements, // Elements in module declaration
BlockStatements, // Statements in block
@ -687,7 +687,7 @@ module ts {
Count // Number of parsing contexts
}
enum Tristate {
const enum Tristate {
False,
True,
Unknown
@ -715,13 +715,13 @@ module ts {
}
};
enum LookAheadMode {
const enum LookAheadMode {
NotLookingAhead,
NoErrorYet,
Error
}
enum ModifierContext {
const enum ModifierContext {
SourceElements, // Top level elements in a source file
ModuleElements, // Elements in module declaration
ClassMembers, // Members in class declaration
@ -730,7 +730,7 @@ module ts {
// Tracks whether we nested (directly or indirectly) in a certain control block.
// Used for validating break and continue statements.
enum ControlBlockContext {
const enum ControlBlockContext {
NotNested,
Nested,
CrossingFunctionBoundary
@ -2734,7 +2734,7 @@ module ts {
currentKind = SetAccesor;
}
else {
Debug.fail("Unexpected syntax kind:" + SyntaxKind[p.kind]);
Debug.fail("Unexpected syntax kind:" + p.kind);
}
if (!hasProperty(seen, p.name.text)) {

View File

@ -9,7 +9,7 @@ module ts {
}
// token > SyntaxKind.Identifer => token is a keyword
export enum SyntaxKind {
export const enum SyntaxKind {
Unknown,
EndOfFileToken,
SingleLineCommentTrivia,
@ -249,7 +249,7 @@ module ts {
LastTemplateToken = TemplateTail
}
export enum NodeFlags {
export const enum NodeFlags {
Export = 0x00000001, // Declarations
Ambient = 0x00000002, // Declarations
QuestionMark = 0x00000004, // Parameter/Property/Method
@ -722,7 +722,7 @@ module ts {
trackSymbol(symbol: Symbol, enclosingDeclaration?: Node, meaning?: SymbolFlags): void;
}
export enum TypeFormatFlags {
export const enum TypeFormatFlags {
None = 0x00000000,
WriteArrayAsGenericType = 0x00000001, // Write Array<T> instead T[]
UseTypeOfFunction = 0x00000002, // Write typeof instead of function type literal
@ -733,7 +733,7 @@ module ts {
InElementType = 0x00000040, // Writing an array or union element type
}
export enum SymbolFormatFlags {
export const enum SymbolFormatFlags {
None = 0x00000000,
WriteTypeParametersOrArguments = 0x00000001, // Write symbols's type argument if it is instantiated symbol
// eg. class C<T> { p: T } <-- Show p as C<T>.p here
@ -744,7 +744,7 @@ module ts {
// When this flag is specified m.c will be used to refer to the class instead of alias symbol x
}
export enum SymbolAccessibility {
export const enum SymbolAccessibility {
Accessible,
NotAccessible,
CannotBeNamed
@ -778,7 +778,7 @@ module ts {
hasEarlyErrors(sourceFile?: SourceFile): boolean;
}
export enum SymbolFlags {
export const enum SymbolFlags {
FunctionScopedVariable = 0x00000001, // Variable (var) or parameter
BlockScopedVariable = 0x00000002, // A block-scoped variable (let or const)
Property = 0x00000004, // Property or enum member
@ -890,7 +890,7 @@ module ts {
[index: string]: Symbol;
}
export enum NodeCheckFlags {
export const enum NodeCheckFlags {
TypeChecked = 0x00000001, // Node has been type checked
LexicalThis = 0x00000002, // Lexical 'this' reference
CaptureThis = 0x00000004, // Lexical 'this' used in body
@ -915,7 +915,7 @@ module ts {
assignmentChecks?: Map<boolean>; // Cache of assignment checks
}
export enum TypeFlags {
export const enum TypeFlags {
Any = 0x00000001,
String = 0x00000002,
Number = 0x00000004,
@ -1012,7 +1012,7 @@ module ts {
mapper?: TypeMapper; // Instantiation mapper
}
export enum SignatureKind {
export const enum SignatureKind {
Call,
Construct,
}
@ -1032,7 +1032,7 @@ module ts {
isolatedSignatureType?: ObjectType; // A manufactured type that just contains the signature for purposes of signature comparison
}
export enum IndexKind {
export const enum IndexKind {
String,
Number,
}
@ -1112,7 +1112,7 @@ module ts {
[option: string]: string | number | boolean;
}
export enum ModuleKind {
export const enum ModuleKind {
None,
CommonJS,
AMD,
@ -1127,7 +1127,7 @@ module ts {
}
export enum ScriptTarget {
export const enum ScriptTarget {
ES3,
ES5,
ES6,
@ -1149,7 +1149,7 @@ module ts {
error?: DiagnosticMessage; // The error given when the argument does not fit a customized 'type'.
}
export enum CharacterCodes {
export const enum CharacterCodes {
nullCharacter = 0,
maxAsciiCharacter = 0x7F,

View File

@ -3,7 +3,7 @@
/// <reference path='typeWriter.ts' />
/// <reference path='syntacticCleaner.ts' />
enum CompilerTestType {
const enum CompilerTestType {
Conformance,
Regressions,
Test262

View File

@ -2368,7 +2368,7 @@ module FourSlash {
};
}
enum State {
const enum State {
none,
inSlashStarMarker,
inObjectMarker

View File

@ -30,7 +30,7 @@ module Utils {
var global = <any>Function("return this").call(null);
// Setup some globals based on the current environment
export enum ExecutionEnvironment {
export const enum ExecutionEnvironment {
Node,
Browser,
CScript

View File

@ -1,7 +1,7 @@
interface TypeWriterResult {
line: number;
column: number;
syntaxKind: string;
syntaxKind: number;
sourceText: string;
type: string;
}
@ -84,7 +84,7 @@ class TypeWriterWalker {
this.results.push({
line: lineAndCharacter.line - 1,
column: lineAndCharacter.character,
syntaxKind: ts.SyntaxKind[node.kind],
syntaxKind: node.kind,
sourceText: sourceText,
type: this.checker.typeToString(type, node.parent, ts.TypeFormatFlags.NoTruncation | ts.TypeFormatFlags.WriteOwnNameForAnyLike)
});

View File

@ -1065,7 +1065,7 @@ module ts {
emitOutputStatus: EmitReturnStatus;
}
export enum OutputFileType {
export const enum OutputFileType {
JavaScript,
SourceMap,
Declaration
@ -1077,7 +1077,7 @@ module ts {
text: string;
}
export enum EndOfLineState {
export const enum EndOfLineState {
Start,
InMultiLineCommentTrivia,
InSingleQuoteStringLiteral,
@ -1780,7 +1780,7 @@ module ts {
var buckets: Map<Map<DocumentRegistryEntry>> = {};
function getKeyFromCompilationSettings(settings: CompilerOptions): string {
return "_" + ScriptTarget[settings.target]; // + "|" + settings.propagateEnumConstantoString()
return "_" + settings.target; // + "|" + settings.propagateEnumConstantoString()
}
function getBucketForCompilationSettings(settings: CompilerOptions, createIfMissing: boolean): Map<DocumentRegistryEntry> {
@ -2028,7 +2028,7 @@ module ts {
}
}
enum SemanticMeaning {
const enum SemanticMeaning {
None = 0x0,
Value = 0x1,
Type = 0x2,
@ -2036,7 +2036,7 @@ module ts {
All = Value | Type | Namespace
}
enum BreakContinueSearchType {
const enum BreakContinueSearchType {
None = 0x0,
Unlabeled = 0x1,
Labeled = 0x2,

View File

@ -171,13 +171,13 @@ module ts {
}
/// TODO: delete this, it is only needed until the VS interface is updated
export enum LanguageVersion {
export const enum LanguageVersion {
EcmaScript3 = 0,
EcmaScript5 = 1,
EcmaScript6 = 2,
}
export enum ModuleGenTarget {
export const enum ModuleGenTarget {
Unspecified = 0,
Synchronous = 1,
Asynchronous = 2,

View File

@ -244,7 +244,7 @@ module ts.SignatureHelp {
// If the node is not a subspan of its parent, this is a big problem.
// There have been crashes that might be caused by this violation.
if (n.pos < n.parent.pos || n.end > n.parent.end) {
Debug.fail("Node of kind " + SyntaxKind[n.kind] + " is not a subspan of its parent of kind " + SyntaxKind[n.parent.kind]);
Debug.fail("Node of kind " + n.kind + " is not a subspan of its parent of kind " + n.parent.kind);
}
var argumentInfo = getImmediatelyContainingArgumentInfo(n);

View File

@ -1,10 +1,11 @@
//// [preserveConstEnums.ts]
const enum E {
Value = 1
Value = 1, Value2 = Value
}
//// [preserveConstEnums.js]
var E;
(function (E) {
E[E["Value"] = 1] = "Value";
E[E["Value2"] = 1] = "Value2";
})(E || (E = {}));

View File

@ -2,6 +2,8 @@
const enum E {
>E : E
Value = 1
Value = 1, Value2 = Value
>Value : E
>Value2 : E
>Value : E
}

View File

@ -1,4 +1,4 @@
// @preserveConstEnums: true
const enum E {
Value = 1
Value = 1, Value2 = Value
}