-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
gatsby-image codemod #28112
Merged
Merged
gatsby-image codemod #28112
Changes from all commits
Commits
Show all changes
68 commits
Select commit
Hold shift + click to select a range
3b27c28
saving initial AST exploration
c1833c9
use the types to build replacement expression
b4af168
missed a line
3719973
work off import parent
71208f6
graphql comment
7e200d2
finish for the nigh
c185293
Codemod for gatsby images
885a9ca
use alias function to pass linter
9dd006d
missed a check that isn't strictly necessary
a503df4
Update packages/gatsby-plugin-image/src/__tests__/options.json
c409253
Update packages/gatsby-plugin-image/src/__tests__/fixtures/default/op…
a031428
handle call expression syntax
f8ae3d3
small refactor
d46a97c
function with recast, use replaceWith for more stability
5009c4d
function with recast, use replaceWith for more stability
ddadd81
use templates instead
1e8235b
handle typescript in recast version
995371e
add comments to test cases
707c02e
put recast and babel in jscodeshift wrapper
07d2ebc
Add tokens so we're parsing with babel through recast as well
6ca096e
dont use a yaml query because tests attempt to access it
91a35ef
don't update components when it isn't necessary
a8777b8
we don't need to return and skip for this case
327a969
add tests
f18dc76
forgot to save and updated README
8406d74
merge master and remove unused dependencies
7e12148
shouldn't have saved it, oops
60dccef
ignore testfixtures
b04cab1
integration should ignore fixtures, maybe
693b627
windows fix, thanks ward
c1e4507
fix syntax to fix tests
a47b1d4
adding some edge cases, need a real warn
33d76d3
object expression case
563261a
fix edge cases, still need to make imageImportName reset per file
f7b6608
use path instead of name to ensure scope
026360d
small refactor, small fix
c344807
still run ts test
1d77d7e
use new localName references
13d3c78
add mansion test now that it works
dc48eda
resolve merge conflicts
971d893
forgot to save
f0d7400
cleanup
31b272f
fix linter issue
f398dd3
expressive warns
c937733
remove false warning
d8e717f
show recommendation if parser fails
52a5d16
readme tweak
bb0493b
recommend silent flag
7342bae
update README with src vs root directories
3d71681
object reference edge case and polyfill
a048a0d
there is only one gatsby-image import extra, so let's not use includes
adca5d8
Update packages/gatsby-plugin-image/src/transforms/gatsby-plugin-imag…
1cbc109
make jscodeshift a dev dependency
6d97ff3
Merge branch 'image-codemod' of https://github.com/gatsbyjs/gatsby in…
16a723d
Move to codemods package
1527b38
Add initial bin implementation
70f5786
update readme
e7dceed
handle invalid cases
aef5a45
ignore cache and public, use proper syntax
f906bcf
support styled and other references but still warn
c7d2ffb
remove redudant assignment
0c6a50b
use process.argv a bit more intelligently
1a9071d
fix last failing test
3b35f13
fix ignore logic for npx
95ab2e0
address comments
d5a2db6
extending use case
fc45c55
Merge remote-tracking branch 'upstream/master' into image-codemod
1814023
Merge remote-tracking branch 'upstream/master' into image-codemod
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
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,3 +2,4 @@ | |
!index.js | ||
yarn.lock | ||
transforms/* | ||
bin/* |
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
70 changes: 70 additions & 0 deletions
70
packages/gatsby-codemods/src/bin/__tests__/gatsby-codemods-test.js
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,70 @@ | ||
let execaReturnValue | ||
|
||
jest.setMock('execa', { | ||
sync: () => execaReturnValue | ||
}); | ||
|
||
import process from 'process' | ||
import path from 'path' | ||
import fs from 'fs' | ||
|
||
const { | ||
runTransform, | ||
run, | ||
transformerDirectory, | ||
jscodeshiftExecutable | ||
} = require('../cli'); | ||
|
||
describe('transform', () => { | ||
|
||
it('finds transformer directory', () => { | ||
fs.lstatSync(transformerDirectory) | ||
}) | ||
|
||
it('finds jscodeshift executable', () => { | ||
fs.lstatSync(jscodeshiftExecutable) | ||
}) | ||
|
||
it('runs jscodeshift for the given transformer', () => { | ||
execaReturnValue = { error: null }; | ||
console.log = jest.fn(); | ||
runTransform(`gatsby-plugin-image`, `src`) | ||
|
||
expect(console.log).toBeCalledWith( | ||
`Executing command: jscodeshift --ignore-pattern=**/node_modules/** --ignore-pattern=**/.cache/** --ignore-pattern=**/public/** --extensions=jsx,js,ts,tsx --transform ${path.join(transformerDirectory, 'gatsby-plugin-image.js')} src` | ||
) | ||
}) | ||
|
||
it('warns when on missing transform', () => { | ||
execaReturnValue = { error: null }; | ||
console.log = jest.fn(); | ||
process.argv = [`node`, `dir`] | ||
run() | ||
|
||
expect(console.log).toBeCalledWith( | ||
`Be sure to pass in the name of the codemod you're attempting to run.` | ||
) | ||
}) | ||
|
||
it('warns when on missing target', () => { | ||
execaReturnValue = { error: null }; | ||
console.log = jest.fn(); | ||
process.argv = [`node`, `dir`, `gatsby-plugin-image`] | ||
run() | ||
|
||
expect(console.log).toBeCalledWith( | ||
`You have not provided a target directory to run the codemod against, will default to root.` | ||
) | ||
}) | ||
|
||
it('warns when invalid transform', () => { | ||
execaReturnValue = { error: null }; | ||
console.log = jest.fn(); | ||
process.argv = [`node`, `dir`, `does-not-exist`] | ||
run() | ||
|
||
expect(console.log).toBeCalledWith( | ||
`You have passed in invalid codemod name: does-not-exist. Please pass in one of the following: gatsby-plugin-image, global-graphql-calls, import-link, navigate-calls, rename-bound-action-creators.` | ||
) | ||
}) | ||
}) |
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,53 @@ | ||
import path from 'path' | ||
import execa from 'execa' | ||
|
||
const codemods = [`gatsby-plugin-image`, `global-graphql-calls`, `import-link`, `navigate-calls`, `rename-bound-action-creators`] | ||
|
||
export const transformerDirectory = path.join(__dirname, '../', 'transforms') | ||
export const jscodeshiftExecutable = require.resolve('.bin/jscodeshift') | ||
|
||
export function runTransform(transform, targetDir) { | ||
const transformerPath = path.join(transformerDirectory, `${transform}.js`) | ||
|
||
let args = [] | ||
|
||
args.push('--ignore-pattern=**/node_modules/**') | ||
args.push('--ignore-pattern=**/.cache/**') | ||
args.push('--ignore-pattern=**/public/**') | ||
|
||
args.push('--extensions=jsx,js,ts,tsx') | ||
|
||
args = args.concat(['--transform', transformerPath, targetDir]) | ||
|
||
console.log(`Executing command: jscodeshift ${args.join(' ')}`); | ||
|
||
const result = execa.sync(jscodeshiftExecutable, args, { | ||
stdio: 'inherit', | ||
stripEof: false | ||
}) | ||
|
||
if (result.error) { | ||
throw result.error | ||
} | ||
} | ||
|
||
export function run() { | ||
let [transform, targetDir] = process.argv.slice(2) | ||
|
||
if (!transform) { | ||
console.log(`Be sure to pass in the name of the codemod you're attempting to run.`) | ||
return | ||
} | ||
|
||
if (!codemods.includes(transform)) { | ||
console.log(`You have passed in invalid codemod name: ${transform}. Please pass in one of the following: ${codemods.join(", ")}.`) | ||
return | ||
} | ||
|
||
if(!targetDir) { | ||
console.log(`You have not provided a target directory to run the codemod against, will default to root.`) | ||
targetDir = `./` | ||
|
||
} | ||
runTransform(transform, targetDir) | ||
} |
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,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('./cli').run(); |
17 changes: 17 additions & 0 deletions
17
...ges/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/baseline.input.js
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,17 @@ | ||
import React from "react" | ||
import { graphql } from "gatsby" | ||
import Img from "gatsby-image" | ||
|
||
<Img fixed={data.file.childImageSharp.fixed} alt="headshot"/> | ||
|
||
export const query = graphql` | ||
{ | ||
file(relativePath: { eq: "headers/default.jpg" }) { | ||
childImageSharp { | ||
fixed(width: 125, height: 125) { | ||
...GatsbyImageSharpFixed | ||
} | ||
} | ||
} | ||
} | ||
` |
14 changes: 14 additions & 0 deletions
14
...es/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/baseline.output.js
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,14 @@ | ||
import React from "react" | ||
import { graphql } from "gatsby" | ||
import { GatsbyImage } from "gatsby-plugin-image"; | ||
|
||
<GatsbyImage image={data.file.childImageSharp.gatsbyImageData} alt="headshot" /> | ||
|
||
export const query = graphql`{ | ||
file(relativePath: {eq: "headers/default.jpg"}) { | ||
childImageSharp { | ||
gatsbyImageData(width: 125, height: 125, layout: FIXED) | ||
} | ||
} | ||
} | ||
` |
11 changes: 11 additions & 0 deletions
11
...sby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/call-expression.input.js
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,11 @@ | ||
const result = graphql(` | ||
query { | ||
file(relativePath: { eq: "headers/headshot.jpg" }) { | ||
childImageSharp { | ||
fluid { | ||
...GatsbyImageSharpFluid | ||
} | ||
} | ||
} | ||
} | ||
`) |
8 changes: 8 additions & 0 deletions
8
...by-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/call-expression.output.js
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,8 @@ | ||
const result = graphql(`{ | ||
file(relativePath: {eq: "headers/headshot.jpg"}) { | ||
childImageSharp { | ||
gatsbyImageData(layout: FLUID) | ||
} | ||
} | ||
} | ||
`) |
8 changes: 8 additions & 0 deletions
8
packages/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/compat.input.js
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,8 @@ | ||
import { GatsbyImage as Img } from "gatsby-plugin-image/compat" | ||
// how do comments work? | ||
|
||
<Img | ||
fixed={data.file.childImageSharp.fixed} | ||
alt="headshot"/> | ||
|
||
//still doing ok? |
6 changes: 6 additions & 0 deletions
6
...ages/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/compat.output.js
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,6 @@ | ||
import { GatsbyImage } from "gatsby-plugin-image"; | ||
// how do comments work? | ||
|
||
<GatsbyImage image={data.file.childImageSharp.gatsbyImageData} alt="headshot" /> | ||
|
||
//still doing ok? | ||
laurieontech marked this conversation as resolved.
Show resolved
Hide resolved
|
46 changes: 46 additions & 0 deletions
46
...gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/extends-warn.input.js
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,46 @@ | ||
import React from "react" | ||
import PropTypes from "prop-types" | ||
import Image from "gatsby-image" | ||
|
||
const svgAssetsContext = require.context( | ||
`!file-loader!svgo-loader?{"plugins":[{"removeViewBox":false}]}!../assets`, | ||
true, | ||
/^\.\/.*\.svg$/ | ||
) | ||
|
||
class SVGGatsbyImage extends Image { | ||
render() { | ||
const { fluid, alt, ...rest } = this.props | ||
|
||
if (!this.state.isVisible) { | ||
return <div ref={this.handleRef} /> | ||
} | ||
|
||
return ( | ||
<img | ||
src={svgAssetsContext(`./${fluid.src}`)} | ||
alt={alt} | ||
ref={this.imageRef} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
SVGGatsbyImage.propTypes = { | ||
alt: PropTypes.string, | ||
} | ||
|
||
SVGGatsbyImage.defaultProps = { | ||
alt: ``, | ||
} | ||
|
||
const LazySVGImg = ({ src, ...rest }) => ( | ||
<SVGGatsbyImage fluid={{ src }} {...rest} /> | ||
) | ||
|
||
LazySVGImg.propTypes = { | ||
src: PropTypes.string, | ||
} | ||
|
||
export default LazySVGImg |
46 changes: 46 additions & 0 deletions
46
...atsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/extends-warn.output.js
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,46 @@ | ||
import React from "react" | ||
import PropTypes from "prop-types" | ||
import { GatsbyImage } from "gatsby-plugin-image"; | ||
|
||
const svgAssetsContext = require.context( | ||
`!file-loader!svgo-loader?{"plugins":[{"removeViewBox":false}]}!../assets`, | ||
true, | ||
/^\.\/.*\.svg$/ | ||
) | ||
|
||
class SVGGatsbyImage extends GatsbyImage { | ||
laurieontech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
render() { | ||
const { fluid, alt, ...rest } = this.props | ||
|
||
if (!this.state.isVisible) { | ||
return <div ref={this.handleRef} /> | ||
} | ||
|
||
return ( | ||
<img | ||
src={svgAssetsContext(`./${fluid.src}`)} | ||
alt={alt} | ||
ref={this.imageRef} | ||
{...rest} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
SVGGatsbyImage.propTypes = { | ||
alt: PropTypes.string, | ||
} | ||
|
||
SVGGatsbyImage.defaultProps = { | ||
alt: ``, | ||
} | ||
|
||
const LazySVGImg = ({ src, ...rest }) => ( | ||
<SVGGatsbyImage fluid={{ src }} {...rest} /> | ||
) | ||
|
||
LazySVGImg.propTypes = { | ||
src: PropTypes.string, | ||
} | ||
|
||
export default LazySVGImg |
1 change: 1 addition & 0 deletions
1
packages/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/ignore.input.js
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 @@ | ||
<GatsbyImage image={getImage(data.fitInside)} alt="chameleon" /> |
1 change: 1 addition & 0 deletions
1
...ages/gatsby-codemods/src/transforms/__testfixtures__/gatsby-plugin-image/ignore.output.js
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 @@ | ||
<GatsbyImage image={getImage(data.fitInside)} alt="chameleon" /> |
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.
I'm late, but starting from Babel 7.8.0 this was enabled by default (because it reached Stage 4), so you don't need the syntax plugin anymore! (blog post)