Skip to content

Commit

Permalink
Merge pull request #2321 from storybooks/remove-actions-eval
Browse files Browse the repository at this point in the history
Addon actions: replace eval with function name assignment
  • Loading branch information
Hypnosphi authored Nov 15, 2017
2 parents 969aadb + 7c72d40 commit e23face
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
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

0 comments on commit e23face

Please sign in to comment.