`fs.write()` and `fs.writeFileSync()` coercion to string
Maintainer thường phản hồi trong vòng 2 ngày
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- javascript, node.js, typescript
- Lĩnh vực
- tooling
Hướng nghiên cứu
Payload không nêu tên tệp nào trong repository, test hay điểm vào của codemod; hãy bắt đầu bằng cách xác định framework migration hiện có và phạm vi bao phủ của nó đối với các API fs của Node.js. Sử dụng các phương thức fs được liệt kê, ví dụ node:fs/promises và các trường hợp Buffer và Uint8Array hợp lệ để xác định phạm vi bao phủ, sau đó xác minh các thay đổi trong việc cưỡng chế kiểu đối tượng mà không làm thay đổi các kiểu hợp lệ.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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
- Ngôn ngữ chính
- TypeScript
- Star
- 85
- Fork
- 58
- Merge trung bình
- 2 ngày 4 giờ
- Pull request đã merge (30 ngày)
- 6
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nodejs/userland-migrations
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
nodejs/userland-migrations#562 · 4 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
nodejs/userland-migrations#561 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
good first issue human help wanted
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
nodejs/userland-migrations#560 · 1 reaction ·
Maintainer thường phản hồi trong vòng 2 ngày
-
good first issue human help wanted
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
nodejs/userland-migrations#559 ·
Maintainer thường phản hồi trong vòng 2 ngày
-
good first issue human help wanted
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
nodejs/userland-migrations#558 ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của nodejs/userland-migrations
Issue tương tự
-
Recording drops clicks on links, filters and checkboxes that share a form with a search fieldCó thể đã có người làm @columbia11 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Tencent/BrowserSkill#657 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Choosing a folder while continuing an Antigravity session submits a history fork the runtime rejectsĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 87/100
-
ai-handle
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
nextlevelbuilder/dewee-web-v2#138 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
NEARBuilders/citynode.app#512 ·
Maintainer thường phản hồi trong vòng 1 ngày