From ec18ebc6c667cd40ee044a8b1a4e9824a3d47221 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Sat, 4 Nov 2017 11:04:45 -0700 Subject: [PATCH] [Graphql] Escape strings (#3143) When I ran it on all the graphql files of fb, none of them had escape, but someone reported this issue! --- src/printer-graphql.js | 2 +- tests/graphql_string/__snapshots__/jsfmt.spec.js.snap | 10 ++++++++++ tests/graphql_string/jsfmt.spec.js | 1 + tests/graphql_string/string.graphql | 1 + 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 tests/graphql_string/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/graphql_string/jsfmt.spec.js create mode 100644 tests/graphql_string/string.graphql diff --git a/src/printer-graphql.js b/src/printer-graphql.js index efaab7cf0..b5fbd6f27 100644 --- a/src/printer-graphql.js +++ b/src/printer-graphql.js @@ -108,7 +108,7 @@ function genericPrint(path, options, print) { return n.value; } case "StringValue": { - return concat(['"', n.value, '"']); + return concat(['"', n.value.replace(/["\\]/g, "\\$&"), '"']); } case "IntValue": case "FloatValue": diff --git a/tests/graphql_string/__snapshots__/jsfmt.spec.js.snap b/tests/graphql_string/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..5b76c1643 --- /dev/null +++ b/tests/graphql_string/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,10 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`string.graphql 1`] = ` +query X($a: Int) @relay(meta: "{\\"lowPri\\": true}") { a } +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +query X($a: Int) @relay(meta: "{\\"lowPri\\": true}") { + a +} + +`; diff --git a/tests/graphql_string/jsfmt.spec.js b/tests/graphql_string/jsfmt.spec.js new file mode 100644 index 000000000..335646fb2 --- /dev/null +++ b/tests/graphql_string/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "graphql" }); diff --git a/tests/graphql_string/string.graphql b/tests/graphql_string/string.graphql new file mode 100644 index 000000000..b33cefef1 --- /dev/null +++ b/tests/graphql_string/string.graphql @@ -0,0 +1 @@ +query X($a: Int) @relay(meta: "{\"lowPri\": true}") { a }