goldstein 5.13.2 → 5.14.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
package/README.md
CHANGED
|
@@ -466,6 +466,18 @@ const a = {
|
|
|
466
466
|
};
|
|
467
467
|
```
|
|
468
468
|
|
|
469
|
+
### Assign from
|
|
470
|
+
|
|
471
|
+
```gs
|
|
472
|
+
const a = from 'a';
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
The same as:
|
|
476
|
+
|
|
477
|
+
```js
|
|
478
|
+
const a = require('a');
|
|
479
|
+
```
|
|
480
|
+
|
|
469
481
|
## How to contribute?
|
|
470
482
|
|
|
471
483
|
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
|
@@ -17,6 +17,8 @@ import keywordBrokenString from '../keyword-broken-string/index.js';
|
|
|
17
17
|
import keywordMissingInitializer from '../keyword-missing-initializer/index.js';
|
|
18
18
|
import keywordUselessComma from '../keyword-useless-comma/index.js';
|
|
19
19
|
import keywordUselessSemicolon from '../keyword-useless-semicolon/index.js';
|
|
20
|
+
import keywordAssignFrom from '../keyword-assign-from/index.js';
|
|
21
|
+
import internalParseMaybeAssign from '../internal-parse-maybe-assign/index.js';
|
|
20
22
|
|
|
21
23
|
const {values} = Object;
|
|
22
24
|
|
|
@@ -37,8 +39,13 @@ const defaultKeywords = {
|
|
|
37
39
|
keywordMissingInitializer,
|
|
38
40
|
keywordUselessComma,
|
|
39
41
|
keywordUselessSemicolon,
|
|
42
|
+
keywordAssignFrom,
|
|
40
43
|
};
|
|
41
44
|
|
|
45
|
+
const internals = [
|
|
46
|
+
internalParseMaybeAssign,
|
|
47
|
+
];
|
|
48
|
+
|
|
42
49
|
export const keywords = defaultKeywords;
|
|
43
50
|
|
|
44
51
|
export const parse = (source, options = {}) => {
|
|
@@ -47,6 +54,7 @@ export const parse = (source, options = {}) => {
|
|
|
47
54
|
|
|
48
55
|
const {parse} = extendParser([
|
|
49
56
|
typescript(),
|
|
57
|
+
...internals,
|
|
50
58
|
...extensions,
|
|
51
59
|
]);
|
|
52
60
|
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {tokTypes as tt} from 'acorn';
|
|
2
|
+
import {types} from 'putout';
|
|
3
|
+
import {DestructuringErrors} from '../operator/index.js';
|
|
4
|
+
|
|
5
|
+
const {isArrayExpression} = types;
|
|
6
|
+
|
|
7
|
+
export default function internalParseMaybeAssign(Parser) {
|
|
8
|
+
return class extends Parser {
|
|
9
|
+
parseMaybeAssign(forInit, refDestructuringErrors, afterLeftParse) {
|
|
10
|
+
return parseMaybeAssign.call(this, forInit, refDestructuringErrors, afterLeftParse);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
goldsteinParseFrom() {
|
|
14
|
+
throw Error(`☝️Looks like 'keyword-assign-from' is missing.`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
goldsteinCreateAddArray() {
|
|
18
|
+
throw Error(`☝️Looks like 'keyword-add-array' is missing.`);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function parseMaybeAssign(forInit, refDestructuringErrors, afterLeftParse) {
|
|
24
|
+
/* c8 ignore start */
|
|
25
|
+
if (this.isContextual('yield')) {
|
|
26
|
+
if (this.inGenerator)
|
|
27
|
+
return this.parseYield(forInit);
|
|
28
|
+
|
|
29
|
+
// The tokenizer will assume an expression is allowed after
|
|
30
|
+
// `yield`, but this isn't that kind of yield
|
|
31
|
+
this.exprAllowed = false; /* c8 ignore end */
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let ownDestructuringErrors = false;
|
|
35
|
+
let oldParenAssign = -1;
|
|
36
|
+
let oldTrailingComma = -1;
|
|
37
|
+
let oldDoubleProto = -1;
|
|
38
|
+
|
|
39
|
+
if (refDestructuringErrors) {
|
|
40
|
+
oldParenAssign = refDestructuringErrors.parenthesizedAssign;
|
|
41
|
+
oldTrailingComma = refDestructuringErrors.trailingComma;
|
|
42
|
+
oldDoubleProto = refDestructuringErrors.doubleProto;
|
|
43
|
+
refDestructuringErrors.parenthesizedAssign = -1;
|
|
44
|
+
refDestructuringErrors.trailingComma = -1;
|
|
45
|
+
} else {
|
|
46
|
+
refDestructuringErrors = new DestructuringErrors();
|
|
47
|
+
ownDestructuringErrors = true;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const startPos = this.start;
|
|
51
|
+
const {startLoc} = this;
|
|
52
|
+
|
|
53
|
+
if (this.type === tt.parenL || this.type === tt.name) {
|
|
54
|
+
this.potentialArrowAt = this.start;
|
|
55
|
+
this.potentialArrowInForAwait = forInit === 'await';
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let left = this.parseMaybeConditional(forInit, refDestructuringErrors);
|
|
59
|
+
|
|
60
|
+
if (afterLeftParse)
|
|
61
|
+
left = afterLeftParse.call(this, left, startPos, startLoc);
|
|
62
|
+
|
|
63
|
+
/* c8 ignore start */
|
|
64
|
+
if (this.type.isAssign) {
|
|
65
|
+
const node = this.startNodeAt(startPos, startLoc);
|
|
66
|
+
|
|
67
|
+
node.operator = this.value;
|
|
68
|
+
|
|
69
|
+
if (this.type === tt.eq)
|
|
70
|
+
left = this.toAssignable(left, false, refDestructuringErrors);
|
|
71
|
+
|
|
72
|
+
if (!ownDestructuringErrors) {
|
|
73
|
+
refDestructuringErrors.parenthesizedAssign = -1;
|
|
74
|
+
refDestructuringErrors.trailingComma = -1;
|
|
75
|
+
refDestructuringErrors.doubleProto = -1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (refDestructuringErrors.shorthandAssign >= left.start)
|
|
79
|
+
refDestructuringErrors.shorthandAssign = -1;
|
|
80
|
+
|
|
81
|
+
// reset because shorthand default was used correctly
|
|
82
|
+
if (this.type === tt.eq)
|
|
83
|
+
this.checkLValPattern(left);
|
|
84
|
+
else
|
|
85
|
+
this.checkLValSimple(left);
|
|
86
|
+
|
|
87
|
+
node.left = left;
|
|
88
|
+
this.next();
|
|
89
|
+
node.right = this.parseMaybeAssign(forInit);
|
|
90
|
+
|
|
91
|
+
if (oldDoubleProto > -1)
|
|
92
|
+
refDestructuringErrors.doubleProto = oldDoubleProto;
|
|
93
|
+
|
|
94
|
+
if (node.operator === '+=' && isArrayExpression(node.right))
|
|
95
|
+
return this.goldsteinCreateAddArray(node);
|
|
96
|
+
|
|
97
|
+
return this.finishNode(node, 'AssignmentExpression');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/* c8 ignore end */
|
|
101
|
+
if (ownDestructuringErrors)
|
|
102
|
+
this.checkExpressionErrors(refDestructuringErrors, true);
|
|
103
|
+
|
|
104
|
+
if (oldParenAssign > -1)
|
|
105
|
+
refDestructuringErrors.parenthesizedAssign = oldParenAssign;
|
|
106
|
+
|
|
107
|
+
if (oldTrailingComma > -1)
|
|
108
|
+
refDestructuringErrors.trailingComma = oldTrailingComma;
|
|
109
|
+
|
|
110
|
+
if (left.name === 'from')
|
|
111
|
+
return this.goldsteinParseFrom(left);
|
|
112
|
+
|
|
113
|
+
return left;
|
|
114
|
+
}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import {types} from 'putout';
|
|
2
|
-
import {
|
|
3
|
-
import {DestructuringErrors} from '../operator/index.js';
|
|
2
|
+
import {parseMaybeAssign} from '../internal-parse-maybe-assign/index.js';
|
|
4
3
|
|
|
5
4
|
const {assign} = Object;
|
|
6
5
|
|
|
7
6
|
const {
|
|
8
7
|
identifier,
|
|
9
|
-
isArrayExpression,
|
|
10
8
|
memberExpression,
|
|
11
9
|
spreadElement,
|
|
12
10
|
} = types;
|
|
@@ -14,103 +12,18 @@ const {
|
|
|
14
12
|
export default function keywordAddArray(Parser) {
|
|
15
13
|
return class extends Parser {
|
|
16
14
|
parseMaybeAssign(forInit, refDestructuringErrors, afterLeftParse) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
// The tokenizer will assume an expression is allowed after
|
|
23
|
-
// `yield`, but this isn't that kind of yield
|
|
24
|
-
this.exprAllowed = false;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/* c8 ignore end */
|
|
28
|
-
let ownDestructuringErrors = false;
|
|
29
|
-
let oldParenAssign = -1;
|
|
30
|
-
let oldTrailingComma = -1;
|
|
31
|
-
let oldDoubleProto = -1;
|
|
32
|
-
|
|
33
|
-
if (refDestructuringErrors) {
|
|
34
|
-
oldParenAssign = refDestructuringErrors.parenthesizedAssign;
|
|
35
|
-
oldTrailingComma = refDestructuringErrors.trailingComma;
|
|
36
|
-
oldDoubleProto = refDestructuringErrors.doubleProto;
|
|
37
|
-
refDestructuringErrors.parenthesizedAssign = -1;
|
|
38
|
-
refDestructuringErrors.trailingComma = -1;
|
|
39
|
-
} else {
|
|
40
|
-
refDestructuringErrors = new DestructuringErrors();
|
|
41
|
-
ownDestructuringErrors = true;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const startPos = this.start;
|
|
45
|
-
const {startLoc} = this;
|
|
46
|
-
|
|
47
|
-
if (this.type === tt.parenL || this.type === tt.name) {
|
|
48
|
-
this.potentialArrowAt = this.start;
|
|
49
|
-
this.potentialArrowInForAwait = forInit === 'await';
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
let left = this.parseMaybeConditional(forInit, refDestructuringErrors);
|
|
53
|
-
|
|
54
|
-
if (afterLeftParse)
|
|
55
|
-
left = afterLeftParse.call(this, left, startPos, startLoc);
|
|
56
|
-
|
|
57
|
-
if (this.type.isAssign) {
|
|
58
|
-
const node = this.startNodeAt(startPos, startLoc);
|
|
59
|
-
|
|
60
|
-
node.operator = this.value;
|
|
61
|
-
|
|
62
|
-
if (this.type === tt.eq)
|
|
63
|
-
left = this.toAssignable(left, false, refDestructuringErrors);
|
|
64
|
-
|
|
65
|
-
if (!ownDestructuringErrors) {
|
|
66
|
-
refDestructuringErrors.parenthesizedAssign = -1;
|
|
67
|
-
refDestructuringErrors.trailingComma = -1;
|
|
68
|
-
refDestructuringErrors.doubleProto = -1;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (refDestructuringErrors.shorthandAssign >= left.start)
|
|
72
|
-
refDestructuringErrors.shorthandAssign = -1;
|
|
73
|
-
|
|
74
|
-
// reset because shorthand default was used correctly
|
|
75
|
-
if (this.type === tt.eq)
|
|
76
|
-
this.checkLValPattern(left);
|
|
77
|
-
else
|
|
78
|
-
this.checkLValSimple(left);
|
|
79
|
-
|
|
80
|
-
node.left = left;
|
|
81
|
-
this.next();
|
|
82
|
-
node.right = this.parseMaybeAssign(forInit);
|
|
83
|
-
|
|
84
|
-
if (oldDoubleProto > -1)
|
|
85
|
-
refDestructuringErrors.doubleProto = oldDoubleProto;
|
|
86
|
-
|
|
87
|
-
if (node.operator === '+=' && isArrayExpression(node.right))
|
|
88
|
-
return createAppendNode(this, node);
|
|
89
|
-
|
|
90
|
-
return this.finishNode(node, 'AssignmentExpression');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (ownDestructuringErrors)
|
|
94
|
-
this.checkExpressionErrors(refDestructuringErrors, true);
|
|
95
|
-
|
|
96
|
-
if (oldParenAssign > -1)
|
|
97
|
-
refDestructuringErrors.parenthesizedAssign = oldParenAssign;
|
|
15
|
+
return parseMaybeAssign.call(this, forInit, refDestructuringErrors, afterLeftParse);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
goldsteinCreateAddArray(node) {
|
|
19
|
+
const {left, right} = node;
|
|
98
20
|
|
|
99
|
-
|
|
100
|
-
|
|
21
|
+
assign(node, {
|
|
22
|
+
callee: memberExpression(left, identifier('push')),
|
|
23
|
+
arguments: [spreadElement(right)],
|
|
24
|
+
});
|
|
101
25
|
|
|
102
|
-
return
|
|
26
|
+
return this.finishNode(node, 'CallExpression');
|
|
103
27
|
}
|
|
104
28
|
};
|
|
105
29
|
}
|
|
106
|
-
|
|
107
|
-
function createAppendNode(context, node) {
|
|
108
|
-
const {left, right} = node;
|
|
109
|
-
|
|
110
|
-
assign(node, {
|
|
111
|
-
callee: memberExpression(left, identifier('push')),
|
|
112
|
-
arguments: [spreadElement(right)],
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
return context.finishNode(node, 'CallExpression');
|
|
116
|
-
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {tokTypes as tt} from '../operator/index.js';
|
|
2
|
+
import {parseMaybeAssign} from '../internal-parse-maybe-assign/index.js';
|
|
3
|
+
|
|
4
|
+
export default function fn(Parser) {
|
|
5
|
+
return class extends Parser {
|
|
6
|
+
parseMaybeAssign(forInit, refDestructuringErrors, afterLeftParse) {
|
|
7
|
+
return parseMaybeAssign.call(this, forInit, refDestructuringErrors, afterLeftParse);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
goldsteinParseFrom(node) {
|
|
11
|
+
if (this.type === tt.semi)
|
|
12
|
+
return node;
|
|
13
|
+
|
|
14
|
+
const arg = this.parseExprAtom();
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
type: 'CallExpression',
|
|
18
|
+
callee: {
|
|
19
|
+
type: 'Identifier',
|
|
20
|
+
name: 'require',
|
|
21
|
+
},
|
|
22
|
+
arguments: [arg],
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|