`to-template-literal` / `to-string-literal` drop or mis-escape operands; `to-ternary` and `to-arrow` emit invalid syntax
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
- Área
- developer-experience, tooling
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: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/
- Lenguaje dominante
- TypeScript
- Estrellas
- 372
- Forks
- 18
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de antfu/eslint-plugin-command
-
Dificultad 3/5 1-2 días Aptitud para principiantes 68/100
antfu/eslint-plugin-command#45 · 2 comentarios · 2 reacciones ·
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
antfu/eslint-plugin-command#33 · 1 reacción ·
-
help wanted
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
antfu/eslint-plugin-command#26 · 3 comentarios ·
-
enhancement
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
antfu/eslint-plugin-command#23 · 3 comentarios ·
-
enhancement
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
antfu/eslint-plugin-command#17 · 2 comentarios ·
Todos los issues de antfu/eslint-plugin-command
Issues similares
-
triage
Dificultad 1/5 Menos de una hora Aptitud para principiantes 64/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
ranxianglei/billion-context#7860 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
-
agent-oc-find-bugs api complexity → simple payment-methods recurring contributions reviewed stripe
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
opencollective/opencollective#9177 ·