-
-
Notifications
You must be signed in to change notification settings - Fork 39
feat(DEP0164): handle non-integer codes passed to process.exit & process.exitCode
#413
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
KevinSailema
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
KevinSailema:feat/process-exit-coercion-to-integer-405
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c9d071a
feat(process-exit-coercion-to-integer): add DEP0164 exit code coercio…
11e43bd
fix(process-exit-coercion-to-integer): address review feedback
d61d4c9
style(process-exit-coercion-to-integer): apply final review suggestions
cf39888
Merge branch 'main' into feat/process-exit-coercion-to-integer-405
AugustinMauroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # `process.exit(code)` / `process.exitCode` DEP0164 | ||
|
|
||
| This recipe migrates non-integer values passed to `process.exit(code)` and assigned to `process.exitCode`. | ||
|
|
||
| See [DEP0164](https://nodejs.org/api/deprecations.html#DEP0164). | ||
|
|
||
| ## What it changes | ||
|
|
||
| - Preserves valid values: | ||
| - integer numbers | ||
| - integer strings | ||
| - `undefined` | ||
| - `null` | ||
| - Converts boolean values to explicit numeric exit codes. | ||
| - Wraps floating-point numeric expressions with `Math.floor(...)`. | ||
| - Converts non-integer string literals to `1`. | ||
| - For `process.exitCode = { code: ... }`, extracts `code` when possible and coerces when needed. | ||
|
|
||
| ## Example | ||
|
|
||
| ```diff | ||
| - process.exit(0.5 + 0.7) | ||
| + process.exit(Math.floor(0.5 + 0.7)) | ||
| ``` | ||
|
|
||
| ```diff | ||
| - const success = false; | ||
| - process.exitCode = success; | ||
| + const success = false; | ||
| + process.exitCode = success ? 0 : 1; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/process-exit-coercion-to-integer" | ||
| version: "1.0.0" | ||
| description: Handle DEP0164 by coercing process.exit(code) and process.exitCode assignments to integer-compatible values. | ||
| author: Kevin Sailema | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - dep0164 | ||
| - process.exit | ||
| - process.exitCode | ||
| - migration | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "name": "@nodejs/process-exit-coercion-to-integer", | ||
| "version": "1.0.0", | ||
| "description": "Handle DEP0164 by coercing process.exit(code) and process.exitCode assignments to integer-compatible values.", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "npx codemod jssg test -l typescript ./src/workflow.ts ./tests" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/process-exit-coercion-to-integer", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "Kevin Sailema", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.5.0" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*" | ||
| } | ||
| } |
274 changes: 274 additions & 0 deletions
274
recipes/process-exit-coercion-to-integer/src/workflow.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| import { getModuleDependencies } from '@nodejs/codemod-utils/ast-grep/module-dependencies'; | ||
| import { resolveBindingPath } from '@nodejs/codemod-utils/ast-grep/resolve-binding-path'; | ||
| import type { Edit, SgNode, SgRoot } from '@codemod.com/jssg-types/main'; | ||
| import type JS from '@codemod.com/jssg-types/langs/javascript'; | ||
|
|
||
| type ExitMode = 'exit' | 'exitCode'; | ||
|
|
||
| type InferredIdentifierKind = | ||
| | 'boolean_true' | ||
| | 'boolean_false' | ||
| | 'integer_number' | ||
| | 'float_number' | ||
| | 'integer_string' | ||
| | 'non_integer_string' | ||
| | 'null' | ||
| | 'undefined' | ||
| | 'object' | ||
| | 'unknown'; | ||
|
|
||
| type InferredIdentifier = { | ||
| kind: InferredIdentifierKind; | ||
| initializerNode: SgNode<JS>; | ||
| }; | ||
|
|
||
| function isIntegerNumber(text: string): boolean { | ||
| const numeric = Number(text); | ||
| return Number.isFinite(numeric) && Number.isInteger(numeric); | ||
| } | ||
|
|
||
| function isIntegerStringValue(value: string): boolean { | ||
| return /^-?\d+$/.test(value); | ||
| } | ||
|
|
||
| function getStringLiteralValue(node: SgNode<JS>): string | null { | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (node.kind() !== 'string') return null; | ||
| const stringFragment = node.find({ | ||
| rule: { | ||
| kind: 'string_fragment', | ||
| }, | ||
| }); | ||
| return stringFragment?.text() ?? ''; | ||
| } | ||
|
|
||
| function inferIdentifierKind(valueNode: SgNode<JS>): InferredIdentifierKind { | ||
| const nodeKind = valueNode.kind(); | ||
| if (nodeKind === 'true') return 'boolean_true'; | ||
| if (nodeKind === 'false') return 'boolean_false'; | ||
| if (nodeKind === 'null') return 'null'; | ||
|
|
||
| if (nodeKind === 'identifier' && valueNode.text() === 'undefined') { | ||
| return 'undefined'; | ||
| } | ||
|
|
||
| if (nodeKind === 'number') { | ||
| return isIntegerNumber(valueNode.text()) | ||
| ? 'integer_number' | ||
| : 'float_number'; | ||
| } | ||
|
|
||
| if (nodeKind === 'string') { | ||
| const value = getStringLiteralValue(valueNode); | ||
| if (value === null) return 'unknown'; | ||
| return isIntegerStringValue(value) | ||
| ? 'integer_string' | ||
| : 'non_integer_string'; | ||
| } | ||
|
|
||
| if (nodeKind === 'object') return 'object'; | ||
|
|
||
| return 'unknown'; | ||
| } | ||
|
|
||
| function floorWrap(expressionText: string): string { | ||
| return `Math.floor(${expressionText})`; | ||
| } | ||
|
|
||
| function coerceBoolean(expressionText: string, mode: ExitMode): string { | ||
| if (mode === 'exit') return `${expressionText} ? 1 : 0`; | ||
| return `${expressionText} ? 0 : 1`; | ||
| } | ||
|
|
||
| function getObjectCodeValue(objectNode: SgNode<JS>): SgNode<JS> | null { | ||
| const pairs = objectNode.findAll({ | ||
| rule: { | ||
| kind: 'pair', | ||
| }, | ||
| }); | ||
|
|
||
| for (const pair of pairs) { | ||
| const key = pair.field('key'); | ||
| const value = pair.field('value'); | ||
| if (!key || !value) continue; | ||
| if (key.text() === 'code') return value; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function coerceFromObjectLiteral( | ||
| objectNode: SgNode<JS>, | ||
| mode: ExitMode, | ||
| ): string { | ||
| if (mode !== 'exitCode') return '1'; | ||
|
|
||
| const codeValue = getObjectCodeValue(objectNode); | ||
| if (!codeValue) return '1'; | ||
|
|
||
| const kind = inferIdentifierKind(codeValue); | ||
| if (kind === 'integer_number' || kind === 'integer_string') { | ||
| return codeValue.text(); | ||
| } | ||
| if (kind === 'null' || kind === 'undefined') { | ||
| return codeValue.text(); | ||
| } | ||
| if (kind === 'boolean_true' || kind === 'boolean_false') { | ||
| return coerceBoolean(codeValue.text(), mode); | ||
| } | ||
| if (kind === 'float_number') { | ||
| return floorWrap(codeValue.text()); | ||
| } | ||
|
|
||
| return '1'; | ||
| } | ||
|
|
||
| function shouldFloorExpression(node: SgNode<JS>): boolean { | ||
| const nodeKind = node.kind(); | ||
| return ( | ||
| nodeKind === 'binary_expression' || | ||
| nodeKind === 'unary_expression' || | ||
| nodeKind === 'update_expression' | ||
| ); | ||
| } | ||
|
|
||
| function coerceValueNode( | ||
| node: SgNode<JS>, | ||
| mode: ExitMode, | ||
| inferredIdentifiers: Map<string, InferredIdentifier>, | ||
| ): string | null { | ||
| const inferredKind = inferIdentifierKind(node); | ||
|
|
||
| if ( | ||
| inferredKind === 'undefined' || | ||
| inferredKind === 'null' || | ||
| inferredKind === 'integer_number' || | ||
| inferredKind === 'integer_string' | ||
| ) { | ||
| return null; | ||
| } | ||
|
|
||
| if (inferredKind === 'boolean_true' || inferredKind === 'boolean_false') { | ||
| return mode === 'exit' | ||
| ? inferredKind === 'boolean_true' | ||
| ? '1' | ||
| : '0' | ||
| : inferredKind === 'boolean_true' | ||
| ? '0' | ||
| : '1'; | ||
| } | ||
|
|
||
| if (inferredKind === 'non_integer_string') return '1'; | ||
|
|
||
| if (inferredKind === 'float_number') return floorWrap(node.text()); | ||
|
|
||
| if (inferredKind === 'object') return coerceFromObjectLiteral(node, mode); | ||
|
|
||
| if (node.kind() === 'identifier') { | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const inferred = inferredIdentifiers.get(node.text()); | ||
| if (!inferred) return null; | ||
|
|
||
| if (inferred.kind === 'boolean_true' || inferred.kind === 'boolean_false') { | ||
| return coerceBoolean(node.text(), mode); | ||
| } | ||
| if (inferred.kind === 'float_number') return floorWrap(node.text()); | ||
| if (inferred.kind === 'non_integer_string') return '1'; | ||
| if (inferred.kind === 'object') { | ||
| return coerceFromObjectLiteral(inferred.initializerNode, mode); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| if (shouldFloorExpression(node)) { | ||
| return floorWrap(node.text()); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function collectInferredIdentifiers( | ||
| rootNode: SgNode<JS>, | ||
| ): Map<string, InferredIdentifier> { | ||
| const inferred = new Map<string, InferredIdentifier>(); | ||
| const declarators = rootNode.findAll({ | ||
| rule: { | ||
| kind: 'variable_declarator', | ||
| }, | ||
| }); | ||
|
|
||
| for (const declarator of declarators) { | ||
| const name = declarator.field('name'); | ||
| const value = declarator.field('value'); | ||
| if (!name || !value || name.kind() !== 'identifier') continue; | ||
|
|
||
| inferred.set(name.text(), { | ||
| kind: inferIdentifierKind(value), | ||
| initializerNode: value, | ||
| }); | ||
| } | ||
|
|
||
| return inferred; | ||
| } | ||
|
|
||
| export default function transform(root: SgRoot<JS>): string | null { | ||
| const rootNode = root.root(); | ||
| const edits: Edit[] = []; | ||
|
|
||
| const inferredIdentifiers = collectInferredIdentifiers(rootNode); | ||
|
|
||
| const exitBindings = new Set<string>(['process.exit']); | ||
| const exitCodeBindings = new Set<string>(['process.exitCode']); | ||
|
|
||
| const processDependencies = getModuleDependencies(root, 'process'); | ||
|
|
||
| for (const dependency of processDependencies) { | ||
| const exitBinding = resolveBindingPath(dependency, '$.exit'); | ||
| if (exitBinding) exitBindings.add(exitBinding); | ||
|
|
||
| const exitCodeBinding = resolveBindingPath(dependency, '$.exitCode'); | ||
| if (exitCodeBinding) exitCodeBindings.add(exitCodeBinding); | ||
| } | ||
|
|
||
| for (const binding of exitBindings) { | ||
| const callNodes = rootNode.findAll({ | ||
| rule: { | ||
| pattern: `${binding}($ARG)`, | ||
| }, | ||
| }); | ||
|
|
||
| for (const callNode of callNodes) { | ||
| const argNode = callNode.getMatch('ARG'); | ||
| if (!argNode) continue; | ||
|
|
||
| const replacement = coerceValueNode(argNode, 'exit', inferredIdentifiers); | ||
|
|
||
| if (!replacement || replacement === argNode.text()) continue; | ||
| edits.push(argNode.replace(replacement)); | ||
| } | ||
| } | ||
|
|
||
| for (const binding of exitCodeBindings) { | ||
| const assignmentNodes = rootNode.findAll({ | ||
| rule: { | ||
| pattern: `${binding} = $VALUE`, | ||
| }, | ||
| }); | ||
|
|
||
| for (const assignmentNode of assignmentNodes) { | ||
| const valueNode = assignmentNode.getMatch('VALUE'); | ||
| if (!valueNode) continue; | ||
|
|
||
| const replacement = coerceValueNode( | ||
| valueNode, | ||
| 'exitCode', | ||
| inferredIdentifiers, | ||
| ); | ||
|
|
||
| if (!replacement || replacement === valueNode.text()) continue; | ||
| edits.push(valueNode.replace(replacement)); | ||
| } | ||
| } | ||
|
|
||
| if (!edits.length) return null; | ||
|
|
||
| return rootNode.commitEdits(edits); | ||
AugustinMauroy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/01-boolean-true-exit/expected.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exit(1); |
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/01-boolean-true-exit/input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exit(true); |
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/02-boolean-false-exit/expected.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exit(0); |
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/02-boolean-false-exit/input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exit(false); |
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/03-floating-exit/expected.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exit(Math.floor(1.5)); |
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/03-floating-exit/input.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exit(1.5); |
1 change: 1 addition & 0 deletions
1
recipes/process-exit-coercion-to-integer/tests/04-object-exitcode/expected.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| process.exitCode = 1; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.