Skip to content

Commit 41551ca

Browse files
Sebastian WendorfSebastian Wendorf
authored andcommitted
Added debug logging option
1 parent 6c8e755 commit 41551ca

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

typescript/src/core/extractor.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { FileUtils } from "./utils/file.utils";
1212
import { POST_PROCESSORS } from "./features";
1313
import { ProjectUtils } from "./utils/project.utils";
1414
import { LCEProject, LCEProjectInfo } from "./project";
15+
import {debug, DEBUG_LOGGING} from "./utils/log.utils";
1516

16-
// eslint-disable-next-line @typescript-eslint/ban-types
1717
export interface ExtractorOptions {
1818
prettyPrint?: boolean;
1919
}
@@ -36,8 +36,8 @@ export async function processProjectsAndOutputResult(scanRoot: string, options:
3636
options.prettyPrint ? 2 : undefined,
3737
);
3838

39-
let dirPath = path.join(scanRoot, ".reports", "jqa");
40-
let filePath = path.join(dirPath, "ts-output.json");
39+
const dirPath = path.join(scanRoot, ".reports", "jqa");
40+
const filePath = path.join(dirPath, "ts-output.json");
4141
fs.mkdir(dirPath, { recursive: true }, (errDir) => {
4242
if (errDir) {
4343
console.log("Could not create directory: " + dirPath);
@@ -86,12 +86,14 @@ export async function processProject(project: LCEProjectInfo): Promise<LCEProjec
8686
const startTime = process.hrtime();
8787
let fileReadingTime = 0;
8888
const progressBar = new SingleBar({}, Presets.shades_classic);
89-
progressBar.start(fileList.length, 0);
89+
if(!DEBUG_LOGGING)
90+
progressBar.start(fileList.length, 0);
9091

9192
// Traverse and process all individual project files
9293
const traverser = new AstTraverser();
9394
for (let i = 0; i < fileList.length; i++) {
94-
progressBar.update(i + 1);
95+
if(!DEBUG_LOGGING)
96+
progressBar.update(i + 1);
9597
const file = fileList[i];
9698

9799
const frStartTime = process.hrtime();
@@ -100,6 +102,8 @@ export async function processProject(project: LCEProjectInfo): Promise<LCEProjec
100102
fileReadingTime += frEndTime[0] + frEndTime[1] / 10 ** 9 - (frStartTime[0] + frStartTime[1] / 10 ** 9);
101103

102104
try {
105+
debug(`Processing file [${i+1}/${fileList.length}]: ${FileUtils.normalizePath(ModulePathUtils.normalize(projectRoot, file))}`);
106+
103107
const { ast, services } = parseAndGenerateServices(code, {
104108
loc: true,
105109
range: true,
@@ -127,7 +131,8 @@ export async function processProject(project: LCEProjectInfo): Promise<LCEProjec
127131
console.log(e);
128132
}
129133
}
130-
progressBar.stop();
134+
if(!DEBUG_LOGGING)
135+
progressBar.stop();
131136
const normalizedConcepts: Map<string, LCEConcept[]> = unifyConceptMap(concepts, "").get("") ?? new Map();
132137

133138
const endTime = process.hrtime();

typescript/src/core/traverser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ConceptMap, mergeConceptMaps } from "./concept";
22
import { ProcessingContext } from "./context";
33
import { Processor, ProcessorMap } from "./processor";
44
import { CoreContextKeys } from "./context.keys";
5+
import {debugTraversalStack} from "./utils/log.utils";
56

67
export interface TraverserContext {
78
parentPropName: string;
@@ -28,6 +29,8 @@ export abstract class Traverser {
2829
validProcessors = processorCandidates.filter((proc) => proc.executionCondition.check(processingContext));
2930
}
3031

32+
debugTraversalStack(processingContext.localContexts);
33+
3134
// pre-processing
3235
if (validProcessors) {
3336
for (const proc of validProcessors) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import {LocalContexts} from "../context";
2+
import {CoreContextKeys} from "../context.keys";
3+
import {TraverserContext} from "../traverser";
4+
5+
export let DEBUG_LOGGING = false;
6+
7+
export function setDebugLogging(enableDebugLogging: boolean): void {
8+
DEBUG_LOGGING = enableDebugLogging;
9+
}
10+
11+
export function debug(logMessage: string): void {
12+
if(DEBUG_LOGGING) {
13+
console.debug(logMessage);
14+
}
15+
}
16+
17+
export function debugTraversalStack(localContexts: LocalContexts): void {
18+
if(DEBUG_LOGGING) {
19+
let astPos = "";
20+
for(const lc of localContexts.contexts) {
21+
const coordNum = (lc.get(CoreContextKeys.TRAVERSER_CONTEXT) as TraverserContext).parentPropIndex;
22+
const coordName = (lc.get(CoreContextKeys.TRAVERSER_CONTEXT) as TraverserContext).parentPropName;
23+
astPos = astPos + "/" + coordName + (coordNum !== undefined ? "[" + coordNum + "]" : "");
24+
}
25+
console.debug(astPos)
26+
}
27+
}

typescript/src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { program } from "commander";
44
import { processProjectsAndOutputResult } from "./core/extractor";
55
import packageInfo from "../package.json";
66
import { initializeReactExtractor } from "./react/react-extractor";
7+
import {setDebugLogging} from "./core/utils/log.utils";
78

89

910
// Setup CLI
@@ -14,14 +15,16 @@ program
1415
.version(packageInfo.version)
1516
.argument("[path]", "path to the root of the TypeScript project to be scanned", ".")
1617
.option("-e, --extension [extensions...]", "space separated list of extensions to activate (currently available: react)")
17-
.option("-p, --pretty", "pretty-print JSON result report");
18+
.option("-p, --pretty", "pretty-print JSON result report")
19+
.option("-d, --debug", "print debug information");
1820
program.parse();
1921

2022
// retrieve CLI arguments and options
2123
const options = program.opts();
2224

2325
const extensions: string[] = options.extension ?? [];
2426
const prettyPrint = !!options.pretty;
27+
setDebugLogging(!!options.debug);
2528

2629
const projectRootPath: string = program.processedArgs[0];
2730

0 commit comments

Comments
 (0)