@bablr/agast-helpers 0.3.0 → 0.3.2

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
  }
@@ -249,10 +258,6 @@ export const stringFromStream = (stream) => {
249
258
  function* __generateCSTMLStrategy(terminals, options) {
250
259
  let { emitEffects = false, inline: inlineOption = true } = options;
251
260
 
252
- if (emitEffects) {
253
- throw new Error('You must use generatePrettyCSTML with emitEffects');
254
- }
255
-
256
261
  if (!terminals) {
257
262
  yield buildWriteEffect('<//>');
258
263
  return;
@@ -297,6 +302,9 @@ function* __generateCSTMLStrategy(terminals, options) {
297
302
  yield buildWriteEffect('\n');
298
303
  }
299
304
 
305
+ export const generateCSTMLStrategy = (terminals, options = {}) =>
306
+ new StreamIterable(__generateCSTMLStrategy(terminals, options));
307
+
300
308
  export const prettyGroupTokens = (terminals) => new StreamIterable(__prettyGroupTokens(terminals));
301
309
 
302
310
  function* __prettyGroupTokens(terminals) {
@@ -334,13 +342,17 @@ function* __prettyGroupTokens(terminals) {
334
342
  state.holding = [];
335
343
  }
336
344
 
337
- if (!isOpenClose) {
345
+ if (!isOpenClose && terminal.type !== 'Effect') {
338
346
  yield terminal;
339
347
  }
340
- } else if (!isOpenClose) {
348
+ } else if (!isOpenClose && terminal.type !== 'Effect') {
341
349
  state.holding.push(terminal);
342
350
  }
343
351
 
352
+ if (terminal.type === 'Effect') {
353
+ yield terminal;
354
+ }
355
+
344
356
  if (terminal.type === 'CloseNodeTag') {
345
357
  if (!state.broken && (isIntrinsicToken(state.open) || state.holding.length === 1)) {
346
358
  state.holding.push(terminal);
@@ -363,9 +375,6 @@ function* __prettyGroupTokens(terminals) {
363
375
  }
364
376
  }
365
377
 
366
- export const generateCSTMLStrategy = (terminals, options = {}) =>
367
- new StreamIterable(__generateCSTMLStrategy(terminals, options));
368
-
369
378
  function* __generatePrettyCSTMLStrategy(terminals, options) {
370
379
  let { indent = ' ', emitEffects = false, inline: inlineOption = true } = options;
371
380
 
@@ -391,9 +400,13 @@ function* __generatePrettyCSTMLStrategy(terminals, options) {
391
400
  const terminal = co.value;
392
401
 
393
402
  if (terminal.type === 'Effect') {
394
- const effect = terminal.value;
403
+ const effect = getEmbeddedExpression(terminal.value);
395
404
  if (emitEffects && effect.verb === 'write') {
396
- yield buildWriteEffect((first ? '' : '\n') + effect.value.text, effect.value.options);
405
+ const writeEffect = getEmbeddedExpression(effect.value);
406
+ yield buildWriteEffect(
407
+ (first ? '' : '\n') + writeEffect.text,
408
+ getEmbeddedExpression(writeEffect.options),
409
+ );
397
410
 
398
411
  inline = false;
399
412
  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.2",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [