-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·134 lines (108 loc) · 2.78 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
/*
* See also:
* https://docs.npmjs.com/misc/coding-style#semicolons
* Switch statement semicolons should be ok to replace with line breaks.
*/
'use strict'
var _ = require('lodash')
var rocambole = require('rocambole')
var NEED_PREFIX_AT_START_OF_LINE = ['(', '[', '-', '+']
module.exports = function (src) {
return rocambole.moonwalk(src, function (node) {
var token = node.endToken
// semicolons inside a for loop don't seem to pop up as tokens
if (token.type !== 'Punctuator') return
if (token.value !== ';') return
if(node.parent && node.parent.type === `ForStatement`) {
return
}
if(token._keep) {
return
}
if(node.type === 'ForStatement') {
token._keep = true
}
if (node.type === 'WhileStatement'
|| node.type === 'ForStatement'
|| node.type === 'EmptyStatement'
|| node.type === 'IfStatement'
) {
return
}
if (token.next && token.next.type === 'WhiteSpace') {
if(token.next.next
&& token.next.next.value != '}'
&& token.next.next.type != 'LineComment'
) {
token.next.value = ''
}
}
var prevToken = _findPrevNonWhiteSpace(token)
var nextToken = _findNextNonWhiteSpace(token)
if (!prevToken && !nextToken) {
token.value = ''
return
}
if (!prevToken || prevToken.type === 'LineBreak') {
if(nextToken.type === 'RegularExpression') return
if (~NEED_PREFIX_AT_START_OF_LINE.indexOf(nextToken.value)) {
return
}
token.value = ''
return
}
if (!nextToken) {
token.value = ''
return
}
if (nextToken.type === 'LineComment' || nextToken.type === 'LineBreak' || nextToken.value === '}') {
token.value = ''
return
}
var indentation = _findIndentation(token)
if (indentation) {
insertIndentationAfter(token, indentation)
}
token.type = 'LineBreak'
token.value = '\n'
})
}
function insertIndentationAfter (token, indentation) {
var newIndentation = {}
_.extend(newIndentation, indentation,
{
prev: token,
next: token.next
}
)
token.next.prev = newIndentation
token.next = newIndentation
}
function _findNextNonWhiteSpace (token) {
while (token.next) {
token = token.next
if (token.type !== 'WhiteSpace') return token
}
}
function _findPrevNonWhiteSpace (token) {
while (token.prev) {
token = token.prev
if (token.type !== 'WhiteSpace') return token
}
}
function _findIndentation (token) {
var indentation
while (token.prev) {
token = token.prev
if (token.type === 'WhiteSpace') {
indentation = token
continue
}
if (token.type === 'LineBreak') {
return indentation
}
indentation = undefined
}
return indentation
}