Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit26e8f77

Browse files
aduh95richardlau
authored andcommitted
fs: introducedirent.parentPath
The goal is to replace `dirent.path` using a name that's less likely tocreate confusion.`dirent.path` value has not been stable, moving it to a differentproperty name should avoid breaking some upgrading user expectations.PR-URL:#50976Backport-PR-URL:#51021Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent96514a8 commit26e8f77

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

‎doc/api/fs.md

Lines changed: 13 additions & 0 deletions
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/internal/fs/dir.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ class Dir {
152152
ArrayPrototypePush(
153153
this[kDirBufferedEntries],
154154
getDirent(
155-
pathModule.join(path,result[i]),
155+
path,
156156
result[i],
157157
result[i+1],
158+
true,// Quirk to not introduce a breaking change.
158159
),
159160
);
160161
}

‎lib/internal/fs/utils.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,10 @@ function assertEncoding(encoding) {
161161
}
162162

163163
classDirent{
164-
constructor(name,type,path){
164+
constructor(name,type,path,filepath=path&&join(path,name)){
165165
this.name=name;
166-
this.path=path;
166+
this.parentPath=path;
167+
this.path=filepath;
167168
this[kType]=type;
168169
}
169170

@@ -197,8 +198,8 @@ class Dirent {
197198
}
198199

199200
classDirentFromStatsextendsDirent{
200-
constructor(name,stats,path){
201-
super(name,null,path);
201+
constructor(name,stats,path,filepath){
202+
super(name,null,path,filepath);
202203
this[kStats]=stats;
203204
}
204205
}
@@ -233,7 +234,7 @@ function join(path, name) {
233234
}
234235

235236
if(typeofpath==='string'&&typeofname==='string'){
236-
returnpathModule.basename(path)===name ?path :pathModule.join(path,name);
237+
returnpathModule.join(path,name);
237238
}
238239

239240
if(isUint8Array(path)&&isUint8Array(name)){
@@ -268,7 +269,7 @@ function getDirents(path, { 0: names, 1: types }, callback) {
268269
callback(err);
269270
return;
270271
}
271-
names[idx]=newDirentFromStats(name,stats,path);
272+
names[idx]=newDirentFromStats(name,stats,path,filepath);
272273
if(--toFinish===0){
273274
callback(null,names);
274275
}
@@ -304,17 +305,21 @@ function getDirent(path, name, type, callback) {
304305
callback(err);
305306
return;
306307
}
307-
callback(null,newDirentFromStats(name,stats,filepath));
308+
callback(null,newDirentFromStats(name,stats,path,filepath));
308309
});
309310
}else{
310311
callback(null,newDirent(name,type,path));
311312
}
312313
}elseif(type===UV_DIRENT_UNKNOWN){
313314
constfilepath=join(path,name);
314315
conststats=lazyLoadFs().lstatSync(filepath);
315-
returnnewDirentFromStats(name,stats,path);
316-
}else{
316+
// callback === true: Quirk to not introduce a breaking change.
317+
returnnewDirentFromStats(name,stats,path,callback===true ?filepath :path);
318+
}elseif(callback===true){
319+
// callback === true: Quirk to not introduce a breaking change.
317320
returnnewDirent(name,type,path);
321+
}else{
322+
returnnewDirent(name,type,path,path);
318323
}
319324
}
320325

‎test/parallel/test-fs-opendir.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ const invalidCallbackObj = {
4848
constentries=files.map(()=>{
4949
constdirent=dir.readSync();
5050
assertDirent(dirent);
51-
returndirent.name;
52-
});
53-
assert.deepStrictEqual(files,entries.sort());
51+
return{name:dirent.name,path:dirent.path,parentPath:dirent.parentPath,toString(){returndirent.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)

[8]ページ先頭

©2009-2025 Movatter.jp