Skip to content

Commit 25bc75e

Browse files
committed
fixes #27: array (patch): clean up source comments
1 parent f816f33 commit 25bc75e

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

src/array.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77
UNDEFINED,
88

99
symbol,
10-
copy_all_comments,
10+
copy_comments,
1111
swap_comments
1212
} = require('./common')
1313

@@ -23,7 +23,7 @@ const reverse_comments = array => {
2323
}
2424

2525
const move_comment = (target, source, i, offset, remove) => {
26-
copy_all_comments(target, source, i + offset, i, remove)
26+
copy_comments(target, source, i + offset, i, remove)
2727
}
2828

2929
const move_comments = (
@@ -50,13 +50,12 @@ const move_comments = (
5050

5151
// From [count - 1, 0]
5252
while (i -- > 0) {
53-
move_comment(target, source, start + i, offset, remove && i < offset)
53+
move_comment(target, source, start + i, offset, remove)
5454
}
5555
return
5656
}
5757

5858
let i = 0
59-
const min_remove = count + offset
6059
// | remove | count |
6160
// -------------
6261
// -------------
@@ -65,10 +64,17 @@ const move_comments = (
6564
// From [0, count - 1]
6665
while (i < count) {
6766
const ii = i ++
68-
move_comment(target, source, start + ii, offset, remove && i >= min_remove)
67+
move_comment(target, source, start + ii, offset, remove)
6968
}
7069
}
7170

71+
const remove_comments = (array, key) => {
72+
SYMBOL_PREFIXES.forEach(prefix => {
73+
const prop = symbol(prefix, key)
74+
delete array[prop]
75+
})
76+
}
77+
7278
const get_mapped = (map, key) => {
7379
let mapped = key
7480

@@ -178,6 +184,7 @@ class CommentArray extends Array {
178184
const ret = super.shift()
179185
const {length} = this
180186

187+
remove_comments(this, 0)
181188
move_comments(this, this, 1, length, - 1, true)
182189

183190
return ret
@@ -195,11 +202,7 @@ class CommentArray extends Array {
195202
const ret = super.pop()
196203

197204
// Removes comments
198-
const {length} = this
199-
SYMBOL_PREFIXES.forEach(prefix => {
200-
const prop = symbol(prefix, length)
201-
delete this[prop]
202-
})
205+
remove_comments(this, this.length)
203206

204207
return ret
205208
}

src/common.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const define = (target, key, value) => Object.defineProperty(target, key, {
4545
configurable: true
4646
})
4747

48-
const copy_comments = (
48+
const copy_comments_by_kind = (
4949
target, source, target_key, source_key, prefix, remove_source
5050
) => {
5151
const source_prop = symbol(prefix, source_key)
@@ -64,11 +64,13 @@ const copy_comments = (
6464
}
6565
}
6666

67-
const copy_all_comments = (
67+
const copy_comments = (
6868
target, source, target_key, source_key, remove_source
6969
) => {
7070
SYMBOL_PREFIXES.forEach(prefix => {
71-
copy_comments(target, source, target_key, source_key, prefix, remove_source)
71+
copy_comments_by_kind(
72+
target, source, target_key, source_key, prefix, remove_source
73+
)
7274
})
7375
}
7476

@@ -80,14 +82,14 @@ const swap_comments = (array, from, to) => {
8082
SYMBOL_PREFIXES.forEach(prefix => {
8183
const target_prop = symbol(prefix, to)
8284
if (!hasOwnProperty(array, target_prop)) {
83-
copy_comments(array, array, to, from, prefix, true)
85+
copy_comments_by_kind(array, array, to, from, prefix, true)
8486
return
8587
}
8688

8789
const comments = array[target_prop]
8890
delete array[target_prop]
8991

90-
copy_comments(array, array, to, from, prefix, true)
92+
copy_comments_by_kind(array, array, to, from, prefix, true)
9193
define(array, symbol(prefix, from), comments)
9294
})
9395
}
@@ -110,7 +112,7 @@ const assign = (target, source, keys) => {
110112
}
111113

112114
target[key] = source[key]
113-
copy_all_comments(target, source, key, key)
115+
copy_comments(target, source, key, key)
114116
})
115117

116118
return target
@@ -144,7 +146,6 @@ module.exports = {
144146
symbol,
145147
define,
146148
copy_comments,
147-
copy_all_comments,
148149
swap_comments,
149150
assign_non_prop_comments,
150151

test/array.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,19 @@ const CASES = [
152152
array => array.slice(0, 1),
153153
slice([0], a3)
154154
],
155+
[
156+
'slice(0, 1), no mess',
157+
`[
158+
// 0
159+
0,
160+
1 // 1
161+
]`,
162+
array => array.slice(0, 1),
163+
slice([0], `[
164+
// 0
165+
0
166+
]`)
167+
],
155168
[
156169
'unshift()',
157170
a1,
@@ -181,6 +194,20 @@ const CASES = [
181194
1,
182195
// 2
183196
2
197+
]`)
198+
],
199+
[
200+
'shift, no mess',
201+
`[
202+
// 0
203+
0,
204+
1 /* 1 */,
205+
2 // 2
206+
]`,
207+
array => array.shift(),
208+
unshift(0, `[
209+
1 /* 1 */,
210+
2 // 2
184211
]`)
185212
],
186213
[

0 commit comments

Comments
 (0)