Skip to content

Commit 7070a45

Browse files
authored
nekojanai:chore(file-access-mode-constants-follow-up): DRY up code (#205)
1 parent a8f8108 commit 7070a45

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

recipes/fs-access-mode-constants/src/workflow.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getNodeImportStatements } from '@nodejs/codemod-utils/ast-grep/import-s
66
export default function tranform(root: SgRoot<Js>): string | null {
77
const rootNode = root.root();
88
const edits: Edit[] = [];
9+
const patterns = ['F_OK', 'R_OK', 'W_OK', 'X_OK'];
910

1011
// @ts-expect-error - ast-grep types are not fully compatible with JSSG types
1112
const requireStatements = getNodeRequireCalls(root, 'fs');
@@ -22,7 +23,7 @@ export default function tranform(root: SgRoot<Js>): string | null {
2223
})
2324
.map((v) => v.text());
2425
objPatArr = objPatArr.filter(
25-
(v) => !['F_OK', 'R_OK', 'W_OK', 'X_OK'].includes(v),
26+
(v) => !patterns.includes(v),
2627
);
2728
objPatArr.push('constants');
2829
edits.push(objectPattern.replace(`{ ${objPatArr.join(', ')} }`));
@@ -45,7 +46,7 @@ export default function tranform(root: SgRoot<Js>): string | null {
4546
})
4647
.map((v) => v.text());
4748
objPatArr = objPatArr.filter(
48-
(v) => !['F_OK', 'R_OK', 'W_OK', 'X_OK'].includes(v),
49+
(v) => !patterns.includes(v),
4950
);
5051
const promisesImport = objPatArr.find((v) => v.startsWith('promises'));
5152
if (promisesImport) {
@@ -65,15 +66,13 @@ export default function tranform(root: SgRoot<Js>): string | null {
6566
}
6667
}
6768

68-
for (const _OK of ['F_OK', 'R_OK', 'W_OK', 'X_OK']) {
69+
for (const _OK of patterns) {
6970
for (const [prefix, replacement] of [
7071
['fs.', 'fs.constants.'],
7172
['', `${promisesImportName ? promisesImportName : ''}constants.`],
7273
]) {
7374
const patterns = rootNode.findAll({
74-
rule: {
75-
pattern: `${prefix}${_OK}`,
76-
},
75+
rule: { pattern: `${prefix}${_OK}` },
7776
});
7877
for (const pattern of patterns) {
7978
edits.push(pattern.replace(`${replacement}${_OK}`));

0 commit comments

Comments
 (0)