Repository navigation
chore: add rule helper utilities #4
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /** | ||
| * @fileoverview Tests for rule helper utilities. | ||
| */ | ||
|
|
||
| import { describe, expect, it } from "vitest"; | ||
| import { parseShell } from "../parser/parse.js"; | ||
| import { | ||
| getCommandName, | ||
| getExpansions, | ||
| getStaticText, | ||
| isCommandNamed, | ||
| } from "./utils.js"; | ||
| import type { CommandNode, WordNode } from "../types.js"; | ||
|
|
||
| function firstCommand(text: string): CommandNode { | ||
| return parseShell(text).ast.body[0] as CommandNode; | ||
| } | ||
|
|
||
| function firstArgument(text: string): WordNode { | ||
| const word = firstCommand(text).arguments[0]; | ||
|
|
||
| if (!word) { | ||
| throw new Error("Expected an argument"); | ||
| } | ||
|
|
||
| return word; | ||
| } | ||
|
|
||
| describe("getStaticText", () => { | ||
| it("should return plain literal text", () => { | ||
| expect(getStaticText(firstArgument("echo hello\n"))).toBe("hello"); | ||
| }); | ||
|
|
||
| it("should unquote single-quoted text", () => { | ||
| expect(getStaticText(firstArgument("echo 'a b'\n"))).toBe("a b"); | ||
| }); | ||
|
|
||
| it("should unquote double-quoted literal text", () => { | ||
| expect(getStaticText(firstArgument('echo "a b"\n'))).toBe("a b"); | ||
| }); | ||
|
|
||
| it("should join mixed static parts", () => { | ||
| expect(getStaticText(firstArgument("echo pre'mid'\"post\"\n"))).toBe( | ||
| "premidpost", | ||
| ); | ||
| }); | ||
|
|
||
| it("should return the text of dollar-quoted strings without escapes", () => { | ||
| expect(getStaticText(firstArgument("echo $'foo bar'\n"))).toBe( | ||
| "foo bar", | ||
| ); | ||
| }); | ||
|
|
||
| it("should return null for dollar-quoted strings with escapes", () => { | ||
| expect(getStaticText(firstArgument("echo $'e\\x63ho'\n"))).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return null for locale-translated strings", () => { | ||
| expect(getStaticText(firstArgument('echo $"foo"\n'))).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return null for words with expansions", () => { | ||
| expect(getStaticText(firstArgument("echo $var\n"))).toBeNull(); | ||
| expect(getStaticText(firstArgument('echo "x$var"\n'))).toBeNull(); | ||
| expect(getStaticText(firstArgument("echo $(pwd)\n"))).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getCommandName", () => { | ||
| it("should return the static command name", () => { | ||
| expect(getCommandName(firstCommand("echo hi\n"))).toBe("echo"); | ||
| }); | ||
|
|
||
| it("should return null for names that need unescaping", () => { | ||
| expect(getCommandName(firstCommand("$'e\\x63ho' hi\n"))).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return null for dynamic names", () => { | ||
| expect(getCommandName(firstCommand("$cmd hi\n"))).toBeNull(); | ||
| }); | ||
|
|
||
| it("should return null for assignment-only commands", () => { | ||
| expect(getCommandName(firstCommand("x=1\n"))).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("isCommandNamed", () => { | ||
| it("should match commands by name", () => { | ||
| expect(isCommandNamed(firstCommand("cd /tmp\n"), "cd")).toBe(true); | ||
| expect(isCommandNamed(firstCommand("cd /tmp\n"), "ls")).toBe(false); | ||
| }); | ||
|
|
||
| it("should not match non-command statements", () => { | ||
| const statement = parseShell("a | b\n").ast.body[0]!; | ||
|
|
||
| expect(isCommandNamed(statement, "a")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getExpansions", () => { | ||
| it("should return top-level expansions", () => { | ||
| const expansions = getExpansions(firstArgument("echo a$b$(c)\n")); | ||
|
|
||
| expect(expansions).toHaveLength(2); | ||
| expect(expansions[0]?.type).toBe("ParameterExpansion"); | ||
| expect(expansions[1]?.type).toBe("CommandSubstitution"); | ||
| }); | ||
|
|
||
| it("should not return expansions nested inside other expansions", () => { | ||
| const expansions = getExpansions(firstArgument("echo ${a:-$b}\n")); | ||
|
|
||
| expect(expansions).toHaveLength(1); | ||
| expect(expansions[0]).toMatchObject({ name: "a" }); | ||
| }); | ||
|
|
||
| it("should skip quoted expansions by default", () => { | ||
| const expansions = getExpansions(firstArgument('echo "$a"\n')); | ||
|
|
||
| expect(expansions).toHaveLength(0); | ||
| }); | ||
|
|
||
| it("should include quoted expansions when requested", () => { | ||
| const expansions = getExpansions(firstArgument('echo "$a$(b)"\n'), { | ||
| includeQuoted: true, | ||
| }); | ||
|
|
||
| expect(expansions).toHaveLength(2); | ||
| }); | ||
| }); |
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,122 @@ | ||
| /** | ||
| * @fileoverview Shared helpers for shell rules. | ||
| */ | ||
|
|
||
| import type { | ||
| CommandNode, | ||
| CommandSubstitutionNode, | ||
| ParameterExpansionNode, | ||
| StatementNode, | ||
| WordNode, | ||
| WordPartNode, | ||
| } from "../types.js"; | ||
|
|
||
| /** | ||
| * Returns the plain text of a word when it is fully static (made up only of | ||
| * literals and quoted literals), or `null` when the word contains | ||
| * expansions. Quotes are removed from the result. | ||
| * | ||
| * Strings whose runtime text can differ from their source text are not | ||
| * static: `$'...'` strings containing escapes, which are not decoded, and | ||
| * locale-translated `$"..."` strings. | ||
| */ | ||
| export function getStaticText(word: WordNode): string | null { | ||
| let result = ""; | ||
|
|
||
| for (const part of word.parts) { | ||
| const text = getStaticPartText(part); | ||
|
|
||
| if (text === null) { | ||
| return null; | ||
| } | ||
|
|
||
| result += text; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| function getStaticPartText(part: WordPartNode): string | null { | ||
| switch (part.type) { | ||
| case "Literal": | ||
| return part.value; | ||
|
|
||
| case "SingleQuotedString": | ||
| return part.dollar && part.value.includes("\\") ? null : part.value; | ||
|
|
||
| case "DoubleQuotedString": { | ||
| if (part.dollar) { | ||
| return null; | ||
| } | ||
|
|
||
| let result = ""; | ||
|
|
||
| for (const inner of part.parts) { | ||
| const text = getStaticPartText(inner); | ||
|
|
||
| if (text === null) { | ||
| return null; | ||
| } | ||
|
|
||
| result += text; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the name of a command when it is a static word, or `null`. | ||
| */ | ||
| export function getCommandName(command: CommandNode): string | null { | ||
| if (!command.name) { | ||
| return null; | ||
| } | ||
|
|
||
| return getStaticText(command.name); | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a command node is a simple command with the given name. | ||
| */ | ||
| export function isCommandNamed( | ||
| statement: StatementNode, | ||
| name: string, | ||
| ): statement is CommandNode { | ||
| return statement.type === "Command" && getCommandName(statement) === name; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the expansions that make up a word, including those nested inside | ||
| * double quotes when `includeQuoted` is `true`. Expansions nested inside | ||
| * another expansion, such as `$b` in `${a:-$b}`, are not returned. | ||
| */ | ||
| export function getExpansions( | ||
| word: WordNode, | ||
| { includeQuoted = false } = {}, | ||
| ): (ParameterExpansionNode | CommandSubstitutionNode)[] { | ||
| const expansions: (ParameterExpansionNode | CommandSubstitutionNode)[] = []; | ||
|
|
||
| const visitParts = (parts: WordPartNode[], quoted: boolean): void => { | ||
| for (const part of parts) { | ||
| if ( | ||
| part.type === "ParameterExpansion" || | ||
| part.type === "CommandSubstitution" | ||
| ) { | ||
| if (!quoted || includeQuoted) { | ||
| expansions.push(part); | ||
| } | ||
| } else if (part.type === "DoubleQuotedString") { | ||
| visitParts(part.parts, true); | ||
| } | ||
|
nzakas marked this conversation as resolved.
|
||
| } | ||
| }; | ||
|
|
||
| visitParts(word.parts, false); | ||
|
|
||
| return expansions; | ||
| } | ||
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: 826
🏁 Script executed:
Repository: eslint/shell
Length of output: 4178
🏁 Script executed:
Repository: eslint/shell
Length of output: 5792
🌐 Web query:
mvdan-sh npm 0.10.1 mvdan.cc/sh syntax Lit.Value backslash escaped literal💡 Result:
🏁 Script executed:
Repository: eslint/shell
Length of output: 5018
🌐 Web query:
GitHub source mvdan-sh npm 0.10.1 GopherJS Lit Value escaped backslash💡 Result:
🌐 Web query:
mvdan-sh npm package 0.10.1 GitHub repository GopherJS source syntax Lit.Value💡 Result:
🏁 Script executed:
Repository: eslint/shell
Length of output: 5552
🏁 Script executed:
Repository: eslint/shell
Length of output: 6213
🏁 Script executed:
Repository: eslint/shell
Length of output: 11610
🏁 Script executed:
Repository: eslint/shell
Length of output: 17991
🏁 Script executed:
Repository: eslint/shell
Length of output: 5154
🏁 Script executed:
Repository: eslint/shell
Length of output: 3872
Decode literal escapes in their shell quoting context.
mvdan-shpreserves unquoted escapes inLit.Value, and the translator copies that value toLiteral.value. Forc\d /tmp,getStaticTexttherefore returnsc\d, while Bash usescdas the command name.isCommandNamed(statement, "cd")can miss that command. Decode escapes according to whether each literal is unquoted or inside double quotes.Suggested fix
🤖 Prompt for AI Agents