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

isValidNameError: Remove node argument #2424

Merged
merged 1 commit into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/error/locatedError.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Maybe from '../tsutils/Maybe';

import { ASTNode } from '../language/ast';

import { GraphQLError } from './GraphQLError';
Expand All @@ -9,6 +11,6 @@ import { GraphQLError } from './GraphQLError';
*/
export function locatedError(
originalError: Error | GraphQLError,
nodes: ReadonlyArray<ASTNode>,
path: ReadonlyArray<string | number>,
nodes: ASTNode | ReadonlyArray<ASTNode> | undefined,
path?: Maybe<ReadonlyArray<string | number>>,
): GraphQLError;
4 changes: 2 additions & 2 deletions src/error/locatedError.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { GraphQLError } from './GraphQLError';
*/
export function locatedError(
originalError: Error | GraphQLError,
nodes: $ReadOnlyArray<ASTNode>,
path: $ReadOnlyArray<string | number>,
nodes: ASTNode | $ReadOnlyArray<ASTNode> | void | null,
path?: ?$ReadOnlyArray<string | number>,
): GraphQLError {
// Note: this uses a brand-check to support GraphQL errors originating from
// other contexts.
Expand Down
5 changes: 3 additions & 2 deletions src/type/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import objectValues from '../polyfills/objectValues';
import inspect from '../jsutils/inspect';

import { GraphQLError } from '../error/GraphQLError';
import { locatedError } from '../error/locatedError';

import { type ASTNode, type NamedTypeNode } from '../language/ast';

Expand Down Expand Up @@ -188,9 +189,9 @@ function validateName(
node: { +name: string, +astNode: ?ASTNode, ... },
): void {
// Ensure names are valid, however introspection types opt out.
const error = isValidNameError(node.name, node.astNode ?? undefined);
const error = isValidNameError(node.name);
if (error) {
context.addError(error);
context.addError(locatedError(error, node.astNode));
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/utilities/assertValidName.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { GraphQLError } from '../error/GraphQLError';
import { ASTNode } from '../language/ast';

/**
* Upholds the spec rules about naming.
Expand All @@ -9,7 +8,4 @@ export function assertValidName(name: string): string;
/**
* Returns an Error if a name is invalid.
*/
export function isValidNameError(
name: string,
node?: ASTNode | undefined,
): GraphQLError | undefined;
export function isValidNameError(name: string): GraphQLError | undefined;
8 changes: 1 addition & 7 deletions src/utilities/assertValidName.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import devAssert from '../jsutils/devAssert';

import { GraphQLError } from '../error/GraphQLError';
import { type ASTNode } from '../language/ast';

const NAME_RX = /^[_a-zA-Z][_a-zA-Z0-9]*$/;

Expand All @@ -21,21 +20,16 @@ export function assertValidName(name: string): string {
/**
* Returns an Error if a name is invalid.
*/
export function isValidNameError(
name: string,
node?: ASTNode | void,
): GraphQLError | void {
export function isValidNameError(name: string): GraphQLError | void {
devAssert(typeof name === 'string', 'Expected name to be a string.');
if (name.length > 1 && name[0] === '_' && name[1] === '_') {
return new GraphQLError(
`Name "${name}" must not begin with "__", which is reserved by GraphQL introspection.`,
node,
);
}
if (!NAME_RX.test(name)) {
return new GraphQLError(
`Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "${name}" does not.`,
node,
);
}
}