-
-
Notifications
You must be signed in to change notification settings - Fork 28
feat(timers deprecation): introduce #255
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
d81e531
7409e1b
b679ff5
a88c1c4
b5635b5
bfc9b18
c3b4d51
3292eac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,60 @@ | ||||||||||||||||||||||||||||||||||||||||||
| # Node.js Timers Deprecations | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| This recipe migrates deprecated internals from `node:timers` to the supported public timers API. It replaces usages of `timers.enroll()`, `timers.unenroll()`, `timers.active()`, and `timers._unrefActive()` with standard constructs built on top of `setTimeout()`, `clearTimeout()`, and `Timer#unref()`. | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| See the upstream notices: [DEP0095](https://nodejs.org/api/deprecations.html#DEP0095), [DEP0096](https://nodejs.org/api/deprecations.html#DEP0096), [DEP0126](https://nodejs.org/api/deprecations.html#DEP0126), and [DEP0127](https://nodejs.org/api/deprecations.html#DEP0127). | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ## Example | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ### Replace `timers.enroll()` | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| **Before:** | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||||
| const timers = require('node:timers'); | ||||||||||||||||||||||||||||||||||||||||||
| const resource = { _idleTimeout: 1500 }; | ||||||||||||||||||||||||||||||||||||||||||
| timers.enroll(resource, 1500); | ||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| **After:** | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||||
| const resource = { timeout: setTimeout(() => { | ||||||||||||||||||||||||||||||||||||||||||
| // timeout handler | ||||||||||||||||||||||||||||||||||||||||||
| }, 1500) }; | ||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ### Replace `timers.unenroll()` | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| **Before:** | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||||
| timers.unenroll(resource); | ||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| **After:** | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ```js | ||||||||||||||||||||||||||||||||||||||||||
| clearTimeout(resource.timeout); | ||||||||||||||||||||||||||||||||||||||||||
| ``` | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| **Before:** | |
| ```js | |
| timers.unenroll(resource); | |
| ``` | |
| **After:** | |
| ```js | |
| clearTimeout(resource.timeout); | |
| ``` | |
| ```diff | |
| - timers.unenroll(resource); | |
| + clearTimeout(resource.timeout); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where do onTimeout and delay come from? they aren't in the original or new code
| **Before:** | |
| ```js | |
| const timers = require('node:timers'); | |
| timers.active(resource); | |
| timers._unrefActive(resource); | |
| ``` | |
| **After:** | |
| ```js | |
| const handle = setTimeout(onTimeout, delay); | |
| handle.unref(); | |
| ``` | |
| ```diff | |
| - const timers = require('node:timers'); | |
| - timers.active(resource); | |
| - timers._unrefActive(resource); | |
| + const handle = setTimeout(onTimeout, delay); | |
| + handle.unref(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/timers-deprecations" | ||
| version: 1.0.0 | ||
| description: Migrate deprecated node:timers APIs to public timer functions. | ||
| author: Augustin Mauroy | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - migration | ||
| - timers | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "name": "@nodejs/timers-deprecations", | ||
| "version": "1.0.0", | ||
| "description": "Migrate deprecated node:timers APIs to public timer functions.", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "node --run test:enroll && node --run test:unenroll && node --run test:active && node --run test:unref && node --run test:imports", | ||
| "test:enroll": "npx codemod jssg test -l typescript ./src/enroll-to-set-timeout.ts ./tests/ --filter dep0095", | ||
| "test:unenroll": "npx codemod jssg test -l typescript ./src/unenroll-to-clear-timer.ts ./tests/ --filter dep0096", | ||
| "test:active": "npx codemod jssg test -l typescript ./src/active-to-standard-timer.ts ./tests/ --filter active", | ||
| "test:unref": "npx codemod jssg test -l typescript ./src/unref-active-to-unref.ts ./tests/ --filter unref", | ||
| "test:imports": "npx codemod jssg test -l typescript ./src/cleanup-imports.ts ./tests/ --filter imports" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/timers-deprecations", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "Augustin Mauroy", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/tree/main/recipes/timers-deprecations#readme", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { EOL } from 'node:os'; | ||
| import { | ||
| getNodeImportStatements, | ||
| getNodeImportCalls, | ||
| } from '@nodejs/codemod-utils/ast-grep/import-statement'; | ||
| import { getNodeRequireCalls } from '@nodejs/codemod-utils/ast-grep/require-call'; | ||
| import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; | ||
| import { | ||
| findParentStatement, | ||
| isSafeResourceTarget, | ||
| } from '@nodejs/codemod-utils/ast-grep/general'; | ||
| import { | ||
| detectIndentUnit, | ||
| getLineIndent, | ||
| } from '@nodejs/codemod-utils/ast-grep/indent'; | ||
| import type { Edit, SgRoot } from '@codemod.com/jssg-types/main'; | ||
| import type Js from '@codemod.com/jssg-types/langs/javascript'; | ||
|
|
||
| const TARGET_METHOD = 'active'; | ||
|
|
||
| export default function transform(root: SgRoot<Js>): string | null { | ||
| const rootNode = root.root(); | ||
| const sourceCode = rootNode.text(); | ||
| const indentUnit = detectIndentUnit(sourceCode); | ||
| const edits: Edit[] = []; | ||
| const handledStatements = new Set<number>(); | ||
|
|
||
| const importNodes = [ | ||
| ...getNodeRequireCalls(root, 'timers'), | ||
| ...getNodeImportStatements(root, 'timers'), | ||
| ...getNodeImportCalls(root, 'timers'), | ||
| ]; | ||
|
|
||
| for (const importNode of importNodes) { | ||
| if (importNode.kind() === 'expression_statement') continue; | ||
| const bindingPath = resolveBindingPath(importNode, `$.${TARGET_METHOD}`); | ||
| if (!bindingPath) continue; | ||
|
|
||
| const matches = rootNode.findAll({ | ||
| rule: { | ||
| any: [ | ||
| { pattern: `${bindingPath}($RESOURCE)` }, | ||
| { pattern: `${bindingPath}($RESOURCE, $$$REST)` }, | ||
| ], | ||
| }, | ||
| }); | ||
|
|
||
| for (const match of matches) { | ||
| const resourceNode = match.getMatch('RESOURCE'); | ||
| if (!resourceNode) continue; | ||
|
|
||
| if (!isSafeResourceTarget(resourceNode)) continue; | ||
|
|
||
| const statement = findParentStatement(match); | ||
| if (!statement) continue; | ||
|
|
||
| if (handledStatements.has(statement.id())) continue; | ||
| handledStatements.add(statement.id()); | ||
|
|
||
| const indent = getLineIndent(sourceCode, statement.range().start.index); | ||
| const resourceText = resourceNode.text(); | ||
| const childIndent = indent + indentUnit; | ||
| const innerIndent = childIndent + indentUnit; | ||
|
|
||
| const replacement = | ||
| `if (${resourceText}.timeout != null) {${EOL}` + | ||
| `${childIndent}clearTimeout(${resourceText}.timeout);${EOL}` + | ||
| `${indent}}${EOL}${EOL}` + | ||
| `${indent}${resourceText}.timeout = setTimeout(() => {${EOL}` + | ||
| `${childIndent}if (typeof ${resourceText}._onTimeout === "function") {${EOL}` + | ||
| `${innerIndent}${resourceText}._onTimeout();${EOL}` + | ||
| `${childIndent}}${EOL}` + | ||
| `${indent}}, ${resourceText}._idleTimeout);`; | ||
|
|
||
| edits.push(statement.replace(replacement)); | ||
| } | ||
| } | ||
|
|
||
| if (!edits.length) return null; | ||
|
|
||
| return rootNode.commitEdits(edits); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does the content for
timeout handlercome from?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my brain 😅