Skip to content

Validity Checker

Daryl Tan edited this page Mar 5, 2020 · 7 revisions

Specifications

Definition: Simple declarations are:

  • function declarations and
  • let and const declarations whose right hand side have no function calls.

The purpose of the validity checker is to:

  • Reject any return statements outside of function definitions and declarations.
  • Reject any return statements inside of while and for loops (that are not in any function definitions and declarations in that loop).
  • Reject any assignments to the loop control variable in for loops.
  • Reject any assignments to names declared with const or function.
  • Annotate any function declaration in a statement sequence as typable, if there are only simple declarations before it in the sequence.
  • Annotate any const or let declaration in a statement sequence as typable, if its name does not occur in its own right-hand side or in any statement before it in the sequence.

Usage

Use validateAndAnnotate from src/validator/validator.ts.

Input: an estree Node and a Context.

Output: a TypeAnnotatedNode<Node>, where all VariableDeclarations and FunctionDeclarations have an extra property typability on them.

This can take four values:

  • undefined (non variable/function declaration nodes will not have the typability property)
  • "Untypable" (variable/function declarations who do not match the above criteria)
  • "NotYetTyped" (variable/function declarations who do match the above criteria, but inference is not performed yet)
  • "Typed" (after inference is performed, set typability to this, and inferredType to the type inferred)

If a TypeAnnotatedNode<Node> has typability "Typed", it should also have a inferredType property with value of type Type.

Type is defined below:

export type Type = Primitive | Variable | FunctionType | List

export interface Primitive {
  kind: 'primitive'
  name: 'number' | 'boolean' | 'string' | 'null' | 'integer' | 'undefined'
}

export interface Variable {
  kind: 'variable'
  name: string
}

// cannot name Function, conflicts with TS
export interface FunctionType {
  kind: 'function'
  parameterTypes: Type[]
  returnType: Type
}

export interface List {
  kind: 'list'
  elementType: Type
}