-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RemoveIndentation() to the lexer for multi-line strings.
- Loading branch information
Showing
4 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* @flow */ | ||
/** | ||
* Copyright (c) 2017, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/** | ||
* Removes leading identation from each line in a multi-line string. | ||
* | ||
* This implements RemoveIndentation() algorithm in the GraphQL spec. | ||
* | ||
* Note: this is similar to Python's docstring "trim" operation. | ||
* https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation | ||
*/ | ||
export default function removeIndentation(rawString: string): string { | ||
// Expand a multi-line string into independent lines. | ||
const lines = rawString.split('\n'); | ||
|
||
// Determine minimum indentation, not including the first line. | ||
let minIndent; | ||
for (let i = 1; i < lines.length; i++) { | ||
const line = lines[i]; | ||
const lineIndent = leadingWhitespace(line); | ||
if ( | ||
lineIndent < line.length && | ||
(minIndent === undefined || lineIndent < minIndent) | ||
) { | ||
minIndent = lineIndent; | ||
if (minIndent === 0) { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Remove indentation, not including the first line. | ||
if (minIndent) { | ||
for (let i = 1; i < lines.length; i++) { | ||
lines[i] = lines[i].slice(minIndent); | ||
} | ||
} | ||
|
||
// Remove trailing and leading empty lines. | ||
while (lines.length > 0 && lines[lines.length - 1].length === 0) { | ||
lines.pop(); | ||
} | ||
while (lines.length > 0 && lines[0].length === 0) { | ||
lines.shift(); | ||
} | ||
|
||
// Return a multi-line string. | ||
return lines.join('\n'); | ||
} | ||
|
||
function leadingWhitespace(str) { | ||
let i = 0; | ||
for (; i < str.length; i++) { | ||
if (str[i] !== ' ' && str[i] !== '\t') { | ||
break; | ||
} | ||
} | ||
return i; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters