Repository navigation
feat: add no-useless-cat rule #10
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
Open
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c968d1b
feat: add no-useless-cat rule
nzakas 3d40043
fix: avoid no-useless-cat expansion false positives
Copilot 36fbf48
fix: add languages and dialects to no-useless-cat meta
nzakas 235b2f2
fix: use the shell plugin namespace and type names in no-useless-cat
nzakas f62c9b8
fix: allow no-useless-cat with every shell language
nzakas 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| # no-useless-cat | ||
|
|
||
| Disallow useless `cat` at the start of a pipeline. | ||
|
|
||
| ## Background | ||
|
|
||
| `cat file | cmd` starts an extra process only to copy a file into a pipe. Most commands accept a filename argument (`cmd file`), and every command can read from a redirect (`cmd < file`). Either form is simpler and avoids the extra process. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule warns when the first command of a pipeline is `cat` with exactly one filename argument and no redirects. | ||
|
|
||
| The rule doesn't warn when `cat` actually does something, such as concatenating several files or using options like `-n`. It also skips arguments that aren't static text, such as `cat "$file"`. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```bash | ||
| # eslint shell/no-useless-cat: "error" | ||
|
|
||
| cat file.txt | grep error | ||
|
|
||
| cat /var/log/syslog | tail -n 20 | grep kernel | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```bash | ||
| # eslint shell/no-useless-cat: "error" | ||
|
|
||
| grep error file.txt | ||
|
|
||
| grep error < file.txt | ||
|
|
||
| cat header.txt body.txt | gzip > out.gz | ||
|
|
||
| cat -n file.txt | head | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has no options. | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| Some people prefer `cat file | cmd` because the data reads left to right. If you prefer that style, you can safely disable this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| - [SC2002](https://www.shellcheck.net/wiki/SC2002) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /** | ||
| * @fileoverview Tests for the no-useless-cat rule. | ||
| */ | ||
|
|
||
| import { RuleTester } from "eslint"; | ||
| import { ShellLanguage } from "../languages/shell-language.js"; | ||
| import rule from "./no-useless-cat.js"; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { | ||
| shell: { | ||
| meta: { namespace: "shell" }, | ||
| languages: { bash: new ShellLanguage() }, | ||
| }, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. | ||
| } as any, | ||
| language: "shell/bash", | ||
| }); | ||
|
|
||
| ruleTester.run("no-useless-cat", rule as never, { | ||
| valid: [ | ||
| // No pipeline at all | ||
| "cat file.txt", | ||
| // cat merging multiple files is legitimate | ||
| "cat a.txt b.txt | sort", | ||
| // cat with flags changes output | ||
| "cat -n file.txt | head", | ||
| // Unquoted glob and brace patterns can expand to multiple files. | ||
| "cat *.txt | sort", | ||
| "cat {a,b} | sort", | ||
| "cat {1..3} | sort", | ||
| "cat {a,{b}} | sort", | ||
| // ANSI-C quoting can transform source text into an option. | ||
| "cat $'\\x2dn' | head", | ||
| // pipeline not starting with cat | ||
| "sort file.txt | uniq | cat", | ||
| // cat with redirect input | ||
| "cat < file.txt | sort", | ||
| // dynamic file name still counts as one file, but command may | ||
| // legitimately be built dynamically; still reported? No: only | ||
| // static single-file cats are reported. | ||
| 'cat "$f" | sort', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "cat file.txt | grep foo", | ||
| errors: [ | ||
| { | ||
| messageId: "uselessCat", | ||
| line: 1, | ||
| column: 1, | ||
| endColumn: 13, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "cat /var/log/syslog | tail -n 5 | grep error", | ||
| errors: [{ messageId: "uselessCat" }], | ||
| }, | ||
| { | ||
| code: 'cat "*.txt" | sort', | ||
| errors: [{ messageId: "uselessCat" }], | ||
| }, | ||
| { | ||
| code: "cat '{a,b}' | sort", | ||
| errors: [{ messageId: "uselessCat" }], | ||
| }, | ||
| ], | ||
| }); |
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,89 @@ | ||
| /** | ||
| * @fileoverview Rule to disallow piping from a single-file `cat`. | ||
| * Mirrors ShellCheck SC2002. | ||
| */ | ||
|
|
||
| import { getCommandName, getStaticText } from "./utils.js"; | ||
| import type { ShellRuleDefinition, WordNode, WordPartNode } from "../types.js"; | ||
|
|
||
| function hasPotentialExpansion(word: WordNode): boolean { | ||
| const checkParts = (parts: WordPartNode[], quoted = false): boolean => | ||
| parts.some(part => { | ||
| switch (part.type) { | ||
| case "Literal": | ||
| return ( | ||
| !quoted && | ||
| (part.value.includes("*") || | ||
| part.value.includes("?") || | ||
| part.value.includes("[") || | ||
| part.value.includes("{") || | ||
| part.value.includes("}")) | ||
| ); | ||
| case "SingleQuotedString": | ||
| return part.dollar; | ||
| case "DoubleQuotedString": | ||
| return checkParts(part.parts, true); | ||
| default: | ||
| return false; | ||
| } | ||
| }); | ||
|
|
||
| return checkParts(word.parts); | ||
| } | ||
|
|
||
| const rule: ShellRuleDefinition<{ MessageIds: "uselessCat" }> = { | ||
| meta: { | ||
| type: "suggestion", | ||
| languages: ["shell/bash", "shell/posix", "shell/mksh"], | ||
| docs: { | ||
| description: "Disallow useless `cat` at the start of a pipeline", | ||
| recommended: true, | ||
| dialects: ["Bash", "POSIX sh", "mksh"], | ||
| url: "https://github.com/eslint/shell/blob/main/docs/rules/no-useless-cat.md", | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| uselessCat: | ||
| "Useless cat. Consider 'cmd < file' or 'cmd file' instead. (ShellCheck SC2002)", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| Pipeline(node) { | ||
| const first = node.commands[0]; | ||
|
|
||
| if ( | ||
| !first || | ||
| first.type !== "Command" || | ||
| getCommandName(first) !== "cat" || | ||
| first.redirects.length > 0 || | ||
| first.arguments.length !== 1 | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const argument = first.arguments[0]; | ||
| const text = argument ? getStaticText(argument) : null; | ||
|
|
||
| // Skip flags, stdin markers, and arguments that can expand to | ||
| // multiple words. | ||
| if ( | ||
| text === null || | ||
| text.startsWith("-") || | ||
| !argument || | ||
| hasPotentialExpansion(argument) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| context.report({ | ||
| node: first, | ||
| messageId: "uselessCat", | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default rule; | ||
Oops, something went wrong.
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.