Skip to content

Commit 0225fce

Browse files
aduh95richardlau
authored andcommitted
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: #50976 Reviewed-By: Ethan Arrowood <[email protected]> Reviewed-By: LiviaMedeiros <[email protected]>
1 parent 4b3cc3c commit 0225fce

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

doc/api/fs.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -6578,6 +6578,19 @@ The file name that this {fs.Dirent} object refers to. The type of this
65786578
value is determined by the `options.encoding` passed to [`fs.readdir()`][] or
65796579
[`fs.readdirSync()`][].
65806580
6581+
#### `dirent.parentPath`
6582+
6583+
<!-- YAML
6584+
added:
6585+
- REPLACEME
6586+
-->
6587+
6588+
> Stability: 1 – Experimental
6589+
6590+
* {string}
6591+
6592+
The path to the parent directory of the file this {fs.Dirent} object refers to.
6593+
65816594
#### `dirent.path`
65826595
65836596
<!-- YAML
@@ -6586,7 +6599,7 @@ added: v20.1.0
65866599
65876600
* {string}
65886601
6589-
The base path that this {fs.Dirent} object refers to.
6602+
Alias for `dirent.parentPath`.
65906603
65916604
### Class: `fs.FSWatcher`
65926605

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ function readdirSyncRecursive(basePath, options) {
14261426
const dirent = getDirent(path, readdirResult[0][i], readdirResult[1][i]);
14271427
ArrayPrototypePush(readdirResults, dirent);
14281428
if (dirent.isDirectory()) {
1429-
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.path, dirent.name));
1429+
ArrayPrototypePush(pathsQueue, pathModule.join(dirent.parentPath, dirent.name));
14301430
}
14311431
}
14321432
} else {

lib/internal/fs/dir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class Dir {
161161
}
162162

163163
readSyncRecursive(dirent) {
164-
const path = pathModule.join(dirent.path, dirent.name);
164+
const path = pathModule.join(dirent.parentPath, dirent.name);
165165
const ctx = { path };
166166
const handle = dirBinding.opendir(
167167
pathModule.toNamespacedPath(path),

lib/internal/fs/utils.js

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ function assertEncoding(encoding) {
167167
class Dirent {
168168
constructor(name, type, path) {
169169
this.name = name;
170+
this.parentPath = path;
170171
this.path = path;
171172
this[kType] = type;
172173
}

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), Array(entries.length).fill(testDir));
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)