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

Addon-actions: Fix types and refactor #2438

Merged
merged 34 commits into from
Dec 14, 2017
Merged

Addon-actions: Fix types and refactor #2438

merged 34 commits into from
Dec 14, 2017

Conversation

rhalff
Copy link
Contributor

@rhalff rhalff commented Dec 5, 2017

Primitive types failed to be logged.

Refactor of the code, split functionality into different files and added tests.

I've added an extra example in the kitchen sink app which logs different types.

The changes from #2401 were merged.

@codecov
Copy link

codecov bot commented Dec 5, 2017

Codecov Report

Merging #2438 into release/3.3 will increase coverage by 0.39%.
The diff coverage is 49.14%.

Impacted file tree graph

@@               Coverage Diff               @@
##           release/3.3    #2438      +/-   ##
===============================================
+ Coverage        19.11%   19.51%   +0.39%     
===============================================
  Files              355      386      +31     
  Lines             8259     8707     +448     
  Branches           901      959      +58     
===============================================
+ Hits              1579     1699     +120     
- Misses            6003     6263     +260     
- Partials           677      745      +68
Impacted Files Coverage Δ
addons/actions/src/lib/errors/index.js 0% <ø> (ø)
app/react/src/client/preview/render.js 0% <ø> (ø) ⬆️
...ddons/actions/src/containers/ActionLogger/index.js 2.98% <0%> (+0.08%) ⬆️
addons/actions/src/lib/util/index.js 0% <0%> (ø)
app/react-native/src/bin/storybook-start.js 0% <0%> (ø) ⬆️
addons/actions/src/lib/errors/DecycleError.js 0% <0%> (ø)
...actions/src/lib/types/function/reservedKeywords.js 25% <25%> (ø)
.../actions/src/lib/types/object/canAccessProperty.js 30% <37.5%> (ø)
...tions/src/lib/types/function/createFunctionEval.js 22.22% <40%> (ø)
addons/actions/src/lib/retrocycle.js 34.09% <40.54%> (ø)
... and 113 more

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 bad51f5...ddb21db. Read the comment docs.

const { hasOwnProperty } = Object.prototype;

