Skip to content
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

adding required methods and functions for "__typename must be const" change #137

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/Errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ export function typeNameInitializeNotString() {
return `Expected \`__typename\` property initializer to be a string literal. For example: \`__typename = "MyType"\` or \`__typename: "MyType";\`. ${TYPENAME_CONTEXT}`;
}

export function typeNameInitializeNotExpression() {
return `Expected \`__typename\` property initializer to be an expression with a const assertion. For example: \`__typename = "MyType" as const\`. ${TYPENAME_CONTEXT}`;
Rash-Hit marked this conversation as resolved.
Show resolved Hide resolved
}

export function typeNameTypeNotReferenceNode() {
return `Expected \`__typename\` property type to be a TypeReferenceNode. ${TYPENAME_CONTEXT}`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to try to avoid using AST type names in error messages if possible. I don't think most developers know (or should need to know) what a TypeReferenceNode or Identifier are. Maybe we should just simplify this down to one error message that looks more like the one in typeNameInitializeNotExpression and use it for all of these new error cases?

}

export function typeNameTypeNameNotIdentifier() {
return `Expected \`__typename\` property type name to be an Identifier. ${TYPENAME_CONTEXT}`;
}

export function typeNameTypeNameNotConst() {
return `Expected \`__typename\` property type name to be "const". ${TYPENAME_CONTEXT}`;
}

export function typeNameInitializerWrong(expected: string, actual: string) {
return `Expected \`__typename\` property initializer to be \`"${expected}"\`, found \`"${actual}"\`. ${TYPENAME_CONTEXT}`;
}
Expand Down
29 changes: 25 additions & 4 deletions src/Extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,18 +740,39 @@ class Extractor {
return false;
}

if (!ts.isStringLiteral(node.initializer)) {
this.report(node.initializer, E.typeNameInitializeNotString());
if (!ts.isAsExpression(node.initializer)) {
this.report(node.initializer, E.typeNameInitializeNotExpression())
return false;
}

if (node.initializer.text !== expectedName) {
if (!ts.isStringLiteral(node.initializer.expression)) {
this.report(node.initializer.expression, E.typeNameInitializeNotString());
return false
}

if (node.initializer.expression.text !== expectedName) {
this.report(
node.initializer,
E.typeNameInitializerWrong(expectedName, node.initializer.text),
E.typeNameInitializerWrong(expectedName, node.initializer.expression.text),
);
return false
}

if (!ts.isTypeReferenceNode(node.initializer.type)) {
this.report(node.initializer.type, E.typeNameTypeNotReferenceNode());
return false;
}

if (!ts.isIdentifier(node.initializer.type.typeName)) {
this.report(node.initializer.type.typeName, E.typeNameTypeNameNotIdentifier());
return false;
}

if (node.initializer.type.typeName.escapedText !== "const") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (node.initializer.type.typeName.escapedText !== "const") {
if (node.initializer.type.typeName.escapedText !== ts.SyntaxKind.ConstKeyword) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried this thing , but getting this error
image

and the reason is
image

image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I guess it doesn't parse as const as a keyword! Leaving this as a string is fine.

this.report(node.initializer.type.typeName, E.typeNameTypeNameNotConst());
return false;
}

return true;
}

Expand Down
Loading