Skip to content

Commit

Permalink
[babel 8] Output minimal strings by default (#12675)
Browse files Browse the repository at this point in the history
Co-authored-by: Justin Ridgewell <justin@ridgewell.name>
Co-authored-by: Justin Ridgewell <jridgewell@google.com>
  • Loading branch information
3 people committed Jan 23, 2021
1 parent 464a02f commit cd090e9
Show file tree
Hide file tree
Showing 37 changed files with 231 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/babel-generator/package.json
Expand Up @@ -20,7 +20,7 @@
],
"dependencies": {
"@babel/types": "workspace:^7.12.11",
"jsesc": "^2.5.1",
"jsesc": "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1",
"source-map": "^0.5.0"
},
"devDependencies": {
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-generator/src/generators/types.js
Expand Up @@ -203,8 +203,6 @@ export function StringLiteral(node: Object) {
return;
}

// ensure the output is ASCII-safe

const val = jsesc(
node.value,
process.env.BABEL_8_BREAKING
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-generator/src/index.js
Expand Up @@ -55,13 +55,15 @@ function normalizeOptions(code, opts): Format {
jsescOption: {
quotes: "double",
wrap: true,
minimal: true,
...opts.jsescOption,
},
recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType,
};

if (!process.env.BABEL_8_BREAKING) {
format.jsonCompatibleStrings = opts.jsonCompatibleStrings;
delete format.jsescOption.minimal;
}

if (format.minified) {
Expand Down
@@ -1,2 +1,2 @@
0;// Not a directive
"\u00A9";
"";
@@ -0,0 +1,18 @@
5;
5.0;
5e1;
5e4;
"foobar";
'\x20';
"\n\r";
"馃槀";
"\uD83D_\uDE02"
`馃槀`;
/foobar/g;
null;
true;
false;
5.;
0b10;
0o70;
0X1F;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1 @@
5;5;50;5e4;"foobar";" ";"\n\r";"\uD83D\uDE02";"\uD83D_\uDE02"`馃槀`;/foobar/g;null;true;false;5;2;56;31;
Expand Up @@ -6,6 +6,7 @@
'\x20';
"\n\r";
"馃槀";
"\uD83D_\uDE02"
`馃槀`;
/foobar/g;
null;
Expand Down
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
@@ -1 +1 @@
5;5;50;5e4;"foobar";" ";"\n\r";"\uD83D\uDE02";`馃槀`;/foobar/g;null;true;false;5;2;56;31;
5;5;50;5e4;"foobar";" ";"\n\r";"馃槀";"\uD83D_\uDE02"`馃槀`;/foobar/g;null;true;false;5;2;56;31;
@@ -0,0 +1,3 @@
<div id="w么w" />;
<div id="\w" />;
<div id="w &lt; w" />;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,16 @@
import { jsx as _jsx } from "react/jsx-runtime";

/*#__PURE__*/
_jsx("div", {
id: "w\xF4w"
});

/*#__PURE__*/
_jsx("div", {
id: "\\w"
});

/*#__PURE__*/
_jsx("div", {
id: "w < w"
});
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Expand Up @@ -2,7 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";

/*#__PURE__*/
_jsx("div", {
id: "w\xF4w"
id: "w么w"
});

/*#__PURE__*/
Expand Down
@@ -0,0 +1,12 @@
<div>wow</div>;
<div>w么w</div>;

<div>w & w</div>;
<div>w &amp; w</div>;

<div>w &nbsp; w</div>;
<div>this should not parse as unicode: \u00a0</div>;
<div>this should parse as nbsp: 聽 </div>;
<div>this should parse as unicode: {'\u00a0聽'}</div>;

<div>w &lt; w</div>;
@@ -0,0 +1 @@
{ "BABEL_8_BREAKING": false }
@@ -0,0 +1,47 @@
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";

/*#__PURE__*/
_jsx("div", {
children: "wow"
});

/*#__PURE__*/
_jsx("div", {
children: "w\xF4w"
});

/*#__PURE__*/
_jsx("div", {
children: "w & w"
});

/*#__PURE__*/
_jsx("div", {
children: "w & w"
});

/*#__PURE__*/
_jsx("div", {
children: "w \xA0 w"
});

/*#__PURE__*/
_jsx("div", {
children: "this should not parse as unicode: \\u00a0"
});

/*#__PURE__*/
_jsx("div", {
children: "this should parse as nbsp: \xA0 "
});

/*#__PURE__*/
_jsxs("div", {
children: ["this should parse as unicode: ", '\u00a0聽']
});

/*#__PURE__*/
_jsx("div", {
children: "w < w"
});
@@ -0,0 +1 @@
{ "BABEL_8_BREAKING": true }
Expand Up @@ -8,7 +8,7 @@ _jsx("div", {

/*#__PURE__*/
_jsx("div", {
children: "w\xF4w"
children: "w么w"
});

/*#__PURE__*/
Expand Down
@@ -0,0 +1,3 @@
<div id="w么w" />;
<div id="\w" />;
<div id="w &lt; w" />;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,14 @@
/*#__PURE__*/
React.createElement("div", {
id: "w\xF4w"
});

/*#__PURE__*/
React.createElement("div", {
id: "\\w"
});

/*#__PURE__*/
React.createElement("div", {
id: "w < w"
});
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
@@ -1,6 +1,6 @@
/*#__PURE__*/
React.createElement("div", {
id: "w\xF4w"
id: "w么w"
});

/*#__PURE__*/
Expand Down
@@ -0,0 +1,12 @@
<div>wow</div>;
<div>w么w</div>;

<div>w & w</div>;
<div>w &amp; w</div>;

<div>w &nbsp; w</div>;
<div>this should not parse as unicode: \u00a0</div>;
<div>this should parse as nbsp: 聽 </div>;
<div>this should parse as unicode: {'\u00a0聽'}</div>;

<div>w &lt; w</div>;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,26 @@
/*#__PURE__*/
React.createElement("div", null, "wow");

/*#__PURE__*/
React.createElement("div", null, "w\xF4w");

/*#__PURE__*/
React.createElement("div", null, "w & w");

/*#__PURE__*/
React.createElement("div", null, "w & w");

/*#__PURE__*/
React.createElement("div", null, "w \xA0 w");

/*#__PURE__*/
React.createElement("div", null, "this should not parse as unicode: \\u00a0");

/*#__PURE__*/
React.createElement("div", null, "this should parse as nbsp: \xA0 ");

/*#__PURE__*/
React.createElement("div", null, "this should parse as unicode: ", '\u00a0聽');

/*#__PURE__*/
React.createElement("div", null, "w < w");
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Expand Up @@ -2,7 +2,7 @@
React.createElement("div", null, "wow");

/*#__PURE__*/
React.createElement("div", null, "w\xF4w");
React.createElement("div", null, "w么w");

/*#__PURE__*/
React.createElement("div", null, "w & w");
Expand Down
@@ -0,0 +1 @@
var foo = bar`\u0061\u{0061}\ud835\udc9c\u{1d49c}`;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,5 @@
var _templateObject;

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Expand Up @@ -2,4 +2,4 @@ var _templateObject;

function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }

var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa\uD835\uDC9C\uD835\uDC9C"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
var foo = bar(_templateObject || (_templateObject = _taggedTemplateLiteral(["aa饾挏饾挏"], ["\\u0061\\u{0061}\\ud835\\udc9c\\u{1d49c}"])));
9 changes: 9 additions & 0 deletions packages/babel-plugin-transform-unicode-escapes/src/index.js
Expand Up @@ -42,6 +42,15 @@ export default declare(api => {

return {
name: "transform-unicode-escapes",
manipulateOptions({ generatorOpts }) {
// Babel 8 will enable jsesc minimal mode by default, which outputs
// unescaped unicode string
if (!generatorOpts.jsescOption) {
generatorOpts.jsescOption = {};
}
// $FlowIgnore: Flow does not support logical assignment
generatorOpts.jsescOption.minimal ??= false;
},
visitor: {
Identifier(path) {
const { node, key } = path;
Expand Down
23 changes: 21 additions & 2 deletions yarn.lock
Expand Up @@ -251,7 +251,7 @@ __metadata:
"@babel/helper-fixtures": "workspace:*"
"@babel/parser": "workspace:*"
"@babel/types": "workspace:^7.12.11"
jsesc: ^2.5.1
jsesc: "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1"
source-map: ^0.5.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -9210,7 +9210,7 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"jsesc@npm:^2.5.1":
"jsesc-BABEL_8_BREAKING-false@npm:jsesc@^2.5.1, jsesc@npm:^2.5.1":
version: 2.5.2
resolution: "jsesc@npm:2.5.2"
bin:
Expand All @@ -9219,6 +9219,25 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"jsesc-BABEL_8_BREAKING-true@npm:jsesc@^3.0.2":
version: 3.0.2
resolution: "jsesc@npm:3.0.2"
bin:
jsesc: bin/jsesc
checksum: 809c7663241f76c42528e2706f011d43c0eaa19b50a5d6f4015b4b9563292c0ffad1f5b12a9a41b71e2206efc5ae3632e8b27c35cb3ef508c0a58a3bb011da90
languageName: node
linkType: hard

"jsesc@condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1":
version: 0.0.0-condition-31d9b4
resolution: "jsesc@condition:BABEL_8_BREAKING?^3.0.2:^2.5.1#31d9b4"
dependencies:
jsesc-BABEL_8_BREAKING-false: "npm:jsesc@^2.5.1"
jsesc-BABEL_8_BREAKING-true: "npm:jsesc@^3.0.2"
checksum: d6751e397e85958087fffcb6fbe5892902d2455e98ff0d69b69d0b15103892465d60aa0d750199a5d45cca75bbd89c4854b80ef4cbb98b229e3e9fa4e567d968
languageName: node
linkType: hard

"jsesc@npm:~0.5.0":
version: 0.5.0
resolution: "jsesc@npm:0.5.0"
Expand Down

0 comments on commit cd090e9

Please sign in to comment.