typir
Version:
General purpose type checking library
54 lines • 2.22 kB
JavaScript
/******************************************************************************
* Copyright 2024 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { registerInferCurrentTypeRules } from '../../utils/utils-definitions.js';
import { assertTrue } from '../../utils/utils.js';
import { TopType } from './top-type.js';
export const TopKindName = 'TopKind';
export class TopKind {
constructor(services, options) {
this.options = this.collectOptions(options);
this.$name = this.options.$name;
this.services = services;
this.services.infrastructure.Kinds.register(this);
}
collectOptions(options) {
return Object.assign({
// the default values:
$name: TopKindName, name: 'any' }, options);
}
get(typeDetails) {
const key = this.calculateIdentifier(typeDetails);
return this.services.infrastructure.Graph.getType(key);
}
create(typeDetails) {
assertTrue(this.get(typeDetails) === undefined, 'The top type already exists.'); // ensure that the type is not created twice
return new TopConfigurationChainImpl(this.services, this, typeDetails);
}
calculateIdentifier(_typeDetails) {
return this.options.name;
}
}
export function isTopKind(kind) {
return kind instanceof TopKind;
}
class TopConfigurationChainImpl {
constructor(services, kind, typeDetails) {
this.services = services;
this.kind = kind;
this.typeDetails = Object.assign(Object.assign({}, typeDetails), { inferenceRules: [] });
}
inferenceRule(rule) {
this.typeDetails.inferenceRules.push(rule);
return this;
}
finish() {
const topType = new TopType(this.kind, this.kind.calculateIdentifier(this.typeDetails), this.typeDetails);
this.services.infrastructure.Graph.addNode(topType);
registerInferCurrentTypeRules(this.typeDetails.inferenceRules, topType, this.services);
return topType;
}
}
//# sourceMappingURL=top-kind.js.map