-
Notifications
You must be signed in to change notification settings - Fork 49.6k
[compiler][playground] (2/N) Config override panel #34344
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
EugeneChoi4
merged 4 commits into
facebook:main
from
EugeneChoi4:compiler-playground-config/2
Sep 2, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,87 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import parserBabel from 'prettier/plugins/babel'; | ||
import prettierPluginEstree from 'prettier/plugins/estree'; | ||
import * as prettier from 'prettier/standalone'; | ||
import {parseConfigPragmaAsString} from '../../../packages/babel-plugin-react-compiler/src/Utils/TestUtils'; | ||
|
||
/** | ||
* Parse config from pragma and format it with prettier | ||
*/ | ||
export async function parseAndFormatConfig(source: string): Promise<string> { | ||
const pragma = source.substring(0, source.indexOf('\n')); | ||
let configString = parseConfigPragmaAsString(pragma); | ||
if (configString !== '') { | ||
configString = `(${configString})`; | ||
} | ||
|
||
try { | ||
const formatted = await prettier.format(configString, { | ||
semi: true, | ||
parser: 'babel-ts', | ||
plugins: [parserBabel, prettierPluginEstree], | ||
}); | ||
return formatted; | ||
} catch (error) { | ||
console.error('Error formatting config:', error); | ||
return ''; // Return empty string if not valid for now | ||
} | ||
} | ||
|
||
function extractCurlyBracesContent(input: string): string { | ||
const startIndex = input.indexOf('{'); | ||
const endIndex = input.lastIndexOf('}'); | ||
if (startIndex === -1 || endIndex === -1 || endIndex <= startIndex) { | ||
throw new Error('No outer curly braces found in input'); | ||
} | ||
return input.slice(startIndex, endIndex + 1); | ||
} | ||
|
||
function cleanContent(content: string): string { | ||
return content | ||
.replace(/[\r\n]+/g, ' ') | ||
.replace(/\s+/g, ' ') | ||
.trim(); | ||
} | ||
|
||
/** | ||
* Generate a the override pragma comment from a formatted config object string | ||
*/ | ||
export async function generateOverridePragmaFromConfig( | ||
formattedConfigString: string, | ||
): Promise<string> { | ||
const content = extractCurlyBracesContent(formattedConfigString); | ||
const cleanConfig = cleanContent(content); | ||
|
||
// Format the config to ensure it's valid | ||
await prettier.format(`(${cleanConfig})`, { | ||
semi: false, | ||
parser: 'babel-ts', | ||
plugins: [parserBabel, prettierPluginEstree], | ||
}); | ||
|
||
return `// @OVERRIDE:${cleanConfig}`; | ||
} | ||
|
||
/** | ||
* Update the override pragma comment in source code. | ||
*/ | ||
export function updateSourceWithOverridePragma( | ||
source: string, | ||
newPragma: string, | ||
): string { | ||
const firstLineEnd = source.indexOf('\n'); | ||
const firstLine = source.substring(0, firstLineEnd); | ||
|
||
const pragmaRegex = /^\/\/\s*@/; | ||
if (firstLineEnd !== -1 && pragmaRegex.test(firstLine.trim())) { | ||
return newPragma + source.substring(firstLineEnd); | ||
} else { | ||
return newPragma + '\n' + source; | ||
} | ||
} |
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
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.
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.
What kinds of errors are we swallowing here? Are these errors due to parsing? Probably we want to display some user feedback.
Seems like the "Apply" button would allow for a better error handling pattern as well. We could reject invalid content with informative messages.
Fine to land this for now since the plan is to follow up with the button sync instead of live sync anyways.
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.
Yes, the errors should all be parsing errors