-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update test-project codemods for stories (#9172)
This updates the test-project rebuild script to be aligned with the changes introduced in #8788
- Loading branch information
Showing
10 changed files
with
262 additions
and
24 deletions.
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
2 changes: 1 addition & 1 deletion
2
__fixtures__/test-project/web/src/components/BlogPost/BlogPost.stories.tsx
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
2 changes: 1 addition & 1 deletion
2
packages/cli/src/commands/generate/component/templates/stories.tsx.template
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 |
---|---|---|
|
@@ -2,6 +2,26 @@ export default (file, api) => { | |
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
// Find `export const Primary: Story = {}` | ||
const exportStatement = root.find(j.ExportNamedDeclaration, { | ||
declaration: { | ||
type: 'VariableDeclaration', | ||
declarations: [ | ||
{ | ||
type: 'VariableDeclarator', | ||
id: { | ||
type: 'Identifier', | ||
name: 'Primary', | ||
}, | ||
init: { | ||
type: 'ObjectExpression', | ||
properties: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
|
||
// const author = { | ||
// email: '[email protected]', | ||
// fullName: 'Story User', | ||
|
@@ -10,28 +30,68 @@ export default (file, api) => { | |
j.variableDeclarator( | ||
j.identifier('author'), | ||
j.objectExpression([ | ||
j.property('init', j.identifier('email'), j.literal('[email protected]')), | ||
j.property( | ||
'init', | ||
j.identifier('email'), | ||
j.literal('[email protected]') | ||
), | ||
j.property('init', j.identifier('fullName'), j.literal('Story User')), | ||
]) | ||
) | ||
), | ||
]) | ||
|
||
root.find(j.ExportNamedDeclaration).insertBefore(authorDeclaration) | ||
// export const Primary: Story = { | ||
// render: () => { | ||
// return <Author author={author} /> | ||
// } | ||
// } | ||
const primaryIdentifier = j.identifier('Primary') | ||
primaryIdentifier.typeAnnotation = j.tsTypeAnnotation( | ||
j.tsTypeReference(j.identifier('Story'), null) | ||
) | ||
|
||
// Change `<Author />` to `<Author author={author} />` | ||
root | ||
.find(j.JSXOpeningElement, { name: { name: 'Author' } } ) | ||
.replaceWith((nodePath) => { | ||
const { node } = nodePath | ||
node.attributes.push( | ||
j.jsxAttribute( | ||
j.jsxIdentifier('author'), | ||
j.jsxExpressionContainer(j.identifier('author')) | ||
) | ||
) | ||
const primaryWithRender = j.exportNamedDeclaration( | ||
j.variableDeclaration('const', [ | ||
j.variableDeclarator( | ||
primaryIdentifier, | ||
j.objectExpression([ | ||
j.property( | ||
'init', | ||
j.identifier('render'), | ||
j.arrowFunctionExpression( | ||
[], | ||
j.blockStatement([ | ||
j.returnStatement( | ||
j.jsxElement( | ||
j.jsxOpeningElement( | ||
j.jsxIdentifier('Author'), | ||
[ | ||
j.jsxAttribute( | ||
j.jsxIdentifier('author'), | ||
j.jsxExpressionContainer(j.identifier('author')) | ||
), | ||
], | ||
true | ||
), | ||
null, | ||
[], | ||
true | ||
) | ||
), | ||
]) | ||
) | ||
), | ||
]) | ||
), | ||
]) | ||
) | ||
|
||
return node | ||
}) | ||
if (exportStatement.length > 0) { | ||
exportStatement.insertBefore(authorDeclaration) | ||
exportStatement.replaceWith(primaryWithRender) | ||
} else { | ||
throw new Error('Could not find export statement in author story') | ||
} | ||
|
||
return root.toSource() | ||
} |
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,83 @@ | ||
export default (file, api) => { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
// Find `export const Primary: Story = {}` | ||
const exportStatement = root.find(j.ExportNamedDeclaration, { | ||
declaration: { | ||
type: 'VariableDeclaration', | ||
declarations: [ | ||
{ | ||
type: 'VariableDeclarator', | ||
id: { | ||
type: 'Identifier', | ||
name: 'Primary', | ||
}, | ||
init: { | ||
type: 'ObjectExpression', | ||
properties: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
|
||
// Create the `Primary` identifier | ||
const primaryIdentifier = j.identifier('Primary') | ||
// Add the `Story` type annotation | ||
primaryIdentifier.typeAnnotation = j.tsTypeAnnotation( | ||
j.tsTypeReference(j.identifier('Story'), null) | ||
) | ||
|
||
// export const Primary: Story = { | ||
// render: (args) => { | ||
// return <BlogPostPage id={42} {...args} /> | ||
// } | ||
// } | ||
const primaryWithRender = j.exportNamedDeclaration( | ||
j.variableDeclaration('const', [ | ||
j.variableDeclarator( | ||
primaryIdentifier, | ||
j.objectExpression([ | ||
j.property( | ||
'init', | ||
j.identifier('render'), | ||
j.arrowFunctionExpression( | ||
[j.identifier('args')], | ||
j.blockStatement([ | ||
j.returnStatement( | ||
j.jsxElement( | ||
j.jsxOpeningElement( | ||
j.jsxIdentifier('BlogPostPage'), | ||
[ | ||
j.jsxAttribute( | ||
j.jsxIdentifier('id'), | ||
j.jsxExpressionContainer(j.numericLiteral(42)) | ||
), | ||
j.jsxSpreadAttribute(j.identifier('args')), | ||
], | ||
true | ||
), | ||
null, | ||
[], | ||
true | ||
) | ||
), | ||
]) | ||
) | ||
), | ||
]) | ||
), | ||
]) | ||
) | ||
|
||
if (exportStatement.length > 0) { | ||
// Replace the empty object export with the object with the `render` | ||
// property | ||
exportStatement.replaceWith(primaryWithRender) | ||
} else { | ||
throw new Error('Could not find export statement in author story') | ||
} | ||
|
||
return root.toSource() | ||
} |
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,83 @@ | ||
export default (file, api) => { | ||
const j = api.jscodeshift | ||
const root = j(file.source) | ||
|
||
// Find `export const Primary: Story = {}` | ||
const exportStatement = root.find(j.ExportNamedDeclaration, { | ||
declaration: { | ||
type: 'VariableDeclaration', | ||
declarations: [ | ||
{ | ||
type: 'VariableDeclarator', | ||
id: { | ||
type: 'Identifier', | ||
name: 'Primary', | ||
}, | ||
init: { | ||
type: 'ObjectExpression', | ||
properties: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
|
||
// Create the `Primary` identifier | ||
const primaryIdentifier = j.identifier('Primary') | ||
// Add the `Story` type annotation | ||
primaryIdentifier.typeAnnotation = j.tsTypeAnnotation( | ||
j.tsTypeReference(j.identifier('Story'), null) | ||
) | ||
|
||
// export const Primary: Story = { | ||
// render: (args) => { | ||
// return <WaterfallPage id={42} {...args} /> | ||
// } | ||
// } | ||
const primaryWithRender = j.exportNamedDeclaration( | ||
j.variableDeclaration('const', [ | ||
j.variableDeclarator( | ||
primaryIdentifier, | ||
j.objectExpression([ | ||
j.property( | ||
'init', | ||
j.identifier('render'), | ||
j.arrowFunctionExpression( | ||
[j.identifier('args')], | ||
j.blockStatement([ | ||
j.returnStatement( | ||
j.jsxElement( | ||
j.jsxOpeningElement( | ||
j.jsxIdentifier('WaterfallPage'), | ||
[ | ||
j.jsxAttribute( | ||
j.jsxIdentifier('id'), | ||
j.jsxExpressionContainer(j.numericLiteral(42)) | ||
), | ||
j.jsxSpreadAttribute(j.identifier('args')), | ||
], | ||
true | ||
), | ||
null, | ||
[], | ||
true | ||
) | ||
), | ||
]) | ||
) | ||
), | ||
]) | ||
), | ||
]) | ||
) | ||
|
||
if (exportStatement.length > 0) { | ||
// Replace the empty object export with the object with the `render` | ||
// property | ||
exportStatement.replaceWith(primaryWithRender) | ||
} else { | ||
throw new Error('Could not find export statement in author story') | ||
} | ||
|
||
return root.toSource() | ||
} |
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