Skip to content
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
18 changes: 14 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions recipes/types-is-web-assembly-compiled-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# `types.isWebAssemblyCompiledModule` DEP0177

This recipe transforms the usage of `types.isWebAssemblyCompiledModule` to use the `instanceof WebAssembly.Module` operator.

See [DEP0177](https://nodejs.org/api/deprecations.html#DEP0177).

## Example

**Before:**

```js
import { types } from "node:util";

if (types.isWebAssemblyCompiledModule(value)) {
// handle the module
}
```

**After:**

```js
if (value instanceof WebAssembly.Module) {
// handle the module
}
```
21 changes: 21 additions & 0 deletions recipes/types-is-web-assembly-compiled-module/codemod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
schema_version: "1.0"
name: "@nodejs/types-is-web-assembly-compiled-module"
version: 1.0.0
description: Handle DEP0177 via transforming `types.isWebAssemblyCompiledModule` to `instanceof WebAssembly.Module`
author: Node.js Contributors
license: MIT
workflow: workflow.yaml
category: migration

targets:
languages:
- javascript
- typescript

keywords:
- transformation
- migration

registry:
access: public
visibility: public
23 changes: 23 additions & 0 deletions recipes/types-is-web-assembly-compiled-module/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "@nodejs/types-is-web-assembly-compiled-module",
"version": "1.0.0",
"description": "Handle DEP0177 via transforming `types.isWebAssemblyCompiledModule` to `instanceof WebAssembly.Module`",
"type": "module",
"scripts": {
"test": "npx codemod jssg test -l typescript ./src/workflow.ts ./"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nodejs/userland-migrations.git",
"directory": "recipes/types-is-web-assembly-compiled-module",
"bugs": "https://github.com/nodejs/userland-migrations/issues"
},
"author": "Node.js Contributors",
"homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/types-is-web-assembly-compiled-module/README.md",
"devDependencies": {
"@codemod.com/jssg-types": "^1.0.9"
},
"dependencies": {
"@nodejs/codemod-utils": "*"
}
}
154 changes: 154 additions & 0 deletions recipes/types-is-web-assembly-compiled-module/src/workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call';
import { getNodeImportStatements } from '@nodejs/codemod-utils/ast-grep/import-statement';
import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path';
import { removeLines } from '@nodejs/codemod-utils/ast-grep/remove-lines';
import { removeBinding } from '@nodejs/codemod-utils/ast-grep/remove-binding';
import type { Edit, Range, SgNode, SgRoot } from '@codemod.com/jssg-types/main';
import type JS from '@codemod.com/jssg-types/langs/javascript';

type Binding = {
path: string;
lastPropertyAccess?: string;
propertyAccess?: string;
depth: number;
node: SgNode<JS>;
};

/**
* Extracts property access information from a dot-notation path string.
*
* @param path - A dot-notation string representing a property path (e.g., "object.property.subProperty")
* @returns An object containing:
* - `path`: The original path string
* - `lastPropertyAccess`: The last segment of the path (e.g., "subProperty" from "object.property.subProperty")
* - `propertyAccess`: The path without the last segment (e.g., "object.property" from "object.property.subProperty")
* - `depth`: The number of segments in the path
*
* @example
* ```typescript
* createPropBinding("foo.bar.baz");
* // Returns: { path: "foo.bar.baz", propertyAccess: "foo.bar", lastPropertyAccess: "baz", depth: 3 }
*
* createPropBinding("foo");
* // Returns: { path: "foo", propertyAccess: "", lastPropertyAccess: "foo", depth: 1 }
* ```
*/
function createPropBinding(
path: string,
): Pick<Binding, 'path' | 'lastPropertyAccess' | 'propertyAccess' | 'depth'> {
const pathArr = path.split('.');

const lastPropertyAccess = pathArr.at(-1);
const propertyAccess = pathArr.slice(0, -1).join('.');

return {
path,
propertyAccess,
lastPropertyAccess,
depth: pathArr.length,
};
}

