layer5 icon indicating copy to clipboard operation
layer5 copied to clipboard

Add Gatsby image validation check

Open KatalKavya96 opened this issue 5 months ago • 4 comments

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:images npm script
  • Added check-images Makefile 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.

Signed commits

  • [x] Yes, I signed my commits.

KatalKavya96 avatar Jun 04 '26 15:06 KatalKavya96

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.

github-actions[bot] avatar Jun 04 '26 15:06 github-actions[bot]

@lekaf974

KatalKavya96 avatar Jun 10 '26 16:06 KatalKavya96

I have two minor suggestions to make it more robust:

  1. False Positives on Spreads: In hasImageProp, if a developer uses a spread operator (<GatsbyImage {...props} />), the attribute type is JSXSpreadAttribute, meaning attribute.name is undefined. The script currently flags these as failures. You might want to update the logic to return true (or skip validation) if attributes.some(attr => attr.type === 'JSXSpreadAttribute') is found, to prevent false positives.
  2. Dependencies: The script directly requires @babel/parser and @babel/traverse, but I don't see them added to package.json. Even if they are hoisted by Gatsby, it's best practice to explicitly add them to devDependencies to prevent "phantom dependency" issues on fresh installs in the future.

Junnygram avatar Jun 11 '26 04:06 Junnygram

@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.

KatalKavya96 avatar Jun 18 '26 13:06 KatalKavya96