From 68925b66f4e7eb4ef6081cdc7a0f05a0bd874171 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 20 Nov 2020 15:03:11 -0800 Subject: [PATCH] Split line and node counts by file extension (#39720) * Split line and node counts by file extension Continue to show only a summary for --diagnostics. * Use SourceFile.isDeclarationFile --- src/executeCommandLine/executeCommandLine.ts | 71 ++++++++++++++++++-- 1 file changed, 65 insertions(+), 6 deletions(-) diff --git a/src/executeCommandLine/executeCommandLine.ts b/src/executeCommandLine/executeCommandLine.ts index f8e07a34bb3..bf7d109e838 100644 --- a/src/executeCommandLine/executeCommandLine.ts +++ b/src/executeCommandLine/executeCommandLine.ts @@ -4,12 +4,57 @@ namespace ts { value: string; } - function countLines(program: Program): number { - let count = 0; + function countLines(program: Program): Map { + const counts = getCountsMap(); forEach(program.getSourceFiles(), file => { - count += getLineStarts(file).length; + const key = getCountKey(program, file); + const lineCount = getLineStarts(file).length; + counts.set(key, counts.get(key)! + lineCount); }); - return count; + return counts; + } + + function countNodes(program: Program): Map { + const counts = getCountsMap(); + forEach(program.getSourceFiles(), file => { + const key = getCountKey(program, file); + counts.set(key, counts.get(key)! + file.nodeCount); + }); + return counts; + } + + function getCountsMap() { + const counts = createMap(); + counts.set("Library", 0); + counts.set("Definitions", 0); + counts.set("TypeScript", 0); + counts.set("JavaScript", 0); + counts.set("JSON", 0); + counts.set("Other", 0); + return counts; + } + + function getCountKey(program: Program, file: SourceFile) { + if (program.isSourceFileDefaultLibrary(file)) { + return "Library"; + } + else if (file.isDeclarationFile) { + return "Definitions"; + } + + const path = file.path; + if (fileExtensionIsOneOf(path, supportedTSExtensions)) { + return "TypeScript"; + } + else if (fileExtensionIsOneOf(path, supportedJSExtensions)) { + return "JavaScript"; + } + else if (fileExtensionIs(path, Extension.Json)) { + return "JSON"; + } + else { + return "Other"; + } } function updateReportDiagnostic( @@ -637,8 +682,22 @@ namespace ts { statistics = []; const memoryUsed = sys.getMemoryUsage ? sys.getMemoryUsage() : -1; reportCountStatistic("Files", program.getSourceFiles().length); - reportCountStatistic("Lines", countLines(program)); - reportCountStatistic("Nodes", program.getNodeCount()); + + const lineCounts = countLines(program); + const nodeCounts = countNodes(program); + if (compilerOptions.extendedDiagnostics) { + for (const key of arrayFrom(lineCounts.keys())) { + reportCountStatistic("Lines of " + key, lineCounts.get(key)!); + } + for (const key of arrayFrom(nodeCounts.keys())) { + reportCountStatistic("Nodes of " + key, nodeCounts.get(key)!); + } + } + else { + reportCountStatistic("Lines", reduceLeftIterator(lineCounts.values(), (sum, count) => sum + count, 0)); + reportCountStatistic("Nodes", reduceLeftIterator(nodeCounts.values(), (sum, count) => sum + count, 0)); + } + reportCountStatistic("Identifiers", program.getIdentifierCount()); reportCountStatistic("Symbols", program.getSymbolCount()); reportCountStatistic("Types", program.getTypeCount());