123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- "use strict";
- exports.__esModule = true;
- var mapping_1 = require("../factories/mapping");
- var mapping_item_1 = require("../factories/mapping-item");
- var position_1 = require("../factories/position");
- var create_slicer_1 = require("../utils/create-slicer");
- var extract_comments_1 = require("../utils/extract-comments");
- var extract_prop_comments_1 = require("../utils/extract-prop-comments");
- var get_last_1 = require("../utils/get-last");
- var pair_1 = require("./pair");
- function transformMap(map, context) {
- var cstNode = map.cstNode;
- cstNode.items
- .filter(function (item) { return item.type === "MAP_KEY" || item.type === "MAP_VALUE"; })
- .forEach(function (item) { return extract_prop_comments_1.extractPropComments(item, context); });
- var cstItemsWithoutComments = extract_comments_1.extractComments(cstNode.items, context);
- var groupedCstItems = groupCstItems(cstItemsWithoutComments);
- var mappingItems = map.items.map(function (pair, index) {
- var cstNodes = groupedCstItems[index];
- var _a = cstNodes[0].type === "MAP_VALUE"
- ? [null, cstNodes[0].range]
- : [
- cstNodes[0].range,
- cstNodes.length === 1 ? null : cstNodes[1].range,
- ], keyRange = _a[0], valueRange = _a[1];
- return pair_1.transformAstPair(pair, context, mapping_item_1.createMappingItem, keyRange, valueRange);
- });
- return mapping_1.createMapping(position_1.createPosition(mappingItems[0].position.start, get_last_1.getLast(mappingItems).position.end), context.transformContent(map), mappingItems);
- }
- exports.transformMap = transformMap;
- function groupCstItems(cstItems) {
- var groups = [];
- var sliceCstItems = create_slicer_1.createSlicer(cstItems, 0);
- var hasKey = false;
- for (var i = 0; i < cstItems.length; i++) {
- var cstItem = cstItems[i];
- if (cstItem.type === "MAP_VALUE") {
- groups.push(sliceCstItems(i + 1));
- hasKey = false;
- continue;
- }
- if (hasKey) {
- groups.push(sliceCstItems(i));
- }
- hasKey = true;
- }
- if (hasKey) {
- groups.push(sliceCstItems(Infinity));
- }
- return groups;
- }
|