Skip to content

Commit 27fa576

Browse files
committed
fix(validate): fix parsing of query files containing irregular whitespace characters
will fix issue #4
1 parent 93b222e commit 27fa576

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ exports[`plain queries 1`] = `
8282
"
8383
`;
8484

85+
exports[`replace all irregular whitespace with space 1`] = `
86+
"
87+
88+
89+
90+
fragment _ on Viewer {
91+
me
92+
}
93+
94+
"
95+
`;
96+
8597
exports[`replace inline js fragment with dummy fragment multiline 1`] = `
8698
"
8799

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,24 @@ describe('replace inline js fragment with dummy fragment', () => {
191191
});
192192
});
193193

194+
test('replace all irregular whitespace with space', () => {
195+
// NOTE between '©' and '2017' there is thin space '\u2009' not regular space ' '
196+
/* eslint-disable no-irregular-whitespace */
197+
const text = `
198+
// © 2017
199+
200+
Relay.QL\`
201+
fragment on Viewer {
202+
me
203+
}
204+
\`,
205+
`;
206+
/* eslint-enable no-irregular-whitespace */
207+
208+
const qd = toQueryDocument(
209+
new Source(text, 'query.js'),
210+
{ parser: relayParser, isRelay: true },
211+
);
212+
213+
expect(qd).toMatchSnapshot();
214+
});

src/query/_shared/parseQuery.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import debug from '../../shared/debug';
99

1010
import MultilineCharacterStream from '../../shared/MultilineCharacterStream';
1111

12-
const whiteSpaceString = text => text.replace(/\S/g, ' ');
12+
const IRREGULAR_WHITESPACE = '\f\v\u0085\u00A0\ufeff\u00a0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u202f\u205f\u3000';
13+
const whiteSpaceString = text => text.replace(new RegExp(`[\\S${IRREGULAR_WHITESPACE}]`, 'g'), ' ');
1314

1415
function placeholderFragment(text: string) {
1516
const fragmentName = 'F'; // dummy frag name

0 commit comments

Comments
 (0)