`fs.write()` and `fs.writeFileSync()` coercion to string
Los mantenedores suelen responder en 2 días
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 48/100
- Tipo de issue
- Nueva funcionalidad
- Claridad
- Bastante claro
- Estado de actividad
- Tranquilo
- Stack tecnológico
- javascript, node.js, typescript
- Área
- tooling
Línea de trabajo
El payload no nombra archivos del repositorio, pruebas ni un punto de entrada de codemod; empieza por localizar el framework de migración existente y su cobertura de las APIs de fs de Node.js. Usa los métodos de fs enumerados, el ejemplo node:fs/promises y los casos válidos de Buffer y Uint8Array para definir la cobertura; después, verifica los cambios en la coerción de objetos sin alterar los tipos válidos.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description
Since implicit coercion of objects in fs.write() and fs.writeFileSync() is deprecated (DEP0162) and has reached End-of-Life status in Node.js v19.0.0, we should provide a codemod to replace them.
- The codemod should convert objects with
toString()method to primitive strings before passing to fs write functions. - The codemod should handle
fs.write(),fs.writeFile(),fs.appendFile(),fs.writeFileSync(), andfs.appendFileSync(). - The codemod should detect objects being passed as the data parameter.
- The codemod should add explicit
.toString()calls orString()conversion.
Additional Information
Note that in Node.js v19.0.0, implicit coercion of objects with own toString property passed as the second parameter (data) in fs write operations now throws an error. This was deprecated because the implicit conversion could lead to unexpected behavior and made the code less explicit.
Instead of relying on implicit coercion, developers should explicitly convert objects to strings using .toString(), String(), or template literals.
Examples
Example 1: Object in fs.writeFileSync()
Before:
const fs = require("node:fs");
const data = { toString: () => "file content" };
fs.writeFileSync("file.txt", data);
After:
const fs = require("node:fs");
const data = { toString: () => "file content" };
fs.writeFileSync("file.txt", data.toString());
Example 2: Object in fs.writeFile()
Before:
const fs = require("node:fs");
const obj = { toString: () => "content" };
fs.writeFile("file.txt", obj, (err) => {
if (err) throw err;
});
After:
const fs = require("node:fs");
const obj = { toString: () => "content" };
fs.writeFile("file.txt", obj.toString(), (err) => {
if (err) throw err;
});
Example 3: Object in fs.appendFileSync()
Before:
const fs = require("node:fs");
const data = { toString: () => "appended data" };
fs.appendFileSync("file.txt", data);
After:
const fs = require("node:fs");
const data = { toString: () => "appended data" };
fs.appendFileSync("file.txt", data.toString());
Example 4: Object in fs.appendFile()
Before:
const fs = require("node:fs");
const content = { toString: () => "more content" };
fs.appendFile("file.txt", content, (err) => {
if (err) throw err;
});
After:
const fs = require("node:fs");
const content = { toString: () => "more content" };
fs.appendFile("file.txt", content.toString(), (err) => {
if (err) throw err;
});
Example 5: Object in fs.write()
Before:
const fs = require("node:fs");
const fd = fs.openSync("file.txt", "w");
const data = { toString: () => "buffer content" };
fs.write(fd, data, (err) => {
if (err) throw err;
fs.closeSync(fd);
});
After:
const fs = require("node:fs");
const fd = fs.openSync("file.txt", "w");
const data = { toString: () => "buffer content" };
fs.write(fd, data.toString(), (err) => {
if (err) throw err;
fs.closeSync(fd);
});
Example 6: Using String() conversion
Before:
const fs = require("node:fs");
const myObj = {
value: 42,
toString() {
return `Value: ${this.value}`;
}
};
fs.writeFileSync("output.txt", myObj);
After:
const fs = require("node:fs");
const myObj = {
value: 42,
toString() {
return `Value: ${this.value}`;
}
};
fs.writeFileSync("output.txt", String(myObj));
Example 7: ESM with promises API
Before:
import { writeFile } from "node:fs/promises";
const data = { toString: () => "async content" };
await writeFile("file.txt", data);
After:
import { writeFile } from "node:fs/promises";
const data = { toString: () => "async content" };
await writeFile("file.txt", data.toString());
Example 8: Valid types (no change needed)
Before:
const fs = require("node:fs");
// These are all valid and don't need changes
fs.writeFileSync("file1.txt", "string");
fs.writeFileSync("file2.txt", Buffer.from("buffer"));
fs.writeFileSync("file3.txt", new Uint8Array([1, 2, 3]));
After:
const fs = require("node:fs");
// These are all valid and don't need changes
fs.writeFileSync("file1.txt", "string");
fs.writeFileSync("file2.txt", Buffer.from("buffer"));
fs.writeFileSync("file3.txt", new Uint8Array([1, 2, 3]));
Refs
- Lenguaje dominante
- TypeScript
- Estrellas
- 85
- Forks
- 58
- Merge medio
- 1 d 21 h
- PR fusionados (30 d)
- 8
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de nodejs/userland-migrations
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
nodejs/userland-migrations#562 · 4 reacciones ·
Los mantenedores suelen responder en 2 días
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
nodejs/userland-migrations#561 · 1 comentario · 1 reacción ·
Los mantenedores suelen responder en 2 días
-
good first issue human help wanted
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
nodejs/userland-migrations#560 · 1 reacción ·
Los mantenedores suelen responder en 2 días
-
good first issue human help wanted
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
nodejs/userland-migrations#559 ·
Los mantenedores suelen responder en 2 días
-
good first issue human help wanted
Dificultad 4/5 3-5 días Aptitud para principiantes 48/100
nodejs/userland-migrations#558 ·
Los mantenedores suelen responder en 2 días
Todos los issues de nodejs/userland-migrations
Issues similares
-
Cleanup SetupPosiblemente ocupada @AlexanderKaran la tomó hoy. Abiertodocumentation enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
e18e/framework-tracker#769 ·
Los mantenedores suelen responder en 1 día
-
fix(docker): correct stale --db-path comments in fetch/git Dockerfiles and clarify git READMEAbiertobug v2
Dificultad 1/5 Menos de una hora Aptitud para principiantes 88/100
modelcontextprotocol/servers#5483 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 Menos de una hora Aptitud para principiantes 84/100
XiaomiMiMo/MiMo-Code#2783 ·
Los mantenedores suelen responder en 1 día
-
area: web bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
kinqsradio/orbyn-engineering#227 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
joeldesante/TurfBuilder#258 ·
Los mantenedores suelen responder en 1 día