-
Notifications
You must be signed in to change notification settings - Fork 535
[eslint-miner] ESLint Miner: add require-fs-chmod-try-catch rule #59408
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pelikhan
merged 6 commits into
main
from
eslint-miner-fs-chmod-try-catch-af2bcd2c3b9ee259
Sep 9, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6b79e39
Add require-fs-chmod-try-catch ESLint rule for actions/setup/js
github-actions[bot] 6c07731
Merge branch 'main' into eslint-miner-fs-chmod-try-catch-af2bcd2c3b9e…
github-actions[bot] dd5ca6b
Document and simplify chmod ESLint rule
Copilot 99571cb
Merge remote-tracking branch 'origin/main' into eslint-miner-fs-chmod…
Copilot 7543bec
Merge branch 'main' into eslint-miner-fs-chmod-try-catch-af2bcd2c3b9e…
github-actions[bot] c5674a7
Merge branch 'main' into eslint-miner-fs-chmod-try-catch-af2bcd2c3b9e…
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
75 changes: 75 additions & 0 deletions
75
eslint-factory/src/rules/require-fs-chmod-try-catch.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { RuleTester } from "eslint"; | ||
| import { describe, it } from "vitest"; | ||
| import { requireFsChmodTryCatchRule } from "./require-fs-chmod-try-catch"; | ||
|
|
||
| const cjsRuleTester = new RuleTester({ | ||
| languageOptions: { | ||
| ecmaVersion: 2022, | ||
| sourceType: "commonjs", | ||
| }, | ||
| }); | ||
|
|
||
| function expectedWrapInTryCatchSuggestion(method: string, statement: string, prefix = "") { | ||
| return { | ||
| messageId: "wrapInTryCatch", | ||
| output: `${prefix}try {\n` + ` ${statement}\n` + `} catch (err) {\n` + ` throw new Error(\n` + ` "fs.${method} failed: " + (err instanceof Error ? err.message : String(err)),\n` + ` { cause: err },\n` + ` );\n` + `}`, | ||
| }; | ||
| } | ||
|
|
||
| describe("require-fs-chmod-try-catch", () => { | ||
| it("valid: fs.chmodSync and fs.fchmodSync inside try block pass", () => { | ||
| cjsRuleTester.run("require-fs-chmod-try-catch", requireFsChmodTryCatchRule, { | ||
| valid: [`try { fs.chmodSync(path, 0o600); } catch (e) {}`, `try { fs.fchmodSync(fd, 0o600); } catch (e) {}`], | ||
| invalid: [], | ||
| }); | ||
| }); | ||
|
|
||
| it("valid: other fs methods and non-fs identifiers are ignored", () => { | ||
| cjsRuleTester.run("require-fs-chmod-try-catch", requireFsChmodTryCatchRule, { | ||
| valid: [`fs.existsSync(path);`, `fs.readFileSync(path, "utf8");`, `fs.statSync(path);`, `mockFs.chmodSync(path, 0o600);`, `storage.fchmodSync(fd, 0o600);`], | ||
| invalid: [], | ||
| }); | ||
| }); | ||
|
|
||
| it("invalid: fs.chmodSync outside try/catch is flagged", () => { | ||
| cjsRuleTester.run("require-fs-chmod-try-catch", requireFsChmodTryCatchRule, { | ||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `fs.chmodSync(path, 0o600);`, | ||
| errors: [{ messageId: "requireTryCatch", data: { method: "chmodSync", arg: "path" }, suggestions: [expectedWrapInTryCatchSuggestion("chmodSync", "fs.chmodSync(path, 0o600);")] }], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("invalid: fs.fchmodSync outside try/catch is flagged", () => { | ||
| cjsRuleTester.run("require-fs-chmod-try-catch", requireFsChmodTryCatchRule, { | ||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `fs.fchmodSync(fd, 0o600);`, | ||
| errors: [{ messageId: "requireTryCatch", data: { method: "fchmodSync", arg: "fd" }, suggestions: [expectedWrapInTryCatchSuggestion("fchmodSync", "fs.fchmodSync(fd, 0o600);")] }], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("invalid: fs.chmodSync after an unrelated try block is still flagged", () => { | ||
| cjsRuleTester.run("require-fs-chmod-try-catch", requireFsChmodTryCatchRule, { | ||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `try { doSomethingElse(); } catch (e) {}\nfs.chmodSync(dir, 0o700);`, | ||
| errors: [ | ||
| { | ||
| messageId: "requireTryCatch", | ||
| data: { method: "chmodSync", arg: "dir" }, | ||
| suggestions: [expectedWrapInTryCatchSuggestion("chmodSync", "fs.chmodSync(dir, 0o700);", "try { doSomethingElse(); } catch (e) {}\n")], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { ESLintUtils } from "@typescript-eslint/utils"; | ||
| import { buildTryCatchSuggestion, createFsSyncMethodResolver, findEnclosingStatement, isInsideTryBlock } from "./try-catch-rule-utils"; | ||
|
|
||
| const createRule = ESLintUtils.RuleCreator(name => `https://github.com/github/gh-aw/tree/main/eslint-factory#${name}`); | ||
|
|
||
|
github-actions[bot] marked this conversation as resolved.
|
||
| const FS_CHMOD_METHODS = new Set(["chmodSync", "fchmodSync"]); | ||
|
|
||
| export const requireFsChmodTryCatchRule = createRule({ | ||
| name: "require-fs-chmod-try-catch", | ||
| meta: { | ||
| type: "problem", | ||
| hasSuggestions: true, | ||
| docs: { | ||
| description: "Require fs.chmodSync and fs.fchmodSync calls in actions/setup/js scripts to be wrapped in try/catch.", | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| requireTryCatch: "Wrap fs.{{method}}({{arg}}) in try/catch.", | ||
| wrapInTryCatch: "Wrap in try { ... } catch { ... } and re-throw with { cause: err } to preserve context.", | ||
|
github-actions[bot] marked this conversation as resolved.
|
||
| }, | ||
| }, | ||
| defaultOptions: [], | ||
| create(context) { | ||
| const sourceCode = context.sourceCode; | ||
| const resolveFsChmodMethod = createFsSyncMethodResolver(sourceCode, FS_CHMOD_METHODS, { allowUnboundFsIdentifier: true }); | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| const methodName = resolveFsChmodMethod(node); | ||
|
|
||
| if (!methodName) return; | ||
|
|
||
| if (isInsideTryBlock(sourceCode, node)) return; | ||
|
|
||
| const argText = node.arguments.length > 0 ? sourceCode.getText(node.arguments[0]) : ""; | ||
| const method = methodName; | ||
| const stmt = findEnclosingStatement(sourceCode, node); | ||
|
|
||
| context.report({ | ||
| node, | ||
| messageId: "requireTryCatch", | ||
| data: { method, arg: argText }, | ||
| suggest: stmt | ||
| ? [ | ||
| { | ||
| messageId: "wrapInTryCatch", | ||
| fix(fixer) { | ||
| const stmtText = sourceCode.getText(stmt); | ||
| const startLine = stmt.loc?.start.line; | ||
| const stmtLine = startLine !== undefined ? (sourceCode.lines[startLine - 1] ?? "") : ""; | ||
| const indent = stmtLine.match(/^(\s*)/)?.[1] ?? ""; | ||
| return fixer.replaceText( | ||
| stmt, | ||
| buildTryCatchSuggestion(stmtText, { | ||
| indent, | ||
| errorPrefix: `fs.${method} failed: `, | ||
| }) | ||
| ); | ||
| }, | ||
|
github-actions[bot] marked this conversation as resolved.
|
||
| }, | ||
| ] | ||
| : [], | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.