Skip to content

Visitor does not support enter and leave for the root node #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/language/__tests__/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,63 @@ import { getNamedType, isCompositeType } from '../../type';


describe('Visitor', () => {

it('allows editing a node both on enter and on leave', () => {

const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });

let selectionSet;

const editedAst = visit(ast, {
OperationDefinition: {
enter(node) {
selectionSet = node.selectionSet;
return {
...node,
selectionSet: {
kind: 'SelectionSet',
selections: []
},
};
},
leave(node) {
return {
...node,
selectionSet,
};
}
}
});

expect(editedAst).to.deep.equal(ast);
});

it('allows editing the root node on enter and on leave', () => {

const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });

const { definitions } = ast;

const editedAst = visit(ast, {
Document: {
enter(node) {
return {
...node,
definitions: []
};
},
leave(node) {
return {
...node,
definitions,
};
}
}
});

expect(editedAst).to.deep.equal(ast);
});

it('allows for editing on enter', () => {

const ast = parse('{ a, b, c { a, b, c } }', { noLocation: true });
Expand Down
2 changes: 1 addition & 1 deletion src/language/visitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export function visit(root, visitor, keyMap) {
} while (stack !== undefined);

if (edits.length !== 0) {
newRoot = edits[0][1];
newRoot = edits[edits.length - 1][1];
}

return newRoot;
Expand Down