Skip to content

Commit a990e8f

Browse files
committed
fs: introduce dirent.parentPath
The goal is to replace `dirent.path` using a name that's less likely to create confusion. `dirent.path` value has not been stable, moving it to a different property name should avoid breaking some upgrading user expectations. PR-URL: nodejs#50976 Reviewed-By: Ethan Arrowood <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]>
1 parent 7fa1551 commit a990e8f

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

doc/api/fs.md

+13
Original file line numberDiff line numberDiff line change
@@ -6437,6 +6437,19 @@ The file name that this {fs.Dirent} object refers to. The type of this
64376437
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
64386438
[`fs.readdirSync()`][].
64396439
6440+
#### `dirent.parentPath`
6441+
6442+
<!-- YAML
6443+
added:
6444+
- REPLACEME
6445+
-->
6446+
6447+
> Stability: 1 – Experimental
6448+
6449+
* {string}
6450+
6451+
The path to the parent directory of the file this {fs.Dirent} object refers to.
6452+
64406453
#### `dirent.path`
64416454
64426455
<!-- YAML

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1431,7 +1431,7 @@ function readdirSyncRecursive(basePath, options) {
14311431
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
14321432
ArrayPrototypePush(readdirResults, dirent);
14331433
if (dirent.isDirectory()) {
1434-
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
1434+
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name));
14351435
}
14361436
}
14371437
} else {

lib/internal/fs/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function assertEncoding(encoding) {
163163
class Dirent {
164164
constructor(name, type, path) {
165165
this.name = name;
166+
this.parentPath = path;
166167
this.path = path;
167168
this[kType] = type;
168169
}

test/parallel/test-fs-opendir.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ const invalidCallbackObj = {
4848
const entries = files.map(() => {
4949
const dirent = dir.readSync();
5050
assertDirent(dirent);
51-
return dirent.name;
52-
});
53-
assert.deepStrictEqual(files, entries.sort());
51+
return { name: dirent.name, path: dirent.path, parentPath: dirent.parentPath, toString() { return dirent.name; } };
52+
}).sort();
53+
assert.deepStrictEqual(entries.map((d) => d.name), files);
54+
assert.deepStrictEqual(entries.map((d) => d.path), files.map((name) => path.join(testDir, name)));
55+
assert.deepStrictEqual(entries.map((d) => d.parentPath), Array(entries.length).fill(testDir));
5456

5557
// dir.read should return null when no more entries exist
5658
assert.strictEqual(dir.readSync(), null);

0 commit comments

Comments
 (0)