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: replace eval with function name assignment #2321

Merged
merged 1 commit into from
Nov 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions addons/actions/src/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ export function action(name) {
});
};

// some day when {[name]: function() {}} syntax is not transpiled by babel
// we can get rid of this eval as by ES2015 spec the above function gets the
// name `name`, but babel transpiles to Object.defineProperty which doesn't do
// the same.
//
// Ref: https://bocoup.com/weblog/whats-in-a-function-name
const fnName = name && typeof name === 'string' ? name.replace(/\W+/g, '_') : 'action';
// eslint-disable-next-line no-eval
const named = eval(`(function ${fnName}() { return handler.apply(this, arguments) })`);
return named;
Object.defineProperty(handler, 'name', { value: fnName });
return handler;
}

export function decorateAction(decorators) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5563,6 +5563,15 @@ exports[`Storyshots Button addons composition 1`] = `
</div>
`;

exports[`Storyshots Button delete 1`] = `
<button
className="css-1yjiefr"
onClick={[Function]}
>
Delete
</button>
`;

exports[`Storyshots Button with new info 1`] = `
<div>
<div
Expand Down
6 changes: 6 additions & 0 deletions examples/cra-kitchen-sink/src/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ storiesOf('Button', module)
😀 😎 👍 💯
</Button>
))
.add('delete', () => (
<Button onClick={action('delete')}>
{setOptions({ selectedAddonPanel: 'storybook/actions/actions-panel' })}
Delete
</Button>
))
.add('with notes', () => (
// deprecated usage
<WithNotes notes="A very simple button">
Expand Down