mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 08:33:11 +00:00
feat: add certification thing
This commit is contained in:
@@ -12,7 +12,11 @@
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {isLightweightClass, isLightweightEnum, isUnionType} from '@openstapps/core-tools/lib/easy-ast/ast-util';
|
||||
import {
|
||||
isLightweightClass,
|
||||
isLightweightEnum,
|
||||
isUnionType,
|
||||
} from '@openstapps/core-tools/lib/easy-ast/ast-util';
|
||||
import {LightweightAliasDefinition} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-alias-definition';
|
||||
import {LightweightProjectWithIndex} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-project';
|
||||
import {LightweightType} from '@openstapps/core-tools/lib/easy-ast/types/lightweight-type';
|
||||
@@ -22,7 +26,7 @@ import {LightweightProperty} from '@openstapps/core-tools/src/easy-ast/types/lig
|
||||
import {expect} from 'chai';
|
||||
import {assign, chain, clone, flatMap, isNil, reduce, reject, some} from 'lodash';
|
||||
|
||||
process.on('unhandledRejection', (err) => {
|
||||
process.on('unhandledRejection', err => {
|
||||
throw err;
|
||||
});
|
||||
|
||||
@@ -47,28 +51,27 @@ describe('Features', () => {
|
||||
referenceName: 'SCDiff',
|
||||
});
|
||||
|
||||
expect(thingsReflection.type?.specificationTypes?.every(it => typeof it.referenceName !== 'undefined')).to.be.true;
|
||||
expect(
|
||||
thingsReflection.type?.specificationTypes?.every(it => typeof it.referenceName !== 'undefined'),
|
||||
).to.be.true;
|
||||
thingNames = thingsReflection.type?.specificationTypes?.map(type => type.referenceName!) ?? [];
|
||||
things = thingNames
|
||||
.map(it => project.definitions[it])
|
||||
.filter(isLightweightClass);
|
||||
things = thingNames.map(it => project.definitions[it]).filter(isLightweightClass);
|
||||
thingsWithoutReferences = thingNames
|
||||
.map(it => project.definitions[`${it}WithoutReferences`])
|
||||
.filter(isLightweightClass);
|
||||
});
|
||||
|
||||
const inheritedProperties = function (classLike: LightweightClassDefinition):
|
||||
Record<string, LightweightProperty> | undefined {
|
||||
const inheritedProperties = function (
|
||||
classLike: LightweightClassDefinition,
|
||||
): Record<string, LightweightProperty> | undefined {
|
||||
return reduce(
|
||||
[...(classLike.implementedDefinitions ?? []), ...(classLike.extendedDefinitions ?? [])],
|
||||
(obj, extension) => {
|
||||
const object = project.definitions[extension.referenceName ?? ''];
|
||||
|
||||
return assign(obj, isLightweightClass(object)
|
||||
? inheritedProperties(object)
|
||||
: obj);
|
||||
return assign(obj, isLightweightClass(object) ? inheritedProperties(object) : obj);
|
||||
},
|
||||
clone(classLike.properties)
|
||||
clone(classLike.properties),
|
||||
);
|
||||
};
|
||||
|
||||
@@ -79,33 +82,45 @@ describe('Features', () => {
|
||||
});
|
||||
|
||||
it('should not have duplicate names', () => {
|
||||
reduce(project.files, (fileResult, file) =>
|
||||
reduce(file, (definitionResult, definition: LightweightDefinition) => {
|
||||
expect(definitionResult[definition.name]).to.be.undefined;
|
||||
definitionResult[definition.name] = true; // something that's not undefined
|
||||
reduce(
|
||||
project.files,
|
||||
(fileResult, file) =>
|
||||
reduce(
|
||||
file,
|
||||
(definitionResult, definition: LightweightDefinition) => {
|
||||
expect(definitionResult[definition.name]).to.be.undefined;
|
||||
definitionResult[definition.name] = true; // something that's not undefined
|
||||
|
||||
return definitionResult;
|
||||
}, fileResult), {} as Record<string, true>);
|
||||
return definitionResult;
|
||||
},
|
||||
fileResult,
|
||||
),
|
||||
{} as Record<string, true>,
|
||||
);
|
||||
});
|
||||
|
||||
it('should not have properties referencing SCThing', () => {
|
||||
const allPropertyReferenceNames: (property: LightweightProperty) => string[] = property => reject([
|
||||
property.type.referenceName!,
|
||||
...flatMap(property.properties, allPropertyReferenceNames),
|
||||
], isNil);
|
||||
const allPropertyReferenceNames: (property: LightweightProperty) => string[] = property =>
|
||||
reject(
|
||||
[property.type.referenceName!, ...flatMap(property.properties, allPropertyReferenceNames)],
|
||||
isNil,
|
||||
);
|
||||
|
||||
const typeHasSCThingReferences: (type?: LightweightType) => boolean = type => type?.referenceName
|
||||
? hasSCThingReferences(project.definitions[type.referenceName])
|
||||
: some(type?.specificationTypes, typeHasSCThingReferences);
|
||||
const typeHasSCThingReferences: (type?: LightweightType) => boolean = type =>
|
||||
type?.referenceName
|
||||
? hasSCThingReferences(project.definitions[type.referenceName])
|
||||
: some(type?.specificationTypes, typeHasSCThingReferences);
|
||||
|
||||
const hasSCThingReferences: (definition?: LightweightDefinition) => boolean = definition =>
|
||||
isLightweightClass(definition)
|
||||
? chain(inheritedProperties(definition))
|
||||
.flatMap(it => flatMap(it.properties, allPropertyReferenceNames))
|
||||
.map(it => project.definitions[it] as LightweightDefinition)
|
||||
.some(it => it.name === 'SCThing' || hasSCThingReferences(it))
|
||||
.value()
|
||||
: definition ? typeHasSCThingReferences(definition.type) : false;
|
||||
.flatMap(it => flatMap(it.properties, allPropertyReferenceNames))
|
||||
.map(it => project.definitions[it] as LightweightDefinition)
|
||||
.some(it => it.name === 'SCThing' || hasSCThingReferences(it))
|
||||
.value()
|
||||
: definition
|
||||
? typeHasSCThingReferences(definition.type)
|
||||
: false;
|
||||
|
||||
for (const thing of things) {
|
||||
expect(hasSCThingReferences(thing)).to.be.false;
|
||||
@@ -115,13 +130,13 @@ describe('Features', () => {
|
||||
function extendsSCThing(definition?: LightweightDefinition): boolean {
|
||||
return isLightweightClass(definition)
|
||||
? chain([
|
||||
...(definition as LightweightClassDefinition).extendedDefinitions ?? [],
|
||||
...(definition as LightweightClassDefinition).implementedDefinitions ?? [],
|
||||
])
|
||||
.map(it => it.referenceName)
|
||||
.reject(isNil)
|
||||
.some(it => it === 'SCThing' || extendsSCThing(project.definitions[it!]))
|
||||
.value()
|
||||
...((definition as LightweightClassDefinition).extendedDefinitions ?? []),
|
||||
...((definition as LightweightClassDefinition).implementedDefinitions ?? []),
|
||||
])
|
||||
.map(it => it.referenceName)
|
||||
.reject(isNil)
|
||||
.some(it => it === 'SCThing' || extendsSCThing(project.definitions[it!]))
|
||||
.value()
|
||||
: false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user