Skip to content

Commit 36ec0e9

Browse files
committed
blockStringValue
1 parent 7c00820 commit 36ec0e9

12 files changed

+222
-112
lines changed

src/jsutils/dedent.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
* @flow
88
*/
99

10-
import removeIndentation from './removeIndentation';
10+
/**
11+
* fixes identation by removing leading spaces from each line
12+
*/
13+
function fixIdent(str: string): string {
14+
const indent = /^\n?( *)/.exec(str)[1]; // figure out ident
15+
return str
16+
.replace(RegExp('^' + indent, 'mg'), '') // remove ident
17+
.replace(/^\n*/m, '') // remove leading newline
18+
.replace(/ *$/, ''); // remove trailing spaces
19+
}
1120

1221
/**
1322
* An ES6 string tag that fixes identation. Also removes leading newlines
@@ -36,5 +45,5 @@ export default function dedent(
3645
}
3746
}
3847

39-
return removeIndentation(res) + '\n';
48+
return fixIdent(res);
4049
}

src/jsutils/removeIndentation.js

-66
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import { expect } from 'chai';
9+
import { describe, it } from 'mocha';
10+
import blockStringValue from '../blockStringValue';
11+
12+
describe('blockStringValue', () => {
13+
14+
it('removes uniform indentation from a string', () => {
15+
const rawValue = [
16+
'',
17+
' Hello,',
18+
' World!',
19+
'',
20+
' Yours,',
21+
' GraphQL.',
22+
].join('\n');
23+
expect(blockStringValue(rawValue)).to.equal([
24+
'Hello,',
25+
' World!',
26+
'',
27+
'Yours,',
28+
' GraphQL.',
29+
].join('\n'));
30+
});
31+
32+
it('removes empty leading and trailing lines', () => {
33+
const rawValue = [
34+
'',
35+
'',
36+
' Hello,',
37+
' World!',
38+
'',
39+
' Yours,',
40+
' GraphQL.',
41+
'',
42+
'',
43+
].join('\n');
44+
expect(blockStringValue(rawValue)).to.equal([
45+
'Hello,',
46+
' World!',
47+
'',
48+
'Yours,',
49+
' GraphQL.',
50+
].join('\n'));
51+
});
52+
53+
it('removes blank leading and trailing lines', () => {
54+
const rawValue = [
55+
' ',
56+
' ',
57+
' Hello,',
58+
' World!',
59+
'',
60+
' Yours,',
61+
' GraphQL.',
62+
' ',
63+
' ',
64+
].join('\n');
65+
expect(blockStringValue(rawValue)).to.equal([
66+
'Hello,',
67+
' World!',
68+
'',
69+
'Yours,',
70+
' GraphQL.',
71+
].join('\n'));
72+
});
73+
74+
it('retains indentation from first line', () => {
75+
const rawValue = [
76+
' Hello,',
77+
' World!',
78+
'',
79+
' Yours,',
80+
' GraphQL.',
81+
].join('\n');
82+
expect(blockStringValue(rawValue)).to.equal([
83+
' Hello,',
84+
' World!',
85+
'',
86+
'Yours,',
87+
' GraphQL.',
88+
].join('\n'));
89+
});
90+
91+
it('does not alter trailing spaces', () => {
92+
const rawValue = [
93+
' ',
94+
' Hello, ',
95+
' World! ',
96+
' ',
97+
' Yours, ',
98+
' GraphQL. ',
99+
' ',
100+
].join('\n');
101+
expect(blockStringValue(rawValue)).to.equal([
102+
'Hello, ',
103+
' World! ',
104+
' ',
105+
'Yours, ',
106+
' GraphQL. ',
107+
].join('\n'));
108+
});
109+
110+
});

src/language/__tests__/kitchen-sink.graphql

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ subscription StoryLikeSubscription($input: StoryLikeSubscribeInput) {
4646
}
4747

4848
fragment frag on Friend {
49-
foo(size: $size, bar: $b, obj: {key: "value", multiLine: """string"""})
49+
foo(size: $size, bar: $b, obj: {key: "value", block: """
50+
51+
block string uses \"""
52+
53+
"""})
5054
}
5155
5256
{

src/language/__tests__/lexer-test.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,12 @@ describe('Lexer', () => {
289289
);
290290
});
291291

292-
it('lexes multi-line strings', () => {
292+
it('lexes block strings', () => {
293293

294294
expect(
295295
lexOne('"""simple"""')
296296
).to.containSubset({
297-
kind: TokenKind.MULTI_LINE_STRING,
297+
kind: TokenKind.BLOCK_STRING,
298298
start: 0,
299299
end: 12,
300300
value: 'simple'
@@ -312,7 +312,7 @@ describe('Lexer', () => {
312312
expect(
313313
lexOne('"""contains " quote"""')
314314
).to.containSubset({
315-
kind: TokenKind.MULTI_LINE_STRING,
315+
kind: TokenKind.BLOCK_STRING,
316316
start: 0,
317317
end: 22,
318318
value: 'contains " quote'
@@ -321,7 +321,7 @@ describe('Lexer', () => {
321321
expect(
322322
lexOne('"""contains \\""" triplequote"""')
323323
).to.containSubset({
324-
kind: TokenKind.MULTI_LINE_STRING,
324+
kind: TokenKind.BLOCK_STRING,
325325
start: 0,
326326
end: 31,
327327
value: 'contains """ triplequote'
@@ -330,7 +330,7 @@ describe('Lexer', () => {
330330
expect(
331331
lexOne('"""multi\nline"""')
332332
).to.containSubset({
333-
kind: TokenKind.MULTI_LINE_STRING,
333+
kind: TokenKind.BLOCK_STRING,
334334
start: 0,
335335
end: 16,
336336
value: 'multi\nline'
@@ -339,7 +339,7 @@ describe('Lexer', () => {
339339
expect(
340340
lexOne('"""multi\rline\r\nnormalized"""')
341341
).to.containSubset({
342-
kind: TokenKind.MULTI_LINE_STRING,
342+
kind: TokenKind.BLOCK_STRING,
343343
start: 0,
344344
end: 28,
345345
value: 'multi\nline\nnormalized'
@@ -348,7 +348,7 @@ describe('Lexer', () => {
348348
expect(
349349
lexOne('"""unescaped \\n\\r\\b\\t\\f\\u1234"""')
350350
).to.containSubset({
351-
kind: TokenKind.MULTI_LINE_STRING,
351+
kind: TokenKind.BLOCK_STRING,
352352
start: 0,
353353
end: 32,
354354
value: 'unescaped \\n\\r\\b\\t\\f\\u1234'
@@ -357,7 +357,7 @@ describe('Lexer', () => {
357357
expect(
358358
lexOne('"""slashes \\\\ \\/"""')
359359
).to.containSubset({
360-
kind: TokenKind.MULTI_LINE_STRING,
360+
kind: TokenKind.BLOCK_STRING,
361361
start: 0,
362362
end: 19,
363363
value: 'slashes \\\\ \\/'
@@ -372,15 +372,15 @@ describe('Lexer', () => {
372372
373373
"""`)
374374
).to.containSubset({
375-
kind: TokenKind.MULTI_LINE_STRING,
375+
kind: TokenKind.BLOCK_STRING,
376376
start: 0,
377377
end: 68,
378378
value: 'spans\n multiple\n lines'
379379
});
380380

381381
});
382382

383-
it('lex reports useful multi-line string errors', () => {
383+
it('lex reports useful block string errors', () => {
384384

385385
expect(
386386
() => lexOne('"""')

src/language/__tests__/parser-test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,19 +326,19 @@ describe('Parser', () => {
326326
});
327327
});
328328

329-
it('parses multi-line strings', () => {
329+
it('parses block strings', () => {
330330
expect(parseValue('["""long""" "short"]')).to.containSubset({
331331
kind: Kind.LIST,
332332
loc: { start: 0, end: 20 },
333333
values: [
334334
{ kind: Kind.STRING,
335335
loc: { start: 1, end: 11},
336336
value: 'long',
337-
multiLine: true },
337+
block: true },
338338
{ kind: Kind.STRING,
339339
loc: { start: 12, end: 19},
340340
value: 'short',
341-
multiLine: false } ]
341+
block: false } ]
342342
});
343343
});
344344

src/language/__tests__/printer-test.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ describe('Printer', () => {
127127
}
128128
129129
fragment frag on Friend {
130-
foo(size: $size, bar: $b, obj: {key: "value", multiLine: """string"""})
130+
foo(size: $size, bar: $b, obj: {key: "value", block: """
131+
block string uses \"""
132+
"""})
131133
}
132134
133135
{

src/language/ast.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ type TokenKind = '<SOF>'
6666
| 'Int'
6767
| 'Float'
6868
| 'String'
69-
| 'MultiLineString'
69+
| 'BlockString'
7070
| 'Comment';
7171

7272
/**
@@ -289,7 +289,7 @@ export type StringValueNode = {
289289
kind: 'StringValue';
290290
loc?: Location;
291291
value: string;
292-
multiLine?: boolean;
292+
block?: boolean;
293293
};
294294

295295
export type BooleanValueNode = {

0 commit comments

Comments
 (0)