bigskysoftware / bigskysoftware/_hyperscript
Error location annotations don't line up when _hs source uses tabs for indentation
- Langage dominant
- JavaScript
- Étoiles
- 3.8k
- Forks
- 172
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
hyperscript's error annotations (the `^^` markers) do not line up with the printed source when it uses tab characters as indentation:
When spaces are used in the source, the marker lines up correctly:
This issue is because, when the annotation creator ([`createParserContext`](https://github.com/bigskysoftware/_hyperscript/blob/455a32d30533c198cdf9a75a053c41520de04249/src/_hyperscript.js#L1311)) is figuring out where to place the marker, it counts tab characters as a single character. If, on the first example, the tab characters are replaces with single spaces, the marker lines up correctly:
To solve this, either:
- Around `createParserContext`'s call to `String.repeat`, tabs would be specially-cased for the offset to count as some number of characters (4, 8, ...) rather than just one.
- Or, the whitespace before the true start of a line could be copied, where the marker offset will only counted from the "real" start of the line (iow, where the indentation ends), and afterwards the saved whitespace is prepended to the `" ".repeat(offset)`, something like
```diff
diff --git a/tmp/1 b/tmp/2
index c054964..a688d74 100644
--- a/tmp/1
+++ b/tmp/2
@@ -4,7 +4,8 @@
var lines = source.split("\n");
var line = currentToken && currentToken.line ? currentToken.line - 1 : lines.length - 1;
var contextLine = lines[line];
+ var leadingWhitespace = contextLine.match(/^\s+/)[0];
var offset = /** @type {number} */ (
currentToken && currentToken.line ? currentToken.column : contextLine.length - 1);
- return contextLine + "\n" + " ".repeat(offset) + "^^\n\n";
+ return contextLine + "\n" + leadingWhitespace + " ".repeat(offset - leadingWhitespace.length) + "^^\n\n";
}
```
This is of course a bit hacky and doesn't take into account things like tabs in the middle of a line (which solution no.1 would do correctly, and only that among other edge cases I'm not smart enough to think of), but it works fine!
Even with mixed indentation!
---
Using:
- _hyperscript 0.9.14, (originally from unpkg)
- Firefox 141.0.3
- Debian 13 `Linux 6.12.38+deb13-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.38-1 (2025-07-16) x86_64 GNU/Linux`
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Commencez par createParserContext dans src/_hyperscript.js et reproduisez la sortie d’annotation avec du code source _hs indenté avec des tabulations, à indentation mixte et indenté avec des espaces. Suivez le calcul du décalage du marqueur et vérifiez que les marqueurs ^^ obtenus s’alignent sur l’emplacement source indiqué dans chaque cas.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- À l'abandon
- Clarté
- Clairement spécifiée
- Accessibilité débutants
- 48/100