map.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. exports.__esModule = true;
  3. var mapping_1 = require("../factories/mapping");
  4. var mapping_item_1 = require("../factories/mapping-item");
  5. var position_1 = require("../factories/position");
  6. var create_slicer_1 = require("../utils/create-slicer");
  7. var extract_comments_1 = require("../utils/extract-comments");
  8. var extract_prop_comments_1 = require("../utils/extract-prop-comments");
  9. var get_last_1 = require("../utils/get-last");
  10. var pair_1 = require("./pair");
  11. function transformMap(map, context) {
  12. var cstNode = map.cstNode;
  13. cstNode.items
  14. .filter(function (item) { return item.type === "MAP_KEY" || item.type === "MAP_VALUE"; })
  15. .forEach(function (item) { return extract_prop_comments_1.extractPropComments(item, context); });
  16. var cstItemsWithoutComments = extract_comments_1.extractComments(cstNode.items, context);
  17. var groupedCstItems = groupCstItems(cstItemsWithoutComments);
  18. var mappingItems = map.items.map(function (pair, index) {
  19. var cstNodes = groupedCstItems[index];
  20. var _a = cstNodes[0].type === "MAP_VALUE"
  21. ? [null, cstNodes[0].range]
  22. : [
  23. cstNodes[0].range,
  24. cstNodes.length === 1 ? null : cstNodes[1].range,
  25. ], keyRange = _a[0], valueRange = _a[1];
  26. return pair_1.transformAstPair(pair, context, mapping_item_1.createMappingItem, keyRange, valueRange);
  27. });
  28. return mapping_1.createMapping(position_1.createPosition(mappingItems[0].position.start, get_last_1.getLast(mappingItems).position.end), context.transformContent(map), mappingItems);
  29. }
  30. exports.transformMap = transformMap;
  31. function groupCstItems(cstItems) {
  32. var groups = [];
  33. var sliceCstItems = create_slicer_1.createSlicer(cstItems, 0);
  34. var hasKey = false;
  35. for (var i = 0; i < cstItems.length; i++) {
  36. var cstItem = cstItems[i];
  37. if (cstItem.type === "MAP_VALUE") {
  38. groups.push(sliceCstItems(i + 1));
  39. hasKey = false;
  40. continue;
  41. }
  42. if (hasKey) {
  43. groups.push(sliceCstItems(i));
  44. }
  45. hasKey = true;
  46. }
  47. if (hasKey) {
  48. groups.push(sliceCstItems(Infinity));
  49. }
  50. return groups;
  51. }