Skip to content

Commit cf932be

Browse files
committed
Revert "No {Ignored} tokens when parsing schema coordinates (graphql#4450)"
This reverts commit 04dd13e.
1 parent 04dd13e commit cf932be

File tree

7 files changed

+17
-110
lines changed

7 files changed

+17
-110
lines changed

src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ export {
230230
printSourceLocation,
231231
// Lex
232232
Lexer,
233-
SchemaCoordinateLexer,
234233
TokenKind,
235234
// Parse
236235
parse,
@@ -262,7 +261,6 @@ export {
262261

263262
export type {
264263
ParseOptions,
265-
ParseSchemaCoordinateOptions,
266264
SourceLocation,
267265
// Visitor utilities
268266
ASTVisitor,

src/language/__tests__/lexer-test.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ import { inspect } from '../../jsutils/inspect.js';
99
import { GraphQLError } from '../../error/GraphQLError.js';
1010

1111
import type { Token } from '../ast.js';
12-
import {
13-
isPunctuatorTokenKind,
14-
Lexer,
15-
SchemaCoordinateLexer,
16-
} from '../lexer.js';
12+
import { isPunctuatorTokenKind, Lexer } from '../lexer.js';
1713
import { Source } from '../source.js';
1814
import { TokenKind } from '../tokenKind.js';
1915

@@ -1193,33 +1189,6 @@ describe('Lexer', () => {
11931189
});
11941190
});
11951191

1196-
describe('SchemaCoordinateLexer', () => {
1197-
it('can be stringified', () => {
1198-
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
1199-
expect(Object.prototype.toString.call(lexer)).to.equal(
1200-
'[object SchemaCoordinateLexer]',
1201-
);
1202-
});
1203-
1204-
it('tracks a schema coordinate', () => {
1205-
const lexer = new SchemaCoordinateLexer(new Source('Name.field'));
1206-
expect(lexer.advance()).to.contain({
1207-
kind: TokenKind.NAME,
1208-
start: 0,
1209-
end: 4,
1210-
value: 'Name',
1211-
});
1212-
});
1213-
1214-
it('forbids ignored tokens', () => {
1215-
const lexer = new SchemaCoordinateLexer(new Source('\nName.field'));
1216-
expectToThrowJSON(() => lexer.advance()).to.deep.equal({
1217-
message: 'Syntax Error: Invalid character: U+000A.',
1218-
locations: [{ line: 1, column: 1 }],
1219-
});
1220-
});
1221-
});
1222-
12231192
describe('isPunctuatorTokenKind', () => {
12241193
function isPunctuatorToken(text: string) {
12251194
return isPunctuatorTokenKind(lexOne(text).kind);

src/language/__tests__/parser-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,11 @@ describe('Parser', () => {
751751
});
752752

753753
it('rejects Name . Name ( Name : Name )', () => {
754-
expect(() => parseSchemaCoordinate('MyType.field(arg:value)'))
754+
expect(() => parseSchemaCoordinate('MyType.field(arg: value)'))
755755
.to.throw()
756756
.to.deep.include({
757757
message: 'Syntax Error: Expected ")", found Name "value".',
758-
locations: [{ line: 1, column: 18 }],
758+
locations: [{ line: 1, column: 19 }],
759759
});
760760
});
761761

src/language/__tests__/printer-test.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,24 +301,16 @@ describe('Printer: Query document', () => {
301301
});
302302

303303
it('prints schema coordinates', () => {
304-
expect(print(parseSchemaCoordinate('Name'))).to.equal('Name');
305-
expect(print(parseSchemaCoordinate('Name.field'))).to.equal('Name.field');
306-
expect(print(parseSchemaCoordinate('Name.field(arg:)'))).to.equal(
307-
'Name.field(arg:)',
304+
expect(print(parseSchemaCoordinate(' Name '))).to.equal('Name');
305+
expect(print(parseSchemaCoordinate(' Name . field '))).to.equal(
306+
'Name.field',
308307
);
309-
expect(print(parseSchemaCoordinate('@name'))).to.equal('@name');
310-
expect(print(parseSchemaCoordinate('@name(arg:)'))).to.equal('@name(arg:)');
311-
});
312-
313-
it('throws syntax error for ignored tokens in schema coordinates', () => {
314-
expect(() => print(parseSchemaCoordinate('# foo\nName'))).to.throw(
315-
'Syntax Error: Invalid character: "#"',
316-
);
317-
expect(() => print(parseSchemaCoordinate('\nName'))).to.throw(
318-
'Syntax Error: Invalid character: U+000A.',
308+
expect(print(parseSchemaCoordinate(' Name . field ( arg: )'))).to.equal(
309+
'Name.field(arg:)',
319310
);
320-
expect(() => print(parseSchemaCoordinate('Name .field'))).to.throw(
321-
'Syntax Error: Invalid character: " "',
311+
expect(print(parseSchemaCoordinate(' @ name '))).to.equal('@name');
312+
expect(print(parseSchemaCoordinate(' @ name (arg:) '))).to.equal(
313+
'@name(arg:)',
322314
);
323315
});
324316
});

src/language/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export { Kind } from './kinds.js';
1111

1212
export { TokenKind } from './tokenKind.js';
1313

14-
export { Lexer, SchemaCoordinateLexer } from './lexer.js';
14+
export { Lexer } from './lexer.js';
1515

1616
export {
1717
parse,
@@ -20,7 +20,7 @@ export {
2020
parseType,
2121
parseSchemaCoordinate,
2222
} from './parser.js';
23-
export type { ParseOptions, ParseSchemaCoordinateOptions } from './parser.js';
23+
export type { ParseOptions } from './parser.js';
2424

2525
export { print } from './printer.js';
2626

src/language/lexer.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,6 @@ export class Lexer {
8383
}
8484
return token;
8585
}
86-
87-
validateIgnoredToken(_position: number): void {
88-
/* noop - ignored tokens are ignored */
89-
}
90-
}
91-
92-
/**
93-
* As `Lexer`, but forbids ignored tokens as required of schema coordinates.
94-
*/
95-
export class SchemaCoordinateLexer extends Lexer {
96-
override get [Symbol.toStringTag]() {
97-
return 'SchemaCoordinateLexer';
98-
}
99-
100-
override validateIgnoredToken(position: number): void {
101-
throw syntaxError(
102-
this.source,
103-
position,
104-
`Invalid character: ${printCodePointAt(this, position)}.`,
105-
);
106-
}
10786
}
10887

10988
/**
@@ -238,21 +217,18 @@ function readNextToken(lexer: Lexer, start: number): Token {
238217
case 0x0009: // \t
239218
case 0x0020: // <space>
240219
case 0x002c: // ,
241-
lexer.validateIgnoredToken(position);
242220
++position;
243221
continue;
244222
// LineTerminator ::
245223
// - "New Line (U+000A)"
246224
// - "Carriage Return (U+000D)" [lookahead != "New Line (U+000A)"]
247225
// - "Carriage Return (U+000D)" "New Line (U+000A)"
248226
case 0x000a: // \n
249-
lexer.validateIgnoredToken(position);
250227
++position;
251228
++lexer.line;
252229
lexer.lineStart = position;
253230
continue;
254231
case 0x000d: // \r
255-
lexer.validateIgnoredToken(position);
256232
if (body.charCodeAt(position + 1) === 0x000a) {
257233
position += 2;
258234
} else {
@@ -263,7 +239,6 @@ function readNextToken(lexer: Lexer, start: number): Token {
263239
continue;
264240
// Comment
265241
case 0x0023: // #
266-
lexer.validateIgnoredToken(position);
267242
return readComment(lexer, position);
268243
// Token ::
269244
// - Punctuator

src/language/parser.ts

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,7 @@ import type {
7070
import { Location, OperationTypeNode } from './ast.js';
7171
import { DirectiveLocation } from './directiveLocation.js';
7272
import { Kind } from './kinds.js';
73-
import {
74-
isPunctuatorTokenKind,
75-
Lexer,
76-
SchemaCoordinateLexer,
77-
} from './lexer.js';
73+
import { isPunctuatorTokenKind, Lexer } from './lexer.js';
7874
import { isSource, Source } from './source.js';
7975
import { TokenKind } from './tokenKind.js';
8076

@@ -118,24 +114,6 @@ export interface ParseOptions {
118114
* ```
119115
*/
120116
experimentalFragmentArguments?: boolean | undefined;
121-
122-
/**
123-
* You may override the Lexer class used to lex the source; this is used by
124-
* schema coordinates to introduce a lexer that forbids ignored tokens.
125-
*/
126-
Lexer?: typeof Lexer | undefined;
127-
}
128-
129-
/**
130-
* Configuration options to control schema coordinate parser behavior
131-
*/
132-
export interface ParseSchemaCoordinateOptions {
133-
/**
134-
* By default, the parser creates AST nodes that know the location
135-
* in the source that they correspond to. This configuration flag
136-
* disables that behavior for performance or testing.
137-
*/
138-
noLocation?: boolean | undefined;
139117
}
140118

141119
/**
@@ -221,13 +199,9 @@ export function parseType(
221199
*/
222200
export function parseSchemaCoordinate(
223201
source: string | Source,
224-
options?: ParseSchemaCoordinateOptions,
202+
options?: ParseOptions,
225203
): SchemaCoordinateNode {
226-
// Ignored tokens are excluded syntax for a Schema Coordinate.
227-
const parser = new Parser(source, {
228-
...options,
229-
Lexer: SchemaCoordinateLexer,
230-
});
204+
const parser = new Parser(source, options);
231205
parser.expectToken(TokenKind.SOF);
232206
const coordinate = parser.parseSchemaCoordinate();
233207
parser.expectToken(TokenKind.EOF);
@@ -253,8 +227,7 @@ export class Parser {
253227
constructor(source: string | Source, options: ParseOptions = {}) {
254228
const sourceObj = isSource(source) ? source : new Source(source);
255229

256-
const LexerClass = options.Lexer ?? Lexer;
257-
this._lexer = new LexerClass(sourceObj);
230+
this._lexer = new Lexer(sourceObj);
258231
this._options = options;
259232
this._tokenCounter = 0;
260233
}

0 commit comments

Comments
 (0)