Open jamesjuett opened 3 years ago
This issue is caused by StringLiteralASTNode
in createFromAST
in StringLiteralExpression
. The string literal returned from AST is unescaped. For example "1\n2\n3"
is unescaped to "1\\n2\\n3\\n"
.
Also, there is an issue in util.escapeString
, this function will only replace the first occurrence with the corresponding sequence. The correct method is to use replaceAll()
method, but it requires ES 2021. So I use a nested loop to fix this.
export function escapeString(text: string) {
for (var i = 0; i < escapes.length; ++i) {
// text = text.replace(escapes[i], escaped[i]);
//! requires ES 2021
// text = text.replaceAll(escapes[i], escaped[i]);
while (text.includes(escapes[i])) {
text = text.replace(escapes[i], escaped[i]);
}
}
return text;
}
And for createFromAST
method in StringLiteralExpression
, we only need to call escapeString
for ast.value
.
public static createFromAST(
ast: StringLiteralASTNode,
context: ExpressionContext
) {
return new StringLiteralExpression(
context,
ast,
escapeString(ast.value)
);
}
This issue can be addressed by above fix. But it will cause a side effect, the string on the right-hand side window is also replaced.
I think a better solution is to call escapeString
on all strings printed in console, but I haven't fully understood the structure of lobster. Maybe you can find a better way to fix this without causing side effect.
Frankly, I personally think this side effect is very useful in some way.
I'm not able to reproduce the case where the "1 2 3" shows up on different lines in the code simulation. Unless, is that what happens with the proposed addition?
I'm not able to reproduce the case where the "1 2 3" shows up on different lines in the code simulation. Unless, is that what happens with the proposed addition?
Yes, that happens with proposed addition.
For newlines, I'm not sure whether I like the way it shows up as a multiline or not. But for other things like \0
it would be important to show the escaped version (rather than actually putting a null character to the html). Probably the best fix would be to escape when printed to the console.
This doesn't seem to print correctly: