From 01d4946ec86e9255de1c9bdec196a3885c82cea0 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 30 Nov 2023 15:20:56 +0100 Subject: [PATCH] fs: runtime deprecate `dirent.path` --- doc/api/deprecations.md | 5 ++++- lib/internal/fs/utils.js | 12 +++++++++++- test/parallel/test-fs-utils-get-dirents.js | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 763c5792650f32..1861fbbcc4f23f 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3535,12 +3535,15 @@ The [`util.types.isWebAssemblyCompiledModule`][] API is deprecated. Please use -Type: Documentation-only +Type: Runtime The [`dirent.path`][] is deprecated due to its lack of consistency across release lines. Please use [`dirent.parentPath`][] instead. diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index fa2851a244b5ee..6027a0d66c18be 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -12,6 +12,7 @@ const { NumberIsFinite, MathMin, MathRound, + ObjectDefineProperty, ObjectIs, ObjectSetPrototypeOf, ReflectApply, @@ -47,6 +48,7 @@ const { isUint8Array, } = require('internal/util/types'); const { + deprecate, kEmptyObject, once, } = require('internal/util'); @@ -166,7 +168,6 @@ class Dirent { constructor(name, type, path) { this.name = name; this.parentPath = path; - this.path = path; this[kType] = type; } @@ -215,6 +216,15 @@ for (const name of ReflectOwnKeys(Dirent.prototype)) { }; } +ObjectDefineProperty(Dirent.prototype, 'path', { + __proto__: null, + get: deprecate(function() { + return this.parentPath; + }, 'dirent.path is deprecated in favor of dirent.parentName', 'DEP0178'), + configurable: true, + enumerable: false, +}); + function copyObject(source) { const target = {}; for (const key in source) diff --git a/test/parallel/test-fs-utils-get-dirents.js b/test/parallel/test-fs-utils-get-dirents.js index 9c53a4146a6768..9791427e4c4cc7 100644 --- a/test/parallel/test-fs-utils-get-dirents.js +++ b/test/parallel/test-fs-utils-get-dirents.js @@ -88,6 +88,11 @@ const filename = 'foo'; common.mustCall((err, dirent) => { assert.strictEqual(err, null); assert.strictEqual(dirent.name, filenameBuffer); + common.expectWarning( + 'DeprecationWarning', + 'dirent.path is deprecated in favor of dirent.parentName', + 'DEP0178'); + assert.deepStrictEqual(dirent.path, Buffer.from(tmpdir.resolve(`${filename}/`))); }, )); }