Skip to content

Commit 3318383

Browse files
committed
Cleanup
1 parent b861b6a commit 3318383

File tree

3 files changed

+46
-33
lines changed

3 files changed

+46
-33
lines changed

src/Extractor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import {
2727
} from "./utils/DiagnosticError";
2828
import { err, ok } from "./utils/Result";
2929
import * as ts from "typescript";
30-
import { NameDefinition, UNRESOLVED_REFERENCE_NAME } from "./TypeContext";
30+
import {
31+
DeclarationDefinition,
32+
NameDefinition,
33+
UNRESOLVED_REFERENCE_NAME,
34+
} from "./TypeContext";
3135
import * as E from "./Errors";
3236
import { traverseJSDocTags } from "./utils/JSDoc";
3337
import { GraphQLConstructor } from "./GraphQLConstructor";
@@ -86,7 +90,10 @@ export type ExtractionSnapshot = {
8690
readonly definitions: DefinitionNode[];
8791
readonly unresolvedNames: Map<ts.EntityName, NameNode>;
8892
readonly nameDefinitions: Map<ts.DeclarationStatement, NameDefinition>;
89-
readonly implicitNameDefinitions: Map<NameDefinition, ts.TypeReferenceNode>;
93+
readonly implicitNameDefinitions: Map<
94+
DeclarationDefinition,
95+
ts.TypeReferenceNode
96+
>;
9097
readonly typesWithTypename: Set<string>;
9198
readonly interfaceDeclarations: Array<ts.InterfaceDeclaration>;
9299
};
@@ -118,7 +125,7 @@ class Extractor {
118125
// Snapshot data
119126
unresolvedNames: Map<ts.EntityName, NameNode> = new Map();
120127
nameDefinitions: Map<ts.DeclarationStatement, NameDefinition> = new Map();
121-
implicitNameDefinitions: Map<NameDefinition, ts.TypeReferenceNode> =
128+
implicitNameDefinitions: Map<DeclarationDefinition, ts.TypeReferenceNode> =
122129
new Map();
123130
typesWithTypename: Set<string> = new Set();
124131
interfaceDeclarations: Array<ts.InterfaceDeclaration> = [];

src/TypeContext.ts

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
gqlRelated,
1414
DiagnosticsResult,
1515
FixableDiagnosticWithLocation,
16-
tsRelated,
1716
} from "./utils/DiagnosticError";
1817
import { err, ok } from "./utils/Result";
1918
import * as E from "./Errors";
@@ -30,20 +29,20 @@ export type DerivedResolverDefinition = {
3029
kind: "DERIVED_CONTEXT";
3130
};
3231

33-
export type NameDefinition =
34-
| {
35-
name: NameNode;
36-
kind:
37-
| "TYPE"
38-
| "INTERFACE"
39-
| "UNION"
40-
| "SCALAR"
41-
| "INPUT_OBJECT"
42-
| "ENUM"
43-
| "CONTEXT"
44-
| "INFO";
45-
}
46-
| DerivedResolverDefinition;
32+
export type NameDefinition = {
33+
name: NameNode;
34+
kind:
35+
| "TYPE"
36+
| "INTERFACE"
37+
| "UNION"
38+
| "SCALAR"
39+
| "INPUT_OBJECT"
40+
| "ENUM"
41+
| "CONTEXT"
42+
| "INFO";
43+
};
44+
45+
export type DeclarationDefinition = NameDefinition | DerivedResolverDefinition;
4746

4847
type TsIdentifier = number;
4948

