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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.checkFlowCollectionEnd = checkFlowCollectionEnd;
  6. exports.checkKeyLength = checkKeyLength;
  7. exports.resolveComments = resolveComments;
  8. var _errors = require("../errors");
  9. var _constants = require("../constants");
  10. function checkFlowCollectionEnd(errors, cst) {
  11. let char, name;
  12. switch (cst.type) {
  13. case _constants.Type.FLOW_MAP:
  14. char = '}';
  15. name = 'flow map';
  16. break;
  17. case _constants.Type.FLOW_SEQ:
  18. char = ']';
  19. name = 'flow sequence';
  20. break;
  21. default:
  22. errors.push(new _errors.YAMLSemanticError(cst, 'Not a flow collection!?'));
  23. return;
  24. }
  25. let lastItem;
  26. for (let i = cst.items.length - 1; i >= 0; --i) {
  27. const item = cst.items[i];
  28. if (!item || item.type !== _constants.Type.COMMENT) {
  29. lastItem = item;
  30. break;
  31. }
  32. }
  33. if (lastItem && lastItem.char !== char) {
  34. const msg = `Expected ${name} to end with ${char}`;
  35. let err;
  36. if (typeof lastItem.offset === 'number') {
  37. err = new _errors.YAMLSemanticError(cst, msg);
  38. err.offset = lastItem.offset + 1;
  39. } else {
  40. err = new _errors.YAMLSemanticError(lastItem, msg);
  41. if (lastItem.range && lastItem.range.end) err.offset = lastItem.range.end - lastItem.range.start;
  42. }
  43. errors.push(err);
  44. }
  45. }
  46. function checkKeyLength(errors, node, itemIdx, key, keyStart) {
  47. if (!key || typeof keyStart !== 'number') return;
  48. const item = node.items[itemIdx];
  49. let keyEnd = item && item.range && item.range.start;
  50. if (!keyEnd) {
  51. for (let i = itemIdx - 1; i >= 0; --i) {
  52. const it = node.items[i];
  53. if (it && it.range) {
  54. keyEnd = it.range.end + 2 * (itemIdx - i);
  55. break;
  56. }
  57. }
  58. }
  59. if (keyEnd > keyStart + 1024) {
  60. const k = String(key).substr(0, 8) + '...' + String(key).substr(-8);
  61. errors.push(new _errors.YAMLSemanticError(node, `The "${k}" key is too long`));
  62. }
  63. }
  64. function resolveComments(collection, comments) {
  65. for (const {
  66. afterKey,
  67. before,
  68. comment
  69. } of comments) {
  70. let item = collection.items[before];
  71. if (!item) {
  72. if (comment !== undefined) {
  73. if (collection.comment) collection.comment += '\n' + comment;else collection.comment = comment;
  74. }
  75. } else {
  76. if (afterKey && item.value) item = item.value;
  77. if (comment === undefined) {
  78. if (afterKey || !item.commentBefore) item.spaceBefore = true;
  79. } else {
  80. if (item.commentBefore) item.commentBefore += '\n' + comment;else item.commentBefore = comment;
  81. }
  82. }
  83. }
  84. }