Skip to content
Compare
Choose a tag to compare

v3.1: experimental alternative interface for child validation

@fredemmott fredemmott released this
83e81a3
Compare
Choose a tag to compare

We are hoping to remove child validation as a language-level feature, replacing it with a new implementation
built on top of more general-purpose language features. This release adds such an alternative, though we do not yet consider it a stable part of the XHP-Lib API.

The current syntax is rarely used, and inconsistent with the rest of the language - both in terms of syntax and in terms of typechecker behavior. We believe removing it will make the language easier to use.

We hope this release will enable experimentation and for work on automated migration; if successful, we aim to stabilize the API and remove the language feature.

This release adds two traits:

  • XHPChildDeclarationConsistencyValidation: requires that the element have consistent old- and new- style child declarations
  • XHPChildValidation: uses new-style validation, and requires that the element does not have an old-style declaration

Both of these traits define abstract protected static function getChildrenDeclaration(): Facebook\XHP\ChildValidation\Constraint {} - this static method must be implemented by any concrete classes using the traits.

Constraints are designed to be a full-featured replacement for the existing language feature, and created through factory functions

The unit tests contain many examples; this class is representative:

class :test:nested-rule extends :x:element {
  use XHPChildDeclarationConsistencyValidation;

  children (:div | (:code+));

  // this is equivalent to the previous declaration
  protected static function getChildrenDeclaration(): XHPChild\Constraint {
    return XHPChild\anyOf(
      XHPChild\ofType<:div>(),
      XHPChild\atLeastOneOf(XHPChild\ofType<:code>()),
    );
  }