Skip to content

feat(util.is()): introduce #119

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open

feat(util.is()): introduce #119

wants to merge 15 commits into from

Conversation

AugustinMauroy
Copy link
Member

Description

Close #116

@AugustinMauroy AugustinMauroy requested a review from a team July 24, 2025 15:03
@AugustinMauroy AugustinMauroy changed the title Feat(util.is()) feat(util.is()): introduce Jul 24, 2025
if (someValue instanceof Date) {
console.log('someValue is a date');
}
if (someValue instanceof Error) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (someValue instanceof Error) {
if (Error.isError(someValue)) {

Copy link
Member

Choose a reason for hiding this comment

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

Let's check the behavior of the original function to be sure.

Copy link
Member

Choose a reason for hiding this comment

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

Good to check, but this is still the more correct future-facing alternative.

if (typeof someValue === 'number') {
console.log('someValue is a number');
}
if (typeof someValue === 'object' && someValue !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

what about functions, which are also objects?

if (Buffer.isBuffer(someValue)) {
console.log('someValue is a buffer');
}
if (someValue instanceof Date) {
Copy link
Member

Choose a reason for hiding this comment

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

instanceof is not reliable and can be forged; i'm not sure this is a good replacement to be recommending

if (someValue !== Object(someValue)) {
console.log('someValue is a primitive');
}
if (someValue instanceof RegExp) {
Copy link
Member

Choose a reason for hiding this comment

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

instanceof is not reliable and can be forged; i'm not sure this is a good replacement to be recommending

@avivkeller
Copy link
Member

@AugustinMauroy Can you check the original implementations and make sure these match them?

If they do, can you make that clear?

@@ -34,7 +34,7 @@ export default function transform(root: SgRoot): string | null {
'isBoolean': (arg: string) => `typeof ${arg} === 'boolean'`,
'isBuffer': (arg: string) => `Buffer.isBuffer(${arg})`,
'isDate': (arg: string) => `${arg} instanceof Date`,
'isError': (arg: string) => `${arg} instanceof Error`,
'isError': (arg: string) => `Object.prototype.toString(${arg}) === '[object Error]' || ${arg} instanceof Error `,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
'isError': (arg: string) => `Object.prototype.toString(${arg}) === '[object Error]' || ${arg} instanceof Error `,
'isError': (arg: string) => `Object.prototype.toString.call(${arg}) === '[object Error]' || ${arg} instanceof Error `,

Copy link
Member Author

Choose a reason for hiding this comment

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

https://nodejs.org/api/deprecations.html#dep0048-utiliserror

The util.isError() API has been removed. Please use Object.prototype.toString(arg) === '[object Error]' || arg instanceof Error instead.

Copy link
Member

Choose a reason for hiding this comment

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

That's just wrong though, it won't work. It shouldn't matter what's in the docs - putting this in the codemod will break projects.

Copy link
Member Author

Choose a reason for hiding this comment

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

Humm idk what should we do I always follow doc. @JakobJingleheimer (the formal maintainer) what is your opinon

'isError': (arg: string) => `Object.prototype.toString(${arg}) === '[object Error]' || ${arg} instanceof Error `,
'isFunction': (arg: string) => `typeof ${arg} === 'function'`,
'isNull': (arg: string) => `${arg} === null`,
'isNullOrUndefined': (arg: string) => `${arg} === null || ${arg} === undefined`,
Copy link
Member

Choose a reason for hiding this comment

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

this is the same as ${arg} == null, and it's simpler, so maybe we should stick with that?

Copy link
Member Author

Choose a reason for hiding this comment

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

https://nodejs.org/api/deprecations.html#DEP0051

The util.isNullOrUndefined() API has been removed. Please use arg === null || arg === undefined instead.

'isNullOrUndefined': (arg: string) => `${arg} === null || ${arg} === undefined`,
'isNumber': (arg: string) => `typeof ${arg} === 'number'`,
'isObject': (arg: string) => `${arg} && typeof ${arg} === 'object'`,
'isPrimitive': (arg: string) => `${arg} === null || (typeof ${arg} !=='object' && typeof ${arg} !== 'function')`,
Copy link
Member

Choose a reason for hiding this comment

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

also, Object(${arg}) !== ${arg} is terser

Copy link
Member Author

Choose a reason for hiding this comment

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

https://nodejs.org/api/deprecations.html#dep0054-utilisprimitive

the util.isPrimitive() API has been removed. Please use arg === null || (typeof arg !=='object' && typeof arg !== 'function') instead.

@AugustinMauroy
Copy link
Member Author

I'm sorry @ljharb but If you think there are better way to handle that please first update node api doc and then we will update this codemod

@ljharb
Copy link
Member

ljharb commented Jul 25, 2025

For the opinion ones, fine, but one of them is a bug and is just broken. It shouldn't matter what's in the docs, but it matters what code does.

@AugustinMauroy
Copy link
Member Author

For the opinion ones, fine, but one of them is a bug and is just broken. It shouldn't matter what's in the docs, but it matters what code does.

I'm annoyed because I'm caught between two stools. On the one hand, I agree with you. On the other, I want to follow the documentation strictly so that the org node is consistent.

@ljharb
Copy link
Member

ljharb commented Jul 26, 2025

That seems like an arbitrary and counterproductive constraint to me. The docs aren’t the source of truth, they need to reflect the actual source of truth, the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: handle util.is**() depreciation
3 participants