-
Notifications
You must be signed in to change notification settings - Fork 48.8k
[compiler][playground][tests] Standardize more pragmas #33146
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
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion
2
...r/apps/playground/__tests__/e2e/__snapshots__/page.spec.ts/compilationMode-all-output.txt
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
2 changes: 1 addition & 1 deletion
2
...apps/playground/__tests__/e2e/__snapshots__/page.spec.ts/compilationMode-infer-output.txt
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// @compilationMode(infer) | ||
// @compilationMode:"infer" | ||
function nonReactFn() { | ||
return {}; | ||
} |
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 |
---|---|---|
|
@@ -10,7 +10,6 @@ import {CompilerError} from '../CompilerError'; | |
import { | ||
CompilationMode, | ||
defaultOptions, | ||
PanicThresholdOptions, | ||
parsePluginOptions, | ||
PluginOptions, | ||
} from '../Entrypoint'; | ||
|
@@ -19,14 +18,24 @@ import { | |
EnvironmentConfigSchema, | ||
PartialEnvironmentConfig, | ||
} from '../HIR/Environment'; | ||
import {Err, Ok, Result} from './Result'; | ||
import {hasOwnProperty} from './utils'; | ||
|
||
function tryParseTestPragmaValue(val: string): Result<unknown, unknown> { | ||
try { | ||
let parsedVal: unknown; | ||
const stringMatch = /^"([^"]*)"$/.exec(val); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was needed for |
||
if (stringMatch && stringMatch.length > 1) { | ||
parsedVal = stringMatch[1]; | ||
} else { | ||
parsedVal = JSON.parse(val); | ||
} | ||
return Ok(parsedVal); | ||
} catch (e) { | ||
return Err(e); | ||
} | ||
} | ||
|
||
/** | ||
* For test fixtures and playground only. | ||
* | ||
* Pragmas are straightforward to parse for boolean options (`:true` and | ||
* `:false`). These are 'enabled' config values for non-boolean configs (i.e. | ||
* what is used when parsing `:true`). | ||
*/ | ||
const testComplexConfigDefaults: PartialEnvironmentConfig = { | ||
validateNoCapitalizedCalls: [], | ||
enableChangeDetectionForDebugging: { | ||
|
@@ -84,34 +93,37 @@ const testComplexConfigDefaults: PartialEnvironmentConfig = { | |
}, | ||
], | ||
}; | ||
|
||
/** | ||
* For snap test fixtures and playground only. | ||
*/ | ||
function parseConfigPragmaEnvironmentForTest( | ||
pragma: string, | ||
): EnvironmentConfig { | ||
const maybeConfig: any = {}; | ||
// Get the defaults to programmatically check for boolean properties | ||
const defaultConfig = EnvironmentConfigSchema.parse({}); | ||
const maybeConfig: Partial<Record<keyof EnvironmentConfig, unknown>> = {}; | ||
|
||
for (const token of pragma.split(' ')) { | ||
if (!token.startsWith('@')) { | ||
continue; | ||
} | ||
const keyVal = token.slice(1); | ||
let [key, val = undefined] = keyVal.split(':'); | ||
const valIdx = keyVal.indexOf(':'); | ||
const key = valIdx === -1 ? keyVal : keyVal.slice(0, valIdx); | ||
const val = valIdx === -1 ? undefined : keyVal.slice(valIdx + 1); | ||
const isSet = val === undefined || val === 'true'; | ||
|
||
if (isSet && key in testComplexConfigDefaults) { | ||
maybeConfig[key] = | ||
testComplexConfigDefaults[key as keyof PartialEnvironmentConfig]; | ||
if (!hasOwnProperty(EnvironmentConfigSchema.shape, key)) { | ||
continue; | ||
} | ||
|
||
if (key === 'customMacros' && val) { | ||
const valSplit = val.split('.'); | ||
if (valSplit.length > 0) { | ||
if (isSet && key in testComplexConfigDefaults) { | ||
maybeConfig[key] = testComplexConfigDefaults[key]; | ||
} else if (isSet) { | ||
maybeConfig[key] = true; | ||
} else if (val === 'false') { | ||
maybeConfig[key] = false; | ||
} else if (val) { | ||
const parsedVal = tryParseTestPragmaValue(val).unwrap(); | ||
if (key === 'customMacros' && typeof parsedVal === 'string') { | ||
const valSplit = parsedVal.split('.'); | ||
const props = []; | ||
for (const elt of valSplit.slice(1)) { | ||
if (elt === '*') { | ||
|
@@ -121,21 +133,9 @@ function parseConfigPragmaEnvironmentForTest( | |
} | ||
} | ||
maybeConfig[key] = [[valSplit[0], props]]; | ||
continue; | ||
} | ||
continue; | ||
} | ||
|
||
if ( | ||
key !== 'enableResetCacheOnSourceFileChanges' && | ||
typeof defaultConfig[key as keyof EnvironmentConfig] !== 'boolean' | ||
) { | ||
// skip parsing non-boolean properties | ||
continue; | ||
} | ||
if (val === undefined || val === 'true') { | ||
maybeConfig[key] = true; | ||
} else { | ||
maybeConfig[key] = false; | ||
maybeConfig[key] = parsedVal; | ||
} | ||
} | ||
const config = EnvironmentConfigSchema.safeParse(maybeConfig); | ||
|
@@ -156,51 +156,55 @@ function parseConfigPragmaEnvironmentForTest( | |
suggestions: null, | ||
}); | ||
} | ||
|
||
const testComplexPluginOptionDefaults: Partial<PluginOptions> = { | ||
gating: { | ||
source: 'ReactForgetFeatureFlag', | ||
importSpecifierName: 'isForgetEnabled_Fixtures', | ||
}, | ||
}; | ||
export function parseConfigPragmaForTests( | ||
pragma: string, | ||
defaults: { | ||
compilationMode: CompilationMode; | ||
}, | ||
): PluginOptions { | ||
const environment = parseConfigPragmaEnvironmentForTest(pragma); | ||
let compilationMode: CompilationMode = defaults.compilationMode; | ||
let panicThreshold: PanicThresholdOptions = 'all_errors'; | ||
let noEmit: boolean = defaultOptions.noEmit; | ||
const options: Record<keyof PluginOptions, unknown> = { | ||
...defaultOptions, | ||
panicThreshold: 'all_errors', | ||
compilationMode: defaults.compilationMode, | ||
environment, | ||
}; | ||
for (const token of pragma.split(' ')) { | ||
if (!token.startsWith('@')) { | ||
continue; | ||
} | ||
switch (token) { | ||
case '@compilationMode(annotation)': { | ||
compilationMode = 'annotation'; | ||
break; | ||
} | ||
case '@compilationMode(infer)': { | ||
compilationMode = 'infer'; | ||
break; | ||
} | ||
case '@compilationMode(all)': { | ||
compilationMode = 'all'; | ||
break; | ||
} | ||
case '@compilationMode(syntax)': { | ||
compilationMode = 'syntax'; | ||
break; | ||
} | ||
case '@panicThreshold(none)': { | ||
panicThreshold = 'none'; | ||
break; | ||
} | ||
case '@noEmit': { | ||
noEmit = true; | ||
break; | ||
const keyVal = token.slice(1); | ||
const idx = keyVal.indexOf(':'); | ||
const key = idx === -1 ? keyVal : keyVal.slice(0, idx); | ||
const val = idx === -1 ? undefined : keyVal.slice(idx + 1); | ||
if (!hasOwnProperty(defaultOptions, key)) { | ||
continue; | ||
} | ||
const isSet = val === undefined || val === 'true'; | ||
if (isSet && key in testComplexPluginOptionDefaults) { | ||
options[key] = testComplexPluginOptionDefaults[key]; | ||
} else if (isSet) { | ||
options[key] = true; | ||
} else if (val === 'false') { | ||
options[key] = false; | ||
} else if (val != null) { | ||
const parsedVal = tryParseTestPragmaValue(val).unwrap(); | ||
if (key === 'target' && parsedVal === 'donotuse_meta_internal') { | ||
options[key] = { | ||
kind: parsedVal, | ||
runtimeModule: 'react', | ||
}; | ||
} else { | ||
options[key] = parsedVal; | ||
} | ||
} | ||
} | ||
return parsePluginOptions({ | ||
environment, | ||
compilationMode, | ||
panicThreshold, | ||
noEmit, | ||
}); | ||
return parsePluginOptions(options); | ||
} |
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
2 changes: 1 addition & 1 deletion
2
...gin-react-compiler/src/__tests__/fixtures/compiler/arrow-function-with-implicit-return.js
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
2 changes: 1 addition & 1 deletion
2
...ugin-react-compiler/src/__tests__/fixtures/compiler/class-component-with-render-helper.js
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
2 changes: 1 addition & 1 deletion
2
...l-plugin-react-compiler/src/__tests__/fixtures/compiler/codegen-instrument-forget-test.js
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
2 changes: 1 addition & 1 deletion
2
...plugin-react-compiler/src/__tests__/fixtures/compiler/component-declaration-basic.flow.js
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
2 changes: 1 addition & 1 deletion
2
...ugin-react-compiler/src/__tests__/fixtures/compiler/conflict-codegen-instrument-forget.js
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
2 changes: 1 addition & 1 deletion
2
...t-compiler/src/__tests__/fixtures/compiler/error.bailout-on-suppression-of-custom-rule.js
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.
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.
We're already spreading
defaultOptions
when parsing PluginOptions, which would seteslintSuppressionRules
andsources
if they were undefined in thePartial<PluginOptions>
parameter. This type change should be a no-op