Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor beta branch for a bit more consistency #528

Merged
merged 5 commits into from Jan 8, 2021

Conversation

mpeyper
Copy link
Member

@mpeyper mpeyper commented Jan 8, 2021

What:

More consistency across the codebase.

Why:

It started with me wanting to have a named function as the result of createRenderHook rather than the anonymous arrow function it is returning now to improve stack traces when debugging, but the more I looked the more I noticed we had a real mix of regular functions and arrow functions through the codebase, even within the same file. This led me down a rabbit hole of a few other things that weren't quite right, names default exports and a few variable names.

How:

For functions I followed the rules:

  1. regular function at the top level
  2. arrow function for inner functions
  3. arrow functions for anonymous functions

We only had a single default export left in the codebase. There has been a growing movement in the community away from them and to just use named exports instead. It's become my personal preference for all exports over the last few months.

Finally, testHookProps was previously renamed to rendererProps in all but one of the renderers (native), so I have changed that on now as well. Also, I renamed testHook to testHarness in all renderers as it is the result of the createTestHarness helper.

Checklist:

  • Ready to be merged

There is also a lot of inconsistency in the types with respect to interface vs type, but I'll do another PR for that as I want change something else and #527 moves one of the types I need to change so I'll wait for that one to be merged also.

@codecov
Copy link

codecov bot commented Jan 8, 2021

Codecov Report

Merging #528 (efc618b) into beta (f6096ba) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              beta      #528   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           12        12           
  Lines          186       189    +3     
  Branches        27        27           
=========================================
+ Hits           186       189    +3     
Impacted Files Coverage Δ
src/core/asyncUtils.ts 100.00% <ø> (ø)
src/core/index.ts 100.00% <100.00%> (ø)
src/dom/pure.ts 100.00% <100.00%> (ø)
src/helpers/createTestHarness.tsx 100.00% <100.00%> (ø)
src/helpers/promises.ts 100.00% <100.00%> (ø)
src/native/pure.ts 100.00% <100.00%> (ø)
src/server/pure.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f6096ba...efc618b. Read the comment docs.

Comment on lines 79 to 82
// If the function name does not get used before it is returned,
// it seems to vanish in nodejs and does not appear in stack traces.
// This dummy usage works around that.
const _name = renderHook.name // eslint-disable-line @typescript-eslint/no-unused-vars
Copy link
Member Author

Choose a reason for hiding this comment

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

This is gross and I have no idea why it is necessary, but in node, if I've got function that returns a function like so:

function createFunc() {
  function func() {
  }
  return func
}

then:

console.log(createFunc.name) // 'createFunc'
console.log(createFunc().name) // ''

But if I use func.name inside createFunc like so:

function createFunc() {
  function func() {
  }
  const name = func.name // not returned, just used
  return func
}

then:

console.log(createFunc.name) // 'createFunc'
console.log(createFunc().name) // 'func'

In the chrome console, both example will return func for the second log. It's very weird.

Copy link
Member

Choose a reason for hiding this comment

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

That is really weird. Can't seem to find much online either how to potentially solve.

Copy link
Member Author

Choose a reason for hiding this comment

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

So I've discovered that this isn't a node thing, but something in the compilation. I've narrowed it down to babel (not TS and not something in jest) as if I use kcd-scripts to compile a javascript only file, this

export function createFunc1() {
  function func() {
  }
  return func
}

export function createFunc2() {
  function func() {
  }
  const name = func.name // not returned, just used
  return func
}

becomes

"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createFunc1 = createFunc1;
exports.createFunc2 = createFunc2;

function createFunc1() {
  return function () {};
}

function createFunc2() {
  function func() {}

  func.name; // not returned, just used

  return func;
}

The name in createFunc1 has been scrubbed!

Now I just have to work out which plugin is causing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @jpeyper. I've raised an issue in kcd-scripts about this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll keep an eye on the other issue, in the meantime, we have this workaround 🤷

@mpeyper mpeyper marked this pull request as draft January 8, 2021 09:54
src/core/index.ts Outdated Show resolved Hide resolved
Co-authored-by: Jonathan Peyper <jpeyper@gmail.com>
@mpeyper mpeyper marked this pull request as ready for review January 8, 2021 11:34
Copy link
Member

@joshuaellis joshuaellis left a comment

Choose a reason for hiding this comment

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

Looks good, interested to hear the feedback on kcd-scripts RE minify plugin.

@joshuaellis joshuaellis merged commit a82d5ef into beta Jan 8, 2021
@github-actions
Copy link

github-actions bot commented Jan 8, 2021

🎉 This PR is included in version 5.0.0-beta.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

@joshuaellis joshuaellis deleted the refactor/consistency branch January 8, 2021 12:25
@github-actions
Copy link

🎉 This PR is included in version 5.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@mpeyper
Copy link
Member Author

mpeyper commented Jan 13, 2021

@allcontributors please add @jpeyper for review, code

@allcontributors
Copy link
Contributor

@mpeyper

I've put up a pull request to add @jpeyper! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants