Skip to content

Allow helpers to be put in separate files#8487

Closed
nicolo-ribaudo wants to merge 1 commit intobabel:masterfrom
nicolo-ribaudo:extract-helpers
Closed

Allow helpers to be put in separate files#8487
nicolo-ribaudo wants to merge 1 commit intobabel:masterfrom
nicolo-ribaudo:extract-helpers

Conversation

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This gives a few advantages:

  1. They can be linted and prettified
  2. It's easier to review them on GitHub, since they are syntax-highlighted.

I had to set up a separate gulp task because, when using make watch, changes to any extracted helper need to trigger a recompilation of babel-helpers/src/helpers.js.
I only extracted one helper, as an example and to check that everything works. The compiled output looks like this:

helpers.jsx = helper("7.0.0-beta.0")("var REACT_ELEMENT_TYPE;\n\nexport ... ");

cc @Kovensky, since you had the original idea (accordingly to what I wrote in bfb3592) 😄

@nicolo-ribaudo nicolo-ribaudo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Aug 17, 2018
{
"files": ["packages/babel-helpers/src/helpers/*.js"],
"rules": {
"no-var": "off"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here probably we will need to add some other rules.

var REACT_ELEMENT_TYPE;

function _jsx(type, props, key, children) { if (!REACT_ELEMENT_TYPE) { REACT_ELEMENT_TYPE = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7; } var defaultProps = type && type.defaultProps; var childrenLength = arguments.length - 3; if (!props && childrenLength !== 0) { props = { children: void 0 }; } if (props && defaultProps) { for (var propName in defaultProps) { if (props[propName] === void 0) { props[propName] = defaultProps[propName]; } } } else if (!props) { props = defaultProps || {}; } 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; } return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key === undefined ? null : '' + key, ref: null, props: props, _owner: null }; }
function _jsx(type, props, key, children) { if (!REACT_ELEMENT_TYPE) { REACT_ELEMENT_TYPE = typeof Symbol === "function" && Symbol.for && Symbol.for("react.element") || 0xeac7; } var defaultProps = type && type.defaultProps; var childrenLength = arguments.length - 3; if (!props && childrenLength !== 0) { props = { children: void 0 }; } if (props && defaultProps) { for (var propName in defaultProps) { if (props[propName] === void 0) { props[propName] = defaultProps[propName]; } } } else if (!props) { props = defaultProps || {}; } 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; } return { $$typeof: REACT_ELEMENT_TYPE, type: type, key: key === undefined ? null : "" + key, ref: null, props: props, _owner: null }; }
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change here is that prettier transformed '' to "".

This gives a few advantages:
1) They can be linted and prettified
2) It's easier to review them on GitHub, snice they are
   sytnax-highlighted.
@babel-bot
Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/8838/

@nicolo-ribaudo
Copy link
Copy Markdown
Member Author

I have some ideas for this, but I'll create a RFC because I don't think that this PR is the best solution.

@nicolo-ribaudo nicolo-ribaudo deleted the extract-helpers branch January 31, 2021 23:51
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 3, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Internal 🏠 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants