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

Abierto
#52 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
eslint, node.js, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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/

Lenguaje dominante
TypeScript
Estrellas
372
Forks
18
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de antfu/eslint-plugin-command

Todos los issues de antfu/eslint-plugin-command

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.