Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
66181db
commit f63d73c
Showing
15 changed files
with
304 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import fs from "fs"; | ||
import { join } from "path"; | ||
import { URL } from "url"; | ||
|
||
const HELPERS_FOLDER = new URL("../src/helpers", import.meta.url); | ||
const IGNORED_FILES = new Set(["package.json"]); | ||
|
||
export default async function generateAsserts() { | ||
let output = `/* | ||
* This file is auto-generated! Do not modify it directly. | ||
* To re-generate run 'make build' | ||
*/ | ||
import template from "@babel/template"; | ||
`; | ||
|
||
for (const file of (await fs.promises.readdir(HELPERS_FOLDER)).sort()) { | ||
if (IGNORED_FILES.has(file)) continue; | ||
|
||
const [helperName] = file.split("."); | ||
const isValidId = isValidBindingIdentifier(helperName); | ||
const varName = isValidId ? helperName : `_${helperName}`; | ||
|
||
const fileContents = await fs.promises.readFile( | ||
join(HELPERS_FOLDER.pathname, file), | ||
"utf8" | ||
); | ||
const { minVersion } = fileContents.match( | ||
/^\s*\/\*\s*@minVersion\s+(?<minVersion>\S+)\s*\*\/\s*$/m | ||
).groups; | ||
|
||
// TODO: We can minify the helpers in production | ||
const source = fileContents | ||
// Remove comments | ||
.replace(/\/\*[^]*?\*\/|\/\/.*/g, "") | ||
// Remove multiple newlines | ||
.replace(/\n{2,}/g, "\n"); | ||
|
||
const intro = isValidId | ||
? "export " | ||
: `export { ${varName} as ${helperName} }\n`; | ||
|
||
output += `\n${intro}const ${varName} = { | ||
minVersion: ${JSON.stringify(minVersion)}, | ||
ast: () => template.program.ast(${JSON.stringify(source)}) | ||
};\n`; | ||
} | ||
|
||
return output; | ||
} | ||
|
||
function isValidBindingIdentifier(name) { | ||
try { | ||
Function(`var ${name}`); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "type": "module" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* This file is auto-generated! Do not modify it directly. | ||
* To re-generate run 'make build' | ||
*/ | ||
|
||
import template from "@babel/template"; | ||
|
||
export const jsx = { | ||
minVersion: "7.0.0-beta.0", | ||
ast: () => | ||
template.program.ast( | ||
'\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', | ||
), | ||
}; | ||
|
||
export const objectSpread2 = { | ||
minVersion: "7.5.0", | ||
ast: () => | ||
template.program.ast( | ||
'\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', | ||
), | ||
}; | ||
|
||
export { _typeof as typeof }; | ||
const _typeof = { | ||
minVersion: "7.0.0-beta.0", | ||
ast: () => | ||
template.program.ast( | ||
'\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', | ||
), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* @minVersion 7.0.0-beta.0 */ | ||
|
||
var REACT_ELEMENT_TYPE; | ||
|
||
export default function _createRawReactElement(type, props, key, children) { | ||
if (!REACT_ELEMENT_TYPE) { | ||
REACT_ELEMENT_TYPE = | ||
(typeof Symbol === "function" && | ||
// "for" is a reserved keyword in ES3 so escaping it here for backward compatibility | ||
Symbol["for"] && | ||
Symbol["for"]("react.element")) || | ||
0xeac7; | ||
} | ||
|
||
var defaultProps = type && type.defaultProps; | ||
var childrenLength = arguments.length - 3; | ||
|
||
if (!props && childrenLength !== 0) { | ||
// If we're going to assign props.children, we create a new object now | ||
// to avoid mutating defaultProps. | ||
props = { children: void 0 }; | ||
} | ||
|
||
if (childrenLength === 1) { | ||
props.children = children; | ||
} else if (childrenLength > 1) { | ||
var childArray = new Array(childrenLength); | ||
for (var i = 0; i < childrenLength; i++) { | ||
childArray[i] = arguments[i + 3]; | ||
} | ||
props.children = childArray; | ||
} | ||
|
||
if (props && defaultProps) { | ||
for (var propName in defaultProps) { | ||
if (props[propName] === void 0) { | ||
props[propName] = defaultProps[propName]; | ||
} | ||
} | ||
} else if (!props) { | ||
props = defaultProps || {}; | ||
} | ||
|
||
return { | ||
$$typeof: REACT_ELEMENT_TYPE, | ||
type: type, | ||
key: key === undefined ? null : "" + key, | ||
ref: null, | ||
props: props, | ||
_owner: null, | ||
}; | ||
} |
Oops, something went wrong.