goldstein 4.3.0 → 4.5.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
+ 2023.10.03, v4.5.0
2
+
3
+ feature:
4
+ - c55a3d1 goldstein: parse: type
5
+ - db6bcbe goldstein: types
6
+
7
+ 2023.10.03, v4.4.0
8
+
9
+ feature:
10
+ - 02f16ef goldstein: add convert js to gs
11
+
1
12
  2023.10.03, v4.3.0
2
13
 
3
14
  feature:
package/build/parser.cjs CHANGED
@@ -1,6 +1,8 @@
1
+ var __create = Object.create;
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
4
6
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
7
  var __export = (target, all) => {
6
8
  for (var name in all)
@@ -14,6 +16,14 @@ var __copyProps = (to, from, except, desc) => {
14
16
  }
15
17
  return to;
16
18
  };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
17
27
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
28
 
19
29
  // packages/goldstein/parser.js
@@ -23,6 +33,7 @@ __export(parser_exports, {
23
33
  parse: () => parse3
24
34
  });
25
35
  module.exports = __toCommonJS(parser_exports);
36
+ var import_estree_to_babel = __toESM(require("estree-to-babel"), 1);
26
37
 
27
38
  // node_modules/acorn/dist/acorn.mjs
28
39
  var astralIdentifierCodes = [509, 0, 227, 0, 150, 4, 294, 9, 1368, 2, 2, 1, 6, 3, 41, 2, 5, 0, 166, 1, 574, 3, 9, 9, 370, 1, 81, 2, 71, 10, 50, 3, 123, 2, 54, 14, 32, 10, 3, 1, 11, 3, 46, 10, 8, 0, 46, 9, 7, 2, 37, 13, 2, 9, 6, 1, 45, 0, 13, 2, 49, 13, 9, 3, 2, 11, 83, 11, 7, 0, 3, 0, 158, 11, 6, 9, 7, 3, 56, 1, 2, 6, 3, 1, 3, 2, 10, 0, 11, 1, 3, 6, 4, 4, 193, 17, 10, 9, 5, 0, 82, 19, 13, 9, 214, 6, 3, 8, 28, 1, 83, 16, 16, 9, 82, 12, 9, 9, 84, 14, 5, 9, 243, 14, 166, 9, 71, 5, 2, 1, 3, 3, 2, 0, 2, 1, 13, 9, 120, 6, 3, 6, 4, 0, 29, 9, 41, 6, 2, 3, 9, 0, 10, 10, 47, 15, 406, 7, 2, 7, 17, 9, 57, 21, 2, 13, 123, 5, 4, 0, 2, 1, 2, 6, 2, 0, 9, 9, 49, 4, 2, 1, 2, 4, 9, 9, 330, 3, 10, 1, 2, 0, 49, 6, 4, 4, 14, 9, 5351, 0, 7, 14, 13835, 9, 87, 9, 39, 4, 60, 6, 26, 9, 1014, 0, 2, 54, 8, 3, 82, 0, 12, 1, 19628, 1, 4706, 45, 3, 22, 543, 4, 4, 5, 9, 7, 3, 6, 31, 3, 149, 2, 1418, 49, 513, 54, 5, 49, 9, 0, 15, 0, 23, 4, 2, 14, 1361, 6, 2, 16, 3, 6, 2, 1, 2, 4, 101, 0, 161, 6, 10, 9, 357, 0, 62, 13, 499, 13, 983, 6, 110, 6, 6, 9, 4759, 9, 787719, 239];
@@ -5469,11 +5480,55 @@ function newSpeak(Parser3) {
5469
5480
  }
5470
5481
 
5471
5482
  // packages/keyword-try/index.js
5483
+ var import_putout2 = require("putout");
5484
+
5485
+ // packages/types/try.js
5472
5486
  var import_putout = require("putout");
5473
5487
  var {
5474
- isCallExpression,
5475
- isAwaitExpression
5488
+ isAwaitExpression,
5489
+ isCallExpression
5476
5490
  } = import_putout.types;
