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: respec special characters inside string #57

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ const indent = docBuilders.indent;
const hardline = docBuilders.hardline;
const softline = docBuilders.softline;

const makeString = util.makeString;

function stringEscape(str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you just add a comment explaining why we need to do this? 🙏

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgrip I think I will first create the problem in php parser, let's wait answer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good to me 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want to push this just with a comment w/ a link to the issue in the parser repo that works for me - that way we dont have to wait for it to get fixed on their end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgrip I think we will do this if we do not receive an answer at the earliest acceptable time

return str
.replace(/[\\]/g, "\\\\")
.replace(/["]/g, '\\"')
.replace(/[/]/g, "\\/")
.replace(/[\b]/g, "\\b")
.replace(/[\f]/g, "\\f")
.replace(/[\n]/g, "\\n")
.replace(/[\r]/g, "\\r")
.replace(/[\t]/g, "\\t");
}

// polyfill for node 4
function includes(array, val) {
return array.indexOf(val) !== -1;
Expand Down Expand Up @@ -218,7 +232,7 @@ function printExpression(path, options, print) {
// use setting from options. need to figure out how this works w/ complex strings and interpolation
// also need to figure out splitting long strings
const quote = node.isDoubleQuote ? '"' : "'";
return quote + node.value + quote;
return makeString(stringEscape(node.value), quote, true);
}
case "number":
return node.value;
Expand Down
18 changes: 18 additions & 0 deletions tests/literals/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ $silent = @$foo;
$nowdoc = <<<'EOL'
"test" foo
EOL;
$newline = "hello\\nworld";
$win_newline = "hello\\r\\nworld";
$tab = "hello\\tworld";
$backslash = "hello\\\\world";
$slash = "hello/world";
$single_quotes_with_double_quotes = '"hello world"';
$single_quotes_with_single_quotes = '\\'hello world\\'';
$double_quotes_with_single_quotes = "'hello world'";
$double_quotes_with_double_quotes = "\\"hello world\\"";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<?php
$boolean_test_true = true;
Expand All @@ -23,4 +32,13 @@ $silent = @$foo;
$nowdoc = <<<'EOL'
"test" foo
EOL;
$newline = "hello\\nworld";
$win_newline = "hello\\r\\nworld";
$tab = "hello\\tworld";
$backslash = "hello\\\\world";
$slash = "hello/world";
$single_quotes_with_double_quotes = '"hello world"';
$single_quotes_with_single_quotes = '\\'hello world\\'';
$double_quotes_with_single_quotes = "'hello world'";
$double_quotes_with_double_quotes = "\\"hello world\\"";
`;
9 changes: 9 additions & 0 deletions tests/literals/literals.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@
$nowdoc = <<<'EOL'
"test" foo
EOL;
$newline = "hello\nworld";
$win_newline = "hello\r\nworld";
$tab = "hello\tworld";
$backslash = "hello\\world";
$slash = "hello/world";
$single_quotes_with_double_quotes = '"hello world"';
$single_quotes_with_single_quotes = '\'hello world\'';
$double_quotes_with_single_quotes = "'hello world'";
$double_quotes_with_double_quotes = "\"hello world\"";