Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/concerto-cto/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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, ' ');
}

Expand Down Expand Up @@ -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`) {
Expand All @@ -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, '');
}

Expand Down
75 changes: 74 additions & 1 deletion packages/concerto-cto/test/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,77 @@ describe('parser', () => {

result.should.include('o Double value range=[0.0000001,1.0]');
});
});

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}');
});
});

Loading