5491
+ function createGoldsteinTry({ async, callee, args }) {
5492
+ return {
5493
+ type: "TryStatement",
5494
+ expression: true,
5495
+ async,
5496
+ argument: {
5497
+ type: "CallExpression",
5498
+ callee,
5499
+ arguments: args
5500
+ }
5501
+ };
5502
+ }
5503
+ var setGoldsteinTry = (node) => {
5504
+ if (!isCallExpression(node) && !isAwaitExpression(node))
5505
+ return;
5506
+ node.goldstein = createGoldsteinTry(parseExpression(node));
5507
+ };
5508
+ var parseExpression = (node) => {
5509
+ const { argument, arguments: allArgs } = node;
5510
+ const async = isAwaitExpression(node);
5511
+ if (!async) {
5512
+ const [callee2, ...args2] = allArgs;
5513
+ return {
5514
+ async,
5515
+ args: args2,
5516
+ callee: callee2
5517
+ };
5518
+ }
5519
+ const [callee, ...args] = argument.arguments;
5520
+ return {
5521
+ async,
5522
+ args,
5523
+ callee
5524
+ };
5525
+ };
5526
+
5527
+ // packages/keyword-try/index.js
5528
+ var {
5529
+ isCallExpression: isCallExpression2,
5530
+ isAwaitExpression: isAwaitExpression2
5531
+ } = import_putout2.types;
5477
5532
  function keywordTry(Parser3) {
5478
5533
  const { keywordTypes } = Parser3.acorn;
5479
5534
  return class extends Parser3 {
@@ -5488,7 +5543,7 @@ function keywordTry(Parser3) {
5488
5543
  if (this.type === types$1.braceL)
5489
5544
  return this.parseUglyTry(node);
5490
5545
  const expression = this.parseExpression();
5491
- if (isCallExpression(expression))
5546
+ if (isCallExpression2(expression))
5492
5547
  node.expression = {
5493
5548
  type: "CallExpression",
5494
5549
  callee: {
@@ -5498,21 +5553,11 @@ function keywordTry(Parser3) {
5498
5553
  arguments: [
5499
5554
  expression.callee,
5500
5555
  ...expression.arguments
5501
- ],
5502
- goldstein: createGoldsteinNode({
5503
- await: false,
5504
- callee: expression.callee,
5505
- arguments: expression.arguments
5506
- })
5556
+ ]
5507
5557
  };
5508
- else if (isAwaitExpression(expression))
5558
+ else if (isAwaitExpression2(expression))
5509
5559
  node.expression = {
5510
5560
  type: "AwaitExpression",
5511
- goldstein: createGoldsteinNode({
5512
- await: true,
5513
- callee: expression.argument.callee,
5514
- arguments: expression.argument.arguments
5515
- }),
5516
5561
  argument: {
5517
5562
  type: "CallExpression",
5518
5563
  callee: {
@@ -5527,6 +5572,7 @@ function keywordTry(Parser3) {
5527
5572
  };
5528
5573
  else
5529
5574
  this.raise(this.start, `After 'try' only '{', 'await' and 'function call' can come`);
5575
+ setGoldsteinTry(node.expression);
5530
5576
  return super.finishNode(node, "ExpressionStatement");
5531
5577
  }
5532
5578
  parseUglyTry(node) {
@@ -5552,29 +5598,18 @@ function keywordTry(Parser3) {
5552
5598
  node.finalizer = this.eat(types$1._finally) ? this.parseBlock() : null;
5553
5599
  if (!node.handler && !node.finalizer)
5554
5600
  this.raise(node.start, "Missing catch or finally clause");
5601
+ setGoldsteinTry(node);
5555
5602
  return this.finishNode(node, "TryStatement");
5556
5603
  }
5557
5604
  };
5558
5605
  }
5559
- function createGoldsteinNode(args) {
5560
- return {
5561
- type: "TryStatement",
5562
- expression: true,
5563
- await: args.await,
5564
- argument: {
5565
- type: "CallExpression",
5566
- callee: args.callee,
5567
- arguments: args.arguments
5568
- }
5569
- };
5570
- }
5571
5606
 
5572
5607
  // packages/keyword-should/index.js
5573
- var import_putout2 = require("putout");
5608
+ var import_putout3 = require("putout");
5574
5609
  var {
5575
- isCallExpression: isCallExpression2,
5576
- isAwaitExpression: isAwaitExpression2
5577
- } = import_putout2.types;
5610
+ isCallExpression: isCallExpression3,
5611
+ isAwaitExpression: isAwaitExpression3
5612
+ } = import_putout3.types;
5578
5613
  function newSpeak2(Parser3) {
5579
5614
  const { keywordTypes } = Parser3.acorn;
5580
5615
  keywordTypes.should = new TokenType("should", {
@@ -5597,7 +5632,7 @@ function newSpeak2(Parser3) {
5597
5632
  if (this.type === types$1.braceL)
5598
5633
  return this.raise(this.start, `After 'should' only 'await' and 'function call' can come, brackets are not supported`);
5599
5634
  const expression = this.parseExpression();
5600
- if (isCallExpression2(expression))
5635
+ if (isCallExpression3(expression))
5601
5636
  node.expression = {
5602
5637
  type: "TryStatement",
5603
5638
  block: {
@@ -5619,7 +5654,7 @@ function newSpeak2(Parser3) {
5619
5654
  }
5620
5655
  }
5621
5656
  };
5622
- else if (isAwaitExpression2(expression))
5657
+ else if (isAwaitExpression3(expression))
5623
5658
  node.expression = {
5624
5659
  type: "TryStatement",
5625
5660
  block: {
@@ -5671,7 +5706,7 @@ function keywordThrow(Parser3) {
5671
5706
  }
5672
5707
 
5673
5708
  // packages/string-interpolation/index.js
5674
- var import_putout3 = require("putout");
5709
+ var import_putout4 = require("putout");
5675
5710
  var isString = (a) => typeof a === "string";
5676
5711
  var { assign: assign2 } = Object;
5677
5712
  function stringInterpolation(Parser3) {
@@ -5706,7 +5741,7 @@ function stringInterpolation(Parser3) {
5706
5741
  if (isTemplateLiteral) {
5707
5742
  const node = this.startNode();
5708
5743
  this.next();
5709
- const { quasis, expressions } = import_putout3.template.ast(`\`${out.join("")}\``);
5744
+ const { quasis, expressions } = import_putout4.template.ast(`\`${out.join("")}\``);
5710
5745
  assign2(node, {
5711
5746
  quasis,
5712
5747
  expressions
@@ -5719,8 +5754,8 @@ function stringInterpolation(Parser3) {
5719
5754
  }
5720
5755
 
5721
5756
  // packages/keyword-curry/index.js
5722
- var import_putout4 = require("putout");
5723
- var { Identifier } = import_putout4.types;
5757
+ var import_putout5 = require("putout");
5758
+ var { Identifier } = import_putout5.types;
5724
5759
  function keywordCurry(Parser3) {
5725
5760
  return class extends Parser3 {
5726
5761
  parseSubscript(base, startPos, startLoc, noCalls, maybeAsyncArrow, optionalChained, forInit) {
@@ -5743,11 +5778,11 @@ function keywordCurry(Parser3) {
5743
5778
  }
5744
5779
 
5745
5780
  // packages/keyword-freeze/index.js
5746
- var import_putout5 = require("putout");
5781
+ var import_putout6 = require("putout");
5747
5782
  var {
5748
5783
  isObjectExpression,
5749
5784
  isArrayExpression
5750
- } = import_putout5.types;
5785
+ } = import_putout6.types;
5751
5786
  function keywordFreeze(Parser3) {
5752
5787
  const { keywordTypes } = Parser3.acorn;
5753
5788
  keywordTypes.freeze = new TokenType("freeze", {
@@ -5787,6 +5822,11 @@ function keywordFreeze(Parser3) {
5787
5822
  };
5788
5823
  }
5789
5824
 
5825
+ // packages/types/if.js
5826
+ var setGoldsteinIf = (node) => {
5827
+ node.goldsteinIf = true;
5828
+ };
5829
+
5790
5830
  // packages/keyword-if/index.js
5791
5831
  function fn2(Parser3) {
5792
5832
  return class extends Parser3 {
@@ -5801,7 +5841,7 @@ function fn2(Parser3) {
5801
5841
  this.raise(this.start, `Use both parens ('(', ')') or none`);
5802
5842
  node.consequent = this.parseStatement("if");
5803
5843
  node.alternate = this.eat(types$1._else) ? this.parseStatement("if") : null;
5804
- node.goldsteinIf = true;
5844
+ setGoldsteinIf(node);
5805
5845
  return this.finishNode(node, "IfStatement");
5806
5846
  }
5807
5847
  };
@@ -5882,5 +5922,8 @@ var parse3 = (source, options = {}, keywords3 = defaultKeywords) => {
5882
5922
  ...options.keywords,
5883
5923
  ...keywords3
5884
5924
  }));
5885
- return parse4(source);
5925
+ const ast = parse4(source);
5926
+ if (options.type === "estree")
5927
+ return ast;
5928
+ return (0, import_estree_to_babel.default)(ast);
5886
5929
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goldstein",
3
- "version": "4.3.0",
3
+ "version": "4.5.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "JavaScript with no limits",
@@ -0,0 +1,23 @@
1
+ import {createGoldsteinTry} from '../../types/try.js';
2
+
3
+ export const report = () => `Use 'try' instead of 'tryCatch/tryToCatch'`;
4
+ export const replace = () => ({
5
+ 'tryCatch(__args)': createTry({
6
+ async: false,
7
+ }),
8
+ 'await tryToCatch(__args)': createTry({
9
+ async: true,
10
+ }),
11
+ });
12
+
13
+ const createTry = ({async}) => ({__args}, path) => {
14
+ const [callee, ...args] = __args;
15
+
16
+ path.node.goldstein = createGoldsteinTry({
17
+ async,
18
+ callee,
19
+ args,
20
+ });
21
+
22
+ return path;
23
+ };
@@ -0,0 +1,20 @@
1
+ import estreeToBabel from 'estree-to-babel';
2
+ import {transform} from 'putout';
3
+ import {print} from '../printer/index.js';
4
+ import * as applyTry from './apply-try/index.js';
5
+ import {
6
+ fixEmpty,
7
+ parse,
8
+ } from '../goldstein/index.js';
9
+
10
+ export const convert = (source) => {
11
+ const ast = estreeToBabel(parse(source));
12
+
13
+ transform(ast, source, {
14
+ plugins: [
15
+ ['apply-try', applyTry],
16
+ ],
17
+ });
18
+
19
+ return fixEmpty(print(ast));
20
+ };
@@ -1,14 +1,14 @@
1
1
  import {transform} from 'putout';
2
2
  import {print} from '@putout/printer';
3
- import {parse} from './parser.js';
4
- import estreeToBabel from 'estree-to-babel';
5
3
  import tryCatchPlugin from '@putout/plugin-try-catch';
6
4
  import declarePlugin from '@putout/plugin-declare';
7
5
  import logicalExpressionsPlugin from '@putout/plugin-logical-expressions';
6
+ import {parse} from './parser.js';
8
7
 
9
8
  export * from './parser.js';
9
+ export {print} from '../printer/index.js';
10
10
  export const compile = (source, options = {}) => {
11
- const ast = estreeToBabel(parse(source, options));
11
+ const ast = parse(source, options);
12
12
 
13
13
  transform(ast, source, {
14
14
  rules: {
@@ -1,3 +1,5 @@
1
+ import estreeToBabel from 'estree-to-babel';
2
+
1
3
  import {extendParser} from '../parser/index.js';
2
4
  import keywordFn from '../keyword-fn/index.js';
3
5
  import keywordGuard from '../keyword-guard/index.js';
@@ -33,5 +35,10 @@ export const parse = (source, options = {}, keywords = defaultKeywords) => {
33
35
  ...keywords,
34
36
  }));
35
37
 
36
- return parse(source);
38
+ const ast = parse(source);
39
+
40
+ if (options.type === 'estree')
41
+ return ast;
42
+
43
+ return estreeToBabel(ast);
37
44
  };
@@ -1,4 +1,5 @@
1
1
  import {tokTypes as tt} from '../operator/index.js';
2
+ import {setGoldsteinIf} from '../types/if.js';
2
3
 
3
4
  export default function fn(Parser) {
4
5
  return class extends Parser {
@@ -6,6 +7,7 @@ export default function fn(Parser) {
6
7
  this.next();
7
8
 
8
9
  const isParenL = this.eat(tt.parenL);
10
+
9
11
  node.test = this.parseExpression();
10
12
  const isParenR = this.eat(tt.parenR);
11
13
 
@@ -18,9 +20,10 @@ export default function fn(Parser) {
18
20
  node.consequent = this.parseStatement('if');
19
21
  node.alternate = this.eat(tt._else) ? this.parseStatement('if') : null;
20
22
 
21
- node.goldsteinIf = true;
23
+ setGoldsteinIf(node);
22
24
 
23
25
  return this.finishNode(node, 'IfStatement');
24
26
  }
25
27
  };
26
28
  }
29
+
@@ -5,6 +5,7 @@ import {
5
5
  SCOPE_SIMPLE_CATCH,
6
6
  tokTypes as tt,
7
7
  } from '../operator/index.js';
8
+ import {setGoldsteinTry} from '../types/try.js';
8
9
 
9
10
  const {
10
11
  isCallExpression,
@@ -43,20 +44,10 @@ export default function keywordTry(Parser) {
43
44
  expression.callee,
44
45
  ...expression.arguments,
45
46
  ],
46
- goldstein: createGoldsteinNode({
47
- await: false,
48
- callee: expression.callee,
49
- arguments: expression.arguments,
50
- }),
51
47
  };
52
48
  else if (isAwaitExpression(expression))
53
49
  node.expression = {
54
50
  type: 'AwaitExpression',
55
- goldstein: createGoldsteinNode({
56
- await: true,
57
- callee: expression.argument.callee,
58
- arguments: expression.argument.arguments,
59
- }),
60
51
  argument: {
61
52
  type: 'CallExpression',
62
53
  callee: {
@@ -72,6 +63,8 @@ export default function keywordTry(Parser) {
72
63
  else
73
64
  this.raise(this.start, `After 'try' only '{', 'await' and 'function call' can come`);
74
65
 
66
+ setGoldsteinTry(node.expression);
67
+
75
68
  return super.finishNode(node, 'ExpressionStatement');
76
69
  }
77
70
 
@@ -105,12 +98,14 @@ export default function keywordTry(Parser) {
105
98
  if (!node.handler && !node.finalizer)
106
99
  this.raise(node.start, 'Missing catch or finally clause');
107
100
 
101
+ setGoldsteinTry(node);
102
+
108
103
  return this.finishNode(node, 'TryStatement');
109
104
  }
110
105
  };
111
106
  }
112
107
 
113
- function createGoldsteinNode(args) {
108
+ export function createGoldsteinTry(args) {
114
109
  return {
115
110
  type: 'TryStatement',
116
111
  expression: true,
@@ -118,7 +113,8 @@ function createGoldsteinNode(args) {
118
113
  argument: {
119
114
  type: 'CallExpression',
120
115
  callee: args.callee,
121
- arguments: args.arguments,
116
+ arguments: args.args,
122
117
  },
123
118
  };
124
119
  }
120
+
@@ -1,5 +1,4 @@
1
1
  import {print as printJS} from '@putout/printer';
2
-
3
2
  import {fixEmpty} from '../goldstein/index.js';
4
3
  import {AwaitExpression} from './visitors/await-expression.js';
5
4
  import {CallExpression} from './visitors/call-expression.js';
@@ -8,6 +8,6 @@ export const TryStatement = (path, printer, semantics) => {
8
8
  return v.TryStatement(path, printer, semantics);
9
9
 
10
10
  print('try ');
11
- maybe.print(node.await, 'await ');
11
+ maybe.print(node.async, 'await ');
12
12
  print('__argument');
13
13
  };
@@ -0,0 +1,3 @@
1
+ export const setGoldsteinIf = (node) => {
2
+ node.goldsteinIf = true;
3
+ };
@@ -0,0 +1,50 @@
1
+ import {types} from 'putout';
2
+
3
+ const {
4
+ isAwaitExpression,
5
+ isCallExpression,
6
+ } = types;
7
+
8
+ export function createGoldsteinTry({async, callee, args}) {
9
+ return {
10
+ type: 'TryStatement',
11
+ expression: true,
12
+ async,
13
+ argument: {
14
+ type: 'CallExpression',
15
+ callee,
16
+ arguments: args,
17
+ },
18
+ };
19
+ }
20
+
21
+ export const setGoldsteinTry = (node) => {
22
+ if (!isCallExpression(node) && !isAwaitExpression(node))
23
+ return;
24
+
25
+ node.goldstein = createGoldsteinTry(parseExpression(node));
26
+ };
27
+
28
+ const parseExpression = (node) => {
29
+ const {argument, arguments: allArgs} = node;
30
+
31
+ const async = isAwaitExpression(node);
32
+
33
+ if (!async) {
34
+ const [callee, ...args] = allArgs;
35
+
36
+ return {
37
+ async,
38
+ args,
39
+ callee,
40
+ };
41
+ }
42
+
43
+ const [callee, ...args] = argument.arguments;
44
+
45
+ return {
46
+ async,
47
+ args,
48
+ callee,
49
+ };
50
+ };