Skip to content

Commit 6d79fc2

Browse files
authored
Fix static analysis not working due to different variations of assignments (#391)
* Set up e2e test * Rebase e2e and handle more assignment cases * Add changeset
1 parent ed1d934 commit 6d79fc2

6 files changed

Lines changed: 59 additions & 6 deletions

File tree

.changeset/funny-toes-turn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@eddeee888/gcg-typescript-resolver-files': patch
3+
---
4+
5+
Fix issue where shorthand assignment are not detected for static analysis
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { TopicResolvers } from '../types.generated';
2+
3+
export const name: TopicResolvers['name'] = () => {
4+
return 'Hot';
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { TopicResolvers } from '../types.generated';
2+
3+
export const Topic_url: TopicResolvers['url'] = () => {
4+
return 'Hot';
5+
};
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import type { TopicResolvers } from './../../types.generated';
2+
import { name } from '../Topic.name';
3+
import { Topic_url } from '../Topic.url';
24
export const Topic: TopicResolvers = {
35
id: ({ id }) => id,
46
createdAt: async (_parent, _arg, _ctx) => {
57
/* existing implementation, must keep */
68
return '2024-01-01T00:00:00.000Z';
79
},
10+
name,
11+
url: Topic_url,
812
creator: ({ creator }, _arg, _ctx) => {
913
/* Topic.creator resolver is required because Topic.creator and TopicMapper.creator are not compatible */
1014
return creator;
1115
},
12-
name: async (_parent, _arg, _ctx) => {
13-
/* Topic.name resolver is required because Topic.name exists but TopicMapper.name does not */
14-
},
15-
url: async (_parent, _arg, _ctx) => {
16-
/* Topic.url resolver is required because Topic.url exists but TopicMapper.url does not */
17-
},
1816
};

packages/typescript-resolver-files-e2e/src/test-mappers-vs-schema-types/testSetup.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ createTestSetup({
77
{
88
file: 'modules/topic/resolvers/Topic.ts',
99
content: `import type { TopicResolvers } from './../../types.generated';
10+
import {name} from '../Topic.name';
11+
import {Topic_url} from '../Topic.url';
1012
export const Topic: TopicResolvers = {
1113
id: ({ id }) => id,
1214
createdAt: async (_parent, _arg, _ctx) => {
1315
/* existing implementation, must keep */
1416
return '2024-01-01T00:00:00.000Z';
1517
},
18+
name,
19+
url: Topic_url,
1620
};`,
1721
},
1822
],

packages/typescript-resolver-files/src/generateResolverFiles/addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ export const addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented = ({
5656
}
5757
> = { ...resolversToGenerate };
5858

59+
/**
60+
* PropertyAssignment
61+
* ```
62+
* const name = () => {};
63+
* const OutputType = {
64+
* id: () => {},
65+
* name: name,
66+
* }
67+
* ```
68+
*/
5969
variableStatement
6070
.getDescendantsOfKind(SyntaxKind.PropertyAssignment)
6171
.forEach((propertyAssignment) => {
@@ -65,6 +75,14 @@ export const addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented = ({
6575
}
6676
});
6777

78+
/**
79+
* MethodDeclaration
80+
* ```
81+
* const OutputType = {
82+
* id(){},
83+
* }
84+
* ```
85+
*/
6886
variableStatement
6987
.getDescendantsOfKind(SyntaxKind.MethodDeclaration)
7088
.forEach((methodDeclaration) => {
@@ -74,6 +92,24 @@ export const addObjectTypeResolversPropertyAssignmentNodesIfNotImplemented = ({
7492
}
7593
});
7694

95+
/**
96+
* ShorthandPropertyAssignment example:
97+
* ```
98+
* const id = () => {};
99+
* const OutputType = {
100+
* id,
101+
* }
102+
* ```
103+
*/
104+
variableStatement
105+
.getDescendantsOfKind(SyntaxKind.ShorthandPropertyAssignment)
106+
.forEach((propertyAssignment) => {
107+
const resolverName = propertyAssignment.getName();
108+
if (resolversData[resolverName]) {
109+
resolversData[resolverName].implemented = true;
110+
}
111+
});
112+
77113
// 2. Add missing resolver properties if they haven't been implemented
78114
Object.values(resolversData).forEach(
79115
({ resolverName, resolverDeclaration, implemented }) => {

0 commit comments

Comments
 (0)