From ea7779c08dbb0faa4078d54501bb7ac780ca442b Mon Sep 17 00:00:00 2001 From: Kilian Seizinger <56249171+pri-kise@users.noreply.github.com> Date: Thu, 19 Dec 2024 21:57:35 +0100 Subject: [PATCH] Add more support for affixes (#309) Fixes #279 --- src/NAVObject.ts | 81 ++++++++++++++++--- .../suite/NAVObject.PrefexAndSuffix.test.ts | 19 +++++ src/test/suite/NAVTestObjectLibrary.ts | 51 ++++++++++++ 3 files changed, 139 insertions(+), 12 deletions(-) diff --git a/src/NAVObject.ts b/src/NAVObject.ts index 05501716..ad57e0a3 100644 --- a/src/NAVObject.ts +++ b/src/NAVObject.ts @@ -307,7 +307,7 @@ export class NAVObject { var reg = NAVObjectAction.actionRegEx(); var result; while ((result = reg.exec(this.NAVObjectText)) !== null) { - this.objectActions.push(new NAVObjectAction(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix])) + this.objectActions.push(new NAVObjectAction(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes])) } var reg = NAVTableField.fieldRegEx(); @@ -320,13 +320,13 @@ export class NAVObject { var reg = NAVPageField.fieldRegEx(); var result; while ((result = reg.exec(this.NAVObjectText)) !== null) { - this.pageFields.push(new NAVPageField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix])) + this.pageFields.push(new NAVPageField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes])) } var reg = NAVPageGroup.fieldRegEx(); var result; while ((result = reg.exec(this.NAVObjectText)) !== null) { - this.pageGroups.push(new NAVPageGroup(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix])) + this.pageGroups.push(new NAVPageGroup(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes])) } } @@ -538,6 +538,7 @@ class NAVObjectAction { public fullActionText: string; private _prefix: string; private _suffix: string; + private _affixes: string[]; private _objectType: string; public static actionRegEx(): RegExp { @@ -545,9 +546,22 @@ class NAVObjectAction { } get nameFixed(): string { - if (!this._prefix && !this._suffix) { return this.name } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.name } if (!this._objectType.toLocaleLowerCase().endsWith('extension')) { return this.name }; //only for extensionobjects + if (this.hasAffixesDefined()) { + var affixNeeded = true; + this._affixes.forEach(affix => { + if (this.name.startsWith(affix) || this.name.endsWith(affix)) { + affixNeeded = false; + return + } + }); + if (!affixNeeded) { + return this.name; + } + } + let result = this.name if (this._prefix && !this.name.startsWith(this._prefix)) { result = this._prefix + result @@ -559,15 +573,16 @@ class NAVObjectAction { } get fullActionTextFixed(): string { - if (!this._prefix && !this._suffix) { return this.fullActionText }; + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullActionText }; return " action(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + ")" } - constructor(fullActionText: string, objectType: string, prefix?: string, suffix?: string) { + constructor(fullActionText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) { this.fullActionText = fullActionText; this._prefix = prefix ? prefix : null; this._suffix = suffix ? suffix : null; + this._affixes = affixes ? affixes : null; this._objectType = objectType; this.parseActionText(); @@ -580,6 +595,10 @@ class NAVObjectAction { this.name = result[3]; } } + + private hasAffixesDefined(): boolean { + return (Array.isArray(this._affixes) && this._affixes.length > 0) + } } class NAVTableField { @@ -661,16 +680,30 @@ class NAVPageField { private _objectType: string; private _prefix: string; private _suffix: string; + private _affixes: string[]; public static fieldRegEx(): RegExp { return /.*(field\( *"?([ a-zA-Z0-9._/&%\/()-]+)"? *; *([" a-zA-Z0-9._/&%\/()-]+(\[([1-9]\d*)\])?) *\))/g; } get nameFixed(): string { - if (!this._prefix && !this._suffix) { return this.name } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.name } if (!this._objectType.toLocaleLowerCase().endsWith('extension')) { return this.name }; //only for extensionobjects if (this._objectType.toLocaleLowerCase().startsWith('table')) { return this.name }; //table-fields should not be parsed as pagefields + if (this.hasAffixesDefined()) { + var affixNeeded = true; + this._affixes.forEach(affix => { + if (this.name.startsWith(affix) || this.name.endsWith(affix)) { + affixNeeded = false; + return + } + }); + if (!affixNeeded) { + return this.name; + } + } + let result = this.name if (this._prefix && !this.name.startsWith(this._prefix)) { result = this._prefix + result @@ -682,15 +715,16 @@ class NAVPageField { } get fullFieldTextFixed(): string { - if (!this._prefix && !this._suffix) { return this.fullFieldText } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullFieldText } return "field(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + "; " + this.expression + ")" } - constructor(fullFieldText: string, objectType: string, prefix?: string, suffix?: string) { + constructor(fullFieldText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) { this.fullFieldText = fullFieldText; this._prefix = prefix ? prefix : null; this._suffix = suffix ? suffix : null; + this._affixes = affixes ? affixes : null; this._objectType = objectType; this.parseFieldText(); @@ -705,6 +739,10 @@ class NAVPageField { } } + private hasAffixesDefined(): boolean { + return (Array.isArray(this._affixes) && this._affixes.length > 0) + } + } class NAVPageGroup { @@ -713,15 +751,29 @@ class NAVPageGroup { private _objectType: string; private _prefix: string; private _suffix: string; + private _affixes: string[]; public static fieldRegEx(): RegExp { return /.*(group\( *"?([ a-zA-Z0-9._/&%\/()-]+)"? *\))/g; } get nameFixed(): string { - if (!this._prefix && !this._suffix) { return this.name } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.name } if (!this._objectType.toLocaleLowerCase().endsWith('extension')) { return this.name }; //only for extensionobjects + if (this.hasAffixesDefined()) { + var affixNeeded = true; + this._affixes.forEach(affix => { + if (this.name.startsWith(affix) || this.name.endsWith(affix)) { + affixNeeded = false; + return + } + }); + if (!affixNeeded) { + return this.name; + } + } + let result = this.name if (this._prefix && !this.name.startsWith(this._prefix)) { result = this._prefix + result @@ -733,15 +785,16 @@ class NAVPageGroup { } get fullGroupTextFixed(): string { - if (!this._prefix && !this._suffix) { return this.fullGroupText } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullGroupText } return "group(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + ")" } - constructor(fullGroupText: string, objectType: string, prefix?: string, suffix?: string) { + constructor(fullGroupText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) { this.fullGroupText = fullGroupText; this._prefix = prefix ? prefix : null; this._suffix = suffix ? suffix : null; + this._affixes = affixes ? affixes : null; this._objectType = objectType; this.parseFieldText(); @@ -754,6 +807,10 @@ class NAVPageGroup { this.name = result[2].trim().toString(); } } + + private hasAffixesDefined(): boolean { + return (Array.isArray(this._affixes) && this._affixes.length > 0) + } } diff --git a/src/test/suite/NAVObject.PrefexAndSuffix.test.ts b/src/test/suite/NAVObject.PrefexAndSuffix.test.ts index 0d5fee95..735d6d5c 100644 --- a/src/test/suite/NAVObject.PrefexAndSuffix.test.ts +++ b/src/test/suite/NAVObject.PrefexAndSuffix.test.ts @@ -334,6 +334,25 @@ suite("NAVObject ObjectNamePrefix Tests", () => { assert.strictEqual(action.name, action.nameFixed); }) }); + + test("Pageextension - avoid setting double affixes", () => { + let testSettings = Settings.GetConfigSettings(null) + testSettings[Settings.MandatoryAffixes] = ['waldo']; + + let navTestObject = NAVTestObjectLibrary.getPageExtensionWithWaldoSuffix(); + let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName) + + assert.notStrictEqual(navObject.objectActions.length, 0) + + let navObject2 = new NAVObject(navObject.NAVObjectTextFixed, testSettings, navTestObject.ObjectFileName) + + assert.strictEqual(navObject2.objectName.endsWith(testSettings[Settings.MandatoryAffixes][0]), true); + assert.strictEqual(navObject2.objectName, navObject2.objectNameFixed) + navObject2.objectActions.forEach(action => { + assert.strictEqual(action.name.endsWith(testSettings[Settings.MandatoryAffixes][0]), true); + assert.strictEqual(action.name, action.nameFixed); + }) + }); test("Page - avoid removing prefixes from actions", () => { let testSettings = Settings.GetConfigSettings(null) testSettings[Settings.ObjectNamePrefix] = 'waldo'; diff --git a/src/test/suite/NAVTestObjectLibrary.ts b/src/test/suite/NAVTestObjectLibrary.ts index a28d024c..408cece2 100644 --- a/src/test/suite/NAVTestObjectLibrary.ts +++ b/src/test/suite/NAVTestObjectLibrary.ts @@ -359,6 +359,57 @@ export function getPageExtensionWithWaldoPrefixWithActions(): NAVTestObject { return object; } +export function getPageExtensionWithWaldoSuffix(): NAVTestObject { + let object = new NAVTestObject; + + object.ObjectFileName = 'SomeFile.al' + object.ObjectText = `pageextension 50100 "Some Page Extwaldo" extends "Customer List" //22 +{ + layout + { + addfirst(Content) + { + field("Telex No. waldo"; "Telex No.") + { + ApplicationArea = All; + } + field("Telex No. waldo"; "Telex No.") + { + ApplicationArea = All; + } + field("Telex No. waldo"; "Telex No.") + { + ApplicationArea = All; + } + } + } + + actions + { + addfirst("&Customer") + { + action(SomeActionwaldo) + { + RunObject = page "_Empl. Absences by Cat. Matrix"; + } + group(someGroupwaldo) + { + action(SomeAction2waldo) + { + RunObject = page "_Empl. Absences by Cat. Matrix"; + } + action("Some Action 3 waldo") + { + RunObject = page "_Empl. Absences by Cat. Matrix"; + } + } + } + } +} + ` + return object; +} + export function getPageExtensionWithSlashInFileName(): NAVTestObject { let object = new NAVTestObject;