-
-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add string escape for encapsed #263
Conversation
I'm going to delete that failing test for now - there's already an open issue for it #253 |
Hmm, are you sure? PHP Script : <?php
'You can also have embedded\n newlines in
strings this way as it is
okay to do'; AST Structure : {
"kind": "program",
"children": [
{
"kind": "string",
"value": "You can also have embedded\\n newlines in \nstrings this way as it is\nokay to do",
"raw": "'You can also have embedded\\n newlines in \nstrings this way as it is\nokay to do'",
"isDoubleQuote": false
}
],
"errors": []
} |
Ya I saw the same thing when using the parser playground, but when I actually debugged in the code |
Oh, ok - maybe this changed in the meantime. Using |
src/printer.js
Outdated
const quote = node.isDoubleQuote ? '"' : "'"; | ||
let quote = node.isDoubleQuote ? '"' : "'"; | ||
if (path.getParentNode().kind === "encapsed") { | ||
quote = ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this working? I've just looked at the implementation of makeString
, but I don't see how passing an empty quote is affecting the escaping ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it would affect the escaping - but when the string is part of an encapsed node, that parent node is what's responsible for printing the actual quote. so if you had $test = "test ${string}";
- that actually gets broken up into multiple strings. the parent node is an encapsed node (which is responsible for printing the quote), and the children are test
and ${string}
i think - which we wouldn't want to re-print quotes for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right - and is the change affecting the tests in any way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so? Other than keeping escaped line breaks escaped (which we're not currently doing)
Why |
@czosel @evilebottnawi switched to use |
} | ||
if (['"', "'"].includes(stringValue[stringValue.length - 1])) { | ||
stringValue = stringValue.substr(0, stringValue.length - 1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm usually not too much into using Regexes, but here it might be shorter:
if (['"', "'"].includes(stringValue[0])) {
stringValue = str.replace(/^["'](.*)["']$/, '$1');
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙏 thank you - im terrible with regex haha
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Me too, i basically copied this from StackOverflow 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hrm that doesn't seem to be working for me though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, looks ok to me:
strip = str => str.replace(/^["'](.*)["']$/, '$1')
strip("\"foo\"")
"foo"
strip("'foo'")
"foo"
strip("'foo' a")
"'foo' a"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for whatever reason this test is failing
echo "This spans
multiple lines. The newlines will be
output as well";
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it was lacking multiline support. Try ^["']([\s\S]*)["']$
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we're slowly reaching the point where the Regex becomes so cryptic that it's better to keep the longer, non-regex version 😉
@@ -19,6 +19,7 @@ function printNumber(rawNumber) { | |||
); | |||
} | |||
|
|||
// @TODO: if we're using the "raw" value from the parser, do we need this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe @evilebottnawi can answer that 😄
$test = | ||
"You can also have embedded\\n newlines in | ||
strings this way as it is | ||
okay to do"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation is strange here, right? (Has not much to do with this PR though)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed. i think thats just the way our assign
node breaks though if we go over the line limit. maybe we need to think about an exception for strings or something? not sure best way to handle it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, that might be an idea. Let's open a new issue about this, it's probably not very urgent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Looks good, with or without regex 😉
#29
Unfortunately I don't think we can fix #253 right now. Maybe we can utilize the
raw
attribute from the parser? Butnode.value
is exactly the same whether its a literal line break or escaped