Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 20 additions & 19 deletions recipes/util-is/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `util.is**()`
# `util.is*()`

This codemod replaces the following deprecated `util.is*()` methods with their modern equivalents:

This codemod replaces the following deprecated `util.is**()` methods with their modern equivalents:
- [DEP0044: `util.isArray()`](https://nodejs.org/docs/latest/api/deprecations.html#DEP0044)
- [DEP0045: `util.isBoolean()`](https://nodejs.org/docs/latest/api/deprecations.html#dep0045-utilisboolean)
- [DEP0046: `util.isBuffer()`](https://nodejs.org/docs/latest/api/deprecations.html#dep0046-utilisbuffer)
Expand All @@ -19,20 +20,20 @@ This codemod replaces the following deprecated `util.is**()` methods with their

## Examples

| **Before** | **After** |
|--------------------------------|---------------------------------|
| `util.isArray(someValue` | `Array.isArray(someValue` |
| `util.isBoolean(someValue` | `typeof someValue === 'boolean'`|
| `util.isBuffer(someValue` | `Buffer.isBuffer(someValue` |
| `util.isDate(someValue` | `someValue instanceof Date` |
| `util.isError(someValue` | `Error.isError(someValue` |
| `util.isFunction(someValue` | `typeof someValue === 'function'`|
| `util.isNull(someValue` | `someValue === null` |
| `util.isNullOrUndefined(someValue` | `someValue == null` |
| `util.isNumber(someValue` | `typeof someValue === 'number'` |
| `util.isObject(someValue` | `someValue && typeof someValue === 'object'` |
| `util.isPrimitive(someValue` | `Object(someValue) !== someValue`|
| `util.isRegExp(someValue` | `someValue instanceof RegExp` |
| `util.isString(someValue` | `typeof someValue === 'string'` |
| `util.isSymbol(someValue` | `typeof someValue === 'symbol'` |
| `util.isUndefined(someValue` | `typeof someValue === 'undefined'` |
| **Before** | **After** |
|-----------------------------------|---------------------------------------------|
| `util.isArray(value)` | `Array.isArray(value)` |
| `util.isBoolean(value)` | `typeof value === 'boolean'` |
| `util.isBuffer(value)` | `Buffer.isBuffer(value)` |
| `util.isDate(value)` | `value instanceof Date` |
| `util.isError(value)` | `Error.isError(value)` |
| `util.isFunction(value)` | `typeof value === 'function'` |
| `util.isNull(value)` | `value === null` |
| `util.isNullOrUndefined(value)` | `value === null || value === undefined` |
| `util.isNumber(value)` | `typeof value === 'number'` |
| `util.isObject(value)` | `value && typeof value === 'object'` |
| `util.isPrimitive(value)` | `Object(value) !== value` |
| `util.isRegExp(value)` | `value instanceof RegExp` |
| `util.isString(value)` | `typeof value === 'string'` |
| `util.isSymbol(value)` | `typeof value === 'symbol'` |
| `util.isUndefined(value)` | `typeof value === 'undefined'` |
8 changes: 5 additions & 3 deletions recipes/util-is/src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const replacements = new Map<string, (arg: string) => string>([
['isError', (arg: string) => `Error.isError(${arg})`],
['isFunction', (arg: string) => `typeof ${arg} === 'function'`],
['isNull', (arg: string) => `${arg} === null`],
['isNullOrUndefined', (arg: string) => `${arg} == null`],
['isNullOrUndefined', (arg: string) => `${arg} === null || ${arg} === undefined`],
['isNumber', (arg: string) => `typeof ${arg} === 'number'`],
['isObject', (arg: string) => `${arg} && typeof ${arg} === 'object'`],
['isPrimitive', (arg: string) => `Object(${arg}) !== ${arg}`],
Expand All @@ -69,7 +69,7 @@ const replacements = new Map<string, (arg: string) => string>([
* 5. util.isError() → value instanceof Error
* 6. util.isFunction() → typeof value === 'function'
* 7. util.isNull() → value === null
* 8. util.isNullOrUndefined() → value == null
* 8. util.isNullOrUndefined() → value === null || value === undefined
Copy link
Member

Choose a reason for hiding this comment

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

very nit since it's a comment:

Suggested change
* 8. util.isNullOrUndefined() value === null || value === undefined
* 8. util.isNullOrUndefined() value == null

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I understand :)

     null == null // true
undefined == null // true
    false == null // false
        0 == null // false
       '' == null // false

Copy link
Member

Choose a reason for hiding this comment

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

The difference is in document.all, I believe, which doesn't apply to node but would if someone cargoculted the info into a browser.

Copy link
Member Author

Choose a reason for hiding this comment

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

so what should I do ?

Copy link
Member

Choose a reason for hiding this comment

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

I don't understand @ljharb's comment, so I'm not sure.

But this thread was based on a nit—it's not blocking. The blocking issue is supporting dynamic import.

Copy link
Member

Choose a reason for hiding this comment

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

document.all == null but document.all !== null && typeof document.all !== 'undefined'.

Copy link
Member

Choose a reason for hiding this comment

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

Ah. I think we can ignore that extreme edge-case.

* 9. util.isNumber() → typeof value === 'number'
* 10. util.isObject() → typeof value === 'object' && value !== null
* 11. util.isPrimitive() → value !== Object(value)
Expand Down Expand Up @@ -207,7 +207,9 @@ export default function transform(root: SgRoot): string | null {
for (const requireNode of requireStatements) {
const objectPattern = requireNode.find({ rule: { kind: 'object_pattern' } });
if (objectPattern) {
const shorthand = objectPattern.findAll({ rule: { kind: 'shorthand_property_identifier_pattern' } });
const shorthand = objectPattern.findAll({
rule: { kind: 'shorthand_property_identifier_pattern' }
});
const pairs = objectPattern.findAll({ rule: { kind: 'pair_pattern' } });
const importedNames: string[] = [];
for (const s of shorthand) importedNames.push(s.text());
Expand Down
2 changes: 1 addition & 1 deletion recipes/util-is/tests/expected/file-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if (typeof someValue === 'function') {
if (someValue === null) {
console.log('someValue is null');
}
if (someValue == null) {
if (someValue === null || someValue === undefined) {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
if (someValue === null || someValue === undefined) {
if (someValue == null) {

console.log('someValue is null or undefined');
}
if (typeof someValue === 'number') {
Expand Down
Loading