Skip to content

Commit d93ec6c

Browse files
committed
fix(query): fix commands throwing error with empty sourceText.
closes #78
1 parent 8068120 commit d93ec6c

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

src/query/_shared/__tests__/__snapshots__/getTokenAtPosition.test.js.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,41 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`[Bug] should works for empty sourceText 1`] = `
4+
Object {
5+
"end": 1,
6+
"prevChar": "",
7+
"start": 0,
8+
"state": Object {
9+
"indentLevel": 0,
10+
"kind": "Document",
11+
"level": 0,
12+
"name": null,
13+
"needsSeperator": false,
14+
"prevState": Object {
15+
"kind": null,
16+
"level": 0,
17+
"name": null,
18+
"needsSeperator": false,
19+
"prevState": null,
20+
"rule": null,
21+
"step": 0,
22+
"type": null,
23+
},
24+
"rule": Array [
25+
Object {
26+
"isList": true,
27+
"ofRule": "Definition",
28+
"separator": undefined,
29+
},
30+
],
31+
"step": 0,
32+
"type": null,
33+
},
34+
"string": " ",
35+
"style": "ws",
36+
}
37+
`;
38+
339
exports[`[Bug] should works for start position 1`] = `
440
Object {
541
"end": 8,

src/query/_shared/__tests__/getTokenAtPosition.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,16 @@ describe('should able to parse Relay.QL queries with interpolation', () => {
100100

101101
expect(() => getTokenAtPosition(sourceText, position, relayQLParser)).not.toThrow();
102102
});
103-
104103
});
105104

106105
it('[Bug] should works for start position', () => {
107106
const sourceText = 'fragment';
108107
const token = getTokenAtPosition(sourceText, { line: 1, column: 1 }, 'QueryParser');
109108
expect(token).toMatchSnapshot();
110109
});
110+
111+
it('[Bug] should works for empty sourceText', () => {
112+
const sourceText = '';
113+
const token = getTokenAtPosition(sourceText, { line: 1, column: 1 }, 'QueryParser');
114+
expect(token).toMatchSnapshot();
115+
});

src/query/commands/__tests__/getHintsAtPosition.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,3 +340,10 @@ describe('[Bug] should work if position is first character', () => {
340340
).toEqual([{ text: 'fragment' }]);
341341
});
342342
});
343+
344+
test('[Bug] should work with empty sourceText', () => {
345+
const sourceText = '';
346+
expect(
347+
getHintsAtPosition(schema, sourceText, { line: 1, column: 1 }, queryConfig),
348+
).toEqual(hints.DocumentLevel);
349+
});

src/shared/getTokenAtPosition.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import toOffset from './toOffset';
1515

1616
export default function getTokenAtPosition(
1717
parser: IParser,
18-
sourceText: string,
18+
_sourceText: string,
1919
position: Position,
2020
): Token {
21+
const sourceText = _sourceText ? _sourceText : ' ';
2122
const state = parser.startState();
2223
const offset = toOffset(sourceText, position);
2324
const stream = new MultilineCharacterStream(sourceText);

0 commit comments

Comments
 (0)