public function testAllowsForEditingOnLeave() { $ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]); $editedAst = Visitor::visit($ast, ['leave' => function ($node) { if ($node instanceof Field && $node->name->value === 'b') { return Visitor::removeNode(); } }]); $this->assertEquals(Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]), $ast); $this->assertEquals(Parser::parse('{ a, c { a, c } }', ['noLocation' => true]), $editedAst); }
/** * @it allows for editing on leave */ public function testAllowsForEditingOnLeave2() { $visited = []; $ast = Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]); $editedAst = Visitor::visit($ast, Visitor::visitInParallel([['leave' => function ($node) use(&$visited) { if ($node->kind === 'Field' && isset($node->name->value) && $node->name->value === 'b') { return Visitor::removeNode(); } }], ['enter' => function ($node) use(&$visited) { $visited[] = ['enter', $node->kind, isset($node->value) ? $node->value : null]; }, 'leave' => function ($node) use(&$visited) { $visited[] = ['leave', $node->kind, isset($node->value) ? $node->value : null]; }]])); $this->assertEquals(Parser::parse('{ a, b, c { a, b, c } }', ['noLocation' => true]), $ast); $this->assertEquals(Parser::parse('{ a, c { a, c } }', ['noLocation' => true]), $editedAst); $this->assertEquals([['enter', 'Document', null], ['enter', 'OperationDefinition', null], ['enter', 'SelectionSet', null], ['enter', 'Field', null], ['enter', 'Name', 'a'], ['leave', 'Name', 'a'], ['leave', 'Field', null], ['enter', 'Field', null], ['enter', 'Name', 'b'], ['leave', 'Name', 'b'], ['enter', 'Field', null], ['enter', 'Name', 'c'], ['leave', 'Name', 'c'], ['enter', 'SelectionSet', null], ['enter', 'Field', null], ['enter', 'Name', 'a'], ['leave', 'Name', 'a'], ['leave', 'Field', null], ['enter', 'Field', null], ['enter', 'Name', 'b'], ['leave', 'Name', 'b'], ['enter', 'Field', null], ['enter', 'Name', 'c'], ['leave', 'Name', 'c'], ['leave', 'Field', null], ['leave', 'SelectionSet', null], ['leave', 'Field', null], ['leave', 'SelectionSet', null], ['leave', 'OperationDefinition', null], ['leave', 'Document', null]], $visited); }