1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.findPropertyInAstObject = exports.appendValueInAstArray = exports.removePropertyInAstObject = exports.insertPropertyInAstObjectInOrder = exports.appendPropertyInAstObject = void 0;
|
4 | function appendPropertyInAstObject(recorder, node, propertyName, value, indent) {
|
5 | const indentStr = _buildIndent(indent);
|
6 | let index = node.start.offset + 1;
|
7 | if (node.properties.length > 0) {
|
8 |
|
9 | const last = node.properties[node.properties.length - 1];
|
10 | const { text, end } = last;
|
11 | const commaIndex = text.endsWith('\n') ? end.offset - 1 : end.offset;
|
12 | recorder.insertRight(commaIndex, ',');
|
13 | index = end.offset;
|
14 | }
|
15 | const content = _stringifyContent(value, indentStr);
|
16 | recorder.insertRight(index, (node.properties.length === 0 && indent ? '\n' : '')
|
17 | + ' '.repeat(indent)
|
18 | + `"${propertyName}":${indent ? ' ' : ''}${content}`
|
19 | + indentStr.slice(0, -indent));
|
20 | }
|
21 | exports.appendPropertyInAstObject = appendPropertyInAstObject;
|
22 | function insertPropertyInAstObjectInOrder(recorder, node, propertyName, value, indent) {
|
23 | if (node.properties.length === 0) {
|
24 | appendPropertyInAstObject(recorder, node, propertyName, value, indent);
|
25 | return;
|
26 | }
|
27 |
|
28 | let insertAfterProp = null;
|
29 | let prev = null;
|
30 | let isLastProp = false;
|
31 | const last = node.properties[node.properties.length - 1];
|
32 | for (const prop of node.properties) {
|
33 | if (prop.key.value > propertyName) {
|
34 | if (prev) {
|
35 | insertAfterProp = prev;
|
36 | }
|
37 | break;
|
38 | }
|
39 | if (prop === last) {
|
40 | isLastProp = true;
|
41 | insertAfterProp = last;
|
42 | }
|
43 | prev = prop;
|
44 | }
|
45 | if (isLastProp) {
|
46 | appendPropertyInAstObject(recorder, node, propertyName, value, indent);
|
47 | return;
|
48 | }
|
49 | const indentStr = _buildIndent(indent);
|
50 | const insertIndex = insertAfterProp === null
|
51 | ? node.start.offset + 1
|
52 | : insertAfterProp.end.offset + 1;
|
53 | const content = _stringifyContent(value, indentStr);
|
54 | recorder.insertRight(insertIndex, indentStr
|
55 | + `"${propertyName}":${indent ? ' ' : ''}${content}`
|
56 | + ',');
|
57 | }
|
58 | exports.insertPropertyInAstObjectInOrder = insertPropertyInAstObjectInOrder;
|
59 | function removePropertyInAstObject(recorder, node, propertyName) {
|
60 |
|
61 | const propIdx = node.properties.findIndex(prop => prop.key.value === propertyName);
|
62 | if (propIdx === -1) {
|
63 |
|
64 | return;
|
65 | }
|
66 | if (node.properties.length === 1) {
|
67 |
|
68 | recorder.remove(node.start.offset, node.end.offset - node.start.offset);
|
69 | recorder.insertRight(node.start.offset, '{}');
|
70 | return;
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 | const previousProp = node.properties[propIdx - 1];
|
76 | const targetProp = node.properties[propIdx];
|
77 | const nextProp = node.properties[propIdx + 1];
|
78 | let start, end;
|
79 | if (previousProp) {
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 | start = previousProp.end;
|
86 | end = targetProp.end;
|
87 | }
|
88 | else {
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | start = targetProp.start;
|
96 | end = nextProp.start;
|
97 | }
|
98 | recorder.remove(start.offset, end.offset - start.offset);
|
99 | if (!nextProp) {
|
100 | recorder.insertRight(start.offset, '\n');
|
101 | }
|
102 | }
|
103 | exports.removePropertyInAstObject = removePropertyInAstObject;
|
104 | function appendValueInAstArray(recorder, node, value, indent = 4) {
|
105 | let indentStr = _buildIndent(indent);
|
106 | let index = node.start.offset + 1;
|
107 |
|
108 | let newNodes;
|
109 | if (node.elements.length > 0) {
|
110 |
|
111 | const { end } = node.elements[node.elements.length - 1];
|
112 | const isClosingOnSameLine = node.end.offset - end.offset === 1;
|
113 | if (isClosingOnSameLine && indent) {
|
114 |
|
115 | recorder.remove(node.start.offset, node.end.offset - node.start.offset);
|
116 | newNodes = [
|
117 | ...node.elements.map(({ value }) => value),
|
118 | value,
|
119 | ];
|
120 | index = node.start.offset;
|
121 |
|
122 |
|
123 | indent = indent - 2;
|
124 | indentStr = _buildIndent(indent);
|
125 | }
|
126 | else {
|
127 | recorder.insertRight(end.offset, ',');
|
128 | index = end.offset;
|
129 | }
|
130 | }
|
131 | recorder.insertRight(index, (newNodes ? '' : indentStr)
|
132 | + _stringifyContent(newNodes || value, indentStr)
|
133 | + (node.elements.length === 0 && indent ? indentStr.substr(0, -indent) + '\n' : ''));
|
134 | }
|
135 | exports.appendValueInAstArray = appendValueInAstArray;
|
136 | function findPropertyInAstObject(node, propertyName) {
|
137 | let maybeNode = null;
|
138 | for (const property of node.properties) {
|
139 | if (property.key.value == propertyName) {
|
140 | maybeNode = property.value;
|
141 | }
|
142 | }
|
143 | return maybeNode;
|
144 | }
|
145 | exports.findPropertyInAstObject = findPropertyInAstObject;
|
146 | function _buildIndent(count) {
|
147 | return count ? '\n' + ' '.repeat(count) : '';
|
148 | }
|
149 | function _stringifyContent(value, indentStr) {
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | return JSON.stringify(value, null, 2).replace(/\n/g, indentStr);
|
166 | }
|