Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: ensure return value type of options.filter in cpSync consistent with doc #52461

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/internal/fs/cp/cp-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const {
parse,
resolve,
} = require('path');
const { isPromise } = require('util/types');

function cpSyncFn(src, dest, opts) {
// Warn about using preserveTimestamps on 32-bit node
Expand All @@ -64,7 +63,7 @@ function cpSyncFn(src, dest, opts) {
function checkPathsSync(src, dest, opts) {
if (opts.filter) {
const shouldCopy = opts.filter(src, dest);
if (isPromise(shouldCopy)) {
if (typeof shouldCopy !== 'boolean') {
Comment on lines -67 to +66
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling this was done on purpose, to support truthy values, and there's a Promise check to prevent mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your feedback. and I have a question, for

opt.filter = (value) => {
 //
}

its return type contradicts the documentation, which states Returns: <boolean>, but it wont throw an error. Should here use a more precise description, such as the type that is coercible to boolean type?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @nodejs/fs @bcoe @aduh95

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with either (changing the code or the docs); if we change the code, let's land this as semver-major.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I might prefer changing the docs to changing the code here.

throw new ERR_INVALID_RETURN_VALUE('boolean', 'filter', shouldCopy);
}
if (!shouldCopy) return { __proto__: null, skipped: true };
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-fs-cp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,22 @@ if (!isWindows) {
cpSync(src, dest, opts);
}

// It will throw error for invalid return value from filter
{
const src = nextdir();
const dest = nextdir();

const opts = {
filter: (path) => {
// Undefined is not a valid return value.
},
};
assert.throws(
() => cpSync(src, dest, opts),
{ code: 'ERR_INVALID_RETURN_VALUE' }
);
}

// Copy should not throw exception if dest is invalid but filtered out.
{
// Create dest as a file.
Expand Down