From 5ac6eb2d79d424fa216a1e01a6dd3dac9fbedb53 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 18 Nov 2015 10:48:03 -0800 Subject: [PATCH] PR feedback --- src/compiler/checker.ts | 5 +- src/compiler/declarationEmitter.ts | 107 +++++++++++++++-------------- src/compiler/program.ts | 10 +-- src/compiler/utilities.ts | 6 +- 4 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 36ca3eeb760..d2ccd9cebc8 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -12034,7 +12034,10 @@ namespace ts { const symbol = getSymbolOfNode(node); const localSymbol = node.localSymbol || symbol; - const firstDeclaration = forEach(symbol.declarations, + // Since the javascript won't do semantic analysis like typescript, + // if the javascript file comes before the typescript file and both contain same name functions, + // checkFunctionOrConstructorSymbol wouldn't be called if we didnt ignore javascript function. + const firstDeclaration = forEach(localSymbol.declarations, // Get first non javascript function declaration declaration => declaration.kind === node.kind && !isSourceFileJavaScript(getSourceFile(declaration)) ? declaration : undefined); diff --git a/src/compiler/declarationEmitter.ts b/src/compiler/declarationEmitter.ts index 954a24ec5b1..0d94deda920 100644 --- a/src/compiler/declarationEmitter.ts +++ b/src/compiler/declarationEmitter.ts @@ -77,64 +77,67 @@ namespace ts { let addedGlobalFileReference = false; let allSourcesModuleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[] = []; forEach(sourceFiles, sourceFile => { - if (!isSourceFileJavaScript(sourceFile)) { - // Check what references need to be added - if (!compilerOptions.noResolve) { - forEach(sourceFile.referencedFiles, fileReference => { - const referencedFile = tryResolveScriptReference(host, sourceFile, fileReference); + // Dont emit for javascript file + if (isSourceFileJavaScript(sourceFile)) { + return; + } - // Emit reference in dts, if the file reference was not already emitted - if (referencedFile && !contains(emittedReferencedFiles, referencedFile)) { - // Add a reference to generated dts file, - // global file reference is added only - // - if it is not bundled emit (because otherwise it would be self reference) - // - and it is not already added - if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { - addedGlobalFileReference = true; - } - emittedReferencedFiles.push(referencedFile); + // Check what references need to be added + if (!compilerOptions.noResolve) { + forEach(sourceFile.referencedFiles, fileReference => { + const referencedFile = tryResolveScriptReference(host, sourceFile, fileReference); + + // Emit reference in dts, if the file reference was not already emitted + if (referencedFile && !contains(emittedReferencedFiles, referencedFile)) { + // Add a reference to generated dts file, + // global file reference is added only + // - if it is not bundled emit (because otherwise it would be self reference) + // - and it is not already added + if (writeReferencePath(referencedFile, !isBundledEmit && !addedGlobalFileReference)) { + addedGlobalFileReference = true; } - }); - } + emittedReferencedFiles.push(referencedFile); + } + }); + } - if (!isBundledEmit || !isExternalModule(sourceFile)) { - noDeclare = false; - emitSourceFile(sourceFile); - } - else if (isExternalModule(sourceFile)) { - noDeclare = true; - write(`declare module "${getResolvedExternalModuleName(host, sourceFile)}" {`); - writeLine(); - increaseIndent(); - emitSourceFile(sourceFile); - decreaseIndent(); - write("}"); - writeLine(); - } + if (!isBundledEmit || !isExternalModule(sourceFile)) { + noDeclare = false; + emitSourceFile(sourceFile); + } + else if (isExternalModule(sourceFile)) { + noDeclare = true; + write(`declare module "${getResolvedExternalModuleName(host, sourceFile)}" {`); + writeLine(); + increaseIndent(); + emitSourceFile(sourceFile); + decreaseIndent(); + write("}"); + writeLine(); + } - // create asynchronous output for the importDeclarations - if (moduleElementDeclarationEmitInfo.length) { - const oldWriter = writer; - forEach(moduleElementDeclarationEmitInfo, aliasEmitInfo => { - if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) { - Debug.assert(aliasEmitInfo.node.kind === SyntaxKind.ImportDeclaration); - createAndSetNewTextWriterWithSymbolWriter(); - Debug.assert(aliasEmitInfo.indent === 0 || (aliasEmitInfo.indent === 1 && isBundledEmit)); - for (let i = 0; i < aliasEmitInfo.indent; i++) { - increaseIndent(); - } - writeImportDeclaration(aliasEmitInfo.node); - aliasEmitInfo.asynchronousOutput = writer.getText(); - for (let i = 0; i < aliasEmitInfo.indent; i++) { - decreaseIndent(); - } + // create asynchronous output for the importDeclarations + if (moduleElementDeclarationEmitInfo.length) { + const oldWriter = writer; + forEach(moduleElementDeclarationEmitInfo, aliasEmitInfo => { + if (aliasEmitInfo.isVisible && !aliasEmitInfo.asynchronousOutput) { + Debug.assert(aliasEmitInfo.node.kind === SyntaxKind.ImportDeclaration); + createAndSetNewTextWriterWithSymbolWriter(); + Debug.assert(aliasEmitInfo.indent === 0 || (aliasEmitInfo.indent === 1 && isBundledEmit)); + for (let i = 0; i < aliasEmitInfo.indent; i++) { + increaseIndent(); } - }); - setWriter(oldWriter); + writeImportDeclaration(aliasEmitInfo.node); + aliasEmitInfo.asynchronousOutput = writer.getText(); + for (let i = 0; i < aliasEmitInfo.indent; i++) { + decreaseIndent(); + } + } + }); + setWriter(oldWriter); - allSourcesModuleElementDeclarationEmitInfo = allSourcesModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo); - moduleElementDeclarationEmitInfo = []; - } + allSourcesModuleElementDeclarationEmitInfo = allSourcesModuleElementDeclarationEmitInfo.concat(moduleElementDeclarationEmitInfo); + moduleElementDeclarationEmitInfo = []; } }); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 8048c8aaad7..36b389662e2 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -342,7 +342,7 @@ namespace ts { host = host || createCompilerHost(options); // Map storing if there is emit blocking diagnostics for given input - const hasEmitBlockingDiagnostics = createFileMap(!host.useCaseSensitiveFileNames() ? key => key.toLocaleLowerCase() : undefined); + const hasEmitBlockingDiagnostics = createFileMap(getCanonicalFileName); const currentDirectory = host.getCurrentDirectory(); const resolveModuleNamesWorker = host.resolveModuleNames @@ -1287,14 +1287,14 @@ namespace ts { if (emitFileName) { const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName); // Report error if the output overwrites input file - if (forEach(files, file => toPath(file.fileName, currentDirectory, getCanonicalFileName) === emitFilePath)) { - createEmitBlockingDiagnostics(emitFileName, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); + if (filesByName.contains(emitFilePath)) { + createEmitBlockingDiagnostics(emitFileName, emitFilePath, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file); } // Report error if multiple files write into same file if (emitFilesSeen.contains(emitFilePath)) { // Already seen the same emit file - report error - createEmitBlockingDiagnostics(emitFileName, Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); + createEmitBlockingDiagnostics(emitFileName, emitFilePath, Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files); } else { emitFilesSeen.set(emitFilePath, true); @@ -1303,7 +1303,7 @@ namespace ts { } } - function createEmitBlockingDiagnostics(emitFileName: string, message: DiagnosticMessage) { + function createEmitBlockingDiagnostics(emitFileName: string, emitFilePath: Path, message: DiagnosticMessage) { hasEmitBlockingDiagnostics.set(toPath(emitFileName, currentDirectory, getCanonicalFileName), true); programDiagnostics.add(createCompilerDiagnostic(message, emitFileName)); } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 4287c0cb49b..db05964af2e 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -1913,11 +1913,11 @@ namespace ts { } else { const sourceFiles = targetSourceFile === undefined ? host.getSourceFiles() : [targetSourceFile]; - forEach(sourceFiles, sourceFile => { + for (const sourceFile of sourceFiles) { if (!isDeclarationFile(sourceFile)) { onSingleFileEmit(host, sourceFile); } - }); + } } function onSingleFileEmit(host: EmitHost, sourceFile: SourceFile) { @@ -1936,7 +1936,7 @@ namespace ts { const bundledSources = filter(host.getSourceFiles(), sourceFile => !isDeclarationFile(sourceFile) && // Not a declaration file (!isExternalModule(sourceFile) || // non module file - (getEmitModuleKind(options) && isExternalModule(sourceFile)))); // module that can emit + (getEmitModuleKind(options) && isExternalModule(sourceFile)))); // module that can emit - note falsy value from getEmitModuleKind means the module kind that shouldn't be emitted if (bundledSources.length) { const jsFilePath = options.outFile || options.out; const emitFileNames: EmitFileNames = {