Skip to content

Commit 2e8eb4e

Browse files
Use 'let' in the ompiler layer.
1 parent 1ab0ef9 commit 2e8eb4e

1 file changed

Lines changed: 63 additions & 59 deletions

File tree

src/compiler/program.ts

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
/// <reference path="emitter.ts" />
33

44
module ts {
5-
/* @internal */ export var emitTime = 0;
6-
/* @internal */ export var ioReadTime = 0;
5+
/* @internal */ export let emitTime = 0;
6+
/* @internal */ export let ioReadTime = 0;
77

88
/** The version of the TypeScript compiler release */
9-
export var version = "1.5.0.0";
9+
export let version = "1.5.0.0";
1010

1111
export function createCompilerHost(options: CompilerOptions): CompilerHost {
12-
var currentDirectory: string;
13-
var existingDirectories: Map<boolean> = {};
12+
let currentDirectory: string;
13+
let existingDirectories: Map<boolean> = {};
1414

1515
function getCanonicalFileName(fileName: string): string {
1616
// if underlying system can distinguish between two files whose names differs only in cases then file name already in canonical form.
@@ -19,12 +19,13 @@ module ts {
1919
}
2020

2121
// returned by CScript sys environment
22-
var unsupportedFileEncodingErrorCode = -2147024809;
22+
let unsupportedFileEncodingErrorCode = -2147024809;
2323

2424
function getSourceFile(fileName: string, languageVersion: ScriptTarget, onError?: (message: string) => void): SourceFile {
25+
let text: string;
2526
try {
26-
var start = new Date().getTime();
27-
var text = sys.readFile(fileName, options.charset);
27+
let start = new Date().getTime();
28+
text = sys.readFile(fileName, options.charset);
2829
ioReadTime += new Date().getTime() - start;
2930
}
3031
catch (e) {
@@ -53,7 +54,7 @@ module ts {
5354

5455
function ensureDirectoriesExist(directoryPath: string) {
5556
if (directoryPath.length > getRootLength(directoryPath) && !directoryExists(directoryPath)) {
56-
var parentDirectory = getDirectoryPath(directoryPath);
57+
let parentDirectory = getDirectoryPath(directoryPath);
5758
ensureDirectoriesExist(parentDirectory);
5859
sys.createDirectory(directoryPath);
5960
}
@@ -82,7 +83,7 @@ module ts {
8283
}
8384

8485
export function getPreEmitDiagnostics(program: Program): Diagnostic[] {
85-
var diagnostics = program.getSyntacticDiagnostics().concat(program.getGlobalDiagnostics()).concat(program.getSemanticDiagnostics());
86+
let diagnostics = program.getSyntacticDiagnostics().concat(program.getGlobalDiagnostics()).concat(program.getSemanticDiagnostics());
8687
return sortAndDeduplicateDiagnostics(diagnostics);
8788
}
8889

@@ -91,15 +92,15 @@ module ts {
9192
return messageText;
9293
}
9394
else {
94-
var diagnosticChain = messageText;
95-
var result = "";
95+
let diagnosticChain = messageText;
96+
let result = "";
9697

97-
var indent = 0;
98+
let indent = 0;
9899
while (diagnosticChain) {
99100
if (indent) {
100101
result += newLine;
101102

102-
for (var i = 0; i < indent; i++) {
103+
for (let i = 0; i < indent; i++) {
103104
result += " ";
104105
}
105106
}
@@ -113,12 +114,12 @@ module ts {
113114
}
114115

115116
export function createProgram(rootNames: string[], options: CompilerOptions, host?: CompilerHost): Program {
116-
var program: Program;
117-
var files: SourceFile[] = [];
118-
var filesByName: Map<SourceFile> = {};
119-
var diagnostics = createDiagnosticCollection();
120-
var seenNoDefaultLib = options.noLib;
121-
var commonSourceDirectory: string;
117+
let program: Program;
118+
let files: SourceFile[] = [];
119+
let filesByName: Map<SourceFile> = {};
120+
let diagnostics = createDiagnosticCollection();
121+
let seenNoDefaultLib = options.noLib;
122+
let commonSourceDirectory: string;
122123
host = host || createCompilerHost(options);
123124

124125
forEach(rootNames, name => processRootFile(name, false));
@@ -127,8 +128,8 @@ module ts {
127128
}
128129
verifyCompilerOptions();
129130

130-
var diagnosticsProducingTypeChecker: TypeChecker;
131-
var noDiagnosticsTypeChecker: TypeChecker;
131+
let diagnosticsProducingTypeChecker: TypeChecker;
132+
let noDiagnosticsTypeChecker: TypeChecker;
132133

133134
program = {
134135
getSourceFile: getSourceFile,
@@ -172,7 +173,7 @@ module ts {
172173
}
173174

174175
function getDeclarationDiagnostics(targetSourceFile: SourceFile): Diagnostic[] {
175-
var resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(targetSourceFile);
176+
let resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(targetSourceFile);
176177
return ts.getDeclarationDiagnostics(getEmitHost(), resolver, targetSourceFile);
177178
}
178179

@@ -186,11 +187,11 @@ module ts {
186187
// Create the emit resolver outside of the "emitTime" tracking code below. That way
187188
// any cost associated with it (like type checking) are appropriate associated with
188189
// the type-checking counter.
189-
var emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile);
190+
let emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile);
190191

191-
var start = new Date().getTime();
192+
let start = new Date().getTime();
192193

193-
var emitResult = emitFiles(
194+
let emitResult = emitFiles(
194195
emitResolver,
195196
getEmitHost(writeFileCallback),
196197
sourceFile);
@@ -209,7 +210,7 @@ module ts {
209210
return getDiagnostics(sourceFile);
210211
}
211212

212-
var allDiagnostics: Diagnostic[] = [];
213+
let allDiagnostics: Diagnostic[] = [];
213214
forEach(program.getSourceFiles(), sourceFile => {
214215
addRange(allDiagnostics, getDiagnostics(sourceFile));
215216
});
@@ -230,20 +231,20 @@ module ts {
230231
}
231232

232233
function getSemanticDiagnosticsForFile(sourceFile: SourceFile): Diagnostic[] {
233-
var typeChecker = getDiagnosticsProducingTypeChecker();
234+
let typeChecker = getDiagnosticsProducingTypeChecker();
234235

235236
Debug.assert(!!sourceFile.bindDiagnostics);
236-
var bindDiagnostics = sourceFile.bindDiagnostics;
237-
var checkDiagnostics = typeChecker.getDiagnostics(sourceFile);
238-
var programDiagnostics = diagnostics.getDiagnostics(sourceFile.fileName);
237+
let bindDiagnostics = sourceFile.bindDiagnostics;
238+
let checkDiagnostics = typeChecker.getDiagnostics(sourceFile);
239+
let programDiagnostics = diagnostics.getDiagnostics(sourceFile.fileName);
239240

240241
return bindDiagnostics.concat(checkDiagnostics).concat(programDiagnostics);
241242
}
242243

243244
function getGlobalDiagnostics(): Diagnostic[] {
244-
var typeChecker = getDiagnosticsProducingTypeChecker();
245+
let typeChecker = getDiagnosticsProducingTypeChecker();
245246

246-
var allDiagnostics: Diagnostic[] = [];
247+
let allDiagnostics: Diagnostic[] = [];
247248
addRange(allDiagnostics, typeChecker.getGlobalDiagnostics());
248249
addRange(allDiagnostics, diagnostics.getGlobalDiagnostics());
249250

@@ -259,11 +260,13 @@ module ts {
259260
}
260261

261262
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number) {
263+
let start: number;
264+
let length: number;
262265
if (refEnd !== undefined && refPos !== undefined) {
263-
var start = refPos;
264-
var length = refEnd - refPos;
266+
start = refPos;
267+
length = refEnd - refPos;
265268
}
266-
var diagnostic: DiagnosticMessage;
269+
let diagnostic: DiagnosticMessage;
267270
if (hasExtension(fileName)) {
268271
if (!options.allowNonTsExtensions && !fileExtensionIs(host.getCanonicalFileName(fileName), ".ts")) {
269272
diagnostic = Diagnostics.File_0_must_have_extension_ts_or_d_ts;
@@ -297,20 +300,20 @@ module ts {
297300

298301
// Get source file from normalized fileName
299302
function findSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refStart?: number, refLength?: number): SourceFile {
300-
var canonicalName = host.getCanonicalFileName(fileName);
303+
let canonicalName = host.getCanonicalFileName(fileName);
301304
if (hasProperty(filesByName, canonicalName)) {
302305
// We've already looked for this file, use cached result
303306
return getSourceFileFromCache(fileName, canonicalName, /*useAbsolutePath*/ false);
304307
}
305308
else {
306-
var normalizedAbsolutePath = getNormalizedAbsolutePath(fileName, host.getCurrentDirectory());
307-
var canonicalAbsolutePath = host.getCanonicalFileName(normalizedAbsolutePath);
309+
let normalizedAbsolutePath = getNormalizedAbsolutePath(fileName, host.getCurrentDirectory());
310+
let canonicalAbsolutePath = host.getCanonicalFileName(normalizedAbsolutePath);
308311
if (hasProperty(filesByName, canonicalAbsolutePath)) {
309312
return getSourceFileFromCache(normalizedAbsolutePath, canonicalAbsolutePath, /*useAbsolutePath*/ true);
310313
}
311314

312315
// We haven't looked for this file, do so now and cache result
313-
var file = filesByName[canonicalName] = host.getSourceFile(fileName, options.target, hostErrorMessage => {
316+
let file = filesByName[canonicalName] = host.getSourceFile(fileName, options.target, hostErrorMessage => {
314317
if (refFile) {
315318
diagnostics.add(createFileDiagnostic(refFile, refStart, refLength,
316319
Diagnostics.Cannot_read_file_0_Colon_1, fileName, hostErrorMessage));
@@ -326,7 +329,7 @@ module ts {
326329
filesByName[canonicalAbsolutePath] = file;
327330

328331
if (!options.noResolve) {
329-
var basePath = getDirectoryPath(fileName);
332+
let basePath = getDirectoryPath(fileName);
330333
processReferencedFiles(file, basePath);
331334
processImportedModules(file, basePath);
332335
}
@@ -337,13 +340,14 @@ module ts {
337340
files.push(file);
338341
}
339342
}
343+
344+
return file;
340345
}
341-
return file;
342346

343347
function getSourceFileFromCache(fileName: string, canonicalName: string, useAbsolutePath: boolean): SourceFile {
344-
var file = filesByName[canonicalName];
348+
let file = filesByName[canonicalName];
345349
if (file && host.useCaseSensitiveFileNames()) {
346-
var sourceFileName = useAbsolutePath ? getNormalizedAbsolutePath(file.fileName, host.getCurrentDirectory()) : file.fileName;
350+
let sourceFileName = useAbsolutePath ? getNormalizedAbsolutePath(file.fileName, host.getCurrentDirectory()) : file.fileName;
347351
if (canonicalName !== sourceFileName) {
348352
diagnostics.add(createFileDiagnostic(refFile, refStart, refLength,
349353
Diagnostics.File_name_0_differs_from_already_included_file_name_1_only_in_casing, fileName, sourceFileName));
@@ -355,25 +359,25 @@ module ts {
355359

356360
function processReferencedFiles(file: SourceFile, basePath: string) {
357361
forEach(file.referencedFiles, ref => {
358-
var referencedFileName = isRootedDiskPath(ref.fileName) ? ref.fileName : combinePaths(basePath, ref.fileName);
362+
let referencedFileName = isRootedDiskPath(ref.fileName) ? ref.fileName : combinePaths(basePath, ref.fileName);
359363
processSourceFile(normalizePath(referencedFileName), /* isDefaultLib */ false, file, ref.pos, ref.end);
360364
});
361365
}
362366

363367
function processImportedModules(file: SourceFile, basePath: string) {
364368
forEach(file.statements, node => {
365369
if (node.kind === SyntaxKind.ImportDeclaration || node.kind === SyntaxKind.ImportEqualsDeclaration || node.kind === SyntaxKind.ExportDeclaration) {
366-
var moduleNameExpr = getExternalModuleName(node);
370+
let moduleNameExpr = getExternalModuleName(node);
367371
if (moduleNameExpr && moduleNameExpr.kind === SyntaxKind.StringLiteral) {
368-
var moduleNameText = (<LiteralExpression>moduleNameExpr).text;
372+
let moduleNameText = (<LiteralExpression>moduleNameExpr).text;
369373
if (moduleNameText) {
370-
var searchPath = basePath;
374+
let searchPath = basePath;
371375
while (true) {
372-
var searchName = normalizePath(combinePaths(searchPath, moduleNameText));
376+
let searchName = normalizePath(combinePaths(searchPath, moduleNameText));
373377
if (findModuleSourceFile(searchName + ".ts", moduleNameExpr) || findModuleSourceFile(searchName + ".d.ts", moduleNameExpr)) {
374378
break;
375379
}
376-
var parentPath = getDirectoryPath(searchPath);
380+
let parentPath = getDirectoryPath(searchPath);
377381
if (parentPath === searchPath) {
378382
break;
379383
}
@@ -392,14 +396,14 @@ module ts {
392396
if (isExternalModuleImportEqualsDeclaration(node) &&
393397
getExternalModuleImportEqualsDeclarationExpression(node).kind === SyntaxKind.StringLiteral) {
394398

395-
var nameLiteral = <LiteralExpression>getExternalModuleImportEqualsDeclarationExpression(node);
396-
var moduleName = nameLiteral.text;
399+
let nameLiteral = <LiteralExpression>getExternalModuleImportEqualsDeclarationExpression(node);
400+
let moduleName = nameLiteral.text;
397401
if (moduleName) {
398402
// TypeScript 1.0 spec (April 2014): 12.1.6
399403
// An ExternalImportDeclaration in anAmbientExternalModuleDeclaration may reference other external modules
400404
// only through top - level external module names. Relative external module names are not permitted.
401-
var searchName = normalizePath(combinePaths(basePath, moduleName));
402-
var tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral);
405+
let searchName = normalizePath(combinePaths(basePath, moduleName));
406+
let tsFile = findModuleSourceFile(searchName + ".ts", nameLiteral);
403407
if (!tsFile) {
404408
findModuleSourceFile(searchName + ".d.ts", nameLiteral);
405409
}
@@ -426,10 +430,10 @@ module ts {
426430
return;
427431
}
428432

429-
var firstExternalModuleSourceFile = forEach(files, f => isExternalModule(f) ? f : undefined);
433+
let firstExternalModuleSourceFile = forEach(files, f => isExternalModule(f) ? f : undefined);
430434
if (firstExternalModuleSourceFile && !options.module) {
431435
// We cannot use createDiagnosticFromNode because nodes do not have parents yet
432-
var span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
436+
let span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator);
433437
diagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_external_modules_unless_the_module_flag_is_provided));
434438
}
435439

@@ -440,15 +444,15 @@ module ts {
440444
(options.mapRoot && // there is --mapRoot Specified and there would be multiple js files generated
441445
(!options.out || firstExternalModuleSourceFile !== undefined))) {
442446

443-
var commonPathComponents: string[];
447+
let commonPathComponents: string[];
444448
forEach(files, sourceFile => {
445449
// Each file contributes into common source file path
446450
if (!(sourceFile.flags & NodeFlags.DeclarationFile)
447451
&& !fileExtensionIs(sourceFile.fileName, ".js")) {
448-
var sourcePathComponents = getNormalizedPathComponents(sourceFile.fileName, host.getCurrentDirectory());
452+
let sourcePathComponents = getNormalizedPathComponents(sourceFile.fileName, host.getCurrentDirectory());
449453
sourcePathComponents.pop(); // FileName is not part of directory
450454
if (commonPathComponents) {
451-
for (var i = 0; i < Math.min(commonPathComponents.length, sourcePathComponents.length); i++) {
455+
for (let i = 0; i < Math.min(commonPathComponents.length, sourcePathComponents.length); i++) {
452456
if (commonPathComponents[i] !== sourcePathComponents[i]) {
453457
if (i === 0) {
454458
diagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_find_the_common_subdirectory_path_for_the_input_files));

0 commit comments

Comments
 (0)