-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
90 lines (72 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {tokTypes as tt} from '../operator/index.js';
import {setGoldsteinIf} from '../types/if.js';
export default function fn(Parser) {
return class extends Parser {
parseIfStatement() {
this.next();
const isParenL = this.eat(tt.parenL);
if (this.isContextual('let'))
return createIfLet.call(this, {
isParenL,
});
const test = this.parseExpression();
return createIf.call(this, {
test,
isParenL,
});
}
};
}
function check({isParenL, isParenR}) {
if (!isParenL && !isParenR && this.type !== tt.braceL)
this.raise(this.start, `Use braces ('{', '}') when omit parens ('(', ')')`);
if (isParenL !== isParenR)
this.raise(this.start, `Use both parens ('(', ')') or none`);
}
function createIfLet({isParenL}) {
this.next();
this.eat(tt.assign);
const assignmentExpression = this.parseExpression();
const isParenR = this.eat(tt.parenR);
check.call(this, {
isParenL,
isParenR,
});
const ifNode = createIf.call(this, {
test: assignmentExpression.left,
isParenL,
});
const node = {
loc: {},
range: [],
type: 'BlockStatement',
body: [{
type: 'VariableDeclaration',
kind: 'let',
declarations: [{
type: 'VariableDeclarator',
id: assignmentExpression.left,
init: assignmentExpression.right,
}],
},
ifNode],
};
return this.finishNode(node, 'BlockStatement');
}
function createIf({test, isParenL}) {
const node = {
test,
};
const isParenR = this.eat(tt.parenR);
check.call(this, {
isParenL,
isParenR,
});
node.consequent = this.parseStatement('if');
node.alternate = this.eat(tt._else) ? this.parseStatement('if') : null;
node.loc = {};
node.range = [];
node.type = 'IfStatement';
setGoldsteinIf(node);
return this.finishNode(node, 'IfStatement');
}