-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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 Storysource improvements #3040
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0ee3362
Remove usage of "line-column" package + Add ugly comments removal
igor-dv 2f3edc3
Add storysource to addons support
igor-dv d781fbd
Make "prettier" and "uglyComments" configurable.
igor-dv 9f7d8c7
Update yarn
igor-dv 8a09021
Minor changes in preview.js
igor-dv 3020317
Merge branch 'master' into storysource-adjustments
igor-dv 11d5e96
Use some instead of find
igor-dv d1166b7
Update README.md
igor-dv 2f0f5ec
Add new line at the end of the README.md
igor-dv a30595e
Merge branch 'master' into storysource-adjustments
Hypnosphi 8575ebf
Add demo.gif
igor-dv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* global window */ | ||
/* eslint-disable global-require, import/no-dynamic-require */ | ||
|
||
import React from 'react'; | ||
|
||
/* | ||
eslint-disable some kind | ||
of multi line ignore, though | ||
I'm not sure it's possible. | ||
*/ | ||
|
||
import { storiesOf } from '@storybook/react'; | ||
|
||
/* eslint-disable-line */ const x = 0; | ||
|
||
// eslint-disable-line | ||
storiesOf('Foo', module) | ||
.add('bar', () => <div>baz</div>); | ||
|
||
/* | ||
This is actually a good comment that will help | ||
users to understand what's going on here. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const defaultOptions = { | ||
prettierConfig: { | ||
printWidth: 120, | ||
tabWidth: 2, | ||
bracketSpacing: true, | ||
trailingComma: 'es5', | ||
singleQuote: true, | ||
}, | ||
uglyCommentsRegex: [/^eslint-.*/, /^global.*/], | ||
}; | ||
|
||
export default defaultOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import prettier from 'prettier'; | ||
import { handleADD, handleSTORYOF } from './parse-helpers'; | ||
|
||
const estraverse = require('estraverse'); | ||
const acorn = require('acorn'); | ||
|
||
require('acorn-stage3/inject')(acorn); | ||
require('acorn-jsx/inject')(acorn); | ||
require('acorn-es7')(acorn); | ||
|
||
const acornConfig = { | ||
ecmaVersion: '9', | ||
sourceType: 'module', | ||
ranges: true, | ||
locations: true, | ||
plugins: { | ||
jsx: true, | ||
stage3: true, | ||
es7: true, | ||
}, | ||
}; | ||
|
||
function isUglyComment(comment, uglyCommentsRegex) { | ||
return uglyCommentsRegex.find(regex => regex.test(comment)); | ||
} | ||
|
||
function generateSourceWithoutUglyComments(source, { comments, uglyCommentsRegex }) { | ||
let lastIndex = 0; | ||
const parts = [source]; | ||
|
||
comments | ||
.filter(comment => isUglyComment(comment.value.trim(), uglyCommentsRegex)) | ||
.forEach(comment => { | ||
parts.pop(); | ||
|
||
const start = source.slice(lastIndex, comment.start); | ||
const end = source.slice(comment.end); | ||
|
||
parts.push(start, end); | ||
lastIndex = comment.end; | ||
}); | ||
|
||
return parts.join(''); | ||
} | ||
|
||
function prettifyCode(source, { prettierConfig }) { | ||
return prettier.format(source, prettierConfig); | ||
} | ||
|
||
export function generateSourceWithDecorators(source, decorator) { | ||
const comments = []; | ||
|
||
const config = { | ||
...acornConfig, | ||
onComment: comments, | ||
}; | ||
|
||
const ast = acorn.parse(source, config); | ||
|
||
let lastIndex = 0; | ||
const parts = [source]; | ||
|
||
estraverse.traverse(ast, { | ||
fallback: 'iteration', | ||
enter: node => { | ||
if (node.type === 'CallExpression') { | ||
lastIndex = handleSTORYOF(node, parts, source, lastIndex); | ||
} | ||
}, | ||
}); | ||
|
||
const newSource = parts.join(decorator); | ||
|
||
return { | ||
changed: lastIndex > 0, | ||
source: newSource, | ||
comments, | ||
}; | ||
} | ||
|
||
export function generateAddsMap(source) { | ||
const ast = acorn.parse(source, acornConfig); | ||
const adds = {}; | ||
|
||
estraverse.traverse(ast, { | ||
fallback: 'iteration', | ||
enter: (node, parent) => { | ||
if (node.type === 'MemberExpression') { | ||
handleADD(node, parent, adds); | ||
} | ||
}, | ||
}); | ||
|
||
return adds; | ||
} | ||
|
||
export function generateStorySource({ source, ...options }) { | ||
let storySource = source; | ||
|
||
storySource = generateSourceWithoutUglyComments(storySource, options); | ||
storySource = prettifyCode(storySource, options); | ||
|
||
return storySource; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.some
not.find