Skip to content

Erector Module Definition

Matthew Dzaman edited this page Oct 18, 2019 · 2 revisions

Goal

To export erector as a template tag with properties that provide access to all helper methods, as well as export each helper method individually.

import * as module from './lib/erector';

// module.erector is a template tag
// module[helper_name] is a helper method (if, cmp, and, or, etc.)
// module.erector[helper_name] is a helper method

Also keep types functional and minimize boilerplate.

Decision

Use function and namespace merging.

Options

Interface

  • define Erector class
  • create IErector interface as function and properties
    • e.g. { (strings, exps): Statement; raw: Raw... }
    • deal breaker: IErector interface must repeat function signatures from Erector class
    • this could also be done without an interface, which would make erector an instance of a union type, which would be nice to avoid
  • assign erector: IErector using Object.assign(fn, properties)
    • set properties to Erector.*
  • export assignment of erector.* to const * in the main module

Namespace

  • functions defined in erector namespace as constants
  • erector function defined as a template tag function
  • re-exported with assignment of erector.* to const * in main module
  • pros
    • only one signature/definition per function and one extra assignment
    • erector.* functions have this
  • cons
    • erector.if cannot be defined *
      • cannot defined if on erector in namespace
      • cannot export _if as if from namespace
      • * - can modify erector namespace in main module
Clone this wiki locally