feat(DEP0162): add codemod for fs write coercion#416
Open
syhstanley wants to merge 13 commits intonodejs:mainfrom
Open
feat(DEP0162): add codemod for fs write coercion#416syhstanley wants to merge 13 commits intonodejs:mainfrom
syhstanley wants to merge 13 commits intonodejs:mainfrom
Conversation
Add explicit String() conversion for objects passed as the data parameter to fs.writeFile(), fs.writeFileSync(), fs.appendFile(), fs.appendFileSync(), fs.write(), and their promises variants. Closes nodejs#412 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Add missing README.md (required per CONTRIBUTING.md) - Add yaml-language-server schema comment to workflow.yaml - Sort include file patterns alphabetically - Fix isSafeType: remove dead template literal check, tighten String() match - Add isLikelyBufferOverload guard for fs.write() buffer overload - Support float numeric literals in isSafeType - Normalize test file naming (file-02 → file-2) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
AugustinMauroy
left a comment
There was a problem hiding this comment.
not too bad but:
for you info you have to use this structure of testing
Codemod leverages a before ("input") + after ("expected") snapshot comparison. Codemod supports 2 options:
- 👍 Single-file fixtures
tests/ some-test-case-description/ input.ts expected.ts another-test-case-description/ input.ts expected.ts - 👎 Directory snapshot fixtures
tests/ input/ some-test-case-description.ts another-test-case-description.ts expected some-test-case-description.ts another-test-case-description.ts
Use the Single-file fixtures option.
- Migrate tests to single-file fixture structure (input.ts + expected.ts per case) - Use getModuleDependencies utility instead of separate import/require helpers - Remove unnecessary escapeRegex() wrapper for identifier matching Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
Author
|
Thanks for your review! Please check the new commits. |
Change test fixture path from ./ to ./tests to match single-file fixture structure. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
brunocroh
reviewed
Apr 15, 2026
Simplify filter logic by using the isNamed() method instead of directly comparing kind() against punctuation characters. This is more semantic and aligns with AST best practices. Fixes review feedback from brunocroh.
brunocroh
reviewed
Apr 19, 2026
Co-authored-by: Bruno Rodrigues <swe@brunocroh.com>
Member
|
your test are failing + lint need to be fixed |
…codemod - Remove unused `Edit` and `SgNode` imports that were causing linter warnings - Fix syntax error on line 133: replace semicolon with closing parenthesis in filter() chain This resolves the CI linting failures and allows tests to pass cleanly. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Contributor
Author
|
Fixed |
brunocroh
reviewed
Apr 24, 2026
| has: { | ||
| field: 'function', | ||
| any: [ | ||
| { kind: 'identifier', regex: `^${local}$` }, |
Member
There was a problem hiding this comment.
Suggested change
| { kind: 'identifier', regex: `^${local}$` }, | |
| { kind: 'identifier', pattern: local }, |
| if (!local) continue; | ||
|
|
||
| // Find all call expressions for this binding | ||
| const calls = rootNode.findAll({ |
Member
There was a problem hiding this comment.
Suggested change
| const calls = rootNode.findAll({ | |
| const calls = rootNode.findAll<'call_expression'>({ |
|
|
||
| for (const call of calls) { | ||
| // Get the arguments node | ||
| const argsNode = call.find({ rule: { kind: 'arguments' } }); |
Member
There was a problem hiding this comment.
Suggested change
| const argsNode = call.find({ rule: { kind: 'arguments' } }); | |
| const argsNode = call.field('arguments'); |
Comment on lines
+19
to
+55
|
|
||
| /** | ||
| * Check if a text expression is already a safe type that doesn't need String() wrapping. | ||
| * Safe types: string literals, template literals, Buffer/TypedArray expressions, | ||
| * already-wrapped String() or .toString() calls. | ||
| */ | ||
| function isSafeType(text: string): boolean { | ||
| const trimmed = text.trim(); | ||
|
|
||
| // String literals and template literals (', ", `) | ||
| if (/^['"`]/.test(trimmed)) return true; | ||
|
|
||
| // Already has .toString() | ||
| if (trimmed.endsWith('.toString()')) return true; | ||
|
|
||
| // Already wrapped in String() — exact match to avoid false positives like Stringify() | ||
| if (/^String\(/.test(trimmed) && trimmed.endsWith(')')) return true; | ||
|
|
||
| // Buffer.from(), Buffer.alloc(), etc. | ||
| if (/^Buffer\.\w+\(/.test(trimmed)) return true; | ||
|
|
||
| // new Uint8Array, new Int8Array, etc. | ||
| if ( | ||
| /^new\s+(Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|DataView)\b/.test( | ||
| trimmed, | ||
| ) | ||
| ) | ||
| return true; | ||
|
|
||
| // Numeric literal (integers and floats) | ||
| if (/^\d+(\.\d+)?$/.test(trimmed)) return true; | ||
|
|
||
| // null or undefined | ||
| if (trimmed === 'null' || trimmed === 'undefined') return true; | ||
|
|
||
| return false; | ||
| } |
Member
There was a problem hiding this comment.
Suggested change
| /** | |
| * Check if a text expression is already a safe type that doesn't need String() wrapping. | |
| * Safe types: string literals, template literals, Buffer/TypedArray expressions, | |
| * already-wrapped String() or .toString() calls. | |
| */ | |
| function isSafeType(text: string): boolean { | |
| const trimmed = text.trim(); | |
| // String literals and template literals (', ", `) | |
| if (/^['"`]/.test(trimmed)) return true; | |
| // Already has .toString() | |
| if (trimmed.endsWith('.toString()')) return true; | |
| // Already wrapped in String() — exact match to avoid false positives like Stringify() | |
| if (/^String\(/.test(trimmed) && trimmed.endsWith(')')) return true; | |
| // Buffer.from(), Buffer.alloc(), etc. | |
| if (/^Buffer\.\w+\(/.test(trimmed)) return true; | |
| // new Uint8Array, new Int8Array, etc. | |
| if ( | |
| /^new\s+(Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|DataView)\b/.test( | |
| trimmed, | |
| ) | |
| ) | |
| return true; | |
| // Numeric literal (integers and floats) | |
| if (/^\d+(\.\d+)?$/.test(trimmed)) return true; | |
| // null or undefined | |
| if (trimmed === 'null' || trimmed === 'undefined') return true; | |
| return false; | |
| } | |
| const TypedArrayRegex = `^(Uint8Array|Int8Array|Uint16Array|Int16Array|Uint32Array|Int32Array|Float32Array|Float64Array|DataView)$`; | |
| /** | |
| * Check if a text expression is already a safe type that doesn't need String() wrapping. | |
| * Safe types: string literals, template literals, Buffer/TypedArray expressions, | |
| * already-wrapped String() or .toString() calls. | |
| */ | |
| function isSafeType(node: SgNode<Js, Kinds<Js>>): boolean { | |
| const safe = node.find({ | |
| constraints: { | |
| METHOD: { | |
| regex: TypedArrayRegex, | |
| }, | |
| }, | |
| rule: { | |
| any: [ | |
| { kind: 'number' }, | |
| { kind: 'string' }, | |
| { kind: 'string_fragment' }, | |
| { pattern: '$ANY.toString()' }, | |
| { pattern: '$METHOD' }, | |
| { pattern: 'Buffer.from($ANY)' }, | |
| { pattern: 'String($ANY)' }, | |
| { pattern: 'null' }, | |
| { pattern: 'undefined' }, | |
| ], | |
| }, | |
| }); | |
| return Boolean(safe); | |
| } | |
This fixes the TS2304 error where Edit was used but not imported. Co-Authored-By: 員工克勞德 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds a codemod for DEP0162 — implicit coercion of objects in
fs.write()andfs.writeFileSync().String()forfs.writeFile(),fs.writeFileSync(),fs.appendFile(),fs.appendFileSync(),fs.write(), and theirfs/promisesvariantsBuffer.from(),new Uint8Array(),.toString(),String()wrappersrequire('fs')and ESMimport { writeFile } from 'node:fs/promises'Test plan
fs.write()andfs.writeFileSync()coercion to string #412npx codemod jssg testCloses #412
🤖 Generated with Claude Code