Skip to content

Commit

Permalink
fix: the file name is not included when the withFileTypes option of r…
Browse files Browse the repository at this point in the history
…eaddir'

Co-authored-by: daeyoen <6630703+daeyeon@users.noreply.github.com>
Co-authored-by: injae-kim <34854527+injae-kim@users.noreply.github.com>"
  • Loading branch information
2 people authored and sonny committed Jul 20, 2024
1 parent 0092358 commit 3ef999e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
1 change: 0 additions & 1 deletion lib/internal/fs/dir.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ class Dir {
path,
result[i],
result[i + 1],
true, // Quirk to not introduce a breaking change.
),
);
}
Expand Down
20 changes: 8 additions & 12 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ function assertEncoding(encoding) {
}

class Dirent {
constructor(name, type, path, filepath = path && join(path, name)) {
constructor(name, type, path) {
this.name = name;
this.parentPath = path;
this.path = filepath;
this.path = path;
this[kType] = type;
}

Expand Down Expand Up @@ -198,8 +198,8 @@ class Dirent {
}

class DirentFromStats extends Dirent {
constructor(name, stats, path, filepath) {
super(name, null, path, filepath);
constructor(name, stats, path) {
super(name, null, path);
this[kStats] = stats;
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) {
callback(err);
return;
}
names[idx] = new DirentFromStats(name, stats, path, filepath);
names[idx] = new DirentFromStats(name, stats, path);
if (--toFinish === 0) {
callback(null, names);
}
Expand Down Expand Up @@ -305,21 +305,17 @@ function getDirent(path, name, type, callback) {
callback(err);
return;
}
callback(null, new DirentFromStats(name, stats, path, filepath));
callback(null, new DirentFromStats(name, stats, path));
});
} else {
callback(null, new Dirent(name, type, path));
}
} else if (type === UV_DIRENT_UNKNOWN) {
const filepath = join(path, name);
const stats = lazyLoadFs().lstatSync(filepath);
// callback === true: Quirk to not introduce a breaking change.
return new DirentFromStats(name, stats, path, callback === true ? filepath : path);
} else if (callback === true) {
// callback === true: Quirk to not introduce a breaking change.
return new Dirent(name, type, path);
return new DirentFromStats(name, stats, path);
} else {
return new Dirent(name, type, path, path);
return new Dirent(name, type, path);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-opendir.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const invalidCallbackObj = {
return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } };
}).sort();
assert.deepStrictEqual(entries.map((d) => d.name), files);
assert.deepStrictEqual(entries.map((d) => d.path), files.map((name) => path.join(testDir, name)));
assert.deepStrictEqual(entries.map((d) => d.path), Array(entries.length).fill(testDir));
assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir));

// dir.read should return null when no more entries exist
Expand Down

0 comments on commit 3ef999e

Please sign in to comment.