export default function getPropertiesList(value) {
const keys = Object.getOwnPropertyNames(value);
Copy link
Member

Choose a reason for hiding this comment

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

This includes non-enumerable props. I don't think they should be displayed. Just for ... in should be enough

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried to get the same behavior as in the console for something like the Math object, which does show the non-enumerable properties, otherwise it would just be empty.

Copy link
Member

@Hypnosphi Hypnosphi Dec 6, 2017

Choose a reason for hiding this comment

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

console shows them in half-transparent style, to indicate that they're somewhat secondary. And it logs __proto__ among them.

And I can't actually imagine someone using Math object as event handler attribute. Let's not add non-enumerable props unless we find a usecase.

@Hypnosphi
Copy link
Member

Live example

@@ -0,0 +1,3 @@
export default function isReserved(name) {
return ['delete'].indexOf(name) >= 0;
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tried to solve it by supporting both methods, preferring the one you have suggests.
The eval version could be removed if you find it unnecessary.

I've included the changes of #2428 but moved the check to a different location.

Object.assign(this, data);
}

Object.defineProperty(FakeConstructor, 'name', {
Copy link
Member

Choose a reason for hiding this comment

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

This will break in android emulator, see #2324

Copy link
Member

Choose a reason for hiding this comment

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

Adding canConfigureName check should solve this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should also be fixed now, I didn't test it on android though.

@@ -0,0 +1,62 @@
/* global File */
Copy link
Member

Choose a reason for hiding this comment

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

require { File, Math } from 'global'

for (const name in value) {
// noinspection JSUnfilteredForInLoop
if (
keys.indexOf(name) === -1 &&
Copy link
Member

@Hypnosphi Hypnosphi Dec 7, 2017

Choose a reason for hiding this comment

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

Why is this check needed? There's no chance that for .. in yields duplicate keys:

const a = {foo: 'foo', bar: 'bar'}
const b = Object.create(a, {foo: {value: 'baz', enumerable: 'true'}})
for (key in b) { console.log(key) }
VM372:1 foo
VM372:1 bar

Note that foo is logged only once

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, a left over from populating the keys array with ownPropertyNames first, I will remove it.

The function check is needed to only show functions in case they are own properties of the current object.

The previous fix only worked because the results were stringified, filtering out all functions e.g. for .. in over a file object does include slice etc. but were not shown, because of the stringification later on.

// noinspection JSUnfilteredForInLoop
if (
keys.indexOf(name) === -1 &&
!(typeof value[name] === 'function' && !hasOwnProperty.call(value, name))
Copy link
Member

@Hypnosphi Hypnosphi Dec 7, 2017

Choose a reason for hiding this comment

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

Let's invert the check, so that it's more clear what it does (picks all the own properties, and non-function delegated properties):
if (hasOwnProperty.call(value, name)) || typeof value[name] !== 'function')

@danielduan danielduan added this to the v3.4.0 milestone Dec 11, 2017
@Hypnosphi
Copy link
Member

Hypnosphi commented Dec 11, 2017

@rhalff can you please extract the fix for primitive types into a separate PR? We need it for 3.3.0 release

@rhalff
Copy link
Contributor Author

rhalff commented Dec 12, 2017

I do not think that will be a better merge than the one currently offered. The main problems I found in the current state of the pull request is Firefox not liking some objects as weakmap keys and IE Edge and down still consider some decycled objects as cyclic. This only happens on logging the 'window' object though. Not at the level of e.g SyntheticEvent. I would prefer updating this pull request and just return the errors to the action logger.

@Hypnosphi Hypnosphi modified the milestones: v3.4.0, v3.3.0 Dec 12, 2017
@Hypnosphi
Copy link
Member

OK, let's do that

@rhalff
Copy link
Contributor Author

rhalff commented Dec 13, 2017

@Hypnosphi I've added the error handling, to trigger some errors you can cllick the window type button in firefox and IE / Edge, perhaps there are better solutions to solve this, but it works for now.

@Hypnosphi
Copy link
Member

Great. Please run yarn locally to update lockfile

@Hypnosphi
Copy link
Member

Hypnosphi commented Dec 13, 2017

Please also update storyshots: yarn test --core --update

The CLI test failure is probably caused by some vue upgrade, I'll try to handle it done

@rhalff
Copy link
Contributor Author

rhalff commented Dec 14, 2017

Storyshots is also updated now, I still get a warning when I run jest though:

$ ./scripts/test.js --core --update
jest-haste-map: duplicate manual mock found:
  Module name: example
  Duplicate Mock path: /home/rhalff/git/storybook/addons/actions/dist/lib/__mocks__/example.js
This warning is caused by two manual mock files with the same file name.
Jest will use the mock file found in: 
/home/rhalff/git/storybook/addons/actions/dist/lib/__mocks__/example.js
 Please delete one of the following two files: 
 /home/rhalff/git/storybook/addons/actions/src/lib/__mocks__/example.js
/home/rhalff/git/storybook/addons/actions/dist/lib/__mocks__/example.js

Should the babel ignore pattern be updated to also ignore the __mocks__ folders?

I think it's defined in scripts/prepare.js

@Hypnosphi
Copy link
Member

Should the babel ignore pattern be updated to also ignore the mocks folders?

Yes, I think so. Please adjust prepare.js

@Hypnosphi Hypnosphi merged commit f4cbe6f into storybookjs:release/3.3 Dec 14, 2017
@ndelangen
Copy link
Member

@rhalff Thank you for this epic PR, I know you spend a lot of work and time on this 🙇

Are you planning to open more PR's? I can add you to the github organisation to make this easier if you want?

@rhalff
Copy link
Contributor Author

rhalff commented Dec 14, 2017

@ndelangen Thanks! I'm not planning more PR's at the moment though. Besides that the current method is easy enough. @Hypnosphi thanks for reviewing.

@Hypnosphi
Copy link
Member

is easy enough

Actually, when the PR branch is in the same repo, it makes things a bit easier for reviewers =)

@rhalff
Copy link
Contributor Author

rhalff commented Dec 15, 2017

In that case, add me.. :)

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.

4 participants