-
Notifications
You must be signed in to change notification settings - Fork 1.5k
docs: add linting and code snippet validation #1983
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
Merged
saihaj
merged 8 commits into
graphql:source
from
sarahxsanders:linting-and-code-validation
Apr 27, 2025
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a8dad1
Add linting and code snippet validation for docs
sarahxsanders 9bd1543
Remove unused link checkers
sarahxsanders e6b5b29
Add link checking to CONTRIBUTING.md and remove package-lock.json
sarahxsanders 195e440
update pnpm lockfile to sync with package.json
sarahxsanders 4fe6cee
feedback from Benjie
sarahxsanders 60e1830
build docs locally to test broken links
sarahxsanders a829d13
fix failing build
saihaj c469154
fix another snippets ignore
saihaj 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Docs validation workflow runs on each PR on main w/ broken link checker | ||
# and code snippet validation | ||
|
||
name: Docs validation | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
link-check: | ||
name: Broken link checker | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Rust | ||
uses: actions-rs/toolchain@v1 | ||
with: | ||
toolchain: stable | ||
|
||
- name: Install lychee | ||
run: cargo install lychee | ||
|
||
- name: Check links | ||
run: lychee --verbose --no-progress './src/pages/learn/**/*.mdx' --base https://graphql.org | ||
sarahxsanders marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
code-validate: | ||
name: Code snippet and GraphQL validation | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "20" | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
sarahxsanders marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- name: Run validation w/ annotations | ||
run: npm run lint:docs:ci | ||
|
||
- name: Validate code snippets | ||
run: npm run validate:snippets |
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 |
---|---|---|
|
@@ -6,14 +6,18 @@ | |
"packageManager": "[email protected]", | ||
"scripts": { | ||
"build": "next build && next-image-export-optimizer", | ||
"check:links": "lychee --verbose --no-progress './src/pages/**/*.mdx' --base https://graphql.org", | ||
saihaj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"dev": "next", | ||
"format": "pnpm format:check --write", | ||
"format:check": "prettier --cache --check .", | ||
"lint": "eslint --ignore-path .gitignore .", | ||
"lint:docs": "eslint --ignore-path .gitignore src/pages/learn --format stylish", | ||
"lint:docs:ci": "eslint --ignore-path .gitignore src/pages/learn --format eslint-formatter-github", | ||
"postbuild": "next-sitemap", | ||
"prebuild": "tsx src/get-github-info.ts", | ||
"start": "next start", | ||
"test": "echo \"no tests\" && exit 1" | ||
"test": "echo \"no tests\" && exit 1", | ||
"validate:snippets": "node scripts/validate-snippets.js" | ||
}, | ||
"dependencies": { | ||
"@graphql-tools/schema": "10.0.15", | ||
|
@@ -23,7 +27,7 @@ | |
"@tailwindcss/typography": "^0.5.10", | ||
"autoprefixer": "^10.4.17", | ||
"clsx": "^2.1.0", | ||
"codemirror": "5.65.1", | ||
"codemirror": "^5.65.19", | ||
"codemirror-graphql": "1.3.2", | ||
"date-fns": "^2.30.0", | ||
"fast-glob": "^3.3.2", | ||
|
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,152 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from "node:fs" | ||
import glob from "glob" | ||
import { parse } from "graphql" | ||
import chalk from "chalk" | ||
|
||
const MDX_GLOB = "./src/pages/learn/**/*.mdx" | ||
const CODE_BLOCK_REGEX = /```(\w+)\s*\r?\n([\s\S]*?)\r?\n```/g | ||
sarahxsanders marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const IGNORE_COMMENT = "snippet-ignore" | ||
|
||
let totalFiles = 0 | ||
let totalSnippets = 0 | ||
let totalErrors = 0 | ||
|
||
// TODO: Add JS linting after JS code snippet modernization | ||
// async function lintJavaScript(code, filePath) { | ||
// const eslint = new ESLint({ | ||
// useEslintrc: true, | ||
// baseConfig: { | ||
// parserOptions: { | ||
// ecmaVersion: "latest", | ||
// sourceType: "module", | ||
// }, | ||
// }, | ||
// }) | ||
|
||
// let preparedCode = code.trim() | ||
|
||
// if (preparedCode.startsWith("function")) { | ||
// preparedCode = "/* eslint-disable no-unused-vars */\n" + preparedCode | ||
// } | ||
|
||
// const results = await eslint.lintText(preparedCode, { filePath }) | ||
// return results.flatMap(result => result.messages) | ||
// } | ||
|
||
function validateGraphQL(code) { | ||
try { | ||
parse(code) | ||
return [] | ||
} catch (error) { | ||
return [{ message: error.message }] | ||
} | ||
} | ||
|
||
function extractSnippets(content, filePath) { | ||
const snippets = [] | ||
let match | ||
|
||
while ((match = CODE_BLOCK_REGEX.exec(content)) !== null) { | ||
const [fullMatch, lang, code] = match | ||
const beforeBlock = content.slice(0, match.index) | ||
const lineNumber = beforeBlock.split(/\r?\n/).length | ||
|
||
if (beforeBlock.includes(IGNORE_COMMENT)) { | ||
continue | ||
} | ||
|
||
snippets.push({ lang, code, lineNumber, filePath }) | ||
} | ||
|
||
return snippets | ||
} | ||
|
||
async function validateSnippet(snippet) { | ||
const { lang, code, lineNumber, filePath } = snippet | ||
|
||
if (!code.trim()) return [] | ||
|
||
// TODO: Add section after JS code snippet modernization | ||
// if (["js", "javascript", "ts", "typescript"].includes(lang)) { | ||
// const messages = await lintJavaScript(code, filePath) | ||
// return messages.map(msg => ({ | ||
// type: "JS/TS", | ||
// file: filePath, | ||
// line: lineNumber + (msg.line || 1), | ||
// message: msg.message, | ||
// })) | ||
// } | ||
|
||
if (lang === "graphql") { | ||
const messages = validateGraphQL(code) | ||
return messages.map(msg => ({ | ||
type: "GraphQL", | ||
file: filePath, | ||
line: lineNumber + (msg.line || 1), | ||
message: msg.message, | ||
})) | ||
} | ||
|
||
return [] | ||
} | ||
|
||
async function main() { | ||
console.log(`Validating code snippets in: ${MDX_GLOB}`) | ||
|
||
const files = glob.sync(MDX_GLOB) | ||
sarahxsanders marked this conversation as resolved.
Show resolved
Hide resolved
|
||
totalFiles = files.length | ||
|
||
if (totalFiles === 0) { | ||
console.log(chalk.green("No MDX files found to validate.")) | ||
return | ||
} | ||
|
||
const errors = [] | ||
|
||
for (const file of files) { | ||
const content = fs.readFileSync(file, "utf8") | ||
const snippets = extractSnippets(content, file) | ||
totalSnippets += snippets.length | ||
|
||
for (const snippet of snippets) { | ||
const snippetErrors = await validateSnippet(snippet) | ||
errors.push(...snippetErrors) | ||
} | ||
} | ||
|
||
totalErrors = errors.length | ||
|
||
if (totalErrors > 0) { | ||
errors.forEach(err => { | ||
const errorMessage = `${err.type} Error in ${err.file} at line ${err.line}: ${err.message}` | ||
console.error(chalk.red(errorMessage)) | ||
|
||
if (process.env.GITHUB_ACTIONS) { | ||
console.log(`::error file=${err.file},line=${err.line}::${err.message}`) | ||
} | ||
}) | ||
|
||
console.error( | ||
chalk.red("\nCode snippet validation failed. Check error logs."), | ||
) | ||
console.error(`Files checked: ${totalFiles}`) | ||
console.error(`Snippets checked: ${totalSnippets}`) | ||
console.error(`Errors found: ${totalErrors}`) | ||
process.exit(1) | ||
} else { | ||
console.log( | ||
chalk.green( | ||
"\nCode snippet validation passed. All code snippets are valid.", | ||
), | ||
) | ||
console.log(`Files checked: ${totalFiles}`) | ||
console.log(`Snippets checked: ${totalSnippets}`) | ||
} | ||
} | ||
|
||
main().catch(err => { | ||
console.error(err) | ||
process.exit(1) | ||
}) |
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.
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.