Skip to content

Commit 266df68

Browse files
committed
✨ Provide feature to clean unexisting directives
1 parent ed41120 commit 266df68

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-js-tree",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"private": false,
55
"license": "MIT",
66
"description": "GraphQL Parser providing simplier structure",
@@ -54,4 +54,4 @@
5454
"pre-commit": "npm run lint"
5555
}
5656
}
57-
}
57+
}

src/TreeOperations/directive.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Options, ParserField } from '@/Models';
1+
import { Options, ParserField, TypeSystemDefinition } from '@/Models';
22
import { checkValueType } from '@/TreeOperations/tree';
33

44
export const recursivelyDeleteDirectiveNodes = (nodes: ParserField[], directiveName: string) => {
@@ -9,6 +9,22 @@ export const recursivelyDeleteDirectiveNodes = (nodes: ParserField[], directiveN
99
recursivelyDeleteDirectiveNodes(n.args, directiveName);
1010
});
1111
};
12+
export const recursivelyDeleteAllDirectiveNodes = (nodes: ParserField[]) => {
13+
const indexesToRemove: number[] = [];
14+
nodes.forEach((n, i) => {
15+
if (n.data.type === TypeSystemDefinition.DirectiveDefinition) {
16+
indexesToRemove.push(i);
17+
}
18+
if (n.directives) {
19+
n.directives = [];
20+
}
21+
recursivelyDeleteAllDirectiveNodes(n.args);
22+
});
23+
indexesToRemove.reverse();
24+
indexesToRemove.forEach((itr) => {
25+
nodes.splice(itr, 1);
26+
});
27+
};
1228

1329
export const recursivelyRenameDirectiveNodes = (
1430
nodes: ParserField[],

src/TreeOperations/tree.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@/Models';
1212
import { getTypeName } from '@/shared';
1313
import {
14+
recursivelyDeleteAllDirectiveNodes,
1415
recursivelyDeleteDirectiveArgument,
1516
recursivelyDeleteDirectiveNodes,
1617
recursivelyRenameDirectiveNodes,
@@ -234,6 +235,9 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
234235
type: checkValueType(node, allNodes),
235236
};
236237
};
238+
const removeAllDirectives = () => {
239+
recursivelyDeleteAllDirectiveNodes(allNodes);
240+
};
237241
return {
238242
updateFieldOnNode,
239243
addFieldToNode,
@@ -242,6 +246,7 @@ export const mutate = (tree: ParserTree, allNodes: ParserField[]) => {
242246
implementInterface,
243247
deImplementInterface,
244248
setValueNode,
249+
removeAllDirectives,
245250
};
246251
};
247252

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createPlainDirectiveImplementation, createRootField } from '@/shared';
2+
import { ParserTree, TypeDefinition } from '../../Models';
3+
import { Parser } from '../../Parser';
4+
5+
// TODO: Add schema directive test
6+
// TODO: Add directive with arguments test
7+
8+
describe('Exotic Directive tests on parser', () => {
9+
test(`Test non existing directives`, () => {
10+
const schema = `
11+
type Person @model
12+
`;
13+
const tree = Parser.parse(schema);
14+
const treeMock: ParserTree = {
15+
nodes: [
16+
createRootField({
17+
name: 'Person',
18+
type: TypeDefinition.ObjectTypeDefinition,
19+
directives: [
20+
createPlainDirectiveImplementation({
21+
name: 'model',
22+
}),
23+
],
24+
}),
25+
],
26+
};
27+
expect(tree.nodes).toEqual(expect.arrayContaining(treeMock.nodes));
28+
});
29+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { mutate } from '@/TreeOperations/tree';
2+
import { createMock } from '@/__tests__/TreeOperations/mocks';
3+
4+
describe('Tree Operations - directive cleaning tests', () => {
5+
test('Delete directive node from tree using cleanall', () => {
6+
const treeMock = createMock();
7+
const directiveNode = JSON.parse(JSON.stringify(treeMock.nodes[5]));
8+
mutate(treeMock, treeMock.nodes).removeAllDirectives();
9+
expect(treeMock.nodes).not.toContainEqual(directiveNode);
10+
expect(treeMock.nodes[0].directives).toHaveLength(0);
11+
});
12+
});

0 commit comments

Comments
 (0)