-
-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add no-unquoted-expansions rule #7
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
nzakas
wants to merge
2
commits into
rule/no-expansions-in-single-quotes
from
rule/no-unquoted-expansions
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,78 @@ | ||
| # no-unquoted-expansions | ||
|
|
||
| Require quoting parameter expansions and command substitutions that are subject to word splitting. | ||
|
|
||
| ## Background | ||
|
|
||
| When an expansion such as `$file` or `$(cmd)` is not quoted, the shell splits its value on whitespace and then expands any glob characters in the pieces. A file named `my report.txt` becomes two arguments, and a value containing `*` can turn into a list of unrelated files. Wrapping the expansion in double quotes (`"$file"`) passes the value through as a single argument. | ||
|
|
||
| ## Rule Details | ||
|
|
||
| This rule warns about unquoted parameter expansions (`$var`, `${var}`, `$@`, `$1`, and so on) and command substitutions (`$(...)` and backticks) in positions where word splitting happens: | ||
|
|
||
| - a command's name and arguments, including arguments to `[ ... ]`; | ||
| - the word list of a `for ... in` loop; | ||
| - redirection targets, such as `> $file`. | ||
|
|
||
| The rule does not warn about: | ||
|
|
||
| - expansions inside double quotes; | ||
| - parameters that can't contain whitespace: `$?`, `$$`, `$!`, `$#`, `$-`, and length expansions such as `${#array[@]}`; | ||
| - contexts where the shell doesn't split words: variable assignments (`x=$y`), `[[ ... ]]`, `case` subjects, arithmetic such as `$((...))`, and heredoc or herestring redirects (`<<`, `<<-`, `<<<`). | ||
|
|
||
| This rule is autofixable when the expansion is the entire word: `$var` becomes `"$var"`. Words that mix an expansion with other text, such as `prefix$var`, are reported but not fixed. | ||
|
|
||
| Examples of **incorrect** code for this rule: | ||
|
|
||
| ```bash | ||
| # eslint shell/no-unquoted-expansions: "error" | ||
|
|
||
| rm $file | ||
|
|
||
| cp $source $destination | ||
|
|
||
| for f in $files; do echo "$f"; done | ||
|
|
||
| echo $(ls) | ||
|
|
||
| sort data.txt > $output | ||
|
|
||
| $command --verbose | ||
| ``` | ||
|
|
||
| Examples of **correct** code for this rule: | ||
|
|
||
| ```bash | ||
| # eslint shell/no-unquoted-expansions: "error" | ||
|
|
||
| rm "$file" | ||
|
|
||
| cp "$source" "$destination" | ||
|
|
||
| for f in "$@"; do echo "$f"; done | ||
|
|
||
| echo "$(ls)" | ||
|
|
||
| name=$other | ||
|
|
||
| [[ $a == "$b" ]] | ||
|
|
||
| echo $? $# ${#items[@]} | ||
|
|
||
| echo $((count + 1)) | ||
|
|
||
| cat <<< $input | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| This rule has no options. | ||
|
|
||
| ## When Not to Use It | ||
|
|
||
| Occasionally word splitting is intended, such as passing a space-separated list of flags stored in a variable. Prefer an array (`"${flags[@]}"`) in that case; otherwise, use a disable comment for the specific line rather than disabling this rule. | ||
|
|
||
| ## Prior Art | ||
|
|
||
| - [SC2086](https://www.shellcheck.net/wiki/SC2086) | ||
| - [SC2046](https://www.shellcheck.net/wiki/SC2046) |
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,132 @@ | ||
| /** | ||
| * @fileoverview Tests for the no-unquoted-expansions rule. | ||
| */ | ||
|
|
||
| import { RuleTester } from "eslint"; | ||
| import { ShellLanguage } from "../languages/shell-language.js"; | ||
| import rule from "./no-unquoted-expansions.js"; | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| plugins: { | ||
| 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-unquoted-expansions", rule as never, { | ||
| valid: [ | ||
| // Quoted expansions | ||
| 'echo "$var"', | ||
| 'echo "${var}"', | ||
| 'echo "$(pwd)"', | ||
| 'cp "$src" "$dest"', | ||
| 'for f in "$@"; do echo "$f"; done', | ||
| // Safe special parameters | ||
| "echo $?", | ||
| "echo $$", | ||
| "echo $#", | ||
| "echo $!", | ||
| "echo $-", | ||
| "echo ${#arr}", | ||
| // Assignments don't word-split | ||
| "x=$y", | ||
| "x=$(pwd)", | ||
| // [[ ]] doesn't word-split | ||
| "[[ $x == foo ]]", | ||
| // Case discriminants don't word-split | ||
| "case $x in a) ;; esac", | ||
| // Arithmetic contexts don't word-split | ||
| "echo $((x + 1))", | ||
| // Heredoc delimiters and herestrings | ||
| "cat <<< $var", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "echo $var", | ||
| output: 'echo "$var"', | ||
| errors: [ | ||
| { | ||
| messageId: "unquotedParameterExpansion", | ||
| data: { expansion: "$var" }, | ||
| line: 1, | ||
| column: 6, | ||
| endColumn: 10, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "echo ${var}", | ||
| output: 'echo "${var}"', | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| // The message quotes the expansion as written | ||
| code: "echo ${arr[@]}", | ||
| output: 'echo "${arr[@]}"', | ||
| errors: [ | ||
| { | ||
| messageId: "unquotedParameterExpansion", | ||
| data: { expansion: "${arr[@]}" }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "echo ${var:-default}", | ||
| output: 'echo "${var:-default}"', | ||
| errors: [ | ||
| { | ||
| messageId: "unquotedParameterExpansion", | ||
| data: { expansion: "${var:-default}" }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "echo $@", | ||
| output: 'echo "$@"', | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| code: "echo $1", | ||
| output: 'echo "$1"', | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| code: "rm $(ls)", | ||
| output: 'rm "$(ls)"', | ||
| errors: [{ messageId: "unquotedCommandSubstitution" }], | ||
| }, | ||
| { | ||
| // Expansion in the command-name position | ||
| code: "$cmd --help", | ||
| output: '"$cmd" --help', | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| // Mixed word: report but do not autofix | ||
| code: "echo prefix$var", | ||
| output: null, | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| code: "for f in $files; do echo ok; done", | ||
| output: 'for f in "$files"; do echo ok; done', | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| code: "cat > $out", | ||
| output: 'cat > "$out"', | ||
| errors: [{ messageId: "unquotedParameterExpansion" }], | ||
| }, | ||
| { | ||
| code: "cp $a $b", | ||
| output: 'cp "$a" "$b"', | ||
| errors: [ | ||
| { messageId: "unquotedParameterExpansion" }, | ||
| { messageId: "unquotedParameterExpansion" }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); |
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,112 @@ | ||
| /** | ||
| * @fileoverview Rule to require quoting expansions that undergo word | ||
| * splitting and globbing. Mirrors ShellCheck SC2086/SC2046. | ||
| */ | ||
|
|
||
| import type { ShellRuleDefinition, WordNode } from "../types.js"; | ||
|
|
||
| /** | ||
| * Parameters that always expand to values that cannot split, so quoting is | ||
| * unnecessary. | ||
| */ | ||
| const SAFE_PARAMETERS = new Set(["?", "$", "!", "#", "-"]); | ||
|
|
||
| /** Heredoc-style redirect operators whose targets never word-split. */ | ||
| const NON_SPLITTING_REDIRECTS = new Set(["<<", "<<-", "<<<"]); | ||
|
|
||
| const rule: ShellRuleDefinition<{ | ||
| MessageIds: "unquotedParameterExpansion" | "unquotedCommandSubstitution"; | ||
| }> = { | ||
| meta: { | ||
| type: "problem", | ||
| docs: { | ||
| description: | ||
| "Require quoting parameter expansions and command substitutions that are subject to word splitting", | ||
| recommended: true, | ||
| url: "https://github.com/eslint/shell/blob/main/docs/rules/no-unquoted-expansions.md", | ||
| }, | ||
| fixable: "code", | ||
| schema: [], | ||
| messages: { | ||
| unquotedParameterExpansion: | ||
| 'Double quote "{{expansion}}" to prevent word splitting and globbing. (ShellCheck SC2086)', | ||
| unquotedCommandSubstitution: | ||
| "Quote this command substitution to prevent word splitting. (ShellCheck SC2046)", | ||
| }, | ||
| }, | ||
|
|
||
| create(context) { | ||
| const { sourceCode } = context; | ||
|
|
||
| function checkWord(word: WordNode): void { | ||
| for (const part of word.parts) { | ||
| if ( | ||
| part.type !== "ParameterExpansion" && | ||
| part.type !== "CommandSubstitution" | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| if (part.type === "ParameterExpansion") { | ||
| if (part.lengthOf || SAFE_PARAMETERS.has(part.name)) { | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // Only offer a fix when the expansion is the entire word. | ||
| const isWholeWord = | ||
| word.parts.length === 1 && | ||
| word.start === part.start && | ||
| word.end === part.end; | ||
|
|
||
| context.report({ | ||
| node: part, | ||
| messageId: | ||
| part.type === "ParameterExpansion" | ||
| ? "unquotedParameterExpansion" | ||
| : "unquotedCommandSubstitution", | ||
| data: | ||
| part.type === "ParameterExpansion" | ||
| ? { expansion: sourceCode.getText(part) } | ||
| : {}, | ||
| fix: isWholeWord | ||
| ? fixer => | ||
| fixer.replaceText( | ||
| word, | ||
| `"${sourceCode.getText(word)}"`, | ||
| ) | ||
| : undefined, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| Command(node) { | ||
| if (node.name) { | ||
| checkWord(node.name); | ||
| } | ||
|
|
||
| for (const argument of node.arguments) { | ||
| checkWord(argument); | ||
| } | ||
| }, | ||
|
|
||
| ForStatement(node) { | ||
| for (const word of node.words) { | ||
| checkWord(word); | ||
| } | ||
| }, | ||
|
|
||
| Redirect(node) { | ||
| if ( | ||
| node.target && | ||
| !NON_SPLITTING_REDIRECTS.has(node.operator) | ||
| ) { | ||
| checkWord(node.target); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default rule; | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: eslint/shell
Length of output: 6872
🏁 Script executed:
Repository: eslint/shell
Length of output: 15404
🏁 Script executed:
Repository: eslint/shell
Length of output: 17445
🌐 Web query:
POSIX shell Bash mksh redirection word parameter expansion field splitting authoritative documentation💡 Result:
🏁 Script executed:
Repository: eslint/shell
Length of output: 503
🌐 Web query:
site:pubs.opengroup.org POSIX redirection operand field splitting word splitting redirection word shall be subjected💡 Result:
Restrict redirect diagnostics by shell dialect.
cat > $filereaches theRedirectlistener inshell/posix. POSIXshredirection operands are not field-split, so this diagnostic and its quoting fix are false positives there.The plugin’s
posixlanguage represents POSIXsh, not Bash POSIX mode. Do not remove the check for every dialect: non-POSIX Bash can report an ambiguous redirect for a multiword unquoted expansion. Make the listener mode-specific, or exclude it only for dialects with POSIX redirection semantics.🤖 Prompt for AI Agents