`to-template-literal` / `to-string-literal` drop or mis-escape operands; `to-ternary` and `to-arrow` emit invalid syntax

Open
#52 0 comments 0 reactions 0 assignees View on GitHub

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

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: getExpressionValue returns '' for any operand that is not an Identifier or a string Literal, 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: extractIdentifiers only replaces ${identifier}; other expressions stay in the output as literal ${...} text instead of being skipped or spliced as + (expr). In addition, JSON.stringify is called on the raw template source slice rather than the cooked value, doubling escapes (\n becomes \\n).

  • to-ternary.ts: branches are copied with ctx.getTextOf(consequent), which on an ExpressionStatement includes 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 ;)
    

    normalizeStatement also does not check that each branch is an ExpressionStatement, so if (c) return 1; else return 2; is rewritten to c ? return 1; : return 2;.

  • to-arrow.ts: fn.generator is not checked, so /// to-arrow on function* gen() { yield 1; } emits const gen = () => { yield 1; }, which fails to parse.

  • findNodeBelow in src/commands/_utils.ts: array holes (const a = [1, , 3];) beneath a command comment throw TypeError: Cannot read properties of null (reading 'loc') because null elements in ArrayExpression.elements are 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from antfu/eslint-plugin-command

All issues in antfu/eslint-plugin-command

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.