diff --git a/packages/concerto-cto/src/printer.ts b/packages/concerto-cto/src/printer.ts index f8c08b26f..99b2bb0b5 100644 --- a/packages/concerto-cto/src/printer.ts +++ b/packages/concerto-cto/src/printer.ts @@ -102,6 +102,9 @@ function decoratorFromMetaModel(mm: IDecorator): string { * @return {string} the string for the decorators */ function decoratorsFromMetaModel(mm: IDecorator[], prefix: string): string { + if (!mm || mm.length === 0) { + return ''; + } let result = ''; result += mm.map(decoratorFromMetaModel).join(`\n${prefix}`); result += `\n${prefix}`; @@ -316,7 +319,7 @@ function toDoubleString(value: number): string { function propertyFromMetaModel(prop: IProperty): string { let result = ''; - if (prop.decorators) { + if (prop.decorators && prop.decorators.length > 0) { result += decoratorsFromMetaModel(prop.decorators, ' '); } @@ -345,7 +348,7 @@ function propertyFromMetaModel(prop: IProperty): string { */ function mapFromMetaModel(entry: any): string { let result = ''; - if (entry.decorators) { + if (entry.decorators && entry.decorators.length > 0) { result += decoratorsFromMetaModel(entry.decorators, ' '); } if (entry.$class === `${MetaModelNamespace}.RelationshipMapValueType`) { @@ -366,7 +369,7 @@ function mapFromMetaModel(entry: any): string { function declFromMetaModel(mm: IDeclaration): string { let result = ''; - if (mm.decorators) { + if (mm.decorators && mm.decorators.length > 0) { result += decoratorsFromMetaModel(mm.decorators, ''); } diff --git a/packages/concerto-cto/test/printer.js b/packages/concerto-cto/test/printer.js index 2592a590d..daa41734a 100644 --- a/packages/concerto-cto/test/printer.js +++ b/packages/concerto-cto/test/printer.js @@ -336,4 +336,77 @@ describe('parser', () => { result.should.include('o Double value range=[0.0000001,1.0]'); }); -}); \ No newline at end of file + + it('Should not print extra blank lines or indentation when decorators array is empty', () => { + const withoutDecorators = { + $class: 'concerto.metamodel@1.0.0.Model', + namespace: 'org.acme@1.0.0', + declarations: [ + { + $class: 'concerto.metamodel@1.0.0.ConceptDeclaration', + name: 'Foo', + isAbstract: false, + properties: [ + { + $class: 'concerto.metamodel@1.0.0.StringProperty', + name: 'bar', + isArray: false, + isOptional: false, + } + ] + } + ] + }; + + const withEmptyDecorators = { + $class: 'concerto.metamodel@1.0.0.Model', + namespace: 'org.acme@1.0.0', + decorators: [], + declarations: [ + { + $class: 'concerto.metamodel@1.0.0.ConceptDeclaration', + name: 'Foo', + isAbstract: false, + decorators: [], + properties: [ + { + $class: 'concerto.metamodel@1.0.0.StringProperty', + name: 'bar', + isArray: false, + isOptional: false, + decorators: [] + } + ] + } + ] + }; + + Printer.toCTO(withEmptyDecorators).should.equal(Printer.toCTO(withoutDecorators)); + }); + + it('Should not print extra blank lines for map with empty decorators', () => { + const mapWithEmptyDecorators = { + $class: 'concerto.metamodel@1.0.0.Model', + namespace: 'org.acme@1.0.0', + declarations: [ + { + $class: 'concerto.metamodel@1.0.0.MapDeclaration', + name: 'MyMap', + decorators: [], + key: { + $class: 'concerto.metamodel@1.0.0.StringMapKeyType', + decorators: [] + }, + value: { + $class: 'concerto.metamodel@1.0.0.StringMapValueType', + decorators: [] + } + } + ] + }; + + const result = Printer.toCTO(mapWithEmptyDecorators); + result.should.equal('namespace org.acme@1.0.0\n\nmap MyMap {\n o String\n o String\n}'); + }); +}); +