Skip to content

Commit

Permalink
Fix #151 should not initialize const in program
Browse files Browse the repository at this point in the history
  • Loading branch information
fdodino committed Nov 29, 2023
1 parent 37dc198 commit 8bfa910
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
5 changes: 1 addition & 4 deletions src/interpreter/runtimeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,18 @@ export const getterMethodShouldReturnAValue = warning<Method>(node =>
export const shouldNotUseReservedWords = warning<Class | Singleton | Variable | Field | Parameter>(node => !usesReservedWords(node))

export const shouldInitializeGlobalReference = error<Variable>(node =>
!(node.isAtPackageLevel && node.value.isSynthetic && node.value.is(Literal) && node.value.isNull())
!(node.isAtPackageLevel && isInitialized(node))
)

export const shouldNotDefineUnusedVariables = warning<Field>(node => !unusedVariable(node))

export const shouldInitializeConst = error<Variable>(node =>
!(
getContainer(node)?.is(Program) &&
node.isConstant &&
isInitialized(node))
)

export const shouldNotDuplicatePackageName = error<Package>(node =>
!node.siblings().some(sibling => sibling.is(Package) && sibling.name == node.name)
)
Expand Down Expand Up @@ -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
// ══════════════════════════════════════════════════════════════════════════════════════════════════════════════════
Expand Down Expand Up @@ -774,7 +786,7 @@ const validationsByKind = (node: Node): Record<string, Validation<any>> => 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 })),
Expand Down

0 comments on commit 8bfa910

Please sign in to comment.