Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f63d73c

Browse files
Allow putting helpers in individual files (#13190)
* Allow putting helpers in individual files * Lint * Also move `objectSpread2` (to show an helper with deps) * Update fixture and package.json * fixture
1 parent 66181db commit f63d73c

File tree

15 files changed

+304
-172
lines changed

15 files changed

+304
-172
lines changed

.eslintrc.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ module.exports = {
111111
],
112112
},
113113
},
114+
{
115+
files: ["packages/babel-helpers/src/helpers/**.js"],
116+
rules: {
117+
"no-var": "off",
118+
"comma-dangle": "off",
119+
},
120+
},
114121
{
115122
files: ["packages/babel-traverse/scripts/**/*.js"],
116123
rules: {

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
"files": [
1313
"**/{codemods,eslint,packages}/*/{src,test}/**/*.{js,ts}"
1414
],
15+
"excludeFiles": ["**/packages/babel-helpers/src/helpers/**/*.js"],
1516
"options": {
1617
"trailingComma": "all"
1718
}
19+
}, {
20+
"files": "**/packages/babel-helpers/src/helpers/**/*.js",
21+
"options": {
22+
"trailingComma": "es5"
23+
}
1824
}, {
1925
"files": [
2026
"**/{codemods,eslint,packages}/*/test/fixtures/**/*.{js,ts}"

Gulpfile.mjs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ function generateHelpers(generator, dest, filename, message) {
108108

109109
file.path = filename;
110110
file.contents = Buffer.from(
111-
formatCode(generateCode(filename), dest + file.path)
111+
formatCode(await generateCode(filename), dest + file.path)
112112
);
113113
fancyLog(`${chalk.green("✔")} Generated ${message}`);
114114
callback(null, file);
@@ -148,6 +148,15 @@ async function generateTraverseHelpers(helperKind) {
148148
);
149149
}
150150

151+
async function generateRuntimeHelpers() {
152+
return generateHelpers(
153+
`./packages/babel-helpers/scripts/generate-helpers.js`,
154+
`./packages/babel-helpers/src/`,
155+
"helpers-generated.js",
156+
"@babel/helpers"
157+
);
158+
}
159+
151160
function generateStandalone() {
152161
const dest = "./packages/babel-standalone/src/generated/";
153162
return gulp
@@ -487,6 +496,12 @@ gulp.task("generate-type-helpers", () => {
487496
]);
488497
});
489498

499+
gulp.task("generate-runtime-helpers", () => {
500+
fancyLog("Generating @babel/helpers runtime helpers");
501+
502+
return generateRuntimeHelpers();
503+
});
504+
490505
gulp.task("generate-standalone", () => generateStandalone());
491506

492507
gulp.task("build-rollup", () => buildRollup(libBundles));
@@ -508,7 +523,7 @@ gulp.task("build-babel", () => buildBabel(/* exclude */ libBundles));
508523
gulp.task(
509524
"build",
510525
gulp.series(
511-
gulp.parallel("build-rollup", "build-babel"),
526+
gulp.parallel("build-rollup", "build-babel", "generate-runtime-helpers"),
512527
gulp.parallel(
513528
"generate-standalone",
514529
gulp.series(
@@ -548,5 +563,9 @@ gulp.task(
548563
gulp.task("generate-standalone")
549564
);
550565
gulp.watch(buildTypingsWatchGlob, gulp.task("generate-type-helpers"));
566+
gulp.watch(
567+
"./packages/babel-helpers/src/helpers/*.js",
568+
gulp.task("generate-runtime-helpers")
569+
);
551570
})
552571
);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import fs from "fs";
2+
import { join } from "path";
3+
import { URL } from "url";
4+
5+
const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url);
6+
const IGNORED_FILES = new Set(["package.json"]);
7+
8+
export default async function generateAsserts() {
9+
let output = `/*
10+
* This file is auto-generated! Do not modify it directly.
11+
* To re-generate run 'make build'
12+
*/
13+
14+
import template from "@babel/template";
15+
16+
`;
17+
18+
for (const file of (await fs.promises.readdir(HELPERS_FOLDER)).sort()) {
19+
if (IGNORED_FILES.has(file)) continue;
20+
21+
const [helperName] = file.split(".");
22+
const isValidId = isValidBindingIdentifier(helperName);
23+
const varName = isValidId ? helperName : `_${helperName}`;
24+
25+
const fileContents = await fs.promises.readFile(
26+
join(HELPERS_FOLDER.pathname, file),
27+
"utf8"
28+
);
29+
const { minVersion } = fileContents.match(
30+
/^\s*\/\*\s*@minVersion\s+(?<minVersion>\S+)\s*\*\/\s*$/m
31+
).groups;
32+
33+
// TODO: We can minify the helpers in production
34+
const source = fileContents
35+
// Remove comments
36+
.replace(/\/\*[^]*?\*\/|\/\/.*/g, "")
37+
// Remove multiple newlines
38+
.replace(/\n{2,}/g, "\n");
39+
40+
const intro = isValidId
41+
? "export "
42+
: `export { ${varName} as ${helperName} }\n`;
43+
44+
output += `\n${intro}const ${varName} = {
45+
minVersion: ${JSON.stringify(minVersion)},
46+
ast: () => template.program.ast(${JSON.stringify(source)})
47+
};\n`;
48+
}
49+
50+
return output;
51+
}
52+
53+
function isValidBindingIdentifier(name) {
54+
try {
55+
Function(`var ${name}`);
56+
return true;
57+
} catch {
58+
return false;
59+
}
60+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is auto-generated! Do not modify it directly.
3+
* To re-generate run 'make build'
4+
*/
5+
6+
import template from "@babel/template";
7+
8+
export const jsx = {
9+
minVersion: "7.0.0-beta.0",
10+
ast: () =>
11+
template.program.ast(
12+
'\nvar REACT_ELEMENT_TYPE;\nexport default function _createRawReactElement(type, props, key, children) {\n if (!REACT_ELEMENT_TYPE) {\n REACT_ELEMENT_TYPE =\n (typeof Symbol === "function" &&\n \n Symbol["for"] &&\n Symbol["for"]("react.element")) ||\n 0xeac7;\n }\n var defaultProps = type && type.defaultProps;\n var childrenLength = arguments.length - 3;\n if (!props && childrenLength !== 0) {\n \n \n props = { children: void 0 };\n }\n if (childrenLength === 1) {\n props.children = children;\n } else if (childrenLength > 1) {\n var childArray = new Array(childrenLength);\n for (var i = 0; i < childrenLength; i++) {\n childArray[i] = arguments[i + 3];\n }\n props.children = childArray;\n }\n if (props && defaultProps) {\n for (var propName in defaultProps) {\n if (props[propName] === void 0) {\n props[propName] = defaultProps[propName];\n }\n }\n } else if (!props) {\n props = defaultProps || {};\n }\n return {\n $$typeof: REACT_ELEMENT_TYPE,\n type: type,\n key: key === undefined ? null : "" + key,\n ref: null,\n props: props,\n _owner: null,\n };\n}\n',
13+
),
14+
};
15+
16+
export const objectSpread2 = {
17+
minVersion: "7.5.0",
18+
ast: () =>
19+
template.program.ast(
20+
'\nimport defineProperty from "defineProperty";\nfunction ownKeys(object, enumerableOnly) {\n var keys = Object.keys(object);\n if (Object.getOwnPropertySymbols) {\n var symbols = Object.getOwnPropertySymbols(object);\n if (enumerableOnly) {\n symbols = symbols.filter(function (sym) {\n return Object.getOwnPropertyDescriptor(object, sym).enumerable;\n });\n }\n keys.push.apply(keys, symbols);\n }\n return keys;\n}\nexport default function _objectSpread2(target) {\n for (var i = 1; i < arguments.length; i++) {\n var source = arguments[i] != null ? arguments[i] : {};\n if (i % 2) {\n ownKeys(Object(source), true).forEach(function (key) {\n defineProperty(target, key, source[key]);\n });\n } else if (Object.getOwnPropertyDescriptors) {\n Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));\n } else {\n ownKeys(Object(source)).forEach(function (key) {\n Object.defineProperty(\n target,\n key,\n Object.getOwnPropertyDescriptor(source, key)\n );\n });\n }\n }\n return target;\n}\n',
21+
),
22+
};
23+
24+
export { _typeof as typeof };
25+
const _typeof = {
26+
minVersion: "7.0.0-beta.0",
27+
ast: () =>
28+
template.program.ast(
29+
'\nexport default function _typeof(obj) {\n "@babel/helpers - typeof";\n if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {\n _typeof = function (obj) {\n return typeof obj;\n };\n } else {\n _typeof = function (obj) {\n return obj &&\n typeof Symbol === "function" &&\n obj.constructor === Symbol &&\n obj !== Symbol.prototype\n ? "symbol"\n : typeof obj;\n };\n }\n return _typeof(obj);\n}\n',
30+
),
31+
};

packages/babel-helpers/src/helpers.js

Lines changed: 3 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,16 @@
22

33
import template from "@babel/template";
44

5-
const helpers = Object.create(null);
5+
import * as generated from "./helpers-generated";
6+
7+
const helpers = { __proto__: null, ...generated };
68
export default helpers;
79

810
const helper = (minVersion: string) => tpl => ({
911
minVersion,
1012
ast: () => template.program.ast(tpl),
1113
});
1214

13-
helpers.typeof = helper("7.0.0-beta.0")`
14-
export default function _typeof(obj) {
15-
"@babel/helpers - typeof";
16-
17-
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
18-
_typeof = function (obj) { return typeof obj; };
19-
} else {
20-
_typeof = function (obj) {
21-
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype
22-
? "symbol"
23-
: typeof obj;
24-
};
25-
}
26-
27-
return _typeof(obj);
28-
}
29-
`;
30-
31-
// "for" is a reserved keyword in ES3 so escaping it here for backward compatibility
32-
helpers.jsx = helper("7.0.0-beta.0")`
33-
var REACT_ELEMENT_TYPE;
34-
35-
export default function _createRawReactElement(type, props, key, children) {
36-
if (!REACT_ELEMENT_TYPE) {
37-
REACT_ELEMENT_TYPE = (
38-
typeof Symbol === "function" && Symbol["for"] && Symbol["for"]("react.element")
39-
) || 0xeac7;
40-
}
41-
42-
var defaultProps = type && type.defaultProps;
43-
var childrenLength = arguments.length - 3;
44-
45-
if (!props && childrenLength !== 0) {
46-
// If we're going to assign props.children, we create a new object now
47-
// to avoid mutating defaultProps.
48-
props = {
49-
children: void 0,
50-
};
51-
}
52-
53-
if (childrenLength === 1) {
54-
props.children = children;
55-
} else if (childrenLength > 1) {
56-
var childArray = new Array(childrenLength);
57-
for (var i = 0; i < childrenLength; i++) {
58-
childArray[i] = arguments[i + 3];
59-
}
60-
props.children = childArray;
61-
}
62-
63-
if (props && defaultProps) {
64-
for (var propName in defaultProps) {
65-
if (props[propName] === void 0) {
66-
props[propName] = defaultProps[propName];
67-
}
68-
}
69-
} else if (!props) {
70-
props = defaultProps || {};
71-
}
72-
73-
return {
74-
$$typeof: REACT_ELEMENT_TYPE,
75-
type: type,
76-
key: key === undefined ? null : '' + key,
77-
ref: null,
78-
props: props,
79-
_owner: null,
80-
};
81-
}
82-
`;
83-
8415
helpers.asyncIterator = helper("7.0.0-beta.0")`
8516
export default function _asyncIterator(iterable) {
8617
var method;
@@ -406,48 +337,6 @@ helpers.objectSpread = helper("7.0.0-beta.0")`
406337
}
407338
`;
408339

409-
helpers.objectSpread2 = helper("7.5.0")`
410-
import defineProperty from "defineProperty";
411-
412-
// This function is different to "Reflect.ownKeys". The enumerableOnly
413-
// filters on symbol properties only. Returned string properties are always
414-
// enumerable. It is good to use in objectSpread.
415-
416-
function ownKeys(object, enumerableOnly) {
417-
var keys = Object.keys(object);
418-
if (Object.getOwnPropertySymbols) {
419-
var symbols = Object.getOwnPropertySymbols(object);
420-
if (enumerableOnly) symbols = symbols.filter(function (sym) {
421-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
422-
});
423-
keys.push.apply(keys, symbols);
424-
}
425-
return keys;
426-
}
427-
428-
export default function _objectSpread2(target) {
429-
for (var i = 1; i < arguments.length; i++) {
430-
var source = (arguments[i] != null) ? arguments[i] : {};
431-
if (i % 2) {
432-
ownKeys(Object(source), true).forEach(function (key) {
433-
defineProperty(target, key, source[key]);
434-
});
435-
} else if (Object.getOwnPropertyDescriptors) {
436-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
437-
} else {
438-
ownKeys(Object(source)).forEach(function (key) {
439-
Object.defineProperty(
440-
target,
441-
key,
442-
Object.getOwnPropertyDescriptor(source, key)
443-
);
444-
});
445-
}
446-
}
447-
return target;
448-
}
449-
`;
450-
451340
helpers.inherits = helper("7.0.0-beta.0")`
452341
import setPrototypeOf from "setPrototypeOf";
453342
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/* @minVersion 7.0.0-beta.0 */
2+
3+
var REACT_ELEMENT_TYPE;
4+
5+
export default function _createRawReactElement(type, props, key, children) {
6+
if (!REACT_ELEMENT_TYPE) {
7+
REACT_ELEMENT_TYPE =
8+
(typeof Symbol === "function" &&
9+
// "for" is a reserved keyword in ES3 so escaping it here for backward compatibility
10+
Symbol["for"] &&
11+
Symbol["for"]("react.element")) ||
12+
0xeac7;
13+
}
14+
15+
var defaultProps = type && type.defaultProps;
16+
var childrenLength = arguments.length - 3;
17+
18+
if (!props && childrenLength !== 0) {
19+
// If we're going to assign props.children, we create a new object now
20+
// to avoid mutating defaultProps.
21+
props = { children: void 0 };
22+
}
23+
24+
if (childrenLength === 1) {
25+
props.children = children;
26+
} else if (childrenLength > 1) {
27+
var childArray = new Array(childrenLength);
28+
for (var i = 0; i < childrenLength; i++) {
29+
childArray[i] = arguments[i + 3];
30+
}
31+
props.children = childArray;
32+
}
33+
34+
if (props && defaultProps) {
35+
for (var propName in defaultProps) {
36+
if (props[propName] === void 0) {
37+
props[propName] = defaultProps[propName];
38+
}
39+
}
40+
} else if (!props) {
41+
props = defaultProps || {};
42+
}
43+
44+
return {
45+
$$typeof: REACT_ELEMENT_TYPE,
46+
type: type,
47+
key: key === undefined ? null : "" + key,
48+
ref: null,
49+
props: props,
50+
_owner: null,
51+
};
52+
}

0 commit comments

Comments
 (0)