Skip to content

Commit

Permalink
fix(core): Remove use of unreliable this.constructor.name
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
Jerome Simeon authored and jeromesimeon committed Mar 1, 2022
1 parent aaf5c90 commit 582e4a5
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 6 deletions.
6 changes: 6 additions & 0 deletions packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Factory {
}
class AssetDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ string declarationKind()
}
class ClassDeclaration extends Decorated {
+ void constructor(ModelFile,Object) throws IllegalModelException
Expand Down Expand Up @@ -58,6 +59,7 @@ class ClassDeclaration extends Decorated {
}
class ConceptDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ string declarationKind()
}
class Decorator {
+ void constructor(ClassDeclaration|Property,Object) throws IllegalModelException
Expand All @@ -71,13 +73,15 @@ class DecoratorFactory {
class EnumDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ String toString()
+ string declarationKind()
}
class EnumValueDeclaration extends Property {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
+ boolean isEnumValue()
}
class EventDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ string declarationKind()
}
class IdentifiedDeclaration extends ClassDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
Expand Down Expand Up @@ -129,6 +133,7 @@ class ModelFile {
}
class ParticipantDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ string declarationKind()
}
class Property extends Decorated {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
Expand All @@ -150,6 +155,7 @@ class RelationshipDeclaration extends Property {
}
class TransactionDeclaration extends IdentifiedDeclaration {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ string declarationKind()
}
class Identifiable extends Typed {
+ string getTimestamp()
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Note that the latest public API is documented using JSDocs and is available in api.txt.
#

Version 1.2.2 {859dc5db772867c4591ed1fd70b8f840} 2021-11-24
Version 1.2.2 {971ab49db13d87551db109316e5b59eb} 2021-11-24
- Remove custom instanceof and add methods to check runtime type
- Remove support for Node 12
- Generate Typescript definitions from JSDoc
Expand Down
9 changes: 9 additions & 0 deletions packages/concerto-core/lib/introspect/assetdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class AssetDeclaration extends IdentifiedDeclaration {
constructor(modelFile, ast) {
super(modelFile, ast);
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'AssetDeclaration';
}
}

module.exports = AssetDeclaration;
5 changes: 3 additions & 2 deletions packages/concerto-core/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ class ClassDeclaration extends Decorated {

// if super type is not a concept, then check that this type and the super type
// are of the same type. E.g. an asset cannot extend a participant
if (classDecl.constructor.name !== 'ConceptDeclaration' && this.constructor.name !== classDecl.constructor.name) {
throw new IllegalModelException(`${this.constructor.name} (${this.getName()}) cannot extend ${classDecl.constructor.name} (${classDecl.getName()})`, this.modelFile, this.ast.location);
if (classDecl.declarationKind() !== 'ConceptDeclaration' && this.declarationKind() !== classDecl.declarationKind()) {
console.log(`CLASSDECL: ${classDecl.constructor.name}`);
throw new IllegalModelException(`${this.declarationKind()} (${this.getName()}) cannot extend ${classDecl.declarationKind()} (${classDecl.getName()})`, this.modelFile, this.ast.location);
}
this.superTypeDeclaration = classDecl;
return classDecl;
Expand Down
9 changes: 9 additions & 0 deletions packages/concerto-core/lib/introspect/conceptdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class ConceptDeclaration extends ClassDeclaration {
constructor(modelFile, ast) {
super(modelFile, ast);
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'ConceptDeclaration';
}
}

module.exports = ConceptDeclaration;
9 changes: 9 additions & 0 deletions packages/concerto-core/lib/introspect/enumdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class EnumDeclaration extends ClassDeclaration {
toString() {
return 'EnumDeclaration {id=' + this.getFullyQualifiedName() + '}';
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'EnumDeclaration';
}
}

module.exports = EnumDeclaration;
9 changes: 9 additions & 0 deletions packages/concerto-core/lib/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ class EventDeclaration extends IdentifiedDeclaration {
process() {
super.process();
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'EventDeclaration';
}
}

module.exports = EventDeclaration;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class ParticipantDeclaration extends IdentifiedDeclaration {
constructor(modelFile, ast) {
super(modelFile, ast);
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'ParticipantDeclaration';
}
}

module.exports = ParticipantDeclaration;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class TransactionDeclaration extends IdentifiedDeclaration {
constructor(modelFile, ast) {
super(modelFile, ast);
}

/**
* Returns the kind of declaration
*
* @return {string} what kind of declaration this is
*/
declarationKind() {
return 'TransactionDeclaration';
}
}

module.exports = TransactionDeclaration;
8 changes: 8 additions & 0 deletions packages/concerto-core/test/introspect/assetdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('AssetDeclaration', () => {
mockClassDeclaration = sinon.createStubInstance(AssetDeclaration);
mockModelManager.getType.returns(mockClassDeclaration);
mockClassDeclaration.getProperties.returns([]);
mockClassDeclaration.declarationKind.returns('AssetDeclaration');
});

afterEach(() => {
Expand Down Expand Up @@ -156,4 +157,11 @@ describe('AssetDeclaration', () => {
});
});

describe('#declarationKind', () => {
it('should return that is is an Asset Declaration', () => {
let asset = loadAssetDeclaration('test/data/parser/assetdeclaration.resolve.cto');
(asset.declarationKind()).should.equal('AssetDeclaration');
});
});

});
8 changes: 8 additions & 0 deletions packages/concerto-core/test/introspect/enumdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ describe('EnumDeclaration', () => {
(value[0].isEnumValue()).should.be.true;
});
});

describe('#declarationKind', () => {
it('should return that is is an Enum Declaration', () => {
let declaration = loadLastDeclaration('test/data/model/enum.cto', EnumDeclaration);
(declaration.declarationKind()).should.equal('EnumDeclaration');
});
});

});
1 change: 1 addition & 0 deletions packages/concerto-core/test/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ describe('EventDeclaration', () => {
abstractEvent.isAbstract().should.be.true;
abstractEvent.isEvent().should.be.true;
abstractEvent.validate();
abstractEvent.declarationKind().should.equal('EventDeclaration');

const concreteEvent = modelFile.getEventDeclaration('ConcreteEvent');
concreteEvent.getFullyQualifiedName().should.equal('org.acme.ConcreteEvent');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ describe('ParticipantDeclaration', () => {

describe('#getParticipantDeclarations', () => {
it('should get participants', () => {
let assets = modelManager.getParticipantDeclarations();
assets.should.have.lengthOf(3);
let participants = modelManager.getParticipantDeclarations();
participants.should.have.lengthOf(3);
participants[0].declarationKind().should.equal('ParticipantDeclaration');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('TransactionDeclaration', () => {
const modelFile = ParserUtil.newModelFile(modelManager, model);
let td = modelFile.getTransactionDeclaration('T');
td.validate();

td.declarationKind().should.equal('TransactionDeclaration');
});
});
});

0 comments on commit 582e4a5

Please sign in to comment.