Skip to content

Commit

Permalink
fix(types): typescript type generation improvements (contributes to a…
Browse files Browse the repository at this point in the history
…ccordproject#402)

Signed-off-by: Simon Stone <[email protected]>
  • Loading branch information
Simon Stone authored and Simon Stone committed Mar 25, 2022
1 parent 75c5b42 commit 3ec0f66
Show file tree
Hide file tree
Showing 100 changed files with 1,599 additions and 229 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion packages/concerto-core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class AssetDeclaration extends IdentifiedDeclaration {
class ClassDeclaration extends Decorated {
+ void constructor(ModelFile,Object) throws IllegalModelException
+ ClassDeclaration _resolveSuperType()
~ void validate() throws IllegalModelException
+ boolean isAbstract()
+ string getName()
+ string getNamespace()
Expand Down Expand Up @@ -167,6 +168,7 @@ class ParticipantDeclaration extends IdentifiedDeclaration {
class Property extends Decorated {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
+ ClassDeclaration getParent()
~ void validate(ClassDeclaration) throws IllegalModelException
+ string getName()
+ string getType()
+ boolean isOptional()
Expand All @@ -179,6 +181,7 @@ class Property extends Decorated {
}
class RelationshipDeclaration extends Property {
+ void constructor(ClassDeclaration,Object) throws IllegalModelException
~ void validate(ClassDeclaration) throws IllegalModelException
+ String toString()
+ boolean isRelationship()
}
Expand All @@ -187,6 +190,7 @@ class TransactionDeclaration extends IdentifiedDeclaration {
+ string declarationKind()
}
class Identifiable extends Typed {
~ void constructor(ModelManager,ClassDeclaration,string,string,string,string)
+ string getTimestamp()
+ string getIdentifier()
+ void setIdentifier(string)
Expand All @@ -209,12 +213,14 @@ class Resource extends Identifiable {
+ Object toJSON()
}
class Typed {
~ void constructor(ModelManager,ClassDeclaration,string,string)
+ string getType()
+ string getFullyQualifiedType()
+ string getNamespace()
+ void setPropertyValue(string,string)
+ void addArrayValue(string,string)
+ boolean instanceOf(String)
~ void toJSON()
}
class ValidatedResource extends Resource {
+ void setPropertyValue(string,string) throws Error
Expand All @@ -239,6 +245,6 @@ class Serializer {
+ Resource fromJSON(Object,Object?,boolean,boolean,number?)
}
class TypeNotFoundException extends BaseException {
+ void constructor(string,string?,string)
+ void constructor(string,string|,string)
+ string getTypeName()
}
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 2.0.0-alpha.1 {ee5fa0479fe2720d720ac500b1b3ead8} 2021-11-24
Version 2.0.0-alpha.1 {4169a97a927d336f3c76f85f00725c13} 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
5 changes: 4 additions & 1 deletion packages/concerto-core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

'use strict';

const fs = require('fs');
const path = require('path');

/**
* Concerto core module. Concerto is a framework for defining domain
* specific models.
Expand Down Expand Up @@ -89,7 +92,7 @@ const Concerto = require('./lib/concerto');
const MetaModel = require('./lib/introspect/metamodel');

// Version
const version = require('./package.json');
const version = JSON.parse(fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8'));

module.exports = {
SecurityException,
Expand Down
21 changes: 17 additions & 4 deletions packages/concerto-core/lib/basemodelmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@

const fsPath = require('path');

const DefaultFileLoader = require('@accordproject/concerto-util').DefaultFileLoader;
const FileDownloader = require('@accordproject/concerto-util').FileDownloader;
const ModelWriter = require('@accordproject/concerto-util').ModelWriter;
const MetaModelUtil = require('@accordproject/concerto-metamodel').MetaModelUtil;
const { DefaultFileLoader, FileDownloader, ModelWriter } = require('@accordproject/concerto-util');
const { MetaModelUtil } = require('@accordproject/concerto-metamodel');

const Factory = require('./factory');
const Globalize = require('./globalize');
Expand All @@ -30,6 +28,21 @@ const Serializer = require('./serializer');
const TypeNotFoundException = require('./typenotfoundexception');
const { rootModelFile, rootModelCto, rootModelAst } = require('./rootmodel');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const AssetDeclaration = require('./introspect/assetdeclaration');
const ClassDeclaration = require('./introspect/classdeclaration');
const ConceptDeclaration = require('./introspect/conceptdeclaration');
const DecoratorFactory = require('./introspect/decoratorfactory');
const EnumDeclaration = require('./introspect/enumdeclaration');
const EventDeclaration = require('./introspect/eventdeclaration');
const ParticipantDeclaration = require('./introspect/participantdeclaration');
const TransactionDeclaration = require('./introspect/transactiondeclaration');
}
/* eslint-enable no-unused-vars */

const debug = require('debug')('concerto:BaseModelManager');

// How to create a modelfile from the external content
Expand Down
10 changes: 9 additions & 1 deletion packages/concerto-core/lib/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const TypedStack = require('@accordproject/concerto-util').TypedStack;
const { TypedStack } = require('@accordproject/concerto-util');

const debug = require('debug')('concerto:Factory');
const Globalize = require('./globalize');
Expand All @@ -35,6 +35,14 @@ const dayjs = require('dayjs');
const utc = require('dayjs/plugin/utc');
dayjs.extend(utc);

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelManager = require('./modelmanager');
}
/* eslint-enable no-unused-vars */

/**
* Use the Factory to create instances of Resource: transactions, participants
* and assets.
Expand Down
8 changes: 8 additions & 0 deletions packages/concerto-core/lib/introspect/assetdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

const IdentifiedDeclaration = require('./identifieddeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/**
* AssetDeclaration defines the schema (aka model or class) for
* an Asset. It extends ClassDeclaration which manages a set of
Expand Down
11 changes: 10 additions & 1 deletion packages/concerto-core/lib/introspect/classdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ const Introspector = require('./introspector');
const ModelUtil = require('../modelutil');
const RelationshipDeclaration = require('./relationshipdeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
const Property = require('./property');
}
/* eslint-enable no-unused-vars */

/**
* ClassDeclaration defines the structure (model/schema) of composite data.
* It is composed of a set of Properties, may have an identifying field, and may
Expand Down Expand Up @@ -184,7 +193,7 @@ class ClassDeclaration extends Decorated {
* contents/relations of fields.
*
* @throws {IllegalModelException}
* @private
* @protected
*/
validate() {
super.validate();
Expand Down
8 changes: 8 additions & 0 deletions packages/concerto-core/lib/introspect/conceptdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

const ClassDeclaration = require('./classdeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/**
* ConceptDeclaration defines the schema (aka model or class) for
* an Concept. It extends ClassDeclaration which manages a set of
Expand Down
13 changes: 11 additions & 2 deletions packages/concerto-core/lib/introspect/decorated.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
const Decorator = require('./decorator');
const IllegalModelException = require('./illegalmodelexception');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/**
* Decorated defines a model element that may have decorators attached.
*
Expand Down Expand Up @@ -99,10 +107,11 @@ class Decorated {
* override this method to impose additional semantic constraints on the
* contents/relations of fields.
*
* @param {...*} args the validation arguments
* @throws {IllegalModelException}
* @private
* @protected
*/
validate() {
validate(...args) {
for(let n=0; n < this.decorators.length; n++) {
let decorator = this.decorators[n];
decorator.validate();
Expand Down
9 changes: 9 additions & 0 deletions packages/concerto-core/lib/introspect/decorator.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

'use strict';

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ClassDeclaration = require('./classdeclaration');
const Property = require('./property');
}
/* eslint-enable no-unused-vars */

/**
* Decorator encapsulates a decorator (annotation) on a class or property.
* @class
Expand Down
10 changes: 10 additions & 0 deletions packages/concerto-core/lib/introspect/decoratorfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

'use strict';

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ClassDeclaration = require('./classdeclaration');
const Decorator = require('./decorator');
const Property = require('./property');
}
/* eslint-enable no-unused-vars */

/**
* An interface for a class that processes a decorator and returns a specific
* implementation class for that decorator.
Expand Down
8 changes: 8 additions & 0 deletions packages/concerto-core/lib/introspect/enumdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

const ClassDeclaration = require('./classdeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/**
* EnumDeclaration defines an enumeration of static values.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/concerto-core/lib/introspect/enumvaluedeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

const Property = require('./property');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ClassDeclaration = require('./classdeclaration');
}
/* eslint-enable no-unused-vars */

/**
* Class representing a value from a set of enumerated values
*
Expand Down
8 changes: 8 additions & 0 deletions packages/concerto-core/lib/introspect/eventdeclaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

const IdentifiedDeclaration = require('./identifieddeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/** Class representing the definition of an Event.
* @extends ClassDeclaration
* @see See {@link ClassDeclaration}
Expand Down
8 changes: 8 additions & 0 deletions packages/concerto-core/lib/introspect/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const Property = require('./property');
const NumberValidator = require('./numbervalidator');
const StringValidator = require('./stringvalidator');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ClassDeclaration = require('./classdeclaration');
}
/* eslint-enable no-unused-vars */

/**
* Class representing the definition of a Field. A Field is owned
* by a ClassDeclaration and has a name, type and additional metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@

const ClassDeclaration = require('./classdeclaration');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/**
* IdentifiedDeclaration
*
Expand Down
10 changes: 9 additions & 1 deletion packages/concerto-core/lib/introspect/illegalmodelexception.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@

'use strict';

const BaseFileException = require('@accordproject/concerto-util').BaseFileException;
const { BaseFileException } = require('@accordproject/concerto-util');

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ModelFile = require('./modelfile');
}
/* eslint-enable no-unused-vars */

/**
* Exception throws when a composer file is semantically invalid
Expand Down
9 changes: 9 additions & 0 deletions packages/concerto-core/lib/introspect/introspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@

'use strict';

// Types needed for TypeScript generation.
/* eslint-disable no-unused-vars */
/* istanbul ignore next */
if (global === undefined) {
const ClassDeclaration = require('./classdeclaration');
const ModelManager = require('../modelmanager');
}
/* eslint-enable no-unused-vars */

/**
*
* Provides access to the structure of transactions, assets and participants.
Expand Down
2 changes: 1 addition & 1 deletion packages/concerto-core/lib/introspect/metamodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const MetaModelUtil = require('@accordproject/concerto-metamodel').MetaModelUtil;
const { MetaModelUtil } = require('@accordproject/concerto-metamodel');

const ModelManager = require('../modelmanager');
const Factory = require('../factory');
Expand Down
Loading

0 comments on commit 3ec0f66

Please sign in to comment.