Skip to content

Commit aa4a23d

Browse files
WARNING: edge-case
1 parent a4d4938 commit aa4a23d

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

recipes/fs-truncate-fd-deprecation/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ open('file.txt', 'w', (err, fd) => {
3131
});
3232
});
3333
```
34+
35+
## Caveats
36+
37+
When using `const fd = fs.openSync('file.txt', 'w');`, the codemod don't care about scope and will always transform `fs.truncate(fd, 10)` to `fs.ftruncateSync(fd, 10)`. So if you have mutiple variables with the same name, you should be careful with the transformation.
38+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fs from 'node:fs';
2+
3+
export default function truncateFile() {
4+
fs.open('file.txt', 'w', (err, strangeName) => {
5+
if (err) throw err;
6+
fs.ftruncate(strangeName, 10, (err) => {
7+
if (err) throw err;
8+
fs.close(strangeName, () => { });
9+
});
10+
});
11+
}
12+
13+
const accesible = fs.openSync('file.txt', 'w');
14+
15+
fs.ftruncateSync(accesible, 10);
16+
17+
fs.closeSync(accesible);
18+
19+
function foo() {
20+
truncateFile(unaccessible, 10);
21+
}
22+
23+
function bar() {
24+
const unaccessible = fs.openSync('file.txt', 'w');
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fs from 'node:fs';
2+
3+
export default function truncateFile() {
4+
fs.open('file.txt', 'w', (err, strangeName) => {
5+
if (err) throw err;
6+
fs.truncate(strangeName, 10, (err) => {
7+
if (err) throw err;
8+
fs.close(strangeName, () => { });
9+
});
10+
});
11+
}
12+
13+
const accesible = fs.openSync('file.txt', 'w');
14+
15+
fs.truncateSync(accesible, 10);
16+
17+
fs.closeSync(accesible);
18+
19+
function foo() {
20+
truncateFile(unaccessible, 10);
21+
}
22+
23+
function bar() {
24+
const unaccessible = fs.openSync('file.txt', 'w');
25+
}

0 commit comments

Comments
 (0)