Skip to content
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

feat: check code examples syntax #7396

Closed
wants to merge 2 commits into from

Conversation

araujogui
Copy link
Member

@araujogui araujogui commented Jan 8, 2025

Description

Check if JavaScript code blocks have valid syntax using the acorn parser. We could use Node's vm module to execute the code, but I feel it would be slower and wouldn't work with incomplete code examples.

Validation

Manually edit any code example to broken code

Related Issues

Fixes #7293

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run npm run format to ensure the code follows the style guide.
  • I have run npm run test to check if all tests are passing.
  • I have run npx turbo build to check if the website builds without errors.
  • I've covered new added functionality with unit tests if necessary.

@araujogui araujogui requested a review from a team as a code owner January 8, 2025 18:47
Copy link

vercel bot commented Jan 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
nodejs-org ✅ Ready (Inspect) Visit Preview Jan 8, 2025 7:09pm

Copy link
Member

@AugustinMauroy AugustinMauroy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM just one question

apps/site/next.mdx.shiki.mjs Outdated Show resolved Hide resolved
@bmuenzenmeyer
Copy link
Collaborator

why didn't this catch #7402 ? that seems like a pretty great test case for this...

@araujogui
Copy link
Member Author

why didn't this catch #7402 ? that seems like a pretty great test case for this...

Yep, I will investigate this.

@araujogui
Copy link
Member Author

why didn't this catch #7402 ? that seems like a pretty great test case for this...

I looked into it, and it turns out this is somewhat expected—the code syntax is correct.

@@ -168,6 +192,11 @@ export default function rehypeShikiji() {
// Grabs the relevant alias/name of the language
const languageId = codeLanguage.slice(languagePrefix.length);

// Check code for syntax errors
if (!checkCodeSyntax(preElementContents, languageId)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good idea? I would rather have a separate CI process that verifies this rather than Acorn right here. Otherwise it will just dramatically slow down the build process.

Copy link
Member

@ovflowd ovflowd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have mixed feelings with its current implementation. I think this should be a standalone script (like we have on the apps/site/scripts folder) and that is added to the package.json and then invoked as one of the test/linting steps.

@araujogui
Copy link
Member Author

I have mixed feelings with its current implementation. I think this should be a standalone script (like we have on the apps/site/scripts folder) and that is added to the package.json and then invoked as one of the test/linting steps.

I agree, I will create this script

@araujogui
Copy link
Member Author

Closing this PR, I will create another one when the script is ready.

@araujogui araujogui closed this Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make sure all code examples are runnable
5 participants