* 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
74 lines
1.3 KiB
JavaScript
74 lines
1.3 KiB
JavaScript
// Prevent strings from being parsed as directives
|
|
// See https://github.com/prettier/prettier/pull/1560#issue-227225960
|
|
0;
|
|
|
|
// 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"
|
|
// 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.
|
|
"abc"
|
|
'abc'
|
|
|
|
// Escape.
|
|
'\0'
|
|
|
|
// Emoji.
|
|
'🐶'
|
|
|
|
// Empty string.
|
|
""
|
|
''
|
|
|
|
// Single double quote.
|
|
"\""
|
|
'"'
|
|
|
|
// Single single quote.
|
|
"'"
|
|
'\''
|
|
|
|
// Unnecessary escapes.
|
|
"\'"
|
|
'\"'
|
|
|
|
// One of each.
|
|
"\"'"
|
|
'"\''
|
|
|
|
// One of each with unnecessary escapes.
|
|
"\"\'"
|
|
'\"\''
|
|
|
|
// More double quotes than single quotes.
|
|
"\"'\""
|
|
'"\'"'
|
|
|
|
// More single quotes than double quotes.
|
|
"\"''"
|
|
'"\'\''
|
|
|
|
// Two of each.
|
|
"\"\"''"
|
|
'""\'\''
|
|
|
|
// Single backslash.
|
|
'\\'
|
|
"\\"
|
|
|
|
// Backslases.
|
|
"\"\\\"\\\\\" '\'\\'\\\'\\\\'"
|
|
'\'\\\'\\\\\' "\"\\"\\\"\\\\"'
|
|
|
|
// Somewhat more real-word example.
|
|
"He's sayin': \"How's it goin'?\" Don't ask me why."
|
|
'He\'s sayin\': "How\'s it goin\'?" Don\'t ask me why.'
|
|
|
|
// Somewhat more real-word example 2.
|
|
"var backslash = \"\\\", doubleQuote = '\"';"
|
|
'var backslash = "\\", doubleQuote = \'"\';'
|