Skip to content

Commit

Permalink
Add jsonCompatibleStrings option to generator (babel#4827)
Browse files Browse the repository at this point in the history
  • Loading branch information
kangax authored and panagosg7 committed Jan 17, 2017
1 parent 0904e84 commit 40d1793
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/babel-generator/src/generators/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,14 @@ export function StringLiteral(node: Object, parent: Object) {
}

// ensure the output is ASCII-safe
let val = jsesc(node.value, {
const opts = {
quotes: t.isJSX(parent) ? "double" : this.format.quotes,
wrap: true
});
};
if (this.format.jsonCompatibleStrings) {
opts.json = true;
}
let val = jsesc(node.value, opts);

return this.token(val);
}
1 change: 1 addition & 0 deletions packages/babel-generator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function normalizeOptions(code, opts, tokens): Format {
minified: opts.minified,
concise: opts.concise,
quotes: opts.quotes || findCommonStringDelimiter(code, tokens),
jsonCompatibleStrings: opts.jsonCompatibleStrings,
indent: {
adjustMultilineComment: true,
style: style,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"©";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"\u00A9";
4 changes: 4 additions & 0 deletions packages/babel-generator/test/fixtures/escapes/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"minified": true,
"jsonCompatibleStrings": true
}

0 comments on commit 40d1793

Please sign in to comment.