Skip to content

Commit ccb5573

Browse files
fix(renaming): fix bad splicing
I thought this was a race condition, turns out I wasn't using splice() correctly :P fixes #10
1 parent ae968bf commit ccb5573

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ module.exports = function graph ({ alias = {} } = {}) {
317317
// only emit if an entry is removed
318318
events.emit('remove', [ids[pointer]])
319319

320-
entryIds.splice(ids[pointer], 1)
320+
entryIds.splice(entryIds.indexOf(ids[pointer]), 1)
321+
ids.splice(pointer, 1)
321322

322323
cleanById(file) // remove any references to removed file
323324
} else {
@@ -355,6 +356,8 @@ module.exports = function graph ({ alias = {} } = {}) {
355356
watcher.removeAllListeners()
356357
},
357358
async add (files) {
359+
debug('add', files)
360+
358361
files = [].concat(files).filter(entry => {
359362
// filter out any already watched files
360363
if (entryIds.includes(entry)) return false

test/index.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -874,17 +874,21 @@ test('kitchen sink', async () => {
874874
fsx.cleanup()
875875
})
876876

877-
test('rename file', async () => {
877+
test.only('rename file', async () => {
878878
const files = {
879879
a: {
880880
url: './rename/a.js',
881881
content: `export default ''`
882+
},
883+
b: {
884+
url: './rename/b.js',
885+
content: `export default ''`
882886
}
883887
}
884888

885889
const fsx = fixtures.create(files)
886890
const w = graph({ cwd: fixtures.getRoot() })
887-
await w.add([fsx.files.a])
891+
await w.add([fsx.files.a, fsx.files.b])
888892

889893
w.on('error', e => {
890894
console.log(e)
@@ -893,10 +897,10 @@ test('rename file', async () => {
893897
await wait(DELAY)
894898

895899
const noChangeEvent = subscribe('change', w)
896-
const newFileName = path.join(fsx.root, '/rename/b.js')
900+
const newFileName = path.join(fsx.root, '/rename/c.js')
897901

898902
// rename
899-
fs.moveSync(fsx.files.a, newFileName)
903+
fs.moveSync(fsx.files.b, newFileName)
900904

901905
// change it
902906
fs.outputFileSync(newFileName, `export default ''`, 'utf8')
@@ -910,7 +914,9 @@ test('rename file', async () => {
910914
}
911915

912916
// renamed file was removed
913-
assert(w.tree[fsx.files.a] === undefined)
917+
assert(w.tree[fsx.files.b] === undefined)
918+
assert(w.ids[0] === fsx.files.a)
919+
assert(w.ids[1] === undefined)
914920

915921
// add renamed file
916922
await w.add([newFileName])

0 commit comments

Comments
 (0)