Skip to content

Commit

Permalink
Fix test with unnecessarily escaped non-quote character
Browse files Browse the repository at this point in the history
Unfortunately, this breaks a couple of other tests...
  • Loading branch information
josephfrazier committed May 10, 2017
1 parent ebe3ef4 commit d056525
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"get-stdin": "5.0.1",
"glob": "7.1.1",
"jest-validate": "20.0.0",
"jsesc": "2.5.1",
"minimist": "1.2.0"
},
"devDependencies": {
Expand Down
40 changes: 8 additions & 32 deletions src/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var comments = require("./comments");
var FastPath = require("./fast-path");
var util = require("./util");
var isIdentifierName = require("esutils").keyword.isIdentifierNameES6;
var jsesc = require('jsesc');

var docBuilders = require("./doc-builders");
var concat = docBuilders.concat;
Expand Down Expand Up @@ -3845,42 +3846,17 @@ function nodeStr(node, options) {
const enclosingQuote = shouldUseAlternateQuote
? alternate.quote
: preferred.quote;
const quoteType = enclosingQuote === '"' ? "double" : "single";

// It might sound unnecessary to use `makeString` even if `node.raw` already
// It might sound unnecessary to use `jsesc` even if `node.raw` already
// is enclosed with `enclosingQuote`, but it isn't. `node.raw` could contain
// unnecessary escapes (such as in `"\'"`). Always using `makeString` makes
// unnecessary escapes (such as in `"\'"`). Always using `jsesc` makes
// sure that we consistently output the minimum amount of escaped quotes.
return makeString(rawContent, enclosingQuote);
}

function makeString(rawContent, enclosingQuote) {
const otherQuote = enclosingQuote === '"' ? "'" : '"';

// Matches _any_ escape and unescaped quotes (both single and double).
const regex = /\\([\s\S])|(['"])/g;

// Escape and unescape single and double quotes as needed to be able to
// enclose `rawContent` with `enclosingQuote`.
const newContent = rawContent.replace(regex, (match, escaped, quote) => {
// If we matched an escape, and the escaped character is a quote of the
// other type than we intend to enclose the string with, there's no need for
// it to be escaped, so return it _without_ the backslash.
if (escaped === otherQuote) {
return escaped;
}

// If we matched an unescaped quote and it is of the _same_ type as we
// intend to enclose the string with, it must be escaped, so return it with
// a backslash.
if (quote === enclosingQuote) {
return "\\" + quote;
}

// Otherwise return the escape or unescaped quote as-is.
return match;
return jsesc(str, {
quotes: quoteType,
wrap: true,
minimal: true
});

return enclosingQuote + newContent + enclosingQuote;
}

function printRegex(node) {
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,10 @@ jsesc@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-1.3.0.tgz#46c3fec8c1892b12b0833db9bc7622176dbab34b"

jsesc@^2.5.1:
version "2.5.1"
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.1.tgz#e421a2a8e20d6b0819df28908f782526b96dd1fe"

json-schema@0.2.3:
version "0.2.3"
resolved "https://registry.yarnpkg.com/json-schema/-/json-schema-0.2.3.tgz#b480c892e59a2f05954ce727bd3f2a4e882f9e13"
Expand Down

0 comments on commit d056525

Please sign in to comment.