2017-02-22 00:59:19 +03:00
|
|
|
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
|
exports[`functions.js 1`] = `
|
2017-04-12 23:41:51 +03:00
|
|
|
|
const a = () => "Foo bar";
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
|
|
|
|
function b(object, key) {
|
2017-02-22 00:59:19 +03:00
|
|
|
|
return object['key'];
|
2017-01-13 09:30:35 +03:00
|
|
|
|
}
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2017-04-12 23:41:51 +03:00
|
|
|
|
const a = () => "Foo bar";
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
|
|
|
|
function b(object, key) {
|
2017-04-12 23:41:51 +03:00
|
|
|
|
return object["key"];
|
2017-01-13 09:30:35 +03:00
|
|
|
|
}
|
2017-04-12 23:41:51 +03:00
|
|
|
|
|
2017-01-13 09:30:35 +03:00
|
|
|
|
`;
|
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
|
exports[`functions.js 2`] = `
|
2017-04-12 23:41:51 +03:00
|
|
|
|
const a = () => "Foo bar";
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
|
|
|
|
function b(object, key) {
|
2017-02-22 00:59:19 +03:00
|
|
|
|
return object['key'];
|
2017-01-13 09:30:35 +03:00
|
|
|
|
}
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2017-02-22 00:59:19 +03:00
|
|
|
|
const a = () => 'Foo bar';
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
|
|
|
|
function b(object, key) {
|
2017-02-22 00:59:19 +03:00
|
|
|
|
return object['key'];
|
2017-01-13 09:30:35 +03:00
|
|
|
|
}
|
2017-04-12 23:41:51 +03:00
|
|
|
|
|
2017-01-13 09:30:35 +03:00
|
|
|
|
`;
|
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
|
exports[`strings.js 1`] = `
|
Preserve code unit sequence of directive literals (#1571)
* Print directive literals verbatim
This addresses https://github.com/prettier/prettier/issues/1555,
but doesn't seem to pass the AST_COMPARE=1 tests:
AST_COMPARE=1 npm test -- tests/quotes -t strings
However, running `prettier --debug-check` on the relevant file *does*
work:
prettier tests/quotes/strings.js --debug-check
* Change directive literal quotes if it doesn't contain quotes
This addresses https://github.com/prettier/prettier/pull/1560#discussion_r115396257
From https://github.com/prettier/prettier/issues/1555#issue-227206837:
> It's okay to change the type of quotation marks used, but only if
doing so does not require changing any characters within the directive.
* Don't change directive literal quotes if it contains a backslash
This passes the `--debug-check` tests again:
prettier tests/quotes/strings.js --debug-check
* Try to add regression test for escaped directive literals
This seems not to work, despite the following command having the correct
output:
echo "'\''" | prettier
You can use the following to get an idea of how flow/typescript parse
this:
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'flow'}), null, 2)"
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'typescript'}), null, 2)"
* WIP Disable Flow/Typescript for ./tests/directives
We don't yet handle escaped directives for them, but Babylon works.
(similar to https://github.com/prettier/prettier/pull/602/commits/90bf93713c78a6a6b3f55e52d7be172ece9b56df#diff-0de18284f37da79ab8af4e4690919abaR1)
* Revert "WIP Disable Flow/Typescript for ./tests/directives"
This reverts commit 2aba6231271f6985a395c31e3df9323e8f3da115.
* Prevent test strings from being parsed as directives
See https://github.com/prettier/prettier/pull/1560#issue-227225960
* Add more escaped directive tests
* Infer DirectiveLiterals from Flow parser
* Don't test TypeScript on directives
See https://github.com/prettier/prettier/pull/1560#issuecomment-300296221
* fixup! Infer DirectiveLiterals from Flow parser
* Don't fake objects that look like a DirectiveLiteral
Instead, add a flag to nodeStr() that deals with the Flow node
accordingly. See https://github.com/prettier/prettier/pull/1560#discussion_r115605758
* Print preferred quotes around escaped DirectiveLiteral when it doesn't contain quotes
See https://github.com/prettier/prettier/pull/1560#discussion_r115606122
* Simplify `canChangeDirectiveQuotes` logic
* Add directive test with unnecessarily escaped non-quote character
* Fix boolean logic error
I thought that this would result in the following if-block executing, as
needed to pass the test case in the previous commit. However, it appears
that it's not actually needed to pass the test case, since `makeString`
doesn't unescape unnecessarily escaped non-quote characters.
Nevertheless, I think we should leave that if-block (`if (canChangeDirectiveQuotes)`)
there, in case `makeString` is updated.
See https://github.com/prettier/prettier/pull/1571#discussion_r115658398
* Make isFlowDirectiveLiteral a separate argument to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115810988
* Simplify isFlowDirectiveLiteral logic by passing n.expression to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115811216
2017-05-10 22:15:27 +03:00
|
|
|
|
// Prevent strings from being parsed as directives
|
|
|
|
|
// See https://github.com/prettier/prettier/pull/1560#issue-227225960
|
|
|
|
|
0;
|
|
|
|
|
|
2017-04-12 23:41:51 +03:00
|
|
|
|
// Every string will be changed to double quotes, unless we end up with fewer
|
|
|
|
|
// escaped quotes by using single quotes. (Vice versa if the "singleQuote"
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// option is true).
|
|
|
|
|
//
|
|
|
|
|
// Note that even if a string already has the correct enclosing quotes, it is
|
|
|
|
|
// still processed in order to remove unnecessarily escaped quotes within it,
|
|
|
|
|
// for consistency.
|
|
|
|
|
|
|
|
|
|
// Simple strings.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"abc"
|
2017-02-22 00:59:19 +03:00
|
|
|
|
'abc'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Escape.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
'\\0'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Emoji.
|
2017-02-22 00:59:19 +03:00
|
|
|
|
'🐶'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Empty string.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
""
|
2017-02-22 00:59:19 +03:00
|
|
|
|
''
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Single double quote.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\""
|
|
|
|
|
'"'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Single single quote.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"'"
|
|
|
|
|
'\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Unnecessary escapes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\'"
|
|
|
|
|
'\\"'
|
2017-05-11 02:02:49 +03:00
|
|
|
|
"\\a"
|
|
|
|
|
'\\a'
|
|
|
|
|
"hol\\a"
|
|
|
|
|
'hol\\a'
|
|
|
|
|
"hol\\\\a (the a is not escaped)"
|
|
|
|
|
'hol\\\\a (the a is not escaped)'
|
|
|
|
|
"multiple \\a unnecessary \\a escapes"
|
|
|
|
|
'multiple \\a unnecessary \\a escapes'
|
|
|
|
|
"unnecessarily escaped character preceded by escaped backslash \\\\\\a"
|
|
|
|
|
'unnecessarily escaped character preceded by escaped backslash \\\\\\a'
|
|
|
|
|
"unescaped character preceded by two escaped backslashes \\\\\\\\a"
|
|
|
|
|
'unescaped character preceded by two escaped backslashes \\\\\\\\a'
|
|
|
|
|
"\\a\\a" // consecutive unnecessarily escaped characters
|
|
|
|
|
'\\a\\a' // consecutive unnecessarily escaped characters
|
|
|
|
|
'escaped \\u2030 \\‰ (should not stay escaped)'
|
|
|
|
|
|
|
|
|
|
// Meaningful escapes
|
|
|
|
|
"octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7"
|
|
|
|
|
'octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7'
|
|
|
|
|
"meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61"
|
|
|
|
|
'meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61'
|
|
|
|
|
'escaped newline \\
|
|
|
|
|
'
|
|
|
|
|
'escaped carriage return \\
|
|
|
|
|
'
|
|
|
|
|
'escaped \\u2028 \\
'
|
|
|
|
|
'escaped \\u2029 \\
'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// One of each.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"'"
|
|
|
|
|
'"\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// One of each with unnecessary escapes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"\\'"
|
|
|
|
|
'\\"\\''
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// More double quotes than single quotes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"'\\""
|
|
|
|
|
'"\\'"'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// More single quotes than double quotes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"''"
|
|
|
|
|
'"\\'\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Two of each.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"\\"''"
|
|
|
|
|
'""\\'\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Single backslash.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
'\\\\'
|
|
|
|
|
"\\\\"
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Backslases.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"\\\\\\"\\\\\\\\\\" '\\'\\\\'\\\\\\'\\\\\\\\'"
|
|
|
|
|
'\\'\\\\\\'\\\\\\\\\\' "\\"\\\\"\\\\\\"\\\\\\\\"'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Somewhat more real-word example.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"He's sayin': \\"How's it goin'?\\" Don't ask me why."
|
|
|
|
|
'He\\'s sayin\\': "How\\'s it goin\\'?" Don\\'t ask me why.'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Somewhat more real-word example 2.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"var backslash = \\"\\\\\\", doubleQuote = '\\"';"
|
|
|
|
|
'var backslash = "\\\\", doubleQuote = \\'"\\';'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Preserve code unit sequence of directive literals (#1571)
* Print directive literals verbatim
This addresses https://github.com/prettier/prettier/issues/1555,
but doesn't seem to pass the AST_COMPARE=1 tests:
AST_COMPARE=1 npm test -- tests/quotes -t strings
However, running `prettier --debug-check` on the relevant file *does*
work:
prettier tests/quotes/strings.js --debug-check
* Change directive literal quotes if it doesn't contain quotes
This addresses https://github.com/prettier/prettier/pull/1560#discussion_r115396257
From https://github.com/prettier/prettier/issues/1555#issue-227206837:
> It's okay to change the type of quotation marks used, but only if
doing so does not require changing any characters within the directive.
* Don't change directive literal quotes if it contains a backslash
This passes the `--debug-check` tests again:
prettier tests/quotes/strings.js --debug-check
* Try to add regression test for escaped directive literals
This seems not to work, despite the following command having the correct
output:
echo "'\''" | prettier
You can use the following to get an idea of how flow/typescript parse
this:
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'flow'}), null, 2)"
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'typescript'}), null, 2)"
* WIP Disable Flow/Typescript for ./tests/directives
We don't yet handle escaped directives for them, but Babylon works.
(similar to https://github.com/prettier/prettier/pull/602/commits/90bf93713c78a6a6b3f55e52d7be172ece9b56df#diff-0de18284f37da79ab8af4e4690919abaR1)
* Revert "WIP Disable Flow/Typescript for ./tests/directives"
This reverts commit 2aba6231271f6985a395c31e3df9323e8f3da115.
* Prevent test strings from being parsed as directives
See https://github.com/prettier/prettier/pull/1560#issue-227225960
* Add more escaped directive tests
* Infer DirectiveLiterals from Flow parser
* Don't test TypeScript on directives
See https://github.com/prettier/prettier/pull/1560#issuecomment-300296221
* fixup! Infer DirectiveLiterals from Flow parser
* Don't fake objects that look like a DirectiveLiteral
Instead, add a flag to nodeStr() that deals with the Flow node
accordingly. See https://github.com/prettier/prettier/pull/1560#discussion_r115605758
* Print preferred quotes around escaped DirectiveLiteral when it doesn't contain quotes
See https://github.com/prettier/prettier/pull/1560#discussion_r115606122
* Simplify `canChangeDirectiveQuotes` logic
* Add directive test with unnecessarily escaped non-quote character
* Fix boolean logic error
I thought that this would result in the following if-block executing, as
needed to pass the test case in the previous commit. However, it appears
that it's not actually needed to pass the test case, since `makeString`
doesn't unescape unnecessarily escaped non-quote characters.
Nevertheless, I think we should leave that if-block (`if (canChangeDirectiveQuotes)`)
there, in case `makeString` is updated.
See https://github.com/prettier/prettier/pull/1571#discussion_r115658398
* Make isFlowDirectiveLiteral a separate argument to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115810988
* Simplify isFlowDirectiveLiteral logic by passing n.expression to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115811216
2017-05-10 22:15:27 +03:00
|
|
|
|
// Prevent strings from being parsed as directives
|
|
|
|
|
// See https://github.com/prettier/prettier/pull/1560#issue-227225960
|
|
|
|
|
0;
|
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Every string will be changed to double quotes, unless we end up with fewer
|
2017-04-12 23:41:51 +03:00
|
|
|
|
// escaped quotes by using single quotes. (Vice versa if the "singleQuote"
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// option is true).
|
|
|
|
|
//
|
|
|
|
|
// Note that even if a string already has the correct enclosing quotes, it is
|
|
|
|
|
// still processed in order to remove unnecessarily escaped quotes within it,
|
|
|
|
|
// for consistency.
|
|
|
|
|
|
|
|
|
|
// Simple strings.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("abc");
|
|
|
|
|
("abc");
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Escape.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\0");
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Emoji.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("🐶");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Empty string.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("");
|
|
|
|
|
("");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Single double quote.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('"');
|
|
|
|
|
('"');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Single single quote.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("'");
|
|
|
|
|
("'");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Unnecessary escapes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("'");
|
|
|
|
|
('"');
|
|
|
|
|
("a");
|
|
|
|
|
("a");
|
|
|
|
|
("hola");
|
|
|
|
|
("hola");
|
|
|
|
|
("hol\\\\a (the a is not escaped)");
|
|
|
|
|
("hol\\\\a (the a is not escaped)");
|
|
|
|
|
("multiple a unnecessary a escapes");
|
|
|
|
|
("multiple a unnecessary a escapes");
|
|
|
|
|
("unnecessarily escaped character preceded by escaped backslash \\\\a");
|
|
|
|
|
("unnecessarily escaped character preceded by escaped backslash \\\\a");
|
|
|
|
|
("unescaped character preceded by two escaped backslashes \\\\\\\\a");
|
|
|
|
|
("unescaped character preceded by two escaped backslashes \\\\\\\\a");
|
|
|
|
|
("aa"); // consecutive unnecessarily escaped characters
|
|
|
|
|
("aa"); // consecutive unnecessarily escaped characters
|
|
|
|
|
("escaped \\u2030 ‰ (should not stay escaped)");
|
2017-05-11 02:02:49 +03:00
|
|
|
|
// Meaningful escapes
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7");
|
|
|
|
|
("octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7");
|
|
|
|
|
("meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61");
|
|
|
|
|
("meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61");
|
|
|
|
|
("escaped newline \\
|
|
|
|
|
");
|
|
|
|
|
("escaped carriage return \\
|
|
|
|
|
");
|
|
|
|
|
("escaped \\u2028 \\
");
|
|
|
|
|
("escaped \\u2029 \\
");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// One of each.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"'");
|
|
|
|
|
("\\"'");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// One of each with unnecessary escapes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"'");
|
|
|
|
|
("\\"'");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// More double quotes than single quotes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('"\\'"');
|
|
|
|
|
('"\\'"');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// More single quotes than double quotes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"''");
|
|
|
|
|
("\\"''");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Two of each.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"\\"''");
|
|
|
|
|
("\\"\\"''");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Single backslash.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\\\");
|
|
|
|
|
("\\\\");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Backslases.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"\\\\\\"\\\\\\\\\\" ''\\\\'\\\\'\\\\\\\\'");
|
|
|
|
|
('\\'\\\\\\'\\\\\\\\\\' ""\\\\"\\\\"\\\\\\\\"');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Somewhat more real-word example.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("He's sayin': \\"How's it goin'?\\" Don't ask me why.");
|
|
|
|
|
("He's sayin': \\"How's it goin'?\\" Don't ask me why.");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Somewhat more real-word example 2.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('var backslash = "\\\\", doubleQuote = \\'"\\';');
|
|
|
|
|
('var backslash = "\\\\", doubleQuote = \\'"\\';');
|
2017-04-12 23:41:51 +03:00
|
|
|
|
|
2017-01-13 09:30:35 +03:00
|
|
|
|
`;
|
|
|
|
|
|
2017-03-22 03:38:28 +03:00
|
|
|
|
exports[`strings.js 2`] = `
|
Preserve code unit sequence of directive literals (#1571)
* Print directive literals verbatim
This addresses https://github.com/prettier/prettier/issues/1555,
but doesn't seem to pass the AST_COMPARE=1 tests:
AST_COMPARE=1 npm test -- tests/quotes -t strings
However, running `prettier --debug-check` on the relevant file *does*
work:
prettier tests/quotes/strings.js --debug-check
* Change directive literal quotes if it doesn't contain quotes
This addresses https://github.com/prettier/prettier/pull/1560#discussion_r115396257
From https://github.com/prettier/prettier/issues/1555#issue-227206837:
> It's okay to change the type of quotation marks used, but only if
doing so does not require changing any characters within the directive.
* Don't change directive literal quotes if it contains a backslash
This passes the `--debug-check` tests again:
prettier tests/quotes/strings.js --debug-check
* Try to add regression test for escaped directive literals
This seems not to work, despite the following command having the correct
output:
echo "'\''" | prettier
You can use the following to get an idea of how flow/typescript parse
this:
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'flow'}), null, 2)"
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'typescript'}), null, 2)"
* WIP Disable Flow/Typescript for ./tests/directives
We don't yet handle escaped directives for them, but Babylon works.
(similar to https://github.com/prettier/prettier/pull/602/commits/90bf93713c78a6a6b3f55e52d7be172ece9b56df#diff-0de18284f37da79ab8af4e4690919abaR1)
* Revert "WIP Disable Flow/Typescript for ./tests/directives"
This reverts commit 2aba6231271f6985a395c31e3df9323e8f3da115.
* Prevent test strings from being parsed as directives
See https://github.com/prettier/prettier/pull/1560#issue-227225960
* Add more escaped directive tests
* Infer DirectiveLiterals from Flow parser
* Don't test TypeScript on directives
See https://github.com/prettier/prettier/pull/1560#issuecomment-300296221
* fixup! Infer DirectiveLiterals from Flow parser
* Don't fake objects that look like a DirectiveLiteral
Instead, add a flag to nodeStr() that deals with the Flow node
accordingly. See https://github.com/prettier/prettier/pull/1560#discussion_r115605758
* Print preferred quotes around escaped DirectiveLiteral when it doesn't contain quotes
See https://github.com/prettier/prettier/pull/1560#discussion_r115606122
* Simplify `canChangeDirectiveQuotes` logic
* Add directive test with unnecessarily escaped non-quote character
* Fix boolean logic error
I thought that this would result in the following if-block executing, as
needed to pass the test case in the previous commit. However, it appears
that it's not actually needed to pass the test case, since `makeString`
doesn't unescape unnecessarily escaped non-quote characters.
Nevertheless, I think we should leave that if-block (`if (canChangeDirectiveQuotes)`)
there, in case `makeString` is updated.
See https://github.com/prettier/prettier/pull/1571#discussion_r115658398
* Make isFlowDirectiveLiteral a separate argument to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115810988
* Simplify isFlowDirectiveLiteral logic by passing n.expression to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115811216
2017-05-10 22:15:27 +03:00
|
|
|
|
// Prevent strings from being parsed as directives
|
|
|
|
|
// See https://github.com/prettier/prettier/pull/1560#issue-227225960
|
|
|
|
|
0;
|
|
|
|
|
|
2017-04-12 23:41:51 +03:00
|
|
|
|
// Every string will be changed to double quotes, unless we end up with fewer
|
|
|
|
|
// escaped quotes by using single quotes. (Vice versa if the "singleQuote"
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// option is true).
|
|
|
|
|
//
|
|
|
|
|
// Note that even if a string already has the correct enclosing quotes, it is
|
|
|
|
|
// still processed in order to remove unnecessarily escaped quotes within it,
|
|
|
|
|
// for consistency.
|
|
|
|
|
|
|
|
|
|
// Simple strings.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"abc"
|
2017-02-22 00:59:19 +03:00
|
|
|
|
'abc'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Escape.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
'\\0'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Emoji.
|
2017-02-22 00:59:19 +03:00
|
|
|
|
'🐶'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Empty string.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
""
|
2017-02-22 00:59:19 +03:00
|
|
|
|
''
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Single double quote.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\""
|
|
|
|
|
'"'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Single single quote.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"'"
|
|
|
|
|
'\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Unnecessary escapes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\'"
|
|
|
|
|
'\\"'
|
2017-05-11 02:02:49 +03:00
|
|
|
|
"\\a"
|
|
|
|
|
'\\a'
|
|
|
|
|
"hol\\a"
|
|
|
|
|
'hol\\a'
|
|
|
|
|
"hol\\\\a (the a is not escaped)"
|
|
|
|
|
'hol\\\\a (the a is not escaped)'
|
|
|
|
|
"multiple \\a unnecessary \\a escapes"
|
|
|
|
|
'multiple \\a unnecessary \\a escapes'
|
|
|
|
|
"unnecessarily escaped character preceded by escaped backslash \\\\\\a"
|
|
|
|
|
'unnecessarily escaped character preceded by escaped backslash \\\\\\a'
|
|
|
|
|
"unescaped character preceded by two escaped backslashes \\\\\\\\a"
|
|
|
|
|
'unescaped character preceded by two escaped backslashes \\\\\\\\a'
|
|
|
|
|
"\\a\\a" // consecutive unnecessarily escaped characters
|
|
|
|
|
'\\a\\a' // consecutive unnecessarily escaped characters
|
|
|
|
|
'escaped \\u2030 \\‰ (should not stay escaped)'
|
|
|
|
|
|
|
|
|
|
// Meaningful escapes
|
|
|
|
|
"octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7"
|
|
|
|
|
'octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7'
|
|
|
|
|
"meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61"
|
|
|
|
|
'meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61'
|
|
|
|
|
'escaped newline \\
|
|
|
|
|
'
|
|
|
|
|
'escaped carriage return \\
|
|
|
|
|
'
|
|
|
|
|
'escaped \\u2028 \\
'
|
|
|
|
|
'escaped \\u2029 \\
'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// One of each.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"'"
|
|
|
|
|
'"\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// One of each with unnecessary escapes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"\\'"
|
|
|
|
|
'\\"\\''
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// More double quotes than single quotes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"'\\""
|
|
|
|
|
'"\\'"'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// More single quotes than double quotes.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"''"
|
|
|
|
|
'"\\'\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Two of each.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"\\"''"
|
|
|
|
|
'""\\'\\''
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Single backslash.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
'\\\\'
|
|
|
|
|
"\\\\"
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Backslases.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"\\"\\\\\\"\\\\\\\\\\" '\\'\\\\'\\\\\\'\\\\\\\\'"
|
|
|
|
|
'\\'\\\\\\'\\\\\\\\\\' "\\"\\\\"\\\\\\"\\\\\\\\"'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Somewhat more real-word example.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"He's sayin': \\"How's it goin'?\\" Don't ask me why."
|
|
|
|
|
'He\\'s sayin\\': "How\\'s it goin\\'?" Don\\'t ask me why.'
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Somewhat more real-word example 2.
|
2017-04-12 23:41:51 +03:00
|
|
|
|
"var backslash = \\"\\\\\\", doubleQuote = '\\"';"
|
|
|
|
|
'var backslash = "\\\\", doubleQuote = \\'"\\';'
|
2017-01-13 09:30:35 +03:00
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
Preserve code unit sequence of directive literals (#1571)
* Print directive literals verbatim
This addresses https://github.com/prettier/prettier/issues/1555,
but doesn't seem to pass the AST_COMPARE=1 tests:
AST_COMPARE=1 npm test -- tests/quotes -t strings
However, running `prettier --debug-check` on the relevant file *does*
work:
prettier tests/quotes/strings.js --debug-check
* Change directive literal quotes if it doesn't contain quotes
This addresses https://github.com/prettier/prettier/pull/1560#discussion_r115396257
From https://github.com/prettier/prettier/issues/1555#issue-227206837:
> It's okay to change the type of quotation marks used, but only if
doing so does not require changing any characters within the directive.
* Don't change directive literal quotes if it contains a backslash
This passes the `--debug-check` tests again:
prettier tests/quotes/strings.js --debug-check
* Try to add regression test for escaped directive literals
This seems not to work, despite the following command having the correct
output:
echo "'\''" | prettier
You can use the following to get an idea of how flow/typescript parse
this:
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'flow'}), null, 2)"
node -p "JSON.stringify(require('./src/parser').parse('\\'\\\\\'\\'', {parser: 'typescript'}), null, 2)"
* WIP Disable Flow/Typescript for ./tests/directives
We don't yet handle escaped directives for them, but Babylon works.
(similar to https://github.com/prettier/prettier/pull/602/commits/90bf93713c78a6a6b3f55e52d7be172ece9b56df#diff-0de18284f37da79ab8af4e4690919abaR1)
* Revert "WIP Disable Flow/Typescript for ./tests/directives"
This reverts commit 2aba6231271f6985a395c31e3df9323e8f3da115.
* Prevent test strings from being parsed as directives
See https://github.com/prettier/prettier/pull/1560#issue-227225960
* Add more escaped directive tests
* Infer DirectiveLiterals from Flow parser
* Don't test TypeScript on directives
See https://github.com/prettier/prettier/pull/1560#issuecomment-300296221
* fixup! Infer DirectiveLiterals from Flow parser
* Don't fake objects that look like a DirectiveLiteral
Instead, add a flag to nodeStr() that deals with the Flow node
accordingly. See https://github.com/prettier/prettier/pull/1560#discussion_r115605758
* Print preferred quotes around escaped DirectiveLiteral when it doesn't contain quotes
See https://github.com/prettier/prettier/pull/1560#discussion_r115606122
* Simplify `canChangeDirectiveQuotes` logic
* Add directive test with unnecessarily escaped non-quote character
* Fix boolean logic error
I thought that this would result in the following if-block executing, as
needed to pass the test case in the previous commit. However, it appears
that it's not actually needed to pass the test case, since `makeString`
doesn't unescape unnecessarily escaped non-quote characters.
Nevertheless, I think we should leave that if-block (`if (canChangeDirectiveQuotes)`)
there, in case `makeString` is updated.
See https://github.com/prettier/prettier/pull/1571#discussion_r115658398
* Make isFlowDirectiveLiteral a separate argument to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115810988
* Simplify isFlowDirectiveLiteral logic by passing n.expression to nodeStr()
See https://github.com/prettier/prettier/pull/1571#discussion_r115811216
2017-05-10 22:15:27 +03:00
|
|
|
|
// Prevent strings from being parsed as directives
|
|
|
|
|
// See https://github.com/prettier/prettier/pull/1560#issue-227225960
|
|
|
|
|
0;
|
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Every string will be changed to double quotes, unless we end up with fewer
|
2017-04-12 23:41:51 +03:00
|
|
|
|
// escaped quotes by using single quotes. (Vice versa if the "singleQuote"
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// option is true).
|
|
|
|
|
//
|
|
|
|
|
// Note that even if a string already has the correct enclosing quotes, it is
|
|
|
|
|
// still processed in order to remove unnecessarily escaped quotes within it,
|
|
|
|
|
// for consistency.
|
|
|
|
|
|
|
|
|
|
// Simple strings.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('abc');
|
|
|
|
|
('abc');
|
2017-01-13 09:30:35 +03:00
|
|
|
|
|
2017-01-22 23:32:43 +03:00
|
|
|
|
// Escape.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('\\0');
|
2017-01-22 23:32:43 +03:00
|
|
|
|
|
|
|
|
|
// Emoji.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('🐶');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Empty string.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('');
|
|
|
|
|
('');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Single double quote.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('"');
|
|
|
|
|
('"');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Single single quote.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("'");
|
|
|
|
|
("'");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Unnecessary escapes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("'");
|
|
|
|
|
('"');
|
|
|
|
|
('a');
|
|
|
|
|
('a');
|
|
|
|
|
('hola');
|
|
|
|
|
('hola');
|
|
|
|
|
('hol\\\\a (the a is not escaped)');
|
|
|
|
|
('hol\\\\a (the a is not escaped)');
|
|
|
|
|
('multiple a unnecessary a escapes');
|
|
|
|
|
('multiple a unnecessary a escapes');
|
|
|
|
|
('unnecessarily escaped character preceded by escaped backslash \\\\a');
|
|
|
|
|
('unnecessarily escaped character preceded by escaped backslash \\\\a');
|
|
|
|
|
('unescaped character preceded by two escaped backslashes \\\\\\\\a');
|
|
|
|
|
('unescaped character preceded by two escaped backslashes \\\\\\\\a');
|
|
|
|
|
('aa'); // consecutive unnecessarily escaped characters
|
|
|
|
|
('aa'); // consecutive unnecessarily escaped characters
|
|
|
|
|
('escaped \\u2030 ‰ (should not stay escaped)');
|
2017-05-11 02:02:49 +03:00
|
|
|
|
// Meaningful escapes
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7');
|
|
|
|
|
('octal escapes \\0 \\1 \\2 \\3 \\4 \\5 \\6 \\7');
|
|
|
|
|
('meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61');
|
|
|
|
|
('meaningfully escaped alphabetical characters \\n \\r \\v \\t \\b \\f \\u2713 \\x61');
|
|
|
|
|
('escaped newline \\
|
|
|
|
|
');
|
|
|
|
|
('escaped carriage return \\
|
|
|
|
|
');
|
|
|
|
|
('escaped \\u2028 \\
');
|
|
|
|
|
('escaped \\u2029 \\
');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// One of each.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('"\\'');
|
|
|
|
|
('"\\'');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// One of each with unnecessary escapes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('"\\'');
|
|
|
|
|
('"\\'');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// More double quotes than single quotes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('"\\'"');
|
|
|
|
|
('"\\'"');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// More single quotes than double quotes.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"''");
|
|
|
|
|
("\\"''");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Two of each.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('""\\'\\'');
|
|
|
|
|
('""\\'\\'');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Single backslash.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('\\\\');
|
|
|
|
|
('\\\\');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Backslases.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("\\"\\\\\\"\\\\\\\\\\" ''\\\\'\\\\'\\\\\\\\'");
|
|
|
|
|
('\\'\\\\\\'\\\\\\\\\\' ""\\\\"\\\\"\\\\\\\\"');
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Somewhat more real-word example.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
("He's sayin': \\"How's it goin'?\\" Don't ask me why.");
|
|
|
|
|
("He's sayin': \\"How's it goin'?\\" Don't ask me why.");
|
2017-04-28 00:59:18 +03:00
|
|
|
|
// Somewhat more real-word example 2.
|
2017-06-06 18:24:13 +03:00
|
|
|
|
('var backslash = "\\\\", doubleQuote = \\'"\\';');
|
|
|
|
|
('var backslash = "\\\\", doubleQuote = \\'"\\';');
|
2017-04-12 23:41:51 +03:00
|
|
|
|
|
2017-01-13 09:30:35 +03:00
|
|
|
|
`;
|