`to-template-literal` / `to-string-literal` drop or mis-escape operands; `to-ternary` and `to-arrow` emit invalid syntax
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- eslint, node.js, typescript
- Domain
- developer-experience, tooling
Research direction
Reproduce first by copying the gist input into a fixture and running npx eslint --fix; then work command by command in src/commands/. Start with to-template-literal.ts (getExpressionValue, escape), then to-string-literal.ts (extractIdentifiers and the JSON.stringify on the raw slice), to-ternary.ts (ctx.getTextOf, normalizeStatement), to-arrow.ts (fn.generator) and findNodeBelow in _utils.ts for null array holes. Done means each rewrite preserves the pre-fix runtime output and tests cover member expressions, numbers, backslashes, ${} and array holes.
Written by the indexing model from the issue text.
Description
Describe the bug
Several built-in commands rewrite code into output with different runtime behavior or invalid syntax.
With eslint-plugin-command 4.0.0 (main a1d3942) and ESLint 10.14.0:
// eslint.config.js
import command from "eslint-plugin-command/config";
export default [command()];
// input.js
const user = { name: "Ada" }, count = 2, dir = "C:\\temp";
/// to-tl
const greeting = "Hello, " + user.name + "!";
/// to-tl
const label = "Items: " + count + 1;
/// to-tl
const path = dir + "\\new";
/// to-sl
const msg = `Hi ${user.name}`;
/// to-sl
const two = `line1\nline2`;
console.log(greeting, "|", label, "|", path, "|", msg, "|", JSON.stringify(two));
Running npx eslint --fix input.js exits cleanly and rewrites input.js to:
const user = { name: "Ada" }, count = 2, dir = "C:\\temp";
const greeting = `Hello, !`;
const label = `Items: ${count}`;
const path = `${dir}\new`;
const msg = "Hi ${user.name}";
const two = "line1\\nline2";
console.log(greeting, "|", label, "|", path, "|", msg, "|", JSON.stringify(two));
Before --fix, node input.js prints:
Hello, Ada! | Items: 21 | C:\temp\new | Hi Ada | "line1\nline2"
After --fix, node input.js prints:
Hello, ! | Items: 2 | C:\temp
ew | Hi ${user.name} | "line1\\nline2"
Causes in src/commands/:
-
to-template-literal.ts:getExpressionValuereturns''for any operand that is not anIdentifieror a stringLiteral, so member expressions, calls, numbers, and nested non-+expressions are dropped.escape()escapes`and${but not\, so a backslash in a string value becomes a template escape sequence (\n,\t,\u...). -
to-string-literal.ts:extractIdentifiersonly replaces${identifier}; other expressions stay in the output as literal${...}text instead of being skipped or spliced as+ (expr). In addition,JSON.stringifyis called on the raw template source slice rather than the cooked value, doubling escapes (\nbecomes\\n). -
to-ternary.ts: branches are copied withctx.getTextOf(consequent), which on anExpressionStatementincludes the trailing;:let size; /// to-ternary if (count > 1) size = "many"; else size = "one"; // --fix produces: size = count > 1 ? "many"; : "one"; (Parsing error: Unexpected token ;)normalizeStatementalso does not check that each branch is anExpressionStatement, soif (c) return 1; else return 2;is rewritten toc ? return 1; : return 2;. -
to-arrow.ts:fn.generatoris not checked, so/// to-arrowonfunction* gen() { yield 1; }emitsconst gen = () => { yield 1; }, which fails to parse. -
findNodeBelowinsrc/commands/_utils.ts: array holes (const a = [1, , 3];) beneath a command comment throwTypeError: Cannot read properties of null (reading 'loc')becausenullelements inArrayExpression.elementsare not skipped.
Reproduction
https://gist.github.com/trackoor/203c95d87020e7f1c801fb43e300cc4f
System Info
System:
OS: Linux 6.1 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
CPU: (64) x64 Intel(R) Xeon(R) CPU @ 2.80GHz
Shell: 5.2.15 - /bin/bash
Binaries:
Node: 26.13.0
npm: 11.20.0
npmPackages:
eslint: 10.14.0
eslint-plugin-command: 4.0.0 (main a1d3942)
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guide.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Check that this is a concrete bug. For Q&A, please open a GitHub Discussion instead.
- The provided reproduction is a minimal reproducible of the bug.
Contributions
- I am willing to submit a PR to fix this issue
- I am willing to submit a PR with failing tests (actually just go ahead and do it, thanks!)
Credit: https://ucsbplse.github.io/
- Dominant language
- TypeScript
- Stars
- 372
- Forks
- 18
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from antfu/eslint-plugin-command
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
antfu/eslint-plugin-command#45 · 2 comments · 2 reactions ·
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 35/100
antfu/eslint-plugin-command#33 · 1 reaction ·
-
help wanted
Difficulty 3/5 1-2 days Newbie friendliness 35/100
antfu/eslint-plugin-command#26 · 3 comments ·
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 35/100
antfu/eslint-plugin-command#23 · 3 comments ·
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
antfu/eslint-plugin-command#17 · 2 comments ·
All issues in antfu/eslint-plugin-command
Similar issues
-
triage
Difficulty 1/5 Under an hour Newbie friendliness 64/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
ranxianglei/billion-context#7860 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
-
agent-oc-find-bugs api complexity → simple payment-methods recurring contributions reviewed stripe
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
opencollective/opencollective#9177 ·