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

Make variableDefinitions not nullable #2405

Closed
wants to merge 1 commit into from

Conversation

brandon-leapyear
Copy link

@brandon-leapyear brandon-leapyear commented Jan 27, 2020

This is an old work account. Please reference @brandonchinn178 for all future communication


It seems like variableDefinitions will always be a non-null array.

parseOperationDefinition(): OperationDefinitionNode {
const start = this._lexer.token;
if (this.peek(TokenKind.BRACE_L)) {
return {
kind: Kind.OPERATION_DEFINITION,
operation: 'query',
name: undefined,
variableDefinitions: [],
directives: [],
selectionSet: this.parseSelectionSet(),
loc: this.loc(start),
};
}
const operation = this.parseOperationType();
let name;
if (this.peek(TokenKind.NAME)) {
name = this.parseName();
}
return {
kind: Kind.OPERATION_DEFINITION,
operation,
name,
variableDefinitions: this.parseVariableDefinitions(),
directives: this.parseDirectives(false),
selectionSet: this.parseSelectionSet(),
loc: this.loc(start),
};
}

parseVariableDefinitions(): Array<VariableDefinitionNode> {

@IvanGoncharov
Copy link
Member

Hi @brandon-leapyear

Thanks for PR 👍
AST format should have a healthy balance between the ability to easily process but at the same to easily construct AST nodes.
For example, at the moment you can easily extract part of the query into separate query:

return {
  kind: Kind.OPERATION_DEFINITION, 
  selectionSet: someSelectionSet,
};

And you don't need to add a bunch of empty arrays, moreover, it makes code less fragile when we add new properties to AST nodes.

But I agree it looks weird and creates complications, for example with testing see #2203
Personally I think a good long term solution would be to always return undefined instead of empty arrays since it's will improve performance and memory consumption.
Also, it would be great if someone researches what other JS parsers do e.g. Babel's parser, ESLint parser, etc.

@yaacovCR
Copy link
Contributor

closing this PR in favor of #4206 based on suggestion above in #2405 (comment)

@yaacovCR yaacovCR closed this Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants