moddle
Advanced tools
Comparing version 6.0.0 to 7.0.0-exp.0
@@ -188,4 +188,5 @@ import { forEach, bind, pick, assign, isString, isObject } from 'min-dash'; | ||
* @param {Boolean} [validate=true] | ||
* @param {Boolean} [inherited=false] | ||
*/ | ||
DescriptorBuilder.prototype.addProperty = function(p, idx, validate) { | ||
DescriptorBuilder.prototype.addProperty = function(p, idx, validate, inherited) { | ||
@@ -197,7 +198,7 @@ if (typeof idx === 'boolean') { | ||
this.addNamedProperty(p, validate !== false); | ||
this.addNamedProperty(p, validate !== false, inherited); | ||
var properties = this.properties; | ||
if (idx !== undefined) { | ||
if (typeof idx === 'number') { | ||
properties.splice(idx, 0, p); | ||
@@ -253,6 +254,12 @@ } else { | ||
// | ||
this.addProperty(newProperty, replace ? undefined : idx, rename); | ||
this.addProperty(newProperty, replace ? undefined : idx, rename, oldProperty.inherited); | ||
if (oldProperty.inherited) { | ||
// make new property available under old localName | ||
propertiesByName[oldNameNs.localName] = newProperty; | ||
} | ||
// make new property available under old name | ||
propertiesByName[oldNameNs.name] = propertiesByName[oldNameNs.localName] = newProperty; | ||
propertiesByName[oldNameNs.name] = newProperty; | ||
}; | ||
@@ -279,3 +286,3 @@ | ||
DescriptorBuilder.prototype.addNamedProperty = function(p, validate) { | ||
DescriptorBuilder.prototype.addNamedProperty = function(p, validate, inherited) { | ||
var ns = p.ns, | ||
@@ -285,7 +292,14 @@ propsByName = this.propertiesByName; | ||
if (validate) { | ||
if (inherited) { | ||
this.assertNotDefined(p, ns.localName); | ||
} | ||
this.assertNotDefined(p, ns.name); | ||
this.assertNotDefined(p, ns.localName); | ||
} | ||
propsByName[ns.name] = propsByName[ns.localName] = p; | ||
if (inherited) { | ||
propsByName[ns.localName] = p; | ||
} | ||
propsByName[ns.name] = p; | ||
}; | ||
@@ -323,2 +337,13 @@ | ||
DescriptorBuilder.prototype.assertNotTrait = function(typeDescriptor) { | ||
const _extends = typeDescriptor.extends || []; | ||
if (_extends.length) { | ||
throw new Error( | ||
`cannot create <${ typeDescriptor.name }> extending <${ typeDescriptor.extends }>` | ||
); | ||
} | ||
}; | ||
DescriptorBuilder.prototype.assertNotDefined = function(p, name) { | ||
@@ -342,2 +367,6 @@ var propertyName = p.name, | ||
if (inherited) { | ||
this.assertNotTrait(t); | ||
} | ||
var typesByName = this.allTypesByName, | ||
@@ -356,4 +385,4 @@ types = this.allTypes; | ||
p = assign({}, p, { | ||
name: p.ns.localName, | ||
inherited: inherited | ||
name: inherited ? p.ns.localName : p.ns.name, | ||
inherited | ||
}); | ||
@@ -378,3 +407,4 @@ | ||
} | ||
this.addProperty(p); | ||
this.addProperty(p, null, true, inherited); | ||
} | ||
@@ -508,2 +538,13 @@ }, this)); | ||
/** | ||
* Traverse the selected super type or trait | ||
* | ||
* @param {String} cls | ||
* @param {Boolean} [trait=false] | ||
*/ | ||
function traverse(cls, trait) { | ||
var parentNs = parseName(cls, isBuiltIn(cls) ? '' : nsName.prefix); | ||
self.mapTypes(parentNs, iterator, trait); | ||
} | ||
/** | ||
* Traverse the selected trait. | ||
@@ -514,14 +555,12 @@ * | ||
function traverseTrait(cls) { | ||
return traverseSuper(cls, true); | ||
return traverse(cls, true); | ||
} | ||
/** | ||
* Traverse the selected super type or trait | ||
* Traverse the selected super type | ||
* | ||
* @param {String} cls | ||
* @param {Boolean} [trait=false] | ||
*/ | ||
function traverseSuper(cls, trait) { | ||
var parentNs = parseName(cls, isBuiltIn(cls) ? '' : nsName.prefix); | ||
self.mapTypes(parentNs, iterator, trait); | ||
function traverseSuper(cls) { | ||
return traverse(cls, false); | ||
} | ||
@@ -609,3 +648,3 @@ | ||
var property = this.model.getPropertyDescriptor(target, name); | ||
var property = this.getProperty(target, name); | ||
@@ -621,3 +660,3 @@ var propertyName = property && property.name; | ||
} else { | ||
delete target.$attrs[name]; | ||
delete target.$attrs[stripGlobal(name)]; | ||
} | ||
@@ -635,3 +674,3 @@ } else { | ||
} else { | ||
target.$attrs[name] = value; | ||
target.$attrs[stripGlobal(name)] = value; | ||
} | ||
@@ -651,6 +690,6 @@ } | ||
var property = this.model.getPropertyDescriptor(target, name); | ||
var property = this.getProperty(target, name); | ||
if (!property) { | ||
return target.$attrs[name]; | ||
return target.$attrs[stripGlobal(name)]; | ||
} | ||
@@ -709,3 +748,36 @@ | ||
/** | ||
* Return property with the given name on the element. | ||
* | ||
* @param {any} target | ||
* @param {string} name | ||
* | ||
* @return {object | null} property | ||
*/ | ||
Properties.prototype.getProperty = function(target, name) { | ||
var model = this.model; | ||
var property = model.getPropertyDescriptor(target, name); | ||
if (property) { | ||
return property; | ||
} | ||
if (name.includes(':')) { | ||
return null; | ||
} | ||
const strict = model.config.strict === true; | ||
if (strict) { | ||
throw new TypeError(`unknown property <${ name }> on <${ target.$type }>`); | ||
} | ||
// eslint-disable-next-line no-undef | ||
typeof console !== 'undefined' && console.warn(`unknown property <${ name }> on <${ target.$type }>`); | ||
return null; | ||
}; | ||
function isUndefined(val) { | ||
@@ -724,2 +796,6 @@ return typeof val === 'undefined'; | ||
function stripGlobal(name) { | ||
return name.replace(/^:/, ''); | ||
} | ||
// Moddle implementation ///////////////////////////////////////////////// | ||
@@ -747,4 +823,6 @@ | ||
* @param {Array<Package>} packages the packages to contain | ||
* | ||
* @param { { strict?: boolean } } [config] moddle configuration | ||
*/ | ||
function Moddle(packages) { | ||
function Moddle(packages, config = {}) { | ||
@@ -757,2 +835,4 @@ this.properties = new Properties(this); | ||
this.typeCache = {}; | ||
this.config = config; | ||
} | ||
@@ -759,0 +839,0 @@ |
{ | ||
"name": "moddle", | ||
"version": "6.0.0", | ||
"version": "7.0.0-exp.0", | ||
"description": "A library for importing meta-model based file formats into JS", | ||
@@ -48,6 +48,6 @@ "scripts": { | ||
"@rollup/plugin-node-resolve": "^14.1.0", | ||
"chai": "^4.3.6", | ||
"chai": "^4.3.7", | ||
"eslint": "^8.24.0", | ||
"eslint-plugin-bpmn-io": "^0.16.0", | ||
"mocha": "^10.0.0", | ||
"mocha": "^10.2.0", | ||
"npm-run-all": "^4.1.2", | ||
@@ -54,0 +54,0 @@ "rollup": "^2.79.1", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
274409
4072
1