Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

v3.1: experimental alternative interface for child validation

Compare
Choose a tag to compare
@fredemmott fredemmott released this 12 Feb 21:53
83e81a3

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>()),
    );
  }