From c0c306f7bb9789c3a9cfccc1e281f766f70e6ae6 Mon Sep 17 00:00:00 2001 From: Ertugrul Karademir Date: Thu, 21 Dec 2023 11:42:09 +0000 Subject: [PATCH] fix(class-declaration): throw with undefined ast properties (#771) Signed-off-by: Ertugrul Karademir --- .../lib/introspect/classdeclaration.js | 7 +++++++ packages/concerto-core/messages/en.json | 1 + .../test/introspect/classdeclaration.js | 17 +++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/packages/concerto-core/lib/introspect/classdeclaration.js b/packages/concerto-core/lib/introspect/classdeclaration.js index ba97a6aca..d78a93b7b 100644 --- a/packages/concerto-core/lib/introspect/classdeclaration.js +++ b/packages/concerto-core/lib/introspect/classdeclaration.js @@ -82,6 +82,13 @@ class ClassDeclaration extends Declaration { } } + if (!Array.isArray(this.ast.properties)) { + let formatter = Globalize.messageFormatter('classdeclaration-validate-undefined-properties'); + throw new IllegalModelException(formatter({ + 'class':this.name + }), this.modelFile, this.ast.location); + } + for (let n = 0; n < this.ast.properties.length; n++) { let thing = this.ast.properties[n]; diff --git a/packages/concerto-core/messages/en.json b/packages/concerto-core/messages/en.json index d4004f560..6a914b8c3 100644 --- a/packages/concerto-core/messages/en.json +++ b/packages/concerto-core/messages/en.json @@ -21,6 +21,7 @@ "classdeclaration-validate-duplicatefieldname": "Class \"{class}\" has more than one field named \"{fieldName}\".", "classdeclaration-validate-missingidentifier" : "Class \"{class}\" is not declared as \"abstract\". It must define an identifying field.", "classdeclaration-validate-selfextending": "Class \"{class}\" cannot extend itself.", + "classdeclaration-validate-undefined-properties": "Properties of Class \"{class}\" has to be defined.", "modelfile-constructor-unrecmodelelem": "Unrecognised model element \"{type}\".", "modelfile-resolvetype-undecltype": "Undeclared type \"{type}\" in \"{context}\".", diff --git a/packages/concerto-core/test/introspect/classdeclaration.js b/packages/concerto-core/test/introspect/classdeclaration.js index 823862ab8..812083444 100644 --- a/packages/concerto-core/test/introspect/classdeclaration.js +++ b/packages/concerto-core/test/introspect/classdeclaration.js @@ -67,6 +67,23 @@ describe('ClassDeclaration', () => { }).should.throw(/Invalid class name '2nd'/); }); + it('should throw when ast properties is null', () => { + (() => { + new ClassDeclaration(modelFile, { + name: 'aconcept', + properties: null, + }); + }).should.throw(/Properties of Class/); + }); + + it('should throw when ast properties is undefined', () => { + (() => { + new ClassDeclaration(modelFile, { + name: 'aconcept', + properties: undefined, + }); + }).should.throw(/Properties of Class/); + }); }); describe('#validate', () => {