Skip to content
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

Merged
merged 4 commits into from
Mar 30, 2018
Merged

fix: add string escape for encapsed #263

merged 4 commits into from
Mar 30, 2018

Conversation

mgrip
Copy link
Contributor

@mgrip mgrip commented Mar 29, 2018

#29

Unfortunately I don't think we can fix #253 right now. Maybe we can utilize the raw attribute from the parser? But node.value is exactly the same whether its a literal line break or escaped

@mgrip
Copy link
Contributor Author

mgrip commented Mar 29, 2018

I'm going to delete that failing test for now - there's already an open issue for it #253

@czosel
Copy link
Collaborator

czosel commented Mar 29, 2018

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": []
}

glayzzle/php-parser#123

@mgrip
Copy link
Contributor Author

mgrip commented Mar 29, 2018

Ya I saw the same thing when using the parser playground, but when I actually debugged in the code node.value was exactly the same

@czosel
Copy link
Collaborator

czosel commented Mar 29, 2018

Oh, ok - maybe this changed in the meantime. Using raw definitely sounds like a plan.

src/printer.js Outdated
const quote = node.isDoubleQuote ? '"' : "'";
let quote = node.isDoubleQuote ? '"' : "'";
if (path.getParentNode().kind === "encapsed") {
quote = "";
Copy link
Collaborator

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 ...

Copy link
Contributor Author

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

Copy link
Collaborator

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?

Copy link
Contributor Author

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)

@alexander-akait
Copy link
Member

Why raw sounds like a pain?

@mgrip
Copy link
Contributor Author

mgrip commented Mar 30, 2018

@czosel @evilebottnawi switched to use node.raw - let me know what you think. Would now fix #253 as well

@mgrip mgrip mentioned this pull request Mar 30, 2018
}
if (['"', "'"].includes(stringValue[stringValue.length - 1])) {
stringValue = stringValue.substr(0, stringValue.length - 1);
}
Copy link
Collaborator

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');
}

Copy link
Contributor Author

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

Copy link
Collaborator

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 😂

Copy link
Contributor Author

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

Copy link
Collaborator

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"

Copy link
Contributor Author

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";

Copy link
Collaborator

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]*)["']$

Copy link
Collaborator

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?
Copy link
Collaborator

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";
Copy link
Collaborator

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)

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Collaborator

@czosel czosel left a 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 😉

@alexander-akait alexander-akait merged commit 7dc8a42 into prettier:master Mar 30, 2018
@mgrip mgrip deleted the string-escape branch April 3, 2018 14:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't change embedded newlines into \n
3 participants