Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Commit 2e0aaa6

Browse files
committed
Support shape subtyping
fixes #98
1 parent 9ba709c commit 2e0aaa6

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/CodegenShape.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ public function setManualAttrsID(?string $id = null): this {
3939
return $this;
4040
}
4141

42+
private bool $allowSubtyping = false;
43+
public function allowsSubtyping(): bool {
44+
return $this->allowSubtyping;
45+
}
46+
47+
public function setAllowsSubtyping(bool $value): this {
48+
$this->allowSubtyping = $value;
49+
return $this;
50+
}
51+
4252
public function appendToBuilder(HackBuilder $builder): HackBuilder {
4353
$builder->addLine('shape(')->indent();
4454

@@ -61,6 +71,10 @@ public function appendToBuilder(HackBuilder $builder): HackBuilder {
6171
->endManualSection();
6272
}
6373

74+
if ($this->allowsSubtyping()) {
75+
$builder->ensureNewLine()->addLine('...');
76+
}
77+
6478
return $builder->unindent()->add(')');
6579
}
6680
}

tests/CodegenShapeTest.codegen

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
@generated
2+
!@#$%codegentest:testImplicitSubtyping
3+
shape(
4+
?'x' => int,
5+
?'y' => int,
6+
'url' => string,
7+
...
8+
)
29
!@#$%codegentest:testMultipleNestedShapes
310
shape(
411
'test' => shape(

tests/CodegenShapeTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public function testShapeOptionalFields(): void {
3636
expect_with_context(static::class, $shape->render())->toBeUnchanged();
3737
}
3838

39+
public function testImplicitSubtyping(): void {
40+
$shape = $this
41+
->getCodegenFactory()
42+
->codegenShape(
43+
(new CodegenShapeMember('x', 'int'))->setIsOptional(),
44+
(new CodegenShapeMember('y', 'int'))->setIsOptional(),
45+
new CodegenShapeMember('url', 'string'),
46+
)
47+
->setAllowsSubtyping(true);
48+
49+
expect_with_context(static::class, $shape->render())->toBeUnchanged();
50+
51+
}
52+
3953
public function testNestedShape(): void {
4054
$nested = $this
4155
->getCodegenFactory()

0 commit comments

Comments
 (0)