@bablr/agast-helpers 0.3.0 → 0.3.1

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.
package/lib/builders.js CHANGED
@@ -19,18 +19,32 @@ export const buildEffect = (value) => {
19
19
 
20
20
  export const buildWriteEffect = (text, options = {}) => {
21
21
  return buildEffect(
22
- freeze({ verb: 'write', value: freeze({ text, options: buildEmbeddedExpression(options) }) }),
22
+ buildEmbeddedExpression(
23
+ freeze({
24
+ verb: 'write',
25
+ value: buildEmbeddedExpression(
26
+ freeze({ text, options: buildEmbeddedExpression(freeze(options)) }),
27
+ ),
28
+ }),
29
+ ),
23
30
  );
24
31
  };
25
32
 
26
33
  export const buildAnsiPushEffect = (spans = '') => {
27
34
  return buildEffect(
28
- freeze({ verb: 'ansi-push', value: { spans: spans === '' ? [] : spans.split(' ') } }),
35
+ buildEmbeddedExpression(
36
+ freeze({
37
+ verb: 'ansi-push',
38
+ value: buildEmbeddedExpression(
39
+ freeze({ spans: spans === '' ? freeze([]) : freeze(spans.split(' ')) }),
40
+ ),
41
+ }),
42
+ ),
29
43
  );
30
44
  };
31
45
 
32
46
  export const buildAnsiPopEffect = () => {
33
- return buildEffect(freeze({ verb: 'ansi-pop', value: undefined }));
47
+ return buildEffect(buildEmbeddedExpression(freeze({ verb: 'ansi-pop', value: undefined })));
34
48
  };
35
49
 
36
50
  export const buildTokenGroup = (tokens) => {
package/lib/stream.js CHANGED
@@ -4,6 +4,11 @@ import { printSelfClosingNodeTag, printTerminal } from './print.js';
4
4
  import { buildWriteEffect, buildTokenGroup } from './builders.js';
5
5
  export * from './print.js';
6
6
 
7
+ const getEmbeddedExpression = (obj) => {
8
+ if (obj.type !== 'EmbeddedExpression') throw new Error();
9
+ return obj.value;
10
+ };
11
+
7
12
  export const getStreamIterator = (obj) => {
8
13
  return obj[Symbol.for('@@streamIterator')]?.() || obj[Symbol.iterator]?.();
9
14
  };
@@ -158,9 +163,12 @@ function* __generateStandardOutput(terminals) {
158
163
  const terminal = co.value;
159
164
 
160
165
  if (terminal.type === 'Effect') {
161
- const effect = terminal.value;
162
- if (effect.verb === 'write' && (effect.value.stream == null || effect.value.stream === 1)) {
163
- yield* effect.value.text;
166
+ const effect = getEmbeddedExpression(terminal.value);
167
+ if (effect.verb === 'write') {
168
+ const writeEffect = getEmbeddedExpression(effect.value);
169
+ if (writeEffect.stream == null || writeEffect.stream === 1) {
170
+ yield* writeEffect.text;
171
+ }
164
172
  }
165
173
  }
166
174
  }
@@ -185,14 +193,15 @@ function* __generateAllOutput(terminals) {
185
193
  const terminal = co.value;
186
194
 
187
195
  if (terminal.type === 'Effect') {
188
- const effect = terminal.value;
196
+ const effect = getEmbeddedExpression(terminal.value);
189
197
  if (effect.verb === 'write') {
198
+ const writeEffect = getEmbeddedExpression(effect.value);
190
199
  const prevStream = currentStream;
191
- currentStream = effect.value.options.stream || 1;
192
- if (prevStream && prevStream !== currentStream && !effect.value.text.startsWith('\n')) {
200
+ currentStream = getEmbeddedExpression(writeEffect.options).stream || 1;
201
+ if (prevStream && prevStream !== currentStream && !writeEffect.text.startsWith('\n')) {
193
202
  yield* '\n';
194
203
  }
195
- yield* effect.value.text;
204
+ yield* writeEffect.text;
196
205
  }
197
206
  }
198
207
  }
@@ -334,13 +343,17 @@ function* __prettyGroupTokens(terminals) {
334
343
  state.holding = [];
335
344
  }
336
345
 
337
- if (!isOpenClose) {
346
+ if (!isOpenClose && terminal.type !== 'Effect') {
338
347
  yield terminal;
339
348
  }
340
- } else if (!isOpenClose) {
349
+ } else if (!isOpenClose && terminal.type !== 'Effect') {
341
350
  state.holding.push(terminal);
342
351
  }
343
352
 
353
+ if (terminal.type === 'Effect') {
354
+ yield terminal;
355
+ }
356
+
344
357
  if (terminal.type === 'CloseNodeTag') {
345
358
  if (!state.broken && (isIntrinsicToken(state.open) || state.holding.length === 1)) {
346
359
  state.holding.push(terminal);
@@ -391,9 +404,13 @@ function* __generatePrettyCSTMLStrategy(terminals, options) {
391
404
  const terminal = co.value;
392
405
 
393
406
  if (terminal.type === 'Effect') {
394
- const effect = terminal.value;
407
+ const effect = getEmbeddedExpression(terminal.value);
395
408
  if (emitEffects && effect.verb === 'write') {
396
- yield buildWriteEffect((first ? '' : '\n') + effect.value.text, effect.value.options);
409
+ const writeEffect = getEmbeddedExpression(effect.value);
410
+ yield buildWriteEffect(
411
+ (first ? '' : '\n') + writeEffect.text,
412
+ getEmbeddedExpression(writeEffect.options),
413
+ );
397
414
 
398
415
  inline = false;
399
416
  first = false;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bablr/agast-helpers",
3
3
  "description": "Helper functions for working with agAST trees",
4
- "version": "0.3.0",
4
+ "version": "0.3.1",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [