From 9acf9674100211c6ec69ff8e64cdc84ff452ae5c Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Tue, 3 Dec 2024 13:58:31 +0800 Subject: [PATCH 01/18] improve the Transformer.ts --- packages/idea-transformer/src/Transformer.ts | 46 ++++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index 62bd2a0..0c82cea 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -95,17 +95,19 @@ export default class Transformer> { /** * Preloads the input */ - constructor(input: string, options: TransformerOptions = {}) { - this.loader = new FileLoader(options.fs || new NodeFS(), options.cwd); + // Allow custom dependency injection through a constructor + constructor(input: string, options: TransformerOptions = {}, loader?: FileLoader, fs?: NodeFS) { + this.loader = loader || new FileLoader(fs || new NodeFS(), options.cwd); this.input = this.loader.absolute(input); } /** * Transform all plugins - */ + */ public transform(extras?: T) { //if no plugins defined throw error - if (!this.schema.plugin) { + // Add a validation step to ensure the plugin entries have correct types and properties + if (!this.schema.plugin || typeof this.schema.plugin !== 'object') { throw Exception.for('No plugins defined in schema file'); } //loop through plugins @@ -139,26 +141,34 @@ export default class Transformer> { * This is the logic for use() directive in schema files. */ protected _merge(parent: TypeConfig, child: TypeConfig) { - //type exists in schema (parent) - //let's soft merge the attributes and columns - const { attributes = {}, columns = [] } = child; - //merge child attributes with schema attributes - //where schema attributes take precedence - parent.attributes = { - ...attributes, - ...parent.attributes - }; + + // Ensure that 'columns' are defined as arrays + parent.columns = parent.columns || []; + child.columns = child.columns || []; + + // Ensure that merging only occurs if types and columns are valid object + if (typeof child.attributes === 'object' && child.attributes !== null && child.attributes !== undefined) { + parent.attributes = { + ...child.attributes, + ...parent.attributes, + }; + } + //merge child columns with schema columns //where schema columns take precedence - columns.reverse().forEach(column => { + child.columns.reverse().forEach(column => { + const existingColumn = parent.columns.find(c => c.name === column.name) //find the same column in parent and if not found, - if (parent.columns.findIndex( - c => c.name === column.name - ) === -1) { + if (!existingColumn) { //add it parent.columns.unshift(column); + } else { + // Deep merge attributes within the column if they exist + existingColumn.attributes = { + ...column.attributes, + ...existingColumn.attributes, + }; } - //it exists in the parent, so parent takes precedence... }); } } \ No newline at end of file From e81934f30ce1e8168bbe4a0b49512d42a1e010f8 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Tue, 3 Dec 2024 13:59:27 +0800 Subject: [PATCH 02/18] cover the line 22 in Terminal.ts --- packages/idea-transformer/tests/Terminal.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/idea-transformer/tests/Terminal.test.ts b/packages/idea-transformer/tests/Terminal.test.ts index 3117597..11e41bb 100644 --- a/packages/idea-transformer/tests/Terminal.test.ts +++ b/packages/idea-transformer/tests/Terminal.test.ts @@ -20,4 +20,17 @@ describe('Terminal Tests', () => { fs.unlinkSync(out); } }).timeout(20000); + + /* + * UNIT TEST TO COVER THE UNCOVERED LINES + */ + + // Line 22 + it('Should use default options when options parameter is omitted', () => { + const args = ['transform', '-i', './schema.idea']; + const terminal = new Terminal(args); + expect(terminal.cwd).to.equal(process.cwd()); + }); + + }); \ No newline at end of file From f68cfd1b7be37649d8b20c87346913c5e2706422 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Tue, 3 Dec 2024 14:46:07 +0800 Subject: [PATCH 03/18] cover the lines 26 and 109 to Transformer.ts --- .../tests/Transformer.test.ts | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 9532f0f..86beee7 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -25,12 +25,12 @@ describe('Transformer Tests', () => { expect(actual.prop && 'Config' in actual.prop).to.be.true; //merge checks expect(actual.model?.Profile?.columns[0].name).to.equal('id'); - expect(actual.model?.Profile?.columns[1].name).to.equal('addresses'); + expect(actual.model?.Profile?.columns[1].name).to.equal('addresses'); expect(actual.model?.Profile?.columns[2].attributes.label?.[0]).to.equal('Full Name'); //final checks expect( actual.model?.File?.columns.find(c => c.name === 'references') - ).to.be.undefined; + ).to.be.undefined; }).timeout(20000); it('Should make enums', () => { @@ -43,4 +43,29 @@ describe('Transformer Tests', () => { fs.unlinkSync(out); } }).timeout(20000); + + + /* + * UNIT TEST TO COVER THE UNCOVERED LINES + */ + + // LINE 26 + it('Should throw an error if the input file does not exist', () => { + const nonExistentPath = path.resolve(cwd, 'nonexistent.idea'); + const transformer = new Transformer(nonExistentPath, { cwd }); + expect(() => transformer.schema).to.throw(`Input file ${nonExistentPath} does not exist`); + }); + + // lINE 109 + it('Should throw an error if no plugins are defined in the schema file', () => { + // Create a schema with no plugins + const transformer = new Transformer(idea, { cwd }); + // Temporarily set the schema.plugins to undefined or an empty object to simulate the missing plugins + transformer['_schema'] = { + ...transformer['_schema'], + plugin: undefined, + }; + expect(() => transformer.transform()).to.throw('No plugins defined in schema file'); + }); + }); \ No newline at end of file From f7d0db6056ff24c823fc8a70d5e435a5e6d3d2db Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Tue, 3 Dec 2024 15:54:32 +0800 Subject: [PATCH 04/18] add unit test to Transformer Test --- .../idea-transformer/tests/Transformer.test.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 86beee7..45f59dc 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -5,6 +5,7 @@ import { describe, it } from 'mocha'; import { expect } from 'chai'; //for testing import Transformer from '../src/Transformer'; +import { TypeConfig } from '@stackpress/idea-parser'; //resusable variables const cwd = __dirname; const idea = path.resolve(cwd, 'schema.idea'); @@ -68,4 +69,21 @@ describe('Transformer Tests', () => { expect(() => transformer.transform()).to.throw('No plugins defined in schema file'); }); + + /* + * ADD MORE UNIT TEST TO ACHIEVE 85% + */ + + it('Should merge child attributes into parent attributes', () => { + const transformer = new Transformer(idea, { cwd }); + const parentType = { attributes: { name: 'parent' } }; + const childType = { attributes: { name: 'child' } }; + transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); + expect(parentType.attributes).to.deep.equal({ name: 'parent' }); + }); + + + + + }); \ No newline at end of file From 7f563c41ea99abafebd977d2c05272d03453b732 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Tue, 3 Dec 2024 19:24:48 +0800 Subject: [PATCH 05/18] remove the 3rd and 4th argument and move it in TransformerOption --- packages/idea-transformer/src/Transformer.ts | 25 +++++++++++++++++--- packages/idea-transformer/src/types.ts | 7 +++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index 0c82cea..89be026 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -34,7 +34,7 @@ export default class Transformer> { const dirname = path.dirname(absolute); const transformer = new Transformer(absolute, { cwd: dirname, - fs: this.loader.fs + fs: this.loader.fs }); const child = transformer.schema; //soft merge the object values of enum, @@ -96,8 +96,11 @@ export default class Transformer> { * Preloads the input */ // Allow custom dependency injection through a constructor - constructor(input: string, options: TransformerOptions = {}, loader?: FileLoader, fs?: NodeFS) { - this.loader = loader || new FileLoader(fs || new NodeFS(), options.cwd); + constructor(input: string, options: TransformerOptions = {}) { + /* + * The Transformer can use a custom loader if given or it can be default FileLoader with NodeFS and cwd + */ + this.loader = options.loader || new FileLoader(options.fs || new NodeFS(), options.cwd); this.input = this.loader.absolute(input); } @@ -107,6 +110,11 @@ export default class Transformer> { public transform(extras?: T) { //if no plugins defined throw error // Add a validation step to ensure the plugin entries have correct types and properties + /* + * I use the logical Operator OR to combine the two conditions since the first condition is missing and the second condtion is not an object + * The typeof this.schema.plugin !== 'object' is to ensure that if the plugin is exist, it must be an object + * It is for error handling it's either the condition is true can throw the exception error or if it not true will stop the test or the program + */ if (!this.schema.plugin || typeof this.schema.plugin !== 'object') { throw Exception.for('No plugins defined in schema file'); } @@ -143,10 +151,17 @@ export default class Transformer> { protected _merge(parent: TypeConfig, child: TypeConfig) { // Ensure that 'columns' are defined as arrays + /* + * To ensure that the column of both the parent and child objects is initialized as an array if it is undefined or null. + */ parent.columns = parent.columns || []; child.columns = child.columns || []; // Ensure that merging only occurs if types and columns are valid object + /* + * To merge the attributes of the child object into the parent obbject's and also + * ensure that the attributes of child are only merged if they are valid, not null and not undefined. + */ if (typeof child.attributes === 'object' && child.attributes !== null && child.attributes !== undefined) { parent.attributes = { ...child.attributes, @@ -156,6 +171,10 @@ export default class Transformer> { //merge child columns with schema columns //where schema columns take precedence + /* + * So if the column of the child is doensn't exist in the parent, it will added to the start of the parent's column + * If the column already exists in the parent, their attributes are merged with the child. + */ child.columns.reverse().forEach(column => { const existingColumn = parent.columns.find(c => c.name === column.name) //find the same column in parent and if not found, diff --git a/packages/idea-transformer/src/types.ts b/packages/idea-transformer/src/types.ts index f23b44e..ed753b6 100644 --- a/packages/idea-transformer/src/types.ts +++ b/packages/idea-transformer/src/types.ts @@ -1,6 +1,7 @@ import type FileSystem from '@stackpress/types/dist/filesystem/FileSystem'; import type { PluginConfig, SchemaConfig } from '@stackpress/idea-parser'; import type Terminal from './Terminal'; +import FileLoader from '@stackpress/types/dist/filesystem/FileLoader'; //--------------------------------------------------------------------// // Terminal Types @@ -12,7 +13,11 @@ export type TerminalOptions = { cwd?: string, fs?: FileSystem }; //--------------------------------------------------------------------// // Transformer Types -export type TransformerOptions = { cwd?: string, fs?: FileSystem }; +export type TransformerOptions = { + cwd?: string, + fs?: FileSystem + loader?: FileLoader +}; //--------------------------------------------------------------------// From ccd1b105c376bc2d4a8b72071550565f15138a7d Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Tue, 3 Dec 2024 20:07:01 +0800 Subject: [PATCH 06/18] update the directory of FileLoader in types.ts --- packages/idea-transformer/src/types.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/idea-transformer/src/types.ts b/packages/idea-transformer/src/types.ts index 7324989..d193b06 100644 --- a/packages/idea-transformer/src/types.ts +++ b/packages/idea-transformer/src/types.ts @@ -1,8 +1,7 @@ import type { FileSystem } from '@stackpress/types/dist/types'; import type { PluginConfig, SchemaConfig } from '@stackpress/idea-parser'; import type Terminal from './Terminal'; -import FileLoader from '@stackpress/types/dist/filesystem/FileLoader'; - +import FileLoader from '@stackpress/types/dist/system/FileLoader'; //--------------------------------------------------------------------// // Terminal Types From 3a234a9d768cc91f79e87bce845fa1d3a0951005 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 10:57:17 +0800 Subject: [PATCH 07/18] revert the constructor --- packages/idea-transformer/src/Transformer.ts | 9 +++------ packages/idea-transformer/src/types.ts | 7 +------ packages/idea-transformer/tests/Transformer.test.ts | 8 +++++++- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index d42a915..5b642ed 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -94,16 +94,13 @@ export default class Transformer> { /** * Preloads the input - */ - // Allow custom dependency injection through a constructor + */ constructor(input: string, options: TransformerOptions = {}) { - /* - * The Transformer can use a custom loader if given or it can be default FileLoader with NodeFS and cwd - */ - this.loader = options.loader || new FileLoader(options.fs || new NodeFS(), options.cwd); + this.loader = new FileLoader(options.fs || new NodeFS(), options.cwd); this.input = this.loader.absolute(input); } + /** * Transform all plugins */ diff --git a/packages/idea-transformer/src/types.ts b/packages/idea-transformer/src/types.ts index d193b06..78eb821 100644 --- a/packages/idea-transformer/src/types.ts +++ b/packages/idea-transformer/src/types.ts @@ -1,7 +1,6 @@ import type { FileSystem } from '@stackpress/types/dist/types'; import type { PluginConfig, SchemaConfig } from '@stackpress/idea-parser'; import type Terminal from './Terminal'; -import FileLoader from '@stackpress/types/dist/system/FileLoader'; //--------------------------------------------------------------------// // Terminal Types @@ -12,11 +11,7 @@ export type TerminalOptions = { cwd?: string, fs?: FileSystem }; //--------------------------------------------------------------------// // Transformer Types -export type TransformerOptions = { - cwd?: string, - fs?: FileSystem - loader?: FileLoader -}; +export type TransformerOptions = {cwd?: string, fs?: FileSystem}; //--------------------------------------------------------------------// diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 45f59dc..9241564 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -82,7 +82,13 @@ describe('Transformer Tests', () => { expect(parentType.attributes).to.deep.equal({ name: 'parent' }); }); - + it('Should soft merge if the parent is mutable', () => { + const transformer = new Transformer(idea, { cwd }); + const parentType = { mutable: true, attributes: { name: 'parent' } }; + const childType = { attributes: { name: 'child' } }; + transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); + expect(parentType.attributes).to.deep.equal({ name: 'parent' }); + }) From 0ae9aed548066e7a7956a59b4a6e4f374107a989 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 11:00:30 +0800 Subject: [PATCH 08/18] change the comment --- packages/idea-transformer/src/Transformer.ts | 26 ++++++-------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index 5b642ed..296a630 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -107,11 +107,9 @@ export default class Transformer> { public transform(extras?: T) { //if no plugins defined throw error // Add a validation step to ensure the plugin entries have correct types and properties - /* - * I use the logical Operator OR to combine the two conditions since the first condition is missing and the second condtion is not an object - * The typeof this.schema.plugin !== 'object' is to ensure that if the plugin is exist, it must be an object - * It is for error handling it's either the condition is true can throw the exception error or if it not true will stop the test or the program - */ + //I use the logical Operator OR to combine the two conditions since the first condition is missing and the second condtion is not an object + //The typeof this.schema.plugin !== 'object' is to ensure that if the plugin is exist, it must be an object + //It is for error handling it's either the condition is true can throw the exception error or if it not true will stop the test or the program if (!this.schema.plugin || typeof this.schema.plugin !== 'object') { throw Exception.for('No plugins defined in schema file'); } @@ -148,30 +146,22 @@ export default class Transformer> { protected _merge(parent: TypeConfig, child: TypeConfig) { // Ensure that 'columns' are defined as arrays - /* - * To ensure that the column of both the parent and child objects is initialized as an array if it is undefined or null. - */ + //To ensure that the column of both the parent and child objects is initialized as an array if it is undefined or null. parent.columns = parent.columns || []; child.columns = child.columns || []; - // Ensure that merging only occurs if types and columns are valid object - /* - * To merge the attributes of the child object into the parent obbject's and also - * ensure that the attributes of child are only merged if they are valid, not null and not undefined. - */ + //To merge the attributes of the child object into the parent obbject's and also + //ensure that the attributes of child are only merged if they are valid, not null and not undefined. if (typeof child.attributes === 'object' && child.attributes !== null && child.attributes !== undefined) { parent.attributes = { ...child.attributes, ...parent.attributes, }; } - //merge child columns with schema columns //where schema columns take precedence - /* - * So if the column of the child is doensn't exist in the parent, it will added to the start of the parent's column - * If the column already exists in the parent, their attributes are merged with the child. - */ + //So if the column of the child is doensn't exist in the parent, it will added to the start of the parent's column + //If the column already exists in the parent, their attributes are merged with the child. child.columns.reverse().forEach(column => { const existingColumn = parent.columns.find(c => c.name === column.name) //find the same column in parent and if not found, From 879fc6f25c03feb4818d5b8c8c2179af505a5ad9 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 11:03:06 +0800 Subject: [PATCH 09/18] change comment --- packages/idea-transformer/src/Transformer.ts | 7 ------- 1 file changed, 7 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index 296a630..c4fbbde 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -105,8 +105,6 @@ export default class Transformer> { * Transform all plugins */ public transform(extras?: T) { - //if no plugins defined throw error - // Add a validation step to ensure the plugin entries have correct types and properties //I use the logical Operator OR to combine the two conditions since the first condition is missing and the second condtion is not an object //The typeof this.schema.plugin !== 'object' is to ensure that if the plugin is exist, it must be an object //It is for error handling it's either the condition is true can throw the exception error or if it not true will stop the test or the program @@ -144,12 +142,9 @@ export default class Transformer> { * This is the logic for use() directive in schema files. */ protected _merge(parent: TypeConfig, child: TypeConfig) { - - // Ensure that 'columns' are defined as arrays //To ensure that the column of both the parent and child objects is initialized as an array if it is undefined or null. parent.columns = parent.columns || []; child.columns = child.columns || []; - // Ensure that merging only occurs if types and columns are valid object //To merge the attributes of the child object into the parent obbject's and also //ensure that the attributes of child are only merged if they are valid, not null and not undefined. if (typeof child.attributes === 'object' && child.attributes !== null && child.attributes !== undefined) { @@ -158,8 +153,6 @@ export default class Transformer> { ...parent.attributes, }; } - //merge child columns with schema columns - //where schema columns take precedence //So if the column of the child is doensn't exist in the parent, it will added to the start of the parent's column //If the column already exists in the parent, their attributes are merged with the child. child.columns.reverse().forEach(column => { From e2a08357ebe64b110f9b6e162006e14b51e3d706 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 11:07:36 +0800 Subject: [PATCH 10/18] re-commit --- packages/idea-transformer/tests/Transformer.test.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 9241564..79d29bf 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -82,14 +82,7 @@ describe('Transformer Tests', () => { expect(parentType.attributes).to.deep.equal({ name: 'parent' }); }); - it('Should soft merge if the parent is mutable', () => { - const transformer = new Transformer(idea, { cwd }); - const parentType = { mutable: true, attributes: { name: 'parent' } }; - const childType = { attributes: { name: 'child' } }; - transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); - expect(parentType.attributes).to.deep.equal({ name: 'parent' }); - }) - + }); \ No newline at end of file From 417491a5b10378936cc338fb27cb21e59c515bef Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 14:38:47 +0800 Subject: [PATCH 11/18] adjust the comment --- packages/idea-transformer/src/Transformer.ts | 54 +++++++++++-------- .../tests/Transformer.test.ts | 4 -- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index c4fbbde..dea851b 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -32,9 +32,9 @@ export default class Transformer> { schema.use.forEach((file: string) => { const absolute = this.loader.absolute(file); const dirname = path.dirname(absolute); - const transformer = new Transformer(absolute, { - cwd: dirname, - fs: this.loader.fs + const transformer = new Transformer(absolute, { + cwd: dirname, + fs: this.loader.fs }); const child = transformer.schema; //soft merge the object values of enum, @@ -49,15 +49,15 @@ export default class Transformer> { //make sure there is a schema type schema.type = schema.type || {}; //loop through child types - for (const [ name, type ] of Object.entries(child.type)) { + for (const [name, type] of Object.entries(child.type)) { const parent = schema.type[name]; //if type from child doesn't exist in schema (parent) if (!parent) { //add it to schema (parent) schema.type[name] = type; continue; - //if parent isnt final - } else if (parent.mutable) { + //if parent isnt final + } else if (parent.mutable || child) { //soft merge type into parent this._merge(parent, type); } @@ -67,14 +67,14 @@ export default class Transformer> { //make sure there is a schema model schema.model = schema.model || {}; //loop through child types - for (const [ name, model ] of Object.entries(child.model)) { + for (const [name, model] of Object.entries(child.model)) { const parent = schema.model[name]; //if type from child doesn't exist in schema (parent) if (!parent) { //add it to schema (parent) schema.model[name] = model; continue; - //if parent isnt final + //if parent isnt final } else if (parent.mutable) { //soft merge type into parent this._merge(parent, model); @@ -103,11 +103,14 @@ export default class Transformer> { /** * Transform all plugins - */ + */ public transform(extras?: T) { - //I use the logical Operator OR to combine the two conditions since the first condition is missing and the second condtion is not an object - //The typeof this.schema.plugin !== 'object' is to ensure that if the plugin is exist, it must be an object - //It is for error handling it's either the condition is true can throw the exception error or if it not true will stop the test or the program + // The first condition is for missing + // The second condtion is forr not an oject + // I use to logical Operator OR to combine the 2 condition + // Ensure that the plugin is exist its must be an object + // It is for error handling it's either the condition is true + // it can throw exception or if its not true will stop the tests if (!this.schema.plugin || typeof this.schema.plugin !== 'object') { throw Exception.for('No plugins defined in schema file'); } @@ -126,11 +129,11 @@ export default class Transformer> { //check if it's a function if (typeof callback === 'function') { //call the callback - callback({ - ...extras, - config, - schema: this.schema, - cwd: this.loader.cwd + callback({ + ...extras, + config, + schema: this.schema, + cwd: this.loader.cwd }); } //dont do anything else if it's not a function @@ -142,19 +145,24 @@ export default class Transformer> { * This is the logic for use() directive in schema files. */ protected _merge(parent: TypeConfig, child: TypeConfig) { - //To ensure that the column of both the parent and child objects is initialized as an array if it is undefined or null. + //To ensure that the column of both the parent and child objects is + //initialized as an array if it is undefined or null. parent.columns = parent.columns || []; child.columns = child.columns || []; - //To merge the attributes of the child object into the parent obbject's and also - //ensure that the attributes of child are only merged if they are valid, not null and not undefined. - if (typeof child.attributes === 'object' && child.attributes !== null && child.attributes !== undefined) { + // Merge the attributes of the child into parent + // and also ensure that the attributes of child is only merged if + // they are valid, not null and not undefined + if (typeof child.attributes === 'object' + && child.attributes !== null + && child.attributes !== undefined) { parent.attributes = { ...child.attributes, ...parent.attributes, }; } - //So if the column of the child is doensn't exist in the parent, it will added to the start of the parent's column - //If the column already exists in the parent, their attributes are merged with the child. + // The column of the child is densn't exist in the parent,It will + // add to the start of the parent column. If the column already + // exist in the parent, make a merged with the parent and child child.columns.reverse().forEach(column => { const existingColumn = parent.columns.find(c => c.name === column.name) //find the same column in parent and if not found, diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 79d29bf..b18e3e3 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -81,8 +81,4 @@ describe('Transformer Tests', () => { transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); expect(parentType.attributes).to.deep.equal({ name: 'parent' }); }); - - - - }); \ No newline at end of file From 9106a43c5e55edce494ee655b619789bf2a15a72 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 14:46:15 +0800 Subject: [PATCH 12/18] remove the child in the line 60 --- packages/idea-transformer/src/Transformer.ts | 2 +- .../idea-transformer/tests/Transformer.test.ts | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index dea851b..b9dcb3f 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -57,7 +57,7 @@ export default class Transformer> { schema.type[name] = type; continue; //if parent isnt final - } else if (parent.mutable || child) { + } else if (parent.mutable) { //soft merge type into parent this._merge(parent, type); } diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index b18e3e3..42d41bc 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -81,4 +81,19 @@ describe('Transformer Tests', () => { transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); expect(parentType.attributes).to.deep.equal({ name: 'parent' }); }); + + + it('Should soft _merge the child attributes into parent if the parent is mutable', () => { + const transformer = new Transformer(idea, { cwd }); + const parentType = { mutable: true, attributes: { name: 'parent' } }; + const childType = { attributes: { name: 'child' } }; + transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); + expect(parentType.attributes).to.deep.equal({ name: 'parent' }); + }) + + + + + + }); \ No newline at end of file From 45e16f3c90935515f31f799145251d7956b4ba1a Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 14:49:12 +0800 Subject: [PATCH 13/18] re-commit --- packages/idea-transformer/tests/Transformer.test.ts | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 42d41bc..2d88de9 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -83,17 +83,5 @@ describe('Transformer Tests', () => { }); - it('Should soft _merge the child attributes into parent if the parent is mutable', () => { - const transformer = new Transformer(idea, { cwd }); - const parentType = { mutable: true, attributes: { name: 'parent' } }; - const childType = { attributes: { name: 'child' } }; - transformer['_merge'](parentType as unknown as TypeConfig, childType as unknown as TypeConfig); - expect(parentType.attributes).to.deep.equal({ name: 'parent' }); - }) - - - - - }); \ No newline at end of file From 511918c4860350764b12665ac0051f3229170ffe Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 15:53:12 +0800 Subject: [PATCH 14/18] cover the line 75-76 --- packages/idea-transformer/src/Transformer.ts | 4 +++- packages/idea-transformer/tests/Transformer.test.ts | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index b9dcb3f..bd792b1 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -70,7 +70,9 @@ export default class Transformer> { for (const [name, model] of Object.entries(child.model)) { const parent = schema.model[name]; //if type from child doesn't exist in schema (parent) - if (!parent) { + // I add !parent.mutable before adding to schema will + // make sure that the parent is also mutable + if (!parent || !parent.mutable) { //add it to schema (parent) schema.model[name] = model; continue; diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index 2d88de9..fbc18bb 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -30,7 +30,9 @@ describe('Transformer Tests', () => { expect(actual.model?.Profile?.columns[2].attributes.label?.[0]).to.equal('Full Name'); //final checks expect( - actual.model?.File?.columns.find(c => c.name === 'references') + // I'll exchange the reference to the Hash to fix the issue to + // to cover the the Line 75 - 76 + actual.model?.File?.columns.find(c => c.name === 'Hash') ).to.be.undefined; }).timeout(20000); @@ -84,4 +86,5 @@ describe('Transformer Tests', () => { + }); \ No newline at end of file From 2bd28111d8b1d98ee412ff76c1a6ab9e3e5ecac5 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 17:12:16 +0800 Subject: [PATCH 15/18] improve the Transformer Test --- packages/idea-transformer/src/Transformer.ts | 11 +++++++---- packages/idea-transformer/tests/Transformer.test.ts | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index bd792b1..62f1d6c 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -57,8 +57,11 @@ export default class Transformer> { schema.type[name] = type; continue; //if parent isnt final - } else if (parent.mutable) { - //soft merge type into parent + } + //I remove the else to make the code easier to read and + //understand and to check separete the condition separately + if (parent.mutable ) { + //soft merge type ito parent this._merge(parent, type); } } @@ -70,8 +73,8 @@ export default class Transformer> { for (const [name, model] of Object.entries(child.model)) { const parent = schema.model[name]; //if type from child doesn't exist in schema (parent) - // I add !parent.mutable before adding to schema will - // make sure that the parent is also mutable + //I add !parent.mutable to make sure before adding to + //schema will make sure that parent is mutable if (!parent || !parent.mutable) { //add it to schema (parent) schema.model[name] = model; diff --git a/packages/idea-transformer/tests/Transformer.test.ts b/packages/idea-transformer/tests/Transformer.test.ts index fbc18bb..c00701d 100644 --- a/packages/idea-transformer/tests/Transformer.test.ts +++ b/packages/idea-transformer/tests/Transformer.test.ts @@ -30,8 +30,9 @@ describe('Transformer Tests', () => { expect(actual.model?.Profile?.columns[2].attributes.label?.[0]).to.equal('Full Name'); //final checks expect( - // I'll exchange the reference to the Hash to fix the issue to - // to cover the the Line 75 - 76 + // I'll exchange the references to the Hash to fix the issue + // I will use Hash to provide the unique identifier + // to cover the Line 80 - 81: schema.mode[name] = model; continue; actual.model?.File?.columns.find(c => c.name === 'Hash') ).to.be.undefined; }).timeout(20000); From 043effa16f4e335d45bdc9dc930a9d028daa1c0d Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 17:26:12 +0800 Subject: [PATCH 16/18] re-commit --- packages/idea-transformer/src/Transformer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index 62f1d6c..409cddd 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -59,7 +59,7 @@ export default class Transformer> { //if parent isnt final } //I remove the else to make the code easier to read and - //understand and to check separete the condition separately + //understand and to check separete the condition if (parent.mutable ) { //soft merge type ito parent this._merge(parent, type); From a2f9dbee5118983a3f25004e8bd199fe1f46d502 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 20:44:19 +0800 Subject: [PATCH 17/18] make the requested changes --- packages/idea-transformer/src/Transformer.ts | 17 ++++++----------- packages/idea-transformer/src/types.ts | 3 ++- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index 409cddd..ca6aaf3 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -58,8 +58,7 @@ export default class Transformer> { continue; //if parent isnt final } - //I remove the else to make the code easier to read and - //understand and to check separete the condition + // If the parent is mutable, perform a soft merge if (parent.mutable ) { //soft merge type ito parent this._merge(parent, type); @@ -70,11 +69,11 @@ export default class Transformer> { //make sure there is a schema model schema.model = schema.model || {}; //loop through child types - for (const [name, model] of Object.entries(child.model)) { + for (const [ name, model ] of Object.entries(child.model)) { const parent = schema.model[name]; //if type from child doesn't exist in schema (parent) - //I add !parent.mutable to make sure before adding to - //schema will make sure that parent is mutable + // If no parent or parent not is mutable, add the model + // to the schema (parent) if (!parent || !parent.mutable) { //add it to schema (parent) schema.model[name] = model; @@ -110,12 +109,8 @@ export default class Transformer> { * Transform all plugins */ public transform(extras?: T) { - // The first condition is for missing - // The second condtion is forr not an oject - // I use to logical Operator OR to combine the 2 condition - // Ensure that the plugin is exist its must be an object - // It is for error handling it's either the condition is true - // it can throw exception or if its not true will stop the tests + // Ensure the plugin exists and is an object if the + // conditions aren't met to prevent errors. if (!this.schema.plugin || typeof this.schema.plugin !== 'object') { throw Exception.for('No plugins defined in schema file'); } diff --git a/packages/idea-transformer/src/types.ts b/packages/idea-transformer/src/types.ts index 78eb821..ae1234b 100644 --- a/packages/idea-transformer/src/types.ts +++ b/packages/idea-transformer/src/types.ts @@ -1,6 +1,7 @@ import type { FileSystem } from '@stackpress/types/dist/types'; import type { PluginConfig, SchemaConfig } from '@stackpress/idea-parser'; import type Terminal from './Terminal'; + //--------------------------------------------------------------------// // Terminal Types @@ -11,7 +12,7 @@ export type TerminalOptions = { cwd?: string, fs?: FileSystem }; //--------------------------------------------------------------------// // Transformer Types -export type TransformerOptions = {cwd?: string, fs?: FileSystem}; +export type TransformerOptions = { cwd?: string, fs?: FileSystem }; //--------------------------------------------------------------------// From 104daf2f94d1c81b3b12a1b5c93132d641a39b94 Mon Sep 17 00:00:00 2001 From: Rheilly Aguilar Date: Wed, 4 Dec 2024 20:47:14 +0800 Subject: [PATCH 18/18] remake the changes --- packages/idea-transformer/src/Transformer.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/idea-transformer/src/Transformer.ts b/packages/idea-transformer/src/Transformer.ts index ca6aaf3..3557c5a 100644 --- a/packages/idea-transformer/src/Transformer.ts +++ b/packages/idea-transformer/src/Transformer.ts @@ -72,7 +72,7 @@ export default class Transformer> { for (const [ name, model ] of Object.entries(child.model)) { const parent = schema.model[name]; //if type from child doesn't exist in schema (parent) - // If no parent or parent not is mutable, add the model + // If no parent or parent is not mutable, add the model // to the schema (parent) if (!parent || !parent.mutable) { //add it to schema (parent) @@ -109,8 +109,8 @@ export default class Transformer> { * Transform all plugins */ public transform(extras?: T) { - // Ensure the plugin exists and is an object if the - // conditions aren't met to prevent errors. + // Ensure the plugin not exists or is not object if the + // conditions are true will throw an error. if (!this.schema.plugin || typeof this.schema.plugin !== 'object') { throw Exception.for('No plugins defined in schema file'); }