goldstein 3.0.0 → 3.1.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,9 @@
1
+ 2023.04.18, v3.1.0
2
+
3
+ feature:
4
+ - 4236346 goldstein: add ability to use arrow in FunctionDeclaration
5
+ - 8e2197f goldstein: add ability tot pass keywords and 🐊Putout optionts
6
+
1
7
  2023.04.02, v3.0.0
2
8
 
3
9
  feature:
package/README.md CHANGED
@@ -99,9 +99,56 @@ function hello() {
99
99
  `;
100
100
  ```
101
101
 
102
+ By default, all keywords mentioned in the next section used, but you can limit the list setting with `keywords` option.
103
+ You can add any keywords, and even create your own:
104
+
105
+ ```js
106
+ import {
107
+ compile,
108
+ keywords,
109
+ } from 'goldstein';
110
+
111
+ const source = `
112
+ fn hello() {
113
+ return id('hello');
114
+ }
115
+ `;
116
+
117
+ const {keywordFn} = keywords;
118
+
119
+ compile(source, {
120
+ keywords: [
121
+ keywordFn,
122
+ function id(Parser) {
123
+ const {keywordTypes} = Parser.acorn;
124
+ return class extends Parser {
125
+ };
126
+ },
127
+ ],
128
+ rules: {
129
+ declare: ['on', {
130
+ declarations: {
131
+ id: 'const id = (a) => a',
132
+ },
133
+ }],
134
+ },
135
+ });
136
+
137
+ // returns
138
+ `
139
+ const id = (a) => a;
140
+
141
+ function hello() {
142
+ return id('hello');
143
+ }
144
+ `;
145
+ ```
146
+
147
+ You can declare variables with using [`@putout/operator-declare`](https://github.com/coderaiser/putout/tree/master/packages/operator-declare).
148
+
102
149
  ### `parse(source)`
103
150
 
104
- When you need to get **JavaScript** ESTree AST use `parse`:
151
+ When you need to get **JavaScript** Babel AST use `parse`:
105
152
 
106
153
  ```js
107
154
  import {parse} from 'goldstein';
@@ -115,7 +162,8 @@ parse(`
115
162
  return "Hello " + text
116
163
  }
117
164
  `);
118
- // returns ESTree AST
165
+
166
+ // returns Babel AST
119
167
  ```
120
168
 
121
169
  ## Keywords
@@ -282,6 +330,22 @@ Will be converted to:
282
330
  import hello from './hello.js';
283
331
  ```
284
332
 
333
+ ### `FunctionDeclaration` with `Arrow`
334
+
335
+ If you mistakenly put `=>` in function declaration:
336
+
337
+ ```gs
338
+ function hello() => {
339
+ }
340
+ ```
341
+
342
+ That absolutely fine, it will be converted to:
343
+
344
+ ```js
345
+ function hello() {
346
+ }
347
+ ```
348
+
285
349
  ## How to contribute?
286
350
 
287
351
  Clone the registry, create a new keyword with a prefix `keyword-`, then create directory `fixture` and put there two files with extensions `.js` and `.gs`. Half way done 🥳!
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "goldstein",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "type": "module",
5
5
  "author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
6
6
  "description": "JavaScript with no limits",
@@ -15,27 +15,34 @@ import keywordImport from '../keyword-import/index.js';
15
15
 
16
16
  import estreeToBabel from 'estree-to-babel';
17
17
 
18
- export const parse = (source) => {
19
- const {parse} = extendParser([
20
- keywordFn,
21
- keywordGuard,
22
- keywordTry,
23
- keywordShould,
24
- keywordThrow,
25
- keywordCurry,
26
- keywordFreeze,
27
- keywordIf,
28
- keywordImport,
29
- stringInterpolation,
30
- ]);
18
+ const defaultKeywords = {
19
+ keywordFn,
20
+ keywordGuard,
21
+ keywordTry,
22
+ keywordShould,
23
+ keywordThrow,
24
+ keywordCurry,
25
+ keywordFreeze,
26
+ keywordIf,
27
+ keywordImport,
28
+ stringInterpolation,
29
+ };
30
+
31
+ export const keywords = defaultKeywords;
32
+
33
+ export const parse = (source, keywords = defaultKeywords) => {
34
+ const {parse} = extendParser(Object.values(keywords));
31
35
 
32
36
  return estreeToBabel(parse(source));
33
37
  };
34
38
 
35
- export const compile = (source) => {
39
+ export const compile = (source, options = {}) => {
36
40
  const ast = parse(source);
37
41
 
38
42
  transform(ast, source, {
43
+ rules: {
44
+ ...options.rules,
45
+ },
39
46
  plugins: [
40
47
  'try-catch',
41
48
  'declare',
@@ -43,7 +50,9 @@ export const compile = (source) => {
43
50
  ],
44
51
  });
45
52
 
46
- return fixEmpty(print(ast));
53
+ const {keywords} = options;
54
+
55
+ return fixEmpty(print(ast, {keywords}));
47
56
  };
48
57
 
49
58
  const fixEmpty = (source) => {
@@ -0,0 +1,43 @@
1
+ import {
2
+ tokTypes as tt,
3
+ } from 'acorn';
4
+
5
+ export default function fn(Parser) {
6
+ return class extends Parser {
7
+ parseBlock(createNewLexicalScope, node, exitStrict) {
8
+ if (createNewLexicalScope === void 0)
9
+ createNewLexicalScope = true;
10
+
11
+ if (node === void 0)
12
+ node = this.startNode();
13
+
14
+ node.body = [];
15
+
16
+ // optionally parse arrow
17
+ this.eat(tt.arrow);
18
+ this.expect(tt.braceL);
19
+
20
+ if (createNewLexicalScope) {
21
+ this.enterScope(0);
22
+ }
23
+
24
+ while (this.type !== tt.braceR) {
25
+ const stmt = this.parseStatement(null);
26
+ node.body.push(stmt);
27
+ }
28
+
29
+ if (exitStrict) {
30
+ this.strict = false;
31
+ }
32
+
33
+ this.next();
34
+
35
+ if (createNewLexicalScope) {
36
+ this.exitScope();
37
+ }
38
+
39
+ return this.finishNode(node, 'BlockStatement');
40
+ }
41
+ };
42
+ }
43
+