/**
* Transforms `util.types.isWebAssemblyCompiledModule` usage to `instanceof WebAssembly.Module`.
*
* This transformation handles various import/require patterns and usage scenarios:
*
* 1. Identifies all require/import statements from 'node:util' or 'util' module that
* include access to `types.isWebAssemblyCompiledModule`
*
* 2. Replaces all matching code references:
* - `util.types.isWebAssemblyCompiledModule(value)` → `value instanceof WebAssembly.Module`
* - `types.isWebAssemblyCompiledModule(value)` → `value instanceof WebAssembly.Module`
* - `isWebAssemblyCompiledModule(value)` → `value instanceof WebAssembly.Module`
*
* 3. Removes unused bindings when all references to the imported/required
* isWebAssemblyCompiledModule have been replaced
*
*/
export default function transform(root: SgRoot<JS>): string | null {
const rootNode = root.root();
const bindings: Binding[] = [];
const edits: Edit[] = [];
const linesToRemove: Range[] = [];

const statements = [
...getNodeRequireCalls(root, 'util'),
...getNodeImportStatements(root, 'util'),
];

// if no imports are present it means that we don't need to process the file
if (!statements.length) return null;

for (const stmt of statements) {
const bindToReplace = resolveBindingPath(stmt, '$.types.isWebAssemblyCompiledModule');

if (!bindToReplace) {
continue;
}

bindings.push({
...createPropBinding(bindToReplace),
node: stmt,
});
}

for (const binding of bindings) {
const nodes = rootNode.findAll({
rule: {
pattern: `${binding.propertyAccess || binding.path}${binding.depth > 1 ? '.$$$FN' : ''}`,
},
});

// Find all function calls to isWebAssemblyCompiledModule
const nodesToEdit = rootNode.findAll({
rule: {
pattern: `${binding.path}($ARG)`,
},
});

for (const node of nodesToEdit) {
// Extract the argument from the function call using getMatch
const argNode = node.getMatch('ARG');
if (argNode) {
const argText = argNode.text();

// Check if this call is inside a unary ! (NOT) expression
// We check if parent is a unary_expression and has '!' operator
const parent = node.parent();
const isNegated = parent?.kind() === 'unary_expression' &&
parent.find({ rule: { pattern: '!' } }) !== undefined;

// Replace the entire call expression with instanceof check
// Wrap in parentheses if negated to preserve operator precedence
const replacement = isNegated
? `(${argText} instanceof WebAssembly.Module)`
: `${argText} instanceof WebAssembly.Module`;

edits.push(node.replace(replacement));
}
}

if (nodes.length === nodesToEdit.length) {
const bindToRemove = binding.path.includes('.')
? binding.path.split('.')[0]
: binding.path;

const result = removeBinding(binding.node, bindToRemove);

if (result?.edit) {
edits.push(result.edit);
}

if (result?.lineToRemove) {
linesToRemove.push(result.lineToRemove);
}
}
}

if (!edits.length) return null;

const sourceCode = rootNode.commitEdits(edits);

return removeLines(sourceCode, linesToRemove);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

if (value instanceof WebAssembly.Module) {
console.log("It's a WebAssembly module");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

const check = data instanceof WebAssembly.Module ? "module" : "not module";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

const result = module instanceof WebAssembly.Module;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

if (compiledModule instanceof WebAssembly.Module) {
// do something
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

const isModule = value instanceof WebAssembly.Module;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

if (obj instanceof WebAssembly.Module) {
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

function isWasmModule(value) {
return value instanceof WebAssembly.Module;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {
types: { isMap },
} = require("util");

if (module instanceof WebAssembly.Module) {
// handle
}

if (isMap([])) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const value = {};

if (value instanceof WebAssembly.Module) {
// handle
}

if (value instanceof WebAssembly.Module) {
// handle
}

if (value instanceof WebAssembly.Module) {
// handle
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

if (!(value instanceof WebAssembly.Module)) {
throw new Error("Not a WebAssembly module");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const util = require("node:util");

if (util.types.isWebAssemblyCompiledModule(value)) {
console.log("It's a WebAssembly module");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const util = require("node:util");

const check = util.types.isWebAssemblyCompiledModule(data) ? "module" : "not module";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { types } = require("node:util");

const result = types.isWebAssemblyCompiledModule(module);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { isWebAssemblyCompiledModule } = require("node:util").types;

if (isWebAssemblyCompiledModule(compiledModule)) {
// do something
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import util from "node:util";

const isModule = util.types.isWebAssemblyCompiledModule(value);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { types } from "node:util";

if (types.isWebAssemblyCompiledModule(obj)) {
return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { types } = require("node:util");

function isWasmModule(value) {
return types.isWebAssemblyCompiledModule(value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {
types: { isWebAssemblyCompiledModule, isMap },
} = require("util");

if (isWebAssemblyCompiledModule(module)) {
// handle
}

if (isMap([])) {
}
Loading