-
Notifications
You must be signed in to change notification settings - Fork 1.4k
chore: Add image validation for Gatsby components and enhance error handling #7213
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
Open
leecalcote
wants to merge
8
commits into
master
Choose a base branch
from
leecalcote/chore/check-images
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0647b7b
chore: Add image validation for Gatsby components and enhance error h…
leecalcote cf22da6
chore: build-javascript stage to enforce stronger runtime chunking: a…
leecalcote 210928e
chore: enhance image component error handling for missing gatsbyImage…
leecalcote acd9b3c
Merge branch 'master' into leecalcote/chore/check-images
leecalcote 4e90d60
Merge branch 'master' into leecalcote/chore/check-images
saurabhraghuvanshii 18d2f11
Update src/sections/Learn-Layer5/Chapters/index.js
leecalcote b5c09cc
Update gatsby-node.js
leecalcote 67f69d4
Merge branch 'master' into leecalcote/chore/check-images
leecalcote 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,141 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const parser = require("@babel/parser"); | ||
| const traverse = require("@babel/traverse").default; | ||
|
|
||
| const defaultRoots = ["src", "content-learn"]; | ||
| const extensions = new Set([".js", ".jsx", ".ts", ".tsx"]); | ||
| const ignoreDirs = new Set([ | ||
| "node_modules", | ||
| ".git", | ||
| ".cache", | ||
| "public", | ||
| "static", | ||
| "scripts", | ||
| "__generated__" | ||
| ]); | ||
|
|
||
| const targets = process.argv.slice(2); | ||
| const roots = targets.length ? targets : defaultRoots; | ||
| const issues = []; | ||
|
|
||
| const parserOptions = { | ||
| sourceType: "unambiguous", | ||
| errorRecovery: true, | ||
| plugins: [ | ||
| "jsx", | ||
| "typescript", | ||
| "classProperties", | ||
| "classPrivateProperties", | ||
| "classPrivateMethods", | ||
| ["decorators", { decoratorsBeforeExport: true }], | ||
| "dynamicImport", | ||
| "exportDefaultFrom", | ||
| "exportNamespaceFrom", | ||
| "importAssertions", | ||
| "topLevelAwait" | ||
| ] | ||
| }; | ||
|
|
||
| function isSkippableDir(name) { | ||
| return ignoreDirs.has(name) || name.startsWith("."); | ||
| } | ||
|
|
||
| function collectFiles(entry) { | ||
| const files = []; | ||
| for (const root of entry) { | ||
| const absRoot = path.resolve(process.cwd(), root); | ||
| if (!fs.existsSync(absRoot)) { | ||
| continue; | ||
| } | ||
| traverseDir(absRoot, files); | ||
| } | ||
| return files; | ||
| } | ||
|
|
||
| function traverseDir(dir, bucket) { | ||
| const entries = fs.readdirSync(dir, { withFileTypes: true }); | ||
| for (const entry of entries) { | ||
| if (entry.isDirectory()) { | ||
| if (isSkippableDir(entry.name)) { | ||
| continue; | ||
| } | ||
| traverseDir(path.join(dir, entry.name), bucket); | ||
| } else if (entry.isFile()) { | ||
| const ext = path.extname(entry.name); | ||
| if (extensions.has(ext)) { | ||
| bucket.push(path.join(dir, entry.name)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function hasAttribute(attrs, attrName) { | ||
| return attrs.some((attr) => { | ||
| if (attr.type !== "JSXAttribute" || !attr.name) { | ||
| return false; | ||
| } | ||
| return attr.name.name === attrName; | ||
| }); | ||
| } | ||
|
|
||
| function getJsxName(node) { | ||
| if (!node) { | ||
| return null; | ||
| } | ||
| if (node.type === "JSXIdentifier") { | ||
| return node.name; | ||
| } | ||
| if (node.type === "JSXMemberExpression") { | ||
| return getJsxName(node.object) + "." + getJsxName(node.property); | ||
| } | ||
| if (node.type === "JSXNamespacedName") { | ||
| return `${node.namespace.name}:${node.name.name}`; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function report(file, node, message) { | ||
| const location = node.loc ? `${node.loc.start.line}:${node.loc.start.column + 1}` : "unknown"; | ||
| issues.push({ file, location, message }); | ||
| } | ||
|
|
||
| function analyzeFile(file) { | ||
| const code = fs.readFileSync(file, "utf8"); | ||
| let ast; | ||
| try { | ||
| ast = parser.parse(code, { ...parserOptions, sourceFilename: file }); | ||
| } catch (err) { | ||
| console.warn(`Skipping ${path.relative(process.cwd(), file)}: ${err.message}`); | ||
| return; | ||
| } | ||
|
|
||
| traverse(ast, { | ||
| JSXOpeningElement(pathRef) { | ||
| const name = getJsxName(pathRef.node.name); | ||
| if (!name) { | ||
| return; | ||
| } | ||
| if (name === "GatsbyImage" && !hasAttribute(pathRef.node.attributes, "image")) { | ||
| report(file, pathRef.node, "Missing required 'image' prop on <GatsbyImage />"); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| const filesToInspect = collectFiles(roots); | ||
| filesToInspect.forEach(analyzeFile); | ||
|
|
||
| if (issues.length) { | ||
| console.error("Found Gatsby image issues:\n"); | ||
| for (const issue of issues) { | ||
| const relative = path.relative(process.cwd(), issue.file); | ||
| console.error(`${relative}:${issue.location} - ${issue.message}`); | ||
| } | ||
| console.error(`\nTotal issues: ${issues.length}`); | ||
| process.exitCode = 1; | ||
| } else { | ||
| console.log("No missing 'image' props detected on GatsbyImage components."); | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.