From 9e6319064c9bd88d58f65dda3c5635a8d45d81cb Mon Sep 17 00:00:00 2001 From: pri-kise <56249171+pri-kise@users.noreply.github.com> Date: Mon, 5 Dec 2022 12:54:14 +0100 Subject: [PATCH] First draft for Support of multiple Prefixes (#254) * First draft for Prefixes * Switch to AppSourceCop.json --- src/NAVObject.ts | 60 ++++++++++++++++--- src/Settings.ts | 22 +++++++ .../suite/NAVObject.PrefexAndSuffix.test.ts | 46 ++++++++++++++ src/test/suite/NAVTestObjectLibrary.ts | 19 ++++++ 4 files changed, 138 insertions(+), 9 deletions(-) diff --git a/src/NAVObject.ts b/src/NAVObject.ts index 633f91d..8a8de3f 100644 --- a/src/NAVObject.ts +++ b/src/NAVObject.ts @@ -300,7 +300,7 @@ export class NAVObject { var reg = NAVTableField.fieldRegEx(); var result; while ((result = reg.exec(this.NAVObjectText)) !== null) { - this.tableFields.push(new NAVTableField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix])) + this.tableFields.push(new NAVTableField(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes])) } var reg = NAVPageField.fieldRegEx(); @@ -318,7 +318,7 @@ export class NAVObject { var reg = NAVReportColumn.columnRegEx(); var result; while ((result = reg.exec(this.NAVObjectText)) !== null) { - this.reportColumns.push(new NAVReportColumn(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix])) + this.reportColumns.push(new NAVReportColumn(result[1], this.objectType, this._workSpaceSettings[Settings.ObjectNamePrefix], this._workSpaceSettings[Settings.ObjectNameSuffix], this._workSpaceSettings[Settings.MandatoryAffixes])) } this.NAVObjectText = initNAVObjectText; @@ -548,6 +548,7 @@ class NAVTableField { public type: string; private _objectType: string; private _prefix: string; + private _affixes: string[]; private _suffix: string; public static fieldRegEx(): RegExp { @@ -555,10 +556,24 @@ class NAVTableField { } 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 + let result = this.name; if (this._prefix && !this.name.startsWith(this._prefix)) { result = this._prefix + result } @@ -569,15 +584,16 @@ class NAVTableField { } get fullFieldTextFixed(): string { - if (!this._prefix && !this._suffix) { return this.fullFieldText } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullFieldText } return "field(" + this.number + "; " + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + "; " + this.type + ")" } - 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(); @@ -591,6 +607,11 @@ class NAVTableField { this.name = result[3].trim().toString(); this.type = result[4].trim().toString(); } + } + + private hasAffixesDefined() : boolean + { + return (Array.isArray(this._affixes) && this._affixes.length > 0 ) } } @@ -704,6 +725,7 @@ class NAVReportColumn { private _objectType: string; private _prefix: string; private _suffix: string; + private _affixes: string[]; public static columnRegEx(): RegExp { // return /.*(column\( *"?([ a-zA-Z0-9._/&%\/()-]+)"? *; *([" a-zA-Z0-9._/&%\/()-]+) *\))/g; @@ -711,9 +733,23 @@ class NAVReportColumn { } 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.replace(" ", ""))) { result = this._prefix + result @@ -726,15 +762,16 @@ class NAVReportColumn { } get fullColumnTextFixed(): string { - if (!this._prefix && !this._suffix) { return this.fullColumnText } + if (!this._prefix && !this._suffix && !this.hasAffixesDefined()) { return this.fullColumnText } return "column(" + StringFunctions.encloseInQuotesIfNecessary(this.nameFixed) + "; " + this.expression + ")" } - constructor(fullColumnText: string, objectType: string, prefix?: string, suffix?: string) { + constructor(fullColumnText: string, objectType: string, prefix?: string, suffix?: string, affixes?: string[]) { this.fullColumnText = fullColumnText; this._prefix = prefix ? prefix : null; this._suffix = suffix ? suffix : null; + this._affixes = affixes ? affixes : null; this._objectType = objectType; this.parseColumnText(); @@ -748,6 +785,11 @@ class NAVReportColumn { this.expression = result[3].trim().toString(); } } + + private hasAffixesDefined() : boolean + { + return (Array.isArray(this._affixes) && this._affixes.length > 0 ) + } } diff --git a/src/Settings.ts b/src/Settings.ts index 4e29186..b50c0ed 100644 --- a/src/Settings.ts +++ b/src/Settings.ts @@ -50,6 +50,7 @@ export class Settings { static readonly DependencyGraphExcludePublishers = 'DependencyGraph.ExcludePublishers'; static readonly DependencyGraphRemovePrefix = 'DependencyGraph.RemovePrefix'; + static readonly MandatoryAffixes = 'AppSourceCop.MandatoryAffixes'; private static config: vscode.WorkspaceConfiguration; private static launchconfig: vscode.WorkspaceConfiguration; @@ -148,10 +149,23 @@ export class Settings { this.SettingCollection[this.SandboxName] = currentLaunchConfig[0].sandboxName; } + private static getAppSourceCopSettings(ResourceUri: vscode.Uri) { + let appSourceCopSettings = ResourceUri ? + require(join(vscode.workspace.getWorkspaceFolder(ResourceUri).uri.fsPath, "AppSourceCop.json")) : + vscode.window.activeTextEditor ? + require(join(vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath, "AppSourceCop.json")) : + vscode.workspace.workspaceFolders ? + require(join(vscode.workspace.workspaceFolders[0].uri.fsPath, "AppSourceCop.json")): null; + if (appSourceCopSettings) { + this.SettingCollection[this.MandatoryAffixes] = appSourceCopSettings.mandatoryAffixes + } + } + public static GetAllSettings(ResourceUri: vscode.Uri) { this.getConfigSettings(ResourceUri); this.getAppSettings(ResourceUri); this.getLaunchSettings(ResourceUri); + this.getAppSourceCopSettings(ResourceUri); return this.SettingCollection; } @@ -162,6 +176,12 @@ export class Settings { return this.SettingCollection; } + public static GetAppSourceCopSettings(ResourceUri: vscode.Uri) { + this.getAppSourceCopSettings(ResourceUri); + + return this.SettingCollection; + } + public static GetLaunchSettings(ResourceUri: vscode.Uri) { this.getLaunchSettings(ResourceUri); @@ -170,6 +190,8 @@ export class Settings { public static GetConfigSettings(ResourceUri: vscode.Uri) { this.getConfigSettings(ResourceUri); + //TODO: How to integrate AppSourceCopSettings into NAVObject? + this.getAppSourceCopSettings(ResourceUri); return this.SettingCollection; } diff --git a/src/test/suite/NAVObject.PrefexAndSuffix.test.ts b/src/test/suite/NAVObject.PrefexAndSuffix.test.ts index 08e20cd..295306b 100644 --- a/src/test/suite/NAVObject.PrefexAndSuffix.test.ts +++ b/src/test/suite/NAVObject.PrefexAndSuffix.test.ts @@ -204,6 +204,30 @@ suite("NAVObject ObjectNamePrefix Tests", () => { assert.strictEqual(field.name.endsWith(testSettings[Settings.ObjectNameSuffix]), true) }) }); + + + test("Tableextension - Don't set double Affix", () => { + let testSettings = Settings.GetConfigSettings(null) + testSettings[Settings.ObjectNamePrefix] = 'waldo'; + testSettings[Settings.MandatoryAffixes] = ['waldo']; + + let navTestObject = NAVTestObjectLibrary.getTableExtensionWithSuffix(); + let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName) + + assert.notStrictEqual(navObject.tableFields.length, 0) + + let navObject2 = new NAVObject(navObject.NAVObjectTextFixed, testSettings, navTestObject.ObjectFileName) + navObject2.tableFields.forEach(tableField => { + assert.strictEqual(tableField.name.endsWith(testSettings[Settings.MandatoryAffixes][0]), true); + assert.strictEqual(tableField.name, tableField.nameFixed); + }) + + for (let i = 0; i < navObject2.tableFields.length; i++) { + assert.strictEqual(navObject2.tableFields[i].name, navObject.tableFields[i].name) + } + }); + + test("Tableextension - Don't change fieldnumber", () => { let testSettings = Settings.GetConfigSettings(null) testSettings[Settings.ObjectNameSuffix] = 'waldo'; @@ -345,6 +369,28 @@ suite("NAVObject ObjectNamePrefix Tests", () => { assert.strictEqual(navObject2.reportColumns[i].name, navObject.reportColumns[i].name) } }); + + test("Reportextension - Don't set double Affix", () => { + let testSettings = Settings.GetConfigSettings(null) + testSettings[Settings.ObjectNamePrefix] = 'waldo'; + testSettings[Settings.MandatoryAffixes] = ['waldo']; + + let navTestObject = NAVTestObjectLibrary.getReportExtensionWithSuffix(); + let navObject = new NAVObject(navTestObject.ObjectText, testSettings, navTestObject.ObjectFileName) + + assert.notStrictEqual(navObject.reportColumns.length, 0) + + let navObject2 = new NAVObject(navObject.NAVObjectTextFixed, testSettings, navTestObject.ObjectFileName) + navObject2.reportColumns.forEach(column => { + assert.strictEqual(column.name.endsWith(testSettings[Settings.MandatoryAffixes][0]), true); + assert.strictEqual(column.name, column.nameFixed); + }) + + for (let i = 0; i < navObject2.reportColumns.length; i++) { + assert.strictEqual(navObject2.reportColumns[i].name, navObject.reportColumns[i].name) + } + }); + test("Reportextension - Fields on request page", () => { let testSettings = Settings.GetConfigSettings(null) testSettings[Settings.ObjectNameSuffix] = 'waldo'; diff --git a/src/test/suite/NAVTestObjectLibrary.ts b/src/test/suite/NAVTestObjectLibrary.ts index d7b123e..e54f4b9 100644 --- a/src/test/suite/NAVTestObjectLibrary.ts +++ b/src/test/suite/NAVTestObjectLibrary.ts @@ -482,6 +482,25 @@ export function getTableExtension(): NAVTestObject { return object; } + +export function getTableExtensionWithSuffix(): NAVTestObject { + let object = new NAVTestObject; + + object.ObjectFileName = 'SomeTableExt.al' + object.ObjectText = `tableextension 50006 "Sales Cr.Memo Header waldo" extends "Sales Cr.Memo Header" + { + fields + { + field(50021; "Test Fieldwaldo "; Code[20]) + { + Caption = 'Test Field'; + DataClassification = CustomerContent; + } + } + ` + return object; +} + export function getTableExtensionWithSkippingFieldForRename(): NAVTestObject { let object = new NAVTestObject;