@@ -62,7 +61,8 @@ type TsIdentifier = number;
6261
export class TypeContext {
6362
checker: ts.TypeChecker;
6463

65-
_declarationToName: Map<ts.Declaration, NameDefinition> = new Map();
64+
_declarationToDefinition: Map<ts.Declaration, DeclarationDefinition> =
65+
new Map();
6666
_unresolvedNodes: Map<TsIdentifier, ts.EntityName> = new Map();
6767
_idToDeclaration: Map<TsIdentifier, ts.Declaration> = new Map();
6868

@@ -76,15 +76,15 @@ export class TypeContext {
7676
self._markUnresolvedType(node, typeName);
7777
}
7878
for (const [node, definition] of snapshot.nameDefinitions) {
79-
self._recordTypeName(node, definition);
79+
self._recordDeclaration(node, definition);
8080
}
8181
for (const [definition, reference] of snapshot.implicitNameDefinitions) {
8282
const declaration = self.maybeTsDeclarationForTsName(reference.typeName);
8383
if (declaration == null) {
8484
errors.push(tsErr(reference.typeName, E.unresolvedTypeReference()));
8585
continue;
8686
}
87-
const existing = self._declarationToName.get(declaration);
87+
const existing = self._declarationToDefinition.get(declaration);
8888
if (existing != null) {
8989
errors.push(
9090
tsErr(
@@ -98,8 +98,7 @@ export class TypeContext {
9898
);
9999
continue;
100100
}
101-
102-
self._recordTypeName(declaration, definition);
101+
self._recordDeclaration(declaration, definition);
103102
}
104103

105104
if (errors.length > 0) {
@@ -114,18 +113,21 @@ export class TypeContext {
114113

115114
// Record that a GraphQL construct of type `kind` with the name `name` is
116115
// declared at `node`.
117-
private _recordTypeName(node: ts.Declaration, definition: NameDefinition) {
116+
private _recordDeclaration(
117+
node: ts.Declaration,
118+
definition: DeclarationDefinition,
119+
) {
118120
this._idToDeclaration.set(definition.name.tsIdentifier, node);
119-
this._declarationToName.set(node, definition);
121+
this._declarationToDefinition.set(node, definition);
120122
}
121123

122124
// Record that a type references `node`
123125
private _markUnresolvedType(node: ts.EntityName, name: NameNode) {
124126
this._unresolvedNodes.set(name.tsIdentifier, node);
125127
}
126128

127-
allNameDefinitions(): Iterable<NameDefinition> {
128-
return this._declarationToName.values();
129+
allDefinitions(): Iterable<DeclarationDefinition> {
130+
return this._declarationToDefinition.values();
129131
}
130132

131133
findSymbolDeclaration(startSymbol: ts.Symbol): ts.Declaration | null {
@@ -173,7 +175,9 @@ export class TypeContext {
173175
);
174176
}
175177

176-
const nameDefinition = this._declarationToName.get(declarationResult.value);
178+
const nameDefinition = this._declarationToDefinition.get(
179+
declarationResult.value,
180+
);
177181
if (nameDefinition == null) {
178182
return err(gqlErr(unresolved, E.unresolvedTypeReference()));
179183
}
@@ -194,12 +198,12 @@ export class TypeContext {
194198
if (referenceNode == null) return false;
195199
const declaration = this.maybeTsDeclarationForTsName(referenceNode);
196200
if (declaration == null) return false;
197-
return this._declarationToName.has(declaration);
201+
return this._declarationToDefinition.has(declaration);
198202
}
199203

200204
gqlNameDefinitionForGqlName(
201205
nameNode: NameNode,
202-
): DiagnosticResult<NameDefinition> {
206+
): DiagnosticResult<DeclarationDefinition> {
203207
const referenceNode = this.getEntityName(nameNode);
204208
if (referenceNode == null) {
205209
throw new Error("Expected to find reference node for name node.");
@@ -209,7 +213,7 @@ export class TypeContext {
209213
if (declaration == null) {
210214
return err(gqlErr(nameNode, E.unresolvedTypeReference()));
211215
}
212-
const definition = this._declarationToName.get(declaration);
216+
const definition = this._declarationToDefinition.get(declaration);
213217
if (definition == null) {
214218
return err(gqlErr(nameNode, E.unresolvedTypeReference()));
215219
}
@@ -230,7 +234,9 @@ export class TypeContext {
230234
);
231235
}
232236

233-
const nameDefinition = this._declarationToName.get(declarationResult.value);
237+
const nameDefinition = this._declarationToDefinition.get(
238+
declarationResult.value,
239+
);
234240
if (nameDefinition == null) {
235241
return err(tsErr(node, E.unresolvedTypeReference()));
236242
}

src/validations/validateDuplicateContextOrInfo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function validateDuplicateContextOrInfo(
1414
const errors: FixableDiagnosticWithLocation[] = [];
1515
let infoDefinition: null | NameDefinition = null;
1616
let ctxDefinition: null | NameDefinition = null;
17-
for (const namedDefinition of ctx.allNameDefinitions()) {
17+
for (const namedDefinition of ctx.allDefinitions()) {
1818
switch (namedDefinition.kind) {
1919
case "CONTEXT":
2020
if (ctxDefinition != null) {

0 commit comments

Comments
 (0)