-
Notifications
You must be signed in to change notification settings - Fork 0
/
esformatter-asi.js
44 lines (37 loc) · 1.72 KB
/
esformatter-asi.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
function isWhitespace(token) {
return token && ~['WhiteSpace', 'Indent'].indexOf(token.type)
}
function isComment(token) {
return token && ~['LineComment', 'BlockComment'].indexOf(token.type)
}
function isSemicolon(token) {
return token && token.type === 'Punctuator' && token.value === ';'
}
exports.nodeAfter = function(node) {
if (~['ExpressionStatement', 'VariableDeclaration', 'ReturnStatement', 'ContinueStatement', 'BreakStatement'].indexOf(node.type) &&
isSemicolon(node.endToken)) {
var tokenItr = node.endToken.next,
semicolonToken = node.endToken,
blockCommentHadLineBreak = false
while (isWhitespace(tokenItr) || isComment(tokenItr)) {
tokenItr = tokenItr.next
blockCommentHadLineBreak = blockCommentHadLineBreak || tokenItr.type === 'BlockComment' && tokenItr.value && tokenItr.value.indexOf('\n') >= 0
}
if (!tokenItr || tokenItr.type === 'LineBreak' || blockCommentHadLineBreak || (tokenItr.type === 'Punctuator' && tokenItr.value === '}')) {
var lineBreakToken = tokenItr
if (node.next && node.next.type === 'ExpressionStatement') {
tokenItr = node.next.startToken
while (isWhitespace(tokenItr) || isComment(tokenItr)) {
tokenItr = tokenItr.next
}
if (tokenItr && tokenItr.type === 'Punctuator' && ~['[', '(', '+', '*', '/', '-', ',', '.'].indexOf(tokenItr.value)) {
// ASI doesn't apply to this line as removing the semicolon would cause the lines to combine to one expression
return
}
}
if (semicolonToken.prev) semicolonToken.prev.next = semicolonToken.next
if (semicolonToken.next) semicolonToken.next.prev = semicolonToken.prev
node.endToken = lineBreakToken
}
}
}