@fulcro/transform-core 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,161 +0,0 @@
1
- "use strict";
2
- /**
3
- * Text produced by rewriting a source file, with the map back to the original.
4
- *
5
- * A rewrite that happens before type checking changes the text the checker,
6
- * the emitter and the editor all read, and every one of them reports positions
7
- * in it. For those positions to mean anything to the person who wrote the
8
- * original, the rewrite keeps a record of where each piece of its output came
9
- * from — the same idea as a source map, kept as offsets rather than lines,
10
- * because the language service speaks in offsets.
11
- *
12
- * The output is made of two kinds of piece. A **copy** is original text moved
13
- * verbatim, and maps offset for offset. A **region** is the output of one
14
- * rewritten node; any position inside it that is not also inside a copy — the
15
- * synthesized `T.add(` around two operands — maps to the start of the node it
16
- * replaced, which is where an error about it belongs.
17
- */
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.composeRewrites = exports.createTextBuilder = void 0;
20
- /**
21
- * Finds the last entry whose start is at or before an offset.
22
- *
23
- * @param entries Entries sorted by the key.
24
- * @param key The start of an entry.
25
- * @param offset Offset looked up.
26
- * @returns The index, or -1.
27
- */
28
- const floorIndex = (entries, key, offset) => {
29
- let low = 0;
30
- let high = entries.length - 1;
31
- let found = -1;
32
- while (low <= high) {
33
- const middle = (low + high) >> 1;
34
- if (key(entries[middle]) <= offset) {
35
- found = middle;
36
- low = middle + 1;
37
- }
38
- else {
39
- high = middle - 1;
40
- }
41
- }
42
- return found;
43
- };
44
- /**
45
- * Starts the output of a rewrite over an original text.
46
- *
47
- * @param original The text being rewritten.
48
- * @returns The builder.
49
- */
50
- const createTextBuilder = (original) => {
51
- const pieces = [];
52
- const copies = [];
53
- const regions = [];
54
- let position = 0;
55
- const copy = (start, end) => {
56
- if (end <= start)
57
- return;
58
- copies.push({
59
- originalStart: start,
60
- generatedStart: position,
61
- length: end - start,
62
- });
63
- pieces.push(original.slice(start, end));
64
- position += end - start;
65
- };
66
- const write = (text) => {
67
- pieces.push(text);
68
- position += text.length;
69
- };
70
- const open = (originalStart, originalEnd) => {
71
- const generatedStart = position;
72
- return () => {
73
- regions.push({
74
- originalStart,
75
- originalEnd,
76
- generatedStart,
77
- generatedEnd: position,
78
- });
79
- };
80
- };
81
- const build = () => {
82
- const text = pieces.join('');
83
- // A region closes after the regions nested in it, so sorting by start —
84
- // and, at equal starts, the wider first — puts the innermost last among
85
- // those containing any given offset.
86
- const byGenerated = [...regions].sort((left, right) => left.generatedStart - right.generatedStart ||
87
- right.generatedEnd - left.generatedEnd);
88
- const byOriginal = [...regions].sort((left, right) => left.originalStart - right.originalStart ||
89
- right.originalEnd - left.originalEnd);
90
- const innermost = (sorted, start, end, offset) => {
91
- let best;
92
- for (const region of sorted) {
93
- if (start(region) > offset)
94
- break;
95
- if (offset < end(region))
96
- best = region;
97
- }
98
- return best;
99
- };
100
- const toOriginal = (generated) => {
101
- const index = floorIndex(copies, (entry) => entry.generatedStart, generated);
102
- const candidate = copies[index];
103
- if (candidate !== undefined &&
104
- generated < candidate.generatedStart + candidate.length) {
105
- return candidate.originalStart + (generated - candidate.generatedStart);
106
- }
107
- const region = innermost(byGenerated, (entry) => entry.generatedStart, (entry) => entry.generatedEnd, generated);
108
- if (region !== undefined)
109
- return region.originalStart;
110
- // Past the last copy, or inside text written outside any region:
111
- // the end of the copy before it is the nearest original position.
112
- return candidate === undefined
113
- ? 0
114
- : candidate.originalStart + candidate.length;
115
- };
116
- const toGenerated = (originalOffset) => {
117
- // Copies are in output order, so the first one holding the offset is
118
- // the earliest in the output — which is the one that counts when a
119
- // node was written twice, as the target of a compound assignment is.
120
- //
121
- // A copy that contains the offset wins over one that merely ends at
122
- // it: the start of an operand is also the end of the copy before its
123
- // operator, and answering with that copy's end would land on the
124
- // synthesized text in front of the operand rather than on the operand.
125
- // Only when nothing contains it — the position just after the last
126
- // word of a file — does a copy ending there count.
127
- const holder = copies.find((entry) => entry.originalStart <= originalOffset &&
128
- originalOffset < entry.originalStart + entry.length) ??
129
- copies.find((entry) => entry.originalStart <= originalOffset &&
130
- originalOffset === entry.originalStart + entry.length);
131
- if (holder !== undefined) {
132
- return holder.generatedStart + (originalOffset - holder.originalStart);
133
- }
134
- const region = innermost(byOriginal, (entry) => entry.originalStart, (entry) => entry.originalEnd, originalOffset);
135
- return region === undefined ? originalOffset : region.generatedStart;
136
- };
137
- return { text, toOriginal, toGenerated };
138
- };
139
- return {
140
- copy,
141
- write,
142
- open,
143
- rewrote: () => regions.length > 0,
144
- build,
145
- };
146
- };
147
- exports.createTextBuilder = createTextBuilder;
148
- /**
149
- * Chains two rewrites of the same file, the second applied to the output of the
150
- * first, into one map from the last text back to the original.
151
- *
152
- * @param first The earlier rewrite.
153
- * @param second The later one, over the earlier one's text.
154
- * @returns The combined rewrite.
155
- */
156
- const composeRewrites = (first, second) => ({
157
- text: second.text,
158
- toOriginal: (generated) => first.toOriginal(second.toOriginal(generated)),
159
- toGenerated: (original) => second.toGenerated(first.toGenerated(original)),
160
- });
161
- exports.composeRewrites = composeRewrites;