Repository navigation
feat: add no-useless-echo rule #8
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
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,53 @@ | ||
| # no-useless-echo | ||
|
|
||
| Disallow useless `echo` inside command substitutions. | ||
|
|
||
| ## Background | ||
|
|
||
| A command substitution that only runs `echo`, such as `$(echo "$name")`, produces the same text that was passed to `echo`. It starts an extra process (or at least an extra step) and makes the code harder to read. Using the value directly is simpler. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule warns about a command substitution, written as `$(...)` or with backticks, whose entire body is a single `echo` command without redirects. | ||
|
|
||
| The rule doesn't inspect `echo`'s options, so `$(echo -n "$value")` is also reported. | ||
|
|
||
| Substitutions that do more than echo, such as pipelines (`$(echo "$name" | tr a-z A-Z)`) or multiple commands, are not reported. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```bash | ||
| # eslint bash/no-useless-echo: "error" | ||
|
|
||
| name=$(echo "$first") | ||
|
|
||
| ls $(echo /tmp) | ||
|
|
||
| files=`echo "$dir"` | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```bash | ||
| # eslint bash/no-useless-echo: "error" | ||
|
|
||
| name="$first" | ||
|
|
||
| upper=$(echo "$name" | tr a-z A-Z) | ||
|
|
||
| both=$(echo hello; echo world) | ||
|
|
||
| echo "hello" | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has no options. | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| `$(echo ...)` is occasionally used on purpose for side effects of `echo`, such as collapsing whitespace in an unquoted value or interpreting escape sequences with `echo -e`. If you rely on those behaviors, use a disable comment for those lines or disable this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| - [SC2116](https://www.shellcheck.net/wiki/SC2116) |
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,50 @@ | ||
| /** | ||
| * @fileoverview Tests for the no-useless-echo rule. | ||
| */ | ||
|
|
||
| import { RuleTester } from "eslint"; | ||
| import { BashLanguage } from "../languages/bash-language.js"; | ||
| import rule from "./no-useless-echo.js"; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { | ||
| bash: { | ||
| meta: { namespace: "shell" }, | ||
| languages: { bash: new BashLanguage() }, | ||
| }, | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any -- plugin shape is validated by ESLint at runtime. | ||
| } as any, | ||
| language: "bash/bash", | ||
| }); | ||
|
|
||
| ruleTester.run("no-useless-echo", rule as never, { | ||
| valid: [ | ||
| "echo hello", | ||
| "x=$(pwd)", | ||
| "x=$(echo foo | tr a-z A-Z)", | ||
| "x=$(echo foo; echo bar)", | ||
| "x=$(echo foo > file)", | ||
| "x=$(! echo foo)", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "x=$(echo foo)", | ||
| errors: [ | ||
| { | ||
| messageId: "uselessEcho", | ||
| line: 1, | ||
| column: 3, | ||
| endColumn: 14, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'cmd "$(echo "$var")"', | ||
| errors: [{ messageId: "uselessEcho" }], | ||
| }, | ||
| { | ||
| code: "x=`echo foo`", | ||
| errors: [{ messageId: "uselessEcho" }], | ||
| }, | ||
| ], | ||
| }); | ||
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,48 @@ | ||
| /** | ||
| * @fileoverview Rule to disallow `$(echo ...)` command substitutions. | ||
| * Mirrors ShellCheck SC2116. | ||
| */ | ||
|
|
||
| import { isCommandNamed } from "./utils.js"; | ||
| import type { BashRuleDefinition } from "../types.js"; | ||
|
|
||
| const rule: BashRuleDefinition<{ MessageIds: "uselessEcho" }> = { | ||
| meta: { | ||
| type: "suggestion", | ||
| languages: ["shell/bash"], | ||
| docs: { | ||
| description: "Disallow useless `echo` inside command substitutions", | ||
| recommended: true, | ||
| dialects: ["Bash", "POSIX sh", "mksh"], | ||
| url: "https://github.com/eslint/bash/blob/main/docs/rules/no-useless-echo.md", | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| uselessEcho: | ||
| "Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'. (ShellCheck SC2116)", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| return { | ||
| CommandSubstitution(node) { | ||
| const first = node.body[0]; | ||
|
|
||
| if ( | ||
| node.body.length === 1 && | ||
| first !== undefined && | ||
| isCommandNamed(first, "echo") && | ||
| !first.negated && | ||
| first.redirects.length === 0 | ||
| ) { | ||
| context.report({ | ||
| node, | ||
| messageId: "uselessEcho", | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| 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.