Skip to content

Commit

Permalink
Parser: allow 'options' to explicitly accept undefined
Browse files Browse the repository at this point in the history
Continuation of graphql#3670
Basically allow to have code like this:
```js
parse(document, GraphQLServerConfig.parserOptions)
```
to pass TS check with 'exactOptionalPropertyTypes'
  • Loading branch information
IvanGoncharov committed Jul 21, 2022
1 parent bef5534 commit cd688d0
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export interface ParseOptions {
*/
export function parse(
source: string | Source,
options?: ParseOptions,
options?: ParseOptions | undefined,
): DocumentNode {
const parser = new Parser(source, options);
return parser.parseDocument();
Expand All @@ -147,7 +147,7 @@ export function parse(
*/
export function parseValue(
source: string | Source,
options?: ParseOptions,
options?: ParseOptions | undefined,
): ValueNode {
const parser = new Parser(source, options);
parser.expectToken(TokenKind.SOF);
Expand All @@ -162,7 +162,7 @@ export function parseValue(
*/
export function parseConstValue(
source: string | Source,
options?: ParseOptions,
options?: ParseOptions | undefined,
): ConstValueNode {
const parser = new Parser(source, options);
parser.expectToken(TokenKind.SOF);
Expand All @@ -183,7 +183,7 @@ export function parseConstValue(
*/
export function parseType(
source: string | Source,
options?: ParseOptions,
options?: ParseOptions | undefined,
): TypeNode {
const parser = new Parser(source, options);
parser.expectToken(TokenKind.SOF);
Expand All @@ -204,10 +204,10 @@ export function parseType(
* @internal
*/
export class Parser {
protected _options: Maybe<ParseOptions>;
protected _options: ParseOptions;
protected _lexer: Lexer;

constructor(source: string | Source, options?: ParseOptions) {
constructor(source: string | Source, options: ParseOptions = {}) {
const sourceObj = isSource(source) ? source : new Source(source);

this._lexer = new Lexer(sourceObj);
Expand Down Expand Up @@ -472,7 +472,7 @@ export class Parser {
parseNullabilityAssertion(): NullabilityAssertionNode | undefined {
// Note: Client Controlled Nullability is experimental and may be changed or
// removed in the future.
if (this._options?.experimentalClientControlledNullability !== true) {
if (this._options.experimentalClientControlledNullability !== true) {
return undefined;
}

Expand Down Expand Up @@ -575,7 +575,7 @@ export class Parser {
// Legacy support for defining variables within fragments changes
// the grammar of FragmentDefinition:
// - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet
if (this._options?.allowLegacyFragmentVariables === true) {
if (this._options.allowLegacyFragmentVariables === true) {
return this.node<FragmentDefinitionNode>(start, {
kind: Kind.FRAGMENT_DEFINITION,
name: this.parseFragmentName(),
Expand Down Expand Up @@ -1455,7 +1455,7 @@ export class Parser {
startToken: Token,
node: T,
): T {
if (this._options?.noLocation !== true) {
if (this._options.noLocation !== true) {
node.loc = new Location(
startToken,
this._lexer.lastToken,
Expand Down

0 comments on commit cd688d0

Please sign in to comment.