diff --git a/src/custom-sort/sorting-spec-processor.spec.ts b/src/custom-sort/sorting-spec-processor.spec.ts index 03cc210..7632209 100644 --- a/src/custom-sort/sorting-spec-processor.spec.ts +++ b/src/custom-sort/sorting-spec-processor.spec.ts @@ -957,6 +957,24 @@ describe('SortingSpecProcessor error detection and reporting', () => { }) }) +const txtInputTargetFolderCCC: string = ` +target-folder: CCC +` + +describe('SortingSpecProcessor advanced error detection', () => { + it('should retain state of duplicates detection in the instance', () => { + let processor: SortingSpecProcessor = new SortingSpecProcessor(errorsLogger); + errorsLogger.mockReset() + const inputTxtArr: Array = txtInputTargetFolderCCC.split('\n') + const result1 = processor.parseSortSpecFromText(inputTxtArr, 'another-mock-folder', 'sortspec.md') + const result2 = processor.parseSortSpecFromText(inputTxtArr, 'mock-folder', 'custom-name-note.md') + expect(result1).not.toBeNull() + expect(result2).toBeNull() + expect(errorsLogger).toHaveBeenCalledTimes(1) + expect(errorsLogger).toHaveBeenCalledWith(`${ERR_PREFIX} 2:DuplicateSortSpecForSameFolder Duplicate sorting spec for folder CCC ${ERR_SUFFIX}`) + }) +}) + describe('convertPlainStringSortingGroupSpecToArraySpec', () => { let processor: SortingSpecProcessor; beforeEach(() => { diff --git a/src/custom-sort/sorting-spec-processor.ts b/src/custom-sort/sorting-spec-processor.ts index e435c68..be2aadf 100644 --- a/src/custom-sort/sorting-spec-processor.ts +++ b/src/custom-sort/sorting-spec-processor.ts @@ -359,6 +359,11 @@ export class SortingSpecProcessor { problemAlreadyReportedForCurrentLine: boolean recentErrorMessage: string + // Helper map to deal with rule priorities for the same path + // and also detect non-wildcard duplicates. + // The wildcard duplicates were detected prior to this point, no need to bother about them + pathMatchPriorityForPath: {[key: string]: WildcardPriority} = {} + // Logger parameter exposed to support unit testing of error cases as well as capturing error messages // for in-app presentation constructor(private errorLogger?: typeof console.log) { @@ -370,10 +375,17 @@ export class SortingSpecProcessor { sortingSpecFileName: string, collection?: SortSpecsCollection ): SortSpecsCollection { + // reset / init processing state after potential previous invocation this.ctx = { folderPath: folderPath, // location of the sorting spec file specs: [] }; + this.currentEntryLine = null + this.currentEntryLineIdx = null + this.currentSortingSpecContainerFilePath = null + this.problemAlreadyReportedForCurrentLine = null + this.recentErrorMessage = null + let success: boolean = false; let lineIdx: number = 0; for (let entryLine of text) { @@ -439,11 +451,6 @@ export class SortingSpecProcessor { collection.sortSpecByWildcard = sortspecByWildcard } - // Helper transient map to deal with rule priorities for the same path - // and also detect non-wildcard duplicates. - // The wildcard duplicates were detected prior to this point, no need to bother about them - const pathMatchPriorityForPath: {[key: string]: WildcardPriority} = {} - for (let spec of this.ctx.specs) { for (let idx = 0; idx < spec.targetFoldersPaths.length; idx++) { const originalPath = spec.targetFoldersPaths[idx] @@ -452,7 +459,7 @@ export class SortingSpecProcessor { let path: string [path, detectedWildcardPriority] = stripWildcardPatternSuffix(originalPath) let storeTheSpec: boolean = true - const preexistingSortSpecPriority: WildcardPriority = pathMatchPriorityForPath[path] + const preexistingSortSpecPriority: WildcardPriority = this.pathMatchPriorityForPath[path] if (preexistingSortSpecPriority) { if (preexistingSortSpecPriority === WildcardPriority.NO_WILDCARD && detectedWildcardPriority === WildcardPriority.NO_WILDCARD) { this.problem(ProblemCode.DuplicateSortSpecForSameFolder, `Duplicate sorting spec for folder ${path}`) @@ -464,7 +471,7 @@ export class SortingSpecProcessor { } if (storeTheSpec) { collection.sortSpecByPath[path] = spec - pathMatchPriorityForPath[path] = detectedWildcardPriority + this.pathMatchPriorityForPath[path] = detectedWildcardPriority } } } diff --git a/src/main.ts b/src/main.ts index 4cf6878..307f439 100644 --- a/src/main.ts +++ b/src/main.ts @@ -55,6 +55,8 @@ export default class CustomSortPlugin extends Plugin { let errorMessage: string // reset cache this.sortSpecCache = null + const processor: SortingSpecProcessor = new SortingSpecProcessor() + Vault.recurseChildren(this.app.vault.getRoot(), (file: TAbstractFile) => { if (failed) return if (file instanceof TFile) { @@ -68,7 +70,6 @@ export default class CustomSortPlugin extends Plugin { const sortingSpecTxt: string = mCache.getCache(aFile.path)?.frontmatter?.[SORTINGSPEC_YAML_KEY] if (sortingSpecTxt) { anySortingSpecFound = true - const processor: SortingSpecProcessor = new SortingSpecProcessor() this.sortSpecCache = processor.parseSortSpecFromText( sortingSpecTxt.split('\n'), parent.path,