From b050d018f2bf8bc4f99ea2c352b44fc7b9f08ba8 Mon Sep 17 00:00:00 2001 From: Marc Durdin Date: Mon, 12 Jun 2023 12:44:09 +0700 Subject: [PATCH] chore(developer): prevent recursion when analyzing projects --- .../kmc-analyze/src/osk-character-use/index.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/developer/src/kmc-analyze/src/osk-character-use/index.ts b/developer/src/kmc-analyze/src/osk-character-use/index.ts index 0aadba2a7f..d14197a3d5 100644 --- a/developer/src/kmc-analyze/src/osk-character-use/index.ts +++ b/developer/src/kmc-analyze/src/osk-character-use/index.ts @@ -16,7 +16,7 @@ export class AnalyzeOskCharacterUse { // Analyze a set of files // - public async analyze(files: string[]) { + public async analyze(files: string[], analyzeProjects: boolean = true) { for(let file of files) { switch(KeymanFileTypes.sourceTypeFromFilename(file)) { case KeymanFileTypes.Source.VisualKeyboard: @@ -26,7 +26,9 @@ export class AnalyzeOskCharacterUse { this.addStrings(this.scanTouchLayout(file)); break; case KeymanFileTypes.Source.Project: - await this.analyzeProject(file); + if(analyzeProjects) { + await this.analyzeProject(file); + } break; case KeymanFileTypes.Source.KeymanKeyboard: // The cleanest way to do this is to compile the .kmn to find the .kvks @@ -50,8 +52,7 @@ export class AnalyzeOskCharacterUse { const source = reader.read(this.callbacks.loadFile(filename)); const project = reader.transform(filename, source); let files = project.files.map(file => this.callbacks.resolveFilename(filename, file.filePath)); - // TODO: ensure no .kpj in files so we don't potentially end up in a loop - await this.analyze(files); + await this.analyze(files, false); // false because we don't want get into a recursive loop for projects } public async analyzeProjectFolder(folder: string) { @@ -69,8 +70,12 @@ export class AnalyzeOskCharacterUse { const project = new KeymanDeveloperProject(kpjFile, '2.0', this.callbacks); project.populateFiles(); let files = project.files.map(file => this.callbacks.resolveFilename(kpjFile, file.filePath)); - // TODO: ensure no .kpj in files so we don't potentially end up in a loop - await this.analyze(files); + + // Make sure no .kpj files are included in this list so we don't + // accidentally end up recursing + files = files.filter(file => !KeymanFileTypes.filenameIs(file, KeymanFileTypes.Source.Project)); + + await this.analyze(files, false); // false because we don't want get into a recursive loop for projects } } @@ -231,6 +236,7 @@ export class AnalyzeOskCharacterUse { return JSON.stringify(data, null, 2).split('\n'); } + // TODO: this only works for single-character strings -- bad bad bad private static escapeMarkdownChar(s: string) { // commonmark 2.4: all punct can be escaped const punct = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~';