Add Gatsby image validation check
Description
This PR fixes #7650
This PR adds a Gatsby image validation guardrail through a new make check-images target.
The validation script scans Gatsby/React source files and reports <GatsbyImage /> components that are missing an explicit image prop. It supports both full project validation and scoped validation for a specific folder or file.
Example:
make check-images
make check-images -- src/pages/community
If missing image props are found, the command prints the file path and location, then exits with code 1.
Notes for Reviewers
Changes made:
- Added
scripts/find-missing-gatsby-images.js - Added
check:imagesnpm script - Added
check-imagesMakefile target - Added support for scoped checks using folder/file paths
Tested with:
npm run check:images
make check-images
make check-images -- src/pages/community
Also manually tested with a temporary invalid <GatsbyImage /> missing the image prop, and the script correctly reported the issue with exit code 1.
- [x] Yes, I signed my commits.
Preview deployment for PR #7782 removed.
This PR preview was automatically pruned because we keep only the 6 most recently updated previews on GitHub Pages to stay within deployment size limits.
If needed, push a new commit to this PR to generate a fresh preview.
@lekaf974
I have two minor suggestions to make it more robust:
- False Positives on Spreads: In
hasImageProp, if a developer uses a spread operator (<GatsbyImage {...props} />), the attribute type isJSXSpreadAttribute, meaningattribute.nameis undefined. The script currently flags these as failures. You might want to update the logic to returntrue(or skip validation) ifattributes.some(attr => attr.type === 'JSXSpreadAttribute')is found, to prevent false positives. - Dependencies: The script directly requires
@babel/parserand@babel/traverse, but I don't see them added topackage.json. Even if they are hoisted by Gatsby, it's best practice to explicitly add them todevDependenciesto prevent "phantom dependency" issues on fresh installs in the future.
@Junnygram Updated the PR based on the suggestions:
-
Added handling for JSXSpreadAttribute so usages like <GatsbyImage {...props} /> are not incorrectly flagged as missing the image prop.
-
Added @babel/parser and @babel/traverse explicitly to devDependencies to avoid relying on transitive dependencies.