From 0b99c741a092afde945c2f01d78ddb0d9879e55e Mon Sep 17 00:00:00 2001 From: michael-whi Date: Thu, 8 Aug 2019 22:46:00 +0300 Subject: [PATCH] [ecore] - Add types definition. (#37375) * Added ecore types. * Little fix. * Little marks. * Fixed default export. * Import/Export marks. * Fixed default export. * Marks. * Test. * Test. --- types/ecore/ecore-tests.ts | 70 ++++++++++++++++++ types/ecore/index.d.ts | 147 +++++++++++++++++++++++++++++++++++++ types/ecore/tsconfig.json | 23 ++++++ types/ecore/tslint.json | 1 + 4 files changed, 241 insertions(+) create mode 100644 types/ecore/ecore-tests.ts create mode 100644 types/ecore/index.d.ts create mode 100644 types/ecore/tsconfig.json create mode 100644 types/ecore/tslint.json diff --git a/types/ecore/ecore-tests.ts b/types/ecore/ecore-tests.ts new file mode 100644 index 0000000000..69ffcb85fa --- /dev/null +++ b/types/ecore/ecore-tests.ts @@ -0,0 +1,70 @@ +import Ecore = require('ecore'); +// Main test + +// Resources contain model elements and are identified by a URI. + +const resourceSet: Ecore.ResourceSet = Ecore.ResourceSet.create(); +const resource: Ecore.Resource = resourceSet.create({ uri: 'model.json' }); + +// EClass are used to define domain elements, they are identified +// by name and a set of structural features (attributes and references). + +const User: Ecore.EObject = Ecore.EClass.create({ + name: 'User', + eStructuralFeatures: [ + // EAttributes are used to define domain elements + // elements properties. + Ecore.EAttribute.create({ + name: 'name', + upperBound: 1, + + eType: Ecore.EString, + }), + // EReference are used to define links between domain + // elements. + Ecore.EReference.create({ + name: 'friends', + upperBound: -1, + containment: false, + + eType() { + return User; + }, + }), + ], +}); + +// EPackages represent namespaces for a set of EClasses. +// It's properties name, nsURI and nsPrefix must be set. + +const SamplePackage: Ecore.EObject = Ecore.EPackage.create({ + name: 'sample', + nsURI: 'http://www.example.org/sample', + nsPrefix: 'sample', + + eClassifiers: [User], +}); + +// Packages must be added directly to the model's Resource. + +resource.add(SamplePackage); + +// Additional tests + +// EList find + +resource.eClass.get('AllStructuralFeatures').find((eObj: Ecore.EObject) => eObj.get('name') === ''); + +// Resource + +resource.getEObject('test'); + +resource.parse(User, () => {}); + +resource.to(); + +// Test interface definition +export interface SampleDef { + name: string; + structuralFeatures: Ecore.EStructuralFeature[]; +} diff --git a/types/ecore/index.d.ts b/types/ecore/index.d.ts new file mode 100644 index 0000000000..452dd80f07 --- /dev/null +++ b/types/ecore/index.d.ts @@ -0,0 +1,147 @@ +// Type definitions for ecore 0.12 +// Project: https://github.com/emfjson/ecore.js +// Definitions by: Mike Tugushev +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export function create(eClass: EClass): EObject; + +export let EClass: EClass; +export let Edit: Edit; +export let EObject: EObject; +export let EList: EList; +export let EString: EString; +export let EInt: EInt; +export let EBoolean: EBoolean; +export let EDouble: EDouble; +export let EDate: EDate; +export let EIntegerObject: EIntegerObject; +export let EFloatObject: EFloatObject; +export let ELongObject: ELongObject; +export let EMap: EMap; +export let EDiagnosticChain: EDiagnosticChain; +export let JSObject: JSObject; +export let EModelElement: EModelElement; +export let EAnnotation: EAnnotation; +export let ENamedElement: ENamedElement; +export let EClassifier: EClassifier; +export let ETypedElement: ETypedElement; +export let EDataType: EDataType; +export let EStructuralFeature: EStructuralFeature; +export let EAttribute: EAttribute; +export let EReference: EReference; +export let EOperation: EOperation; +export let EParameter: EParameter; +export let EEnum: EEnum; +export let EEnumLiteral: EEnumLiteral; +export let EGenericType: EGenericType; +export let ETypeParameter: ETypeParameter; +export let Resource: Resource; +export let EPackageRegistry: EPackageRegistry; +export let EPackage: EPackage; + +export interface Edit { + childTypes: (object: EObject, createDescriptor: any) => EObject[]; + siblingTypes: (object: EObject, createDescriptor: any) => EObject[]; + childDescriptors: (object: EObject) => EObject[]; + siblingDescriptors: (object: EObject) => EObject[]; + choiceOfValues: (owner: EObject, feature: EObject) => EObject[]; +} +export interface EObject { + setEClass: (eClass: EClass) => void; + create: (attributes: any) => EObject; + has: (name: string) => boolean; + isSet: (name: string) => boolean; + set: (attrs: any, options: any) => EObject; + unset: (attrs: any, options: any) => EObject; + get: (feature: string) => any; + getEObject: (uri: string) => EObject; + isTypeOf: (type: string | EObject) => any; + isKindOf: (type: string | EObject) => any; + eResource: () => Resource; + eContent: () => EObject[]; + eURI: () => string; + fragment: () => string; + eClass: EClass; + eContents: () => EObject[]; + eContainer: EObject; + _id: string; + getEStructuralFeature: (feature: string | EObject) => any; +} +export interface EList extends EObject { + add: (eObject: EObject) => EList; + addAll: (args: EObject[] | EObject) => EList; + remove: (eObject: EObject) => EList; + size: () => number; + at: (position: number) => EObject; + array: () => EObject[]; + first: () => EObject; + find: (iterator: (value: any, key: any, list: EList) => boolean, context?: any) => EObject[]; + last: () => EObject; + rest: (position: number) => EObject[]; + each: (iterator: (value: any, key: any, list: EList) => void, context?: any) => void; + filter: (iterator: (value: any, key: any, list: EList) => boolean, context?: any) => EObject[]; + map: (iterator: (value: any, key: any, list: EList) => any, context?: any) => any[]; + reject: (iterator: (value: any, key: any, list: EList) => boolean, context?: any) => EObject[]; + contains: (eObject: EObject) => boolean; + indexOf: (eObject: EObject) => number; +} +export type EString = EObject; +export type EInt = EObject; +export type EBoolean = EObject; +export type EDouble = EObject; +export type EDate = EObject; +export type EIntegerObject = EObject; +export type EFloatObject = EObject; +export type ELongObject = EObject; +export type EMap = EObject; +export type EDiagnosticChain = EObject; +export type JSObject = EObject; +export type EModelElement = EObject; +export type EAnnotation = EModelElement; +export type ENamedElement = EModelElement; +export type EClassifier = ENamedElement; +export type EClass = EClassifier; +export type EDataType = EClassifier; +export type ETypedElement = ENamedElement; +export type EStructuralFeature = ETypedElement; +export type EAttribute = EStructuralFeature; +export type EReference = EStructuralFeature; +export type EOperation = ETypedElement; +export type EParameter = ETypedElement; +export type EEnum = EDataType; +export type EEnumLiteral = ENamedElement; +export type EGenericType = EObject; +export type ETypeParameter = ENamedElement; +export interface Resource extends EObject { + add: (value: EObject) => void; + addAll: (values: EObject[]) => EObject; + clear: () => EList; + each: (iterator: (value: any, key: any, list: EList) => void, context?: any) => void; + save: (callback: () => void, options: any) => void; + parse: (data: EObject, loader: () => void) => any; + remove: () => void; + rev: string; + load: (res: any) => void; + to: () => any; +} +export interface EPackage extends EObject { + getEPackage: (nsURI: string) => EPackage; + register: (ePackage: EPackage) => void; + ePackages: () => EPackage[]; + elements: (type: EObject) => EObject[]; + Registry: EPackageRegistry; +} +export interface EPackageRegistry extends EObject { + register: (ePackage: EPackage) => void; + ePackages: () => EPackage[]; +} +export interface ResourceSet extends EObject { + create: (uri: any) => Resource; + getEObject: (uri: string) => EObject; + parse: (data: EObject) => void; + elements: (type?: string | EClass) => EObject[]; + toJSON: () => any; +} +export namespace ResourceSet { + function create(): ResourceSet; +} diff --git a/types/ecore/tsconfig.json b/types/ecore/tsconfig.json new file mode 100644 index 0000000000..ea0cf3425b --- /dev/null +++ b/types/ecore/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ecore-tests.ts" + ] +} \ No newline at end of file diff --git a/types/ecore/tslint.json b/types/ecore/tslint.json new file mode 100644 index 0000000000..2750cc0197 --- /dev/null +++ b/types/ecore/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } \ No newline at end of file