-
Notifications
You must be signed in to change notification settings - Fork 842
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
Fixed a bug in EuiCodeBlock docs where import statement example was raised up and evaluated. #6595
Fixed a bug in EuiCodeBlock docs where import statement example was raised up and evaluated. #6595
Conversation
…aised up and evaluated.
Preview documentation changes for this PR: https://eui.elastic.co/pr_6595/ |
@1Copenut IMO we shouldn't change the example. But we should fix the regex that transforms the demo code to the If we pass a code like the following one: const jsCode = `/* I'm an example of JS */
import React from 'react';`; When we open the The regex that generates the CC @cee-chen. |
Excellent call. I refactored the regex that populates the |
Preview documentation changes for this PR: https://eui.elastic.co/pr_6595/ |
@@ -80,7 +79,7 @@ export const renderJsSourceCode = (code) => { | |||
// import - import + whitespace | |||
// ([^]+?) - capture any characters (including newlines) | |||
// from ('[A-Za-z0-9 -_.@/]*';) - ` from 'someLibrary';` - alphanumeric and certain special characters only | |||
/(\/\/.+\n)?import ([^]+?) from ('[A-Za-z0-9 -_.@/]*';)/g, | |||
/(\/\/.+\n)?import ((?!React)[^]+?) from ('[A-Za-z0-9 -_.@/]*';)/g, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this works, I'm not a fan because it's not actually targeting the issue we want to solve - which is to say, it's finding an import
string within a code snippet that isn't actually an import. So if the import snippet in question changed to import foo from 'bar'
, we'd still have the exact same issue.
I would strongly rather fix this with a negative lookbehind to find any backtick character before any import
statement and not React
specifically. You should be able to accomplish this like so:
/(\/\/.+\n)?import ((?!React)[^]+?) from ('[A-Za-z0-9 -_.@/]*';)/g, | |
/(?<!(`[\s\S]*))(\/\/.+\n)?import ([^]+?) from ('[A-Za-z0-9 -_.@/]*';)/g, |
Since regexes are incredibly hard to read, we should also document this new addition in the code above line 78:
// (?<!(`[\s\S]*)) - negative lookbehind ensuring that import statements aren't part of a template literal
// (\/\/.+\n)? - optional preceding comments that must be above specific imports, e.g. // @ts-ignore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be no scenario where a real import comes after a template literal backtick - our current eslint rules ensure that all legitimate import
s are at the top of the file, so this is a fairly safe assertion to make.
Code changes LGTM - waiting for CI/staging to update and will approve then |
Preview documentation changes for this PR: https://eui.elastic.co/pr_6595/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Staging LGTM! Nice attention to detail when catching this bug Trevor!
Summary
EUI end user reported a bug in the
EuiCodeBlock
sandbox this weekend. After investigating I determined the sampleimport React from 'react'
was being raised up and evaluated by CodeSandbox instead of being rendered as a code block. Ichanged the block to a general JS object and tested locally to ensure it rendered properly.adjusted the regex logic to ignore the string "React" in code comments.Before
After
QA
Remove or strikethrough items that do not apply to your PR.
General checklist