Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QuoteSingle.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _errors = require("../errors");
  7. var _Node = _interopRequireDefault(require("./Node"));
  8. var _Range = _interopRequireDefault(require("./Range"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. class QuoteSingle extends _Node.default {
  11. static endOfQuote(src, offset) {
  12. let ch = src[offset];
  13. while (ch) {
  14. if (ch === "'") {
  15. if (src[offset + 1] !== "'") break;
  16. ch = src[offset += 2];
  17. } else {
  18. ch = src[offset += 1];
  19. }
  20. }
  21. return offset + 1;
  22. }
  23. /**
  24. * @returns {string | { str: string, errors: YAMLSyntaxError[] }}
  25. */
  26. get strValue() {
  27. if (!this.valueRange || !this.context) return null;
  28. const errors = [];
  29. const {
  30. start,
  31. end
  32. } = this.valueRange;
  33. const {
  34. indent,
  35. src
  36. } = this.context;
  37. if (src[end - 1] !== "'") errors.push(new _errors.YAMLSyntaxError(this, "Missing closing 'quote"));
  38. let str = '';
  39. for (let i = start + 1; i < end - 1; ++i) {
  40. const ch = src[i];
  41. if (ch === '\n') {
  42. if (_Node.default.atDocumentBoundary(src, i + 1)) errors.push(new _errors.YAMLSemanticError(this, 'Document boundary indicators are not allowed within string values'));
  43. const {
  44. fold,
  45. offset,
  46. error
  47. } = _Node.default.foldNewline(src, i, indent);
  48. str += fold;
  49. i = offset;
  50. if (error) errors.push(new _errors.YAMLSemanticError(this, 'Multi-line single-quoted string needs to be sufficiently indented'));
  51. } else if (ch === "'") {
  52. str += ch;
  53. i += 1;
  54. if (src[i] !== "'") errors.push(new _errors.YAMLSyntaxError(this, 'Unescaped single quote? This should not happen.'));
  55. } else if (ch === ' ' || ch === '\t') {
  56. // trim trailing whitespace
  57. const wsStart = i;
  58. let next = src[i + 1];
  59. while (next === ' ' || next === '\t') {
  60. i += 1;
  61. next = src[i + 1];
  62. }
  63. if (next !== '\n') str += i > wsStart ? src.slice(wsStart, i + 1) : ch;
  64. } else {
  65. str += ch;
  66. }
  67. }
  68. return errors.length > 0 ? {
  69. errors,
  70. str
  71. } : str;
  72. }
  73. /**
  74. * Parses a 'single quoted' value from the source
  75. *
  76. * @param {ParseContext} context
  77. * @param {number} start - Index of first character
  78. * @returns {number} - Index of the character after this scalar
  79. */
  80. parse(context, start) {
  81. this.context = context;
  82. const {
  83. src
  84. } = context;
  85. let offset = QuoteSingle.endOfQuote(src, start + 1);
  86. this.valueRange = new _Range.default(start, offset);
  87. offset = _Node.default.endOfWhiteSpace(src, offset);
  88. offset = this.parseComment(offset);
  89. return offset;
  90. }
  91. }
  92. exports.default = QuoteSingle;