From 18932d06e8ca0bc65a2316d5c1becc85e3bd9e00 Mon Sep 17 00:00:00 2001 From: palumbon Date: Sun, 19 Nov 2023 01:35:49 +0100 Subject: [PATCH] Linter --- src/typeSystem/constraintBasedTypeSystem.ts | 8 ++-- src/typeSystem/typeVariables.ts | 47 ++++++++++----------- src/typeSystem/wollokTypes.ts | 40 +++++++++--------- test/typeSystem.test.ts | 6 +-- 4 files changed, 49 insertions(+), 52 deletions(-) diff --git a/src/typeSystem/constraintBasedTypeSystem.ts b/src/typeSystem/constraintBasedTypeSystem.ts index 7ffe25e9..411e8a81 100644 --- a/src/typeSystem/constraintBasedTypeSystem.ts +++ b/src/typeSystem/constraintBasedTypeSystem.ts @@ -1,7 +1,7 @@ import { anyPredicate, is } from '../extensions' import { Environment, Module, Node, Reference } from '../model' import { newTypeVariables, TypeVariable, typeVariableFor } from './typeVariables' -import { ANY, PARAM, RETURN, TypeRegistry, TypeSystemProblem, WollokModuleType, WollokType } from './wollokTypes' +import { PARAM, RETURN, TypeRegistry, TypeSystemProblem, WollokModuleType, WollokType } from './wollokTypes' const { assign } = Object @@ -96,7 +96,7 @@ export const bindReceivedMessages = (tVar: TypeVariable): boolean => { methodInstance.atParam(RETURN).addSupertype(typeVariableFor(send)) logger.log(`NEW SUPERTYPE |${typeVariableFor(send)}| for |${methodInstance.atParam(RETURN)}|`) method.parameters.forEach((_param, i) => { - const argTVAR= typeVariableFor(send.args[i]) + const argTVAR = typeVariableFor(send.args[i]) methodInstance.atParam(`${PARAM}${i}`).addSubtype(argTVAR) logger.log(`NEW SUBTYPE |${argTVAR}| for |${methodInstance.atParam(`${PARAM}${i}`)}|`) }) @@ -161,12 +161,12 @@ export const mergeSuperAndSubTypes = (tVar: TypeVariable): boolean => { export const closeTypes = (tVar: TypeVariable): boolean => { // if(tVar.syntetic) return false let changed = false - if(tVar.allMaxTypes().length === 0 && tVar.allMinTypes().length > 0 && tVar.supertypes.length === 0) { + if (tVar.allMaxTypes().length === 0 && tVar.allMinTypes().length > 0 && tVar.supertypes.length === 0) { tVar.typeInfo.maxTypes = tVar.allMinTypes() logger.log(`MAX TYPES FROM MIN FOR |${tVar}|`) changed = true } - if(tVar.allMinTypes().length === 0 && tVar.allMaxTypes().length > 0 && tVar.subtypes.length === 0) { + if (tVar.allMinTypes().length === 0 && tVar.allMaxTypes().length > 0 && tVar.subtypes.length === 0) { tVar.typeInfo.minTypes = tVar.allMaxTypes() logger.log(`MIN TYPES FROM MAX FOR |${tVar}|`) changed = true diff --git a/src/typeSystem/typeVariables.ts b/src/typeSystem/typeVariables.ts index 304c7aa4..0cef72f6 100644 --- a/src/typeSystem/typeVariables.ts +++ b/src/typeSystem/typeVariables.ts @@ -265,95 +265,94 @@ export class TypeVariable { } instanceFor(instance: TypeVariable, send?: TypeVariable, name?: string): TypeVariable { return this.type().instanceFor(instance, send, name) || this } - hasType(type: WollokType) { return this.allPossibleTypes().some(_type => _type.contains(type)) } + hasType(type: WollokType): boolean { return this.allPossibleTypes().some(_type => _type.contains(type)) } - setType(type: WollokType, closed?: boolean) { + setType(type: WollokType, closed?: boolean): this { this.typeInfo.setType(type, closed) return this } - addMinType(type: WollokType) { + addMinType(type: WollokType): void { this.typeInfo.addMinType(type) } - addMaxType(type: WollokType) { + addMaxType(type: WollokType): void { this.typeInfo.addMaxType(type) } - beSubtypeOf(tVar: TypeVariable) { + beSubtypeOf(tVar: TypeVariable): this { this.addSupertype(tVar) tVar.addSubtype(this) return this } - beSupertypeOf(tVar: TypeVariable) { + beSupertypeOf(tVar: TypeVariable): this { this.addSubtype(tVar) tVar.addSupertype(this) return this } - unify(tVar: TypeVariable) { + unify(tVar: TypeVariable): void { // Unification means same type, so min and max types should be propagated in both directions this.beSupertypeOf(tVar) this.beSubtypeOf(tVar) } - hasSubtype(tVar: TypeVariable) { + hasSubtype(tVar: TypeVariable): boolean { return this.subtypes.includes(tVar) } - hasSupertype(tVar: TypeVariable) { + hasSupertype(tVar: TypeVariable): boolean { return this.supertypes.includes(tVar) } - addSend(send: Send) { + addSend(send: Send): void { this.messages.push(send) } - allMinTypes() { + allMinTypes(): WollokType[] { return this.typeInfo.minTypes } - allMaxTypes() { + allMaxTypes(): WollokType[] { return this.typeInfo.maxTypes } - allPossibleTypes() { + allPossibleTypes(): WollokType[] { return [...this.allMinTypes(), ...this.allMaxTypes()] } - hasTypeInfered() { + hasTypeInfered(): boolean { return this.allPossibleTypes().some(t => t.isComplete) } - - validSubtypes() { + validSubtypes(): TypeVariable[] { return this.subtypes.filter(tVar => !tVar.hasProblems) } - validSupertypes() { + validSupertypes(): TypeVariable[] { return this.supertypes.filter(tVar => !tVar.hasProblems) } - addSubtype(tVar: TypeVariable) { + addSubtype(tVar: TypeVariable): void { this.subtypes.push(tVar) } - addSupertype(tVar: TypeVariable) { + addSupertype(tVar: TypeVariable): void { this.supertypes.push(tVar) } - addProblem(problem: TypeSystemProblem) { + addProblem(problem: TypeSystemProblem): void { assign(this.node, { problems: [...this.node.problems ?? [], problem] }) this.hasProblems = true } - beSyntetic() { + beSyntetic(): this { this.syntetic = true return this } - get closed() { return this.typeInfo.closed } + get closed(): boolean { return this.typeInfo.closed } - toString() { return `TVar(${this.syntetic ? 'SYNTEC' + this.node?.sourceInfo : this.node})` } + toString(): string { return `TVar(${this.syntetic ? 'SYNTEC' + this.node?.sourceInfo : this.node})` } } class TypeInfo { @@ -371,7 +370,7 @@ class TypeInfo { throw new Error('Halt') } - setType(type: WollokType, closed: boolean = true) { + setType(type: WollokType, closed = true) { this.minTypes = [type] this.maxTypes = [type] this.closed = closed diff --git a/src/typeSystem/wollokTypes.ts b/src/typeSystem/wollokTypes.ts index bb982922..ebd3c60f 100644 --- a/src/typeSystem/wollokTypes.ts +++ b/src/typeSystem/wollokTypes.ts @@ -1,5 +1,5 @@ import { List } from '../extensions' -import { BaseProblem, Level, Module, Name, Node, Singleton } from '../model' +import { BaseProblem, Level, Method, Module, Name, Node, Singleton } from '../model' import { TypeVariable } from './typeVariables' const { entries, fromEntries } = Object @@ -28,7 +28,7 @@ export class WollokAtomicType { this.id = id } - lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }) { + lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method { throw new Error('Atomic types has no methods') } @@ -39,9 +39,9 @@ export class WollokAtomicType { return type instanceof WollokAtomicType && this.id === type.id } - asList() { return [this] } + asList(): WollokType[] { return [this] } - isSubtypeOf(_type: WollokType) { return false } + isSubtypeOf(_type: WollokType): boolean { return false } get name(): string { return this.id } get kind(): string { return this.name } @@ -56,27 +56,27 @@ export class WollokModuleType { this.module = module } - lookupMethod(name: Name, arity: number, options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }) { + lookupMethod(name: Name, arity: number, options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method | undefined { return this.module.lookupMethod(name, arity, options) } contains(type: WollokType): boolean { return type instanceof WollokModuleType && (this.module === type.module || - (this.module instanceof Singleton && type.module instanceof Singleton - && this.module.isClosure() && type.module.isClosure())) + this.module instanceof Singleton && type.module instanceof Singleton + && this.module.isClosure() && type.module.isClosure()) } atParam(_name: string): TypeVariable { throw new Error('Module types has no params') } instanceFor(_instance: TypeVariable, _send?: TypeVariable): TypeVariable | null { return null } - asList() { return [this] } + asList(): WollokType[] { return [this] } - isSubtypeOf(type: WollokType) { + isSubtypeOf(type: WollokType): boolean { return type instanceof WollokModuleType && this.module !== type.module && (type.module.name === 'Object' || this.module.inherits(type.module)) } - get name(): string { return this.module?.name! } + get name(): string { return this.module.name! } get kind(): string { return this.module?.name ?? 'null' } get isComplete(): boolean { return true } @@ -117,14 +117,14 @@ export class WollokParametricType extends WollokModuleType { return instance.newInstance(name).setType(new WollokParametricType(this.module, resolvedParamTypes), false) } - addMinType(minType: WollokParametricType) { + addMinType(minType: WollokParametricType): void { this.params.forEach((paramTVar, name) => minType.atParam(name).allPossibleTypes().forEach(paramMinType => paramTVar.addMinType(paramMinType) ) ) } - addMaxType(minType: WollokParametricType) { + addMaxType(minType: WollokParametricType): void { this.params.forEach((paramTVar, name) => minType.atParam(name).allPossibleTypes().forEach(paramMaxType => paramTVar.addMaxType(paramMaxType) @@ -146,7 +146,7 @@ export class WollokParametricType extends WollokModuleType { return [...this.params.values()].every((tVar) => tVar.hasTypeInfered()) } - sameParams(type: WollokParametricType) { + sameParams(type: WollokParametricType): boolean { return [...this.params.entries()].every(([name, tVar]) => type.atParam(name).type().name == ANY || tVar.type().contains(type.atParam(name).type())) } @@ -158,7 +158,7 @@ export class WollokMethodType extends WollokParametricType { super(base!, { ...fromEntries(params.map((p, i) => [`${PARAM}${i}`, p])), [RETURN]: returnVar, - ...extra + ...extra, }) } @@ -194,7 +194,7 @@ export class WollokParameterType { return instance.atParam(this.name) || send?.newInstance(this.name) } - lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }) { + lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method { throw new Error('Parameters types has no methods') } @@ -207,9 +207,9 @@ export class WollokParameterType { throw new Error('Parameters types does not contains other types') } - asList() { return [this] } + asList(): WollokType[] { return [this] } - isSubtypeOf(_type: WollokType) { + isSubtypeOf(_type: WollokType): boolean { throw new Error('Parameters types cannot be subtype of other types (invariant)') } @@ -226,18 +226,18 @@ export class WollokUnionType { this.types = types } - lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }) { + lookupMethod(_name: Name, _arity: number, _options?: { lookupStartFQN?: Name, allowAbstractMethods?: boolean }): Method { throw new Error('Halt') } atParam(_name: string): TypeVariable { throw new Error('Union types has no params') } - instanceFor(_instance: TypeVariable) { return null } + instanceFor(_instance: TypeVariable): TypeVariable | null { return null } contains(type: WollokType): boolean { return type.asList().every(t => this.types.some(_ => _.contains(t))) } - asList() { return this.types } + asList(): WollokType[] { return this.types } isSubtypeOf(type: WollokType): boolean { return this.types.every(t => t.isSubtypeOf(type)) } diff --git a/test/typeSystem.test.ts b/test/typeSystem.test.ts index 7a91fa5a..ddb8051f 100644 --- a/test/typeSystem.test.ts +++ b/test/typeSystem.test.ts @@ -247,7 +247,7 @@ describe('Wollok Type System', () => { parametricType =new WollokParametricType(module, { 'param1': newSynteticTVar(), 'param2': newSynteticTVar().setType(otherStubType), - 'param3': newSynteticTVar() + 'param3': newSynteticTVar(), }) const param1 = newSynteticTVar().setType(stubType) const param2 = newSynteticTVar() @@ -328,8 +328,6 @@ describe('Wollok Type System', () => { }) - - const env = new Environment({ members: [] }) @@ -364,4 +362,4 @@ const testMethod = newMethod('TEST_METHOD') const otherTestMethod = newMethod('OTHER_TEST_METHOD') const stubType = new TestWollokType('TEST_TYPE', testMethod) -const otherStubType = new TestWollokType('OTHER_TEST_TYPE', otherTestMethod) +const otherStubType = new TestWollokType('OTHER_TEST_TYPE', otherTestMethod) \ No newline at end of file