From d93ec6c5715c10c15de6b83ac6e8de7080d48b1b Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Sun, 10 Dec 2017 20:23:19 +0530 Subject: [PATCH] fix(query): fix commands throwing error with empty sourceText. closes #78 --- .../getTokenAtPosition.test.js.snap | 36 +++++++++++++++++++ .../__tests__/getTokenAtPosition.test.js | 7 +++- .../__tests__/getHintsAtPosition.test.js | 7 ++++ src/shared/getTokenAtPosition.js | 3 +- 4 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/query/_shared/__tests__/__snapshots__/getTokenAtPosition.test.js.snap b/src/query/_shared/__tests__/__snapshots__/getTokenAtPosition.test.js.snap index 23d84a9..c9d7140 100644 --- a/src/query/_shared/__tests__/__snapshots__/getTokenAtPosition.test.js.snap +++ b/src/query/_shared/__tests__/__snapshots__/getTokenAtPosition.test.js.snap @@ -1,5 +1,41 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`[Bug] should works for empty sourceText 1`] = ` +Object { + "end": 1, + "prevChar": "", + "start": 0, + "state": Object { + "indentLevel": 0, + "kind": "Document", + "level": 0, + "name": null, + "needsSeperator": false, + "prevState": Object { + "kind": null, + "level": 0, + "name": null, + "needsSeperator": false, + "prevState": null, + "rule": null, + "step": 0, + "type": null, + }, + "rule": Array [ + Object { + "isList": true, + "ofRule": "Definition", + "separator": undefined, + }, + ], + "step": 0, + "type": null, + }, + "string": " ", + "style": "ws", +} +`; + exports[`[Bug] should works for start position 1`] = ` Object { "end": 8, diff --git a/src/query/_shared/__tests__/getTokenAtPosition.test.js b/src/query/_shared/__tests__/getTokenAtPosition.test.js index 458b48d..fdf6926 100644 --- a/src/query/_shared/__tests__/getTokenAtPosition.test.js +++ b/src/query/_shared/__tests__/getTokenAtPosition.test.js @@ -100,7 +100,6 @@ describe('should able to parse Relay.QL queries with interpolation', () => { expect(() => getTokenAtPosition(sourceText, position, relayQLParser)).not.toThrow(); }); - }); it('[Bug] should works for start position', () => { @@ -108,3 +107,9 @@ it('[Bug] should works for start position', () => { const token = getTokenAtPosition(sourceText, { line: 1, column: 1 }, 'QueryParser'); expect(token).toMatchSnapshot(); }); + +it('[Bug] should works for empty sourceText', () => { + const sourceText = ''; + const token = getTokenAtPosition(sourceText, { line: 1, column: 1 }, 'QueryParser'); + expect(token).toMatchSnapshot(); +}); diff --git a/src/query/commands/__tests__/getHintsAtPosition.test.js b/src/query/commands/__tests__/getHintsAtPosition.test.js index 8b22ad6..3a590cc 100644 --- a/src/query/commands/__tests__/getHintsAtPosition.test.js +++ b/src/query/commands/__tests__/getHintsAtPosition.test.js @@ -340,3 +340,10 @@ describe('[Bug] should work if position is first character', () => { ).toEqual([{ text: 'fragment' }]); }); }); + +test('[Bug] should work with empty sourceText', () => { + const sourceText = ''; + expect( + getHintsAtPosition(schema, sourceText, { line: 1, column: 1 }, queryConfig), + ).toEqual(hints.DocumentLevel); +}); diff --git a/src/shared/getTokenAtPosition.js b/src/shared/getTokenAtPosition.js index 0c1d916..f14f0f0 100644 --- a/src/shared/getTokenAtPosition.js +++ b/src/shared/getTokenAtPosition.js @@ -15,9 +15,10 @@ import toOffset from './toOffset'; export default function getTokenAtPosition( parser: IParser, - sourceText: string, + _sourceText: string, position: Position, ): Token { + const sourceText = _sourceText ? _sourceText : ' '; const state = parser.startState(); const offset = toOffset(sourceText, position); const stream = new MultilineCharacterStream(sourceText);