swc/bundler/tests/.cache/deno/987fe90be7227917481e81f94403aeafa64835ca.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

103 lines
2.7 KiB
TypeScript

// Loaded from https://deno.land/x/graphql_deno@v15.0.0/lib/language/blockString.js
/**
* Produces the value of a block string from its parsed raw value, similar to
* CoffeeScript's block string, Python's docstring trim or Ruby's strip_heredoc.
*
* This implements the GraphQL spec's BlockStringValue() static algorithm.
*
* @internal
*/
export function dedentBlockStringValue(rawString) {
// Expand a block string's raw value into independent lines.
const lines = rawString.split(/\r\n|[\n\r]/g); // Remove common indentation from all lines but first.
const commonIndent = getBlockStringIndentation(lines);
if (commonIndent !== 0) {
for (let i = 1; i < lines.length; i++) {
lines[i] = lines[i].slice(commonIndent);
}
} // Remove leading and trailing blank lines.
while (lines.length > 0 && isBlank(lines[0])) {
lines.shift();
}
while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
lines.pop();
} // Return a string of the lines joined with U+000A.
return lines.join('\n');
}
/**
* @internal
*/
export function getBlockStringIndentation(lines) {
let commonIndent = null;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
const indent = leadingWhitespace(line);
if (indent === line.length) {
continue; // skip empty lines
}
if (commonIndent === null || indent < commonIndent) {
commonIndent = indent;
if (commonIndent === 0) {
break;
}
}
}
return commonIndent === null ? 0 : commonIndent;
}
function leadingWhitespace(str) {
let i = 0;
while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
i++;
}
return i;
}
function isBlank(str) {
return leadingWhitespace(str) === str.length;
}
/**
* Print a block string in the indented block form by adding a leading and
* trailing blank line. However, if a block string starts with whitespace and is
* a single-line, adding a leading blank line would strip that whitespace.
*
* @internal
*/
export function printBlockString(value, indentation = '', preferMultipleLines = false) {
const isSingleLine = value.indexOf('\n') === -1;
const hasLeadingSpace = value[0] === ' ' || value[0] === '\t';
const hasTrailingQuote = value[value.length - 1] === '"';
const printAsMultipleLines = !isSingleLine || hasTrailingQuote || preferMultipleLines;
let result = ''; // Format a multi-line block quote to account for leading space.
if (printAsMultipleLines && !(isSingleLine && hasLeadingSpace)) {
result += '\n' + indentation;
}
result += indentation ? value.replace(/\n/g, '\n' + indentation) : value;
if (printAsMultipleLines) {
result += '\n';
}
return '"""' + result.replace(/"""/g, '\\"""') + '"""';
}