123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true,
- });
- exports.dedentBlockStringLines = dedentBlockStringLines;
- exports.isPrintableAsBlockString = isPrintableAsBlockString;
- exports.printBlockString = printBlockString;
- var _characterClasses = require('./characterClasses.js');
- function dedentBlockStringLines(lines) {
- var _firstNonEmptyLine2;
- let commonIndent = Number.MAX_SAFE_INTEGER;
- let firstNonEmptyLine = null;
- let lastNonEmptyLine = -1;
- for (let i = 0; i < lines.length; ++i) {
- var _firstNonEmptyLine;
- const line = lines[i];
- const indent = leadingWhitespace(line);
- if (indent === line.length) {
- continue;
- }
- firstNonEmptyLine =
- (_firstNonEmptyLine = firstNonEmptyLine) !== null &&
- _firstNonEmptyLine !== void 0
- ? _firstNonEmptyLine
- : i;
- lastNonEmptyLine = i;
- if (i !== 0 && indent < commonIndent) {
- commonIndent = indent;
- }
- }
- return lines
- .map((line, i) => (i === 0 ? line : line.slice(commonIndent)))
- .slice(
- (_firstNonEmptyLine2 = firstNonEmptyLine) !== null &&
- _firstNonEmptyLine2 !== void 0
- ? _firstNonEmptyLine2
- : 0,
- lastNonEmptyLine + 1,
- );
- }
- function leadingWhitespace(str) {
- let i = 0;
- while (
- i < str.length &&
- (0, _characterClasses.isWhiteSpace)(str.charCodeAt(i))
- ) {
- ++i;
- }
- return i;
- }
- function isPrintableAsBlockString(value) {
- if (value === '') {
- return true;
- }
- let isEmptyLine = true;
- let hasIndent = false;
- let hasCommonIndent = true;
- let seenNonEmptyLine = false;
- for (let i = 0; i < value.length; ++i) {
- switch (value.codePointAt(i)) {
- case 0x0000:
- case 0x0001:
- case 0x0002:
- case 0x0003:
- case 0x0004:
- case 0x0005:
- case 0x0006:
- case 0x0007:
- case 0x0008:
- case 0x000b:
- case 0x000c:
- case 0x000e:
- case 0x000f:
- return false;
-
- case 0x000d:
-
- return false;
-
- case 10:
-
- if (isEmptyLine && !seenNonEmptyLine) {
- return false;
- }
- seenNonEmptyLine = true;
- isEmptyLine = true;
- hasIndent = false;
- break;
- case 9:
- case 32:
-
- hasIndent || (hasIndent = isEmptyLine);
- break;
- default:
- hasCommonIndent && (hasCommonIndent = hasIndent);
- isEmptyLine = false;
- }
- }
- if (isEmptyLine) {
- return false;
- }
- if (hasCommonIndent && seenNonEmptyLine) {
- return false;
- }
- return true;
- }
- function printBlockString(value, options) {
- const escapedValue = value.replace(/"""/g, '\\"""');
- const lines = escapedValue.split(/\r\n|[\n\r]/g);
- const isSingleLine = lines.length === 1;
- const forceLeadingNewLine =
- lines.length > 1 &&
- lines
- .slice(1)
- .every(
- (line) =>
- line.length === 0 ||
- (0, _characterClasses.isWhiteSpace)(line.charCodeAt(0)),
- );
- const hasTrailingTripleQuotes = escapedValue.endsWith('\\"""');
- const hasTrailingQuote = value.endsWith('"') && !hasTrailingTripleQuotes;
- const hasTrailingSlash = value.endsWith('\\');
- const forceTrailingNewline = hasTrailingQuote || hasTrailingSlash;
- const printAsMultipleLines =
- !(options !== null && options !== void 0 && options.minimize) &&
- (!isSingleLine ||
- value.length > 70 ||
- forceTrailingNewline ||
- forceLeadingNewLine ||
- hasTrailingTripleQuotes);
- let result = '';
- const skipLeadingNewLine =
- isSingleLine && (0, _characterClasses.isWhiteSpace)(value.charCodeAt(0));
- if ((printAsMultipleLines && !skipLeadingNewLine) || forceLeadingNewLine) {
- result += '\n';
- }
- result += escapedValue;
- if (printAsMultipleLines || forceTrailingNewline) {
- result += '\n';
- }
- return '"""' + result + '"""';
- }
|