From 8bfa9104c94e4c49961279ec3851ae691e673ddf Mon Sep 17 00:00:00 2001 From: Fernando Dodino Date: Sun, 19 Nov 2023 19:55:18 -0300 Subject: [PATCH] Fix #151 should not initialize const in program --- src/interpreter/runtimeModel.ts | 5 +---- src/validator.ts | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/interpreter/runtimeModel.ts b/src/interpreter/runtimeModel.ts index 8d7245ba..ecd6114f 100644 --- a/src/interpreter/runtimeModel.ts +++ b/src/interpreter/runtimeModel.ts @@ -487,11 +487,8 @@ export class Evaluation { yield node const target = node.instantiated.target ?? raise(new Error(`Could not resolve reference to instantiated module ${node.instantiated.name}`)) - const name = node.instantiated.name - - if (!target.is(Class)) raise(new Error(`${name} is not a class, you cannot generate instances of a ${target?.kind}`)) - + if (!target.is(Class)) raise(new Error(`${name} is not a class, you cannot generate instances of it`)) if (target.isAbstract) raise(new Error(`${name} is an abstract class, you cannot generate instances`)) return yield* this.instantiate(target, args) diff --git a/src/validator.ts b/src/validator.ts index 3d9c1302..8506046f 100644 --- a/src/validator.ts +++ b/src/validator.ts @@ -436,11 +436,18 @@ export const getterMethodShouldReturnAValue = warning(node => export const shouldNotUseReservedWords = warning(node => !usesReservedWords(node)) export const shouldInitializeGlobalReference = error(node => - !(node.isAtPackageLevel && node.value.isSynthetic && node.value.is(Literal) && node.value.isNull()) + !(node.isAtPackageLevel && isInitialized(node)) ) export const shouldNotDefineUnusedVariables = warning(node => !unusedVariable(node)) +export const shouldInitializeConst = error(node => + !( + getContainer(node)?.is(Program) && + node.isConstant && + isInitialized(node)) +) + export const shouldNotDuplicatePackageName = error(node => !node.siblings().some(sibling => sibling.is(Package) && sibling.name == node.name) ) @@ -745,6 +752,11 @@ const methodExists = (node: Send): boolean => match(node.receiver)( when(Node)(() => true), ) +const isInitialized = (node: Variable) => + node.value.isSynthetic && + node.value.is(Literal) && + node.value.isNull() + // ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════ // REPORT HELPERS // ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════ @@ -774,7 +786,7 @@ const validationsByKind = (node: Node): Record> => match when(Mixin)(() => ({ nameShouldBeginWithUppercase, shouldNotHaveLoopInHierarchy, shouldOnlyInheritFromMixin, shouldNotDuplicateGlobalDefinitions, shouldNotDuplicateVariablesInLinearization, shouldNotDuplicateEntities })), when(Field)(() => ({ nameShouldBeginWithLowercase, shouldNotAssignToItselfInDeclaration, nameShouldNotBeKeyword, shouldNotDuplicateFields, shouldNotUseReservedWords, shouldNotDefineUnusedVariables, shouldDefineConstInsteadOfVar, shouldInitializeSingletonAttribute })), when(Method)(() => ({ onlyLastParameterCanBeVarArg, nameShouldNotBeKeyword, methodShouldHaveDifferentSignature, shouldNotOnlyCallToSuper, shouldUseOverrideKeyword, possiblyReturningBlock, shouldNotUseOverride, shouldMatchSuperclassReturnValue, shouldNotDefineNativeMethodsOnUnnamedSingleton, overridingMethodShouldHaveABody, getterMethodShouldReturnAValue, shouldHaveBody })), - when(Variable)(() => ({ nameShouldBeginWithLowercase, nameShouldNotBeKeyword, shouldNotAssignToItselfInDeclaration, shouldNotDuplicateLocalVariables, shouldNotDuplicateGlobalDefinitions, shouldNotDefineGlobalMutableVariables, shouldNotUseReservedWords, shouldInitializeGlobalReference, shouldDefineConstInsteadOfVar, shouldNotDuplicateEntities })), + when(Variable)(() => ({ nameShouldBeginWithLowercase, nameShouldNotBeKeyword, shouldNotAssignToItselfInDeclaration, shouldNotDuplicateLocalVariables, shouldNotDuplicateGlobalDefinitions, shouldNotDefineGlobalMutableVariables, shouldNotUseReservedWords, shouldInitializeGlobalReference, shouldDefineConstInsteadOfVar, shouldNotDuplicateEntities, shouldInitializeConst })), when(Assignment)(() => ({ shouldNotAssignToItself, shouldNotReassignConst })), when(Reference)(() => ({ missingReference, shouldUseSelfAndNotSingletonReference })), when(Self)(() => ({ shouldNotUseSelf })),