goldstein 5.0.2 → 5.2.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.
package/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2024.02.20, v5.2.0
2
+
3
+ feature:
4
+ - 5728421 goldstein: improve support of comments
5
+ - 0d997c3 goldstein: printer: visitors: await-expression: maybeVisitor
6
+
7
+ 2024.02.19, v5.1.0
8
+
9
+ feature:
10
+ - c329ff0 goldstein: printer: visitors: try-statememnt: improve support
11
+
1
12
  2024.02.19, v5.0.2
2
13
 
3
14
  feature:
package/build/parser.cjs CHANGED
@@ -5380,6 +5380,75 @@ Parser.acorn = {
5380
5380
  nonASCIIwhitespace
5381
5381
  };
5382
5382
 
5383
+ // node_modules/estree-util-attach-comments/lib/index.js
5384
+ var own = {}.hasOwnProperty;
5385
+ var emptyComments = [];
5386
+ function attachComments(tree, comments) {
5387
+ const list = comments ? [...comments].sort(compare) : emptyComments;
5388
+ if (list.length > 0)
5389
+ walk(tree, { comments: list, index: 0 });
5390
+ }
5391
+ function walk(node, state) {
5392
+ if (state.index === state.comments.length) {
5393
+ return;
5394
+ }
5395
+ const children = [];
5396
+ const comments = [];
5397
+ let key;
5398
+ for (key in node) {
5399
+ if (own.call(node, key)) {
5400
+ const value = node[key];
5401
+ if (value && typeof value === "object" && key !== "comments") {
5402
+ if (Array.isArray(value)) {
5403
+ let index2 = -1;
5404
+ while (++index2 < value.length) {
5405
+ if (value[index2] && typeof value[index2].type === "string") {
5406
+ children.push(value[index2]);
5407
+ }
5408
+ }
5409
+ } else if (typeof value.type === "string") {
5410
+ children.push(value);
5411
+ }
5412
+ }
5413
+ }
5414
+ }
5415
+ children.sort(compare);
5416
+ comments.push(...slice(state, node, false, { leading: true, trailing: false }));
5417
+ let index = -1;
5418
+ while (++index < children.length) {
5419
+ walk(children[index], state);
5420
+ }
5421
+ comments.push(
5422
+ ...slice(state, node, true, {
5423
+ leading: false,
5424
+ trailing: children.length > 0
5425
+ })
5426
+ );
5427
+ if (comments.length > 0) {
5428
+ node.comments = comments;
5429
+ }
5430
+ }
5431
+ function slice(state, node, compareEnd, fields) {
5432
+ const result = [];
5433
+ while (state.comments[state.index] && compare(state.comments[state.index], node, compareEnd) < 1) {
5434
+ result.push(Object.assign({}, state.comments[state.index++], fields));
5435
+ }
5436
+ return result;
5437
+ }
5438
+ function compare(left, right, compareEnd) {
5439
+ const field = compareEnd ? "end" : "start";
5440
+ if (left.range && right.range) {
5441
+ return left.range[0] - right.range[compareEnd ? 1 : 0];
5442
+ }
5443
+ if (left.loc && left.loc.start && right.loc && right.loc[field]) {
5444
+ return left.loc.start.line - right.loc[field].line || left.loc.start.column - right.loc[field].column;
5445
+ }
5446
+ if ("start" in left && field in right) {
5447
+ return left.start - right[field];
5448
+ }
5449
+ return Number.NaN;
5450
+ }
5451
+
5383
5452
  // packages/parser/index.js
5384
5453
  var extendParser = (keywords3) => {
5385
5454
  const parser = Parser.extend(...keywords3);
@@ -5396,15 +5465,13 @@ var createParse = (parser) => (source) => {
5396
5465
  locations: true,
5397
5466
  comment: true,
5398
5467
  ranges: true,
5399
- onComment: (a) => {
5400
- comments.push(a);
5401
- }
5468
+ onComment: comments
5402
5469
  };
5403
- const result = parser.parse(source, options);
5404
- const tokens = Array.from(parser.tokenizer(source, options));
5470
+ const ast = parser.parse(source, options);
5471
+ attachComments(ast, comments);
5405
5472
  return {
5406
- ...result,
5407
- tokens,
5473
+ ...ast,
5474
+ tokens: Array.from(parser.tokenizer(source, options)),
5408
5475
  comments
5409
5476
  };
5410
5477
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goldstein",
3
- "version": "5.0.2",
3
+ "version": "5.2.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "JavaScript with no limits",
@@ -37,6 +37,7 @@
37
37
  "esbuild": "^0.20.1",
38
38
  "esbuild-node-builtins": "^0.1.0",
39
39
  "estree-to-babel": "^9.0.0",
40
+ "estree-util-attach-comments": "^3.0.0",
40
41
  "putout": "^35.0.0",
41
42
  "try-catch": "^3.0.1"
42
43
  },
@@ -1,4 +1,5 @@
1
1
  import {Parser} from 'acorn';
2
+ import {attachComments} from 'estree-util-attach-comments';
2
3
 
3
4
  export const extendParser = (keywords) => {
4
5
  const parser = Parser.extend(...keywords);
@@ -17,17 +18,16 @@ const createParse = (parser) => (source) => {
17
18
  locations: true,
18
19
  comment: true,
19
20
  ranges: true,
20
- onComment: (a) => {
21
- comments.push(a);
22
- },
21
+ onComment: comments,
23
22
  };
24
23
 
25
- const result = parser.parse(source, options);
26
- const tokens = Array.from(parser.tokenizer(source, options));
24
+ const ast = parser.parse(source, options);
25
+
26
+ attachComments(ast, comments);
27
27
 
28
28
  return {
29
- ...result,
30
- tokens,
29
+ ...ast,
30
+ tokens: Array.from(parser.tokenizer(source, options)),
31
31
  comments,
32
32
  };
33
33
  };
@@ -1,10 +1,13 @@
1
- import {visitors as v} from '@putout/printer';
1
+ import {
2
+ maybeVisitor,
3
+ visitors as v,
4
+ } from '@putout/printer';
2
5
 
3
6
  export const AwaitExpression = (path, printer, semantics) => {
4
7
  const {print} = printer;
5
8
 
6
9
  if (!path.node.goldstein)
7
- return v.AwaitExpression(path, printer, semantics);
10
+ return maybeVisitor(v.AwaitExpression, path, printer, semantics);
8
11
 
9
12
  print('__goldstein');
10
13
  };
@@ -1,11 +1,14 @@
1
- import {visitors as v} from '@putout/printer';
1
+ import {
2
+ visitors as v,
3
+ maybeVisitor,
4
+ } from '@putout/printer';
2
5
 
3
6
  export const TryStatement = (path, printer, semantics) => {
4
7
  const {maybe, print} = printer;
5
8
  const {node} = path;
6
9
 
7
10
  if (!node.expression)
8
- return v.TryStatement(path, printer, semantics);
11
+ return maybeVisitor(v.TryStatement, path, printer, semantics);
9
12
 
10
13
  print('try ');
11
14
  maybe.print(node.async, 'await ');