Skip to content

Commit

Permalink
Update programLinkNode name when updating programNode name
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Jan 1, 2024
1 parent e7c378f commit 4a1aaad
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinkableDictionary } from 'src/shared';
import { AccountNode, ProgramNode, isNodeFilter } from '../../../nodes';
import { LinkableDictionary } from '../../../shared';
import { NameApi } from '../nameTransformers';
import { Fragment, fragment, fragmentFromTemplate } from './common';

Expand Down
2 changes: 1 addition & 1 deletion src/visitors/getByteSizeVisitor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LinkableDictionary } from 'src/shared';
import {
REGISTERED_TYPE_NODE_KEYS,
RegisteredTypeNodes,
isNode,
isScalarEnum,
} from '../nodes';
import { LinkableDictionary } from '../shared';
import { mergeVisitor } from './mergeVisitor';
import { Visitor, visit } from './visitor';

Expand Down
6 changes: 3 additions & 3 deletions src/visitors/updateDefinedTypesVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function updateDefinedTypesVisitor(
? mainCase(updates.name)
: undefined;

const transforms: BottomUpNodeTransformerWithSelector[] = [
const transformers: BottomUpNodeTransformerWithSelector[] = [
{
select: `${selectorStack.join('.')}.[definedTypeNode]${name}`,
transform: (node) => {
Expand All @@ -56,7 +56,7 @@ export function updateDefinedTypesVisitor(
];

if (newName) {
transforms.push({
transformers.push({
select: `${selectorStack.join('.')}.[definedTypeLinkNode]${name}`,
transform: (node) => {
assertIsNode(node, 'definedTypeLinkNode');
Expand All @@ -66,7 +66,7 @@ export function updateDefinedTypesVisitor(
});
}

return transforms;
return transformers;
}
)
);
Expand Down
48 changes: 38 additions & 10 deletions src/visitors/updateProgramsVisitor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { ProgramNodeInput, assertIsNode, programNode } from '../nodes';
import {
ProgramNodeInput,
assertIsNode,
programLinkNode,
programNode,
} from '../nodes';
import { mainCase } from '../shared';
import {
BottomUpNodeTransformerWithSelector,
bottomUpTransformerVisitor,
Expand All @@ -15,15 +21,37 @@ export type ProgramUpdates =

export function updateProgramsVisitor(map: Record<string, ProgramUpdates>) {
return bottomUpTransformerVisitor(
Object.entries(map).map(
([name, updates]): BottomUpNodeTransformerWithSelector => ({
select: `[programNode]${name}`,
transform: (node) => {
assertIsNode(node, 'programNode');
if ('delete' in updates) return null;
return programNode({ ...node, ...updates });
},
})
Object.entries(map).flatMap(
([name, updates]): BottomUpNodeTransformerWithSelector[] => {
const newName =
typeof updates === 'object' && 'name' in updates && updates.name
? mainCase(updates.name)
: undefined;

const transformers: BottomUpNodeTransformerWithSelector[] = [
{
select: `[programNode]${name}`,
transform: (node) => {
assertIsNode(node, 'programNode');
if ('delete' in updates) return null;
return programNode({ ...node, ...updates });
},
},
];

if (newName) {
transformers.push({
select: `[programLinkNode]${name}`,
transform: (node) => {
assertIsNode(node, 'programLinkNode');
if (node.importFrom) return node;
return programLinkNode(newName);
},
});
}

return transformers;
}
)
);
}

0 comments on commit 4a1aaad

Please sign in to comment.