@@ -528,16 +528,33 @@ export interface FolderNameToSortSpecMap {
528528}
529529
530530export interface SortSpecsCollection {
531- sortSpecByPath : FolderPathToSortSpecMap
532- sortSpecByName : FolderNameToSortSpecMap
531+ sortSpecByPath ? : FolderPathToSortSpecMap
532+ sortSpecByName ? : FolderNameToSortSpecMap
533533 sortSpecByWildcard ?: FolderWildcardMatching < CustomSortSpec >
534534}
535535
536- export const newSortSpecsCollection = ( ) : SortSpecsCollection => {
537- return {
538- sortSpecByPath : { } ,
539- sortSpecByName : { }
536+ const ensureCollectionHasSortSpecByPath = ( collection ?: SortSpecsCollection | null ) => {
537+ collection = collection ?? { }
538+ if ( ! collection . sortSpecByPath ) {
539+ collection . sortSpecByPath = { }
540+ }
541+ return collection
542+ }
543+
544+ const ensureCollectionHasSortSpecByName = ( collection ?: SortSpecsCollection | null ) => {
545+ collection = collection ?? { }
546+ if ( ! collection . sortSpecByName ) {
547+ collection . sortSpecByName = { }
540548 }
549+ return collection
550+ }
551+
552+ const ensureCollectionHasSortSpecByWildcard = ( collection ?: SortSpecsCollection | null ) => {
553+ collection = collection ?? { }
554+ if ( ! collection . sortSpecByWildcard ) {
555+ collection . sortSpecByWildcard = new FolderWildcardMatching < CustomSortSpec > ( )
556+ }
557+ return collection
541558}
542559
543560interface AdjacencyInfo {
@@ -744,7 +761,6 @@ export class SortingSpecProcessor {
744761 }
745762 }
746763
747- let sortspecByName : FolderNameToSortSpecMap | undefined
748764 for ( let spec of this . ctx . specs ) {
749765 // Consume the folder names prefixed by the designated lexeme
750766 for ( let idx = 0 ; idx < spec . targetFoldersPaths . length ; idx ++ ) {
@@ -756,42 +772,36 @@ export class SortingSpecProcessor {
756772 `Empty '${ TargetFolderLexeme } ${ MatchFolderNameLexeme } ' value` )
757773 return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
758774 }
759- sortspecByName = sortspecByName ?? { }
760- if ( sortspecByName [ folderNameToMatch ] ) {
775+ collection = ensureCollectionHasSortSpecByName ( collection )
776+ if ( collection . sortSpecByName ! [ folderNameToMatch ] ) {
761777 this . problem ( ProblemCode . DuplicateByNameSortSpecForFolder ,
762778 `Duplicate '${ TargetFolderLexeme } ${ MatchFolderNameLexeme } ' definition for the same name <${ folderNameToMatch } >` )
763779 return null // Failure - not allow duplicate by folderNameToMatch specs for the same folder folderNameToMatch
764780 } else {
765- sortspecByName [ folderNameToMatch ] = spec
781+ collection . sortSpecByName ! [ folderNameToMatch ] = spec
766782 }
767783 }
768784 }
769785 }
770786
771- if ( sortspecByName ) {
772- collection = collection ?? newSortSpecsCollection ( )
773- collection . sortSpecByName = sortspecByName
774- }
775-
776- let sortspecByWildcard : FolderWildcardMatching < CustomSortSpec > | undefined
777787 for ( let spec of this . ctx . specs ) {
778788 // Consume the folder paths ending with wildcard specs or regexp-based
779789 for ( let idx = 0 ; idx < spec . targetFoldersPaths . length ; idx ++ ) {
780790 const path = spec . targetFoldersPaths [ idx ]
781791 if ( path . startsWith ( MatchFolderByRegexpLexeme ) ) {
782- sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching < CustomSortSpec > ( )
792+ collection = ensureCollectionHasSortSpecByWildcard ( collection )
783793 const folderByRegexpExpression : string = path . substring ( MatchFolderByRegexpLexeme . length ) . trim ( )
784794 try {
785795 const r : ConsumedFolderMatchingRegexp = consumeFolderByRegexpExpression ( folderByRegexpExpression )
786- sortspecByWildcard . addRegexpDefinition ( r . regexp , r . againstName , r . priority , r . log , spec )
796+ collection . sortSpecByWildcard ! . addRegexpDefinition ( r . regexp , r . againstName , r . priority , r . log , spec )
787797 } catch ( e ) {
788798 this . problem ( ProblemCode . InvalidOrEmptyFolderMatchingRegexp ,
789799 `Invalid or empty folder regexp expression <${ folderByRegexpExpression } >` )
790800 return null
791801 }
792802 } else if ( endsWithWildcardPatternSuffix ( path ) ) {
793- sortspecByWildcard = sortspecByWildcard ?? new FolderWildcardMatching < CustomSortSpec > ( )
794- const ruleAdded = sortspecByWildcard . addWildcardDefinition ( path , spec )
803+ collection = ensureCollectionHasSortSpecByWildcard ( collection )
804+ const ruleAdded = collection . sortSpecByWildcard ! . addWildcardDefinition ( path , spec )
795805 if ( ruleAdded ?. errorMsg ) {
796806 this . problem ( ProblemCode . DuplicateWildcardSortSpecForSameFolder , ruleAdded ?. errorMsg )
797807 return null // Failure - not allow duplicate wildcard specs for the same folder
@@ -800,16 +810,10 @@ export class SortingSpecProcessor {
800810 }
801811 }
802812
803- if ( sortspecByWildcard ) {
804- collection = collection ?? newSortSpecsCollection ( )
805- collection . sortSpecByWildcard = sortspecByWildcard
806- }
807-
808813 for ( let spec of this . ctx . specs ) {
809814 for ( let idx = 0 ; idx < spec . targetFoldersPaths . length ; idx ++ ) {
810815 const originalPath = spec . targetFoldersPaths [ idx ]
811816 if ( ! originalPath . startsWith ( MatchFolderNameLexeme ) && ! originalPath . startsWith ( MatchFolderByRegexpLexeme ) ) {
812- collection = collection ?? newSortSpecsCollection ( )
813817 const { path, detectedWildcardPriority} = stripWildcardPatternSuffix ( originalPath )
814818 let storeTheSpec : boolean = true
815819 const preexistingSortSpecPriority : WildcardPriority = this . pathMatchPriorityForPath [ path ]
@@ -823,7 +827,8 @@ export class SortingSpecProcessor {
823827 }
824828 }
825829 if ( storeTheSpec ) {
826- collection . sortSpecByPath [ path ] = spec
830+ collection = ensureCollectionHasSortSpecByPath ( collection )
831+ collection . sortSpecByPath ! [ path ] = spec
827832 this . pathMatchPriorityForPath [ path ] = detectedWildcardPriority
828833 }
829834 }
0 commit comments