Skip to content

Commit d3da200

Browse files
mvitousekmvitousek
andauthored
[compiler] Remove OXC and SWC plugins in favor of them being handled by those projects (react#36743)
I believe the consensus is that the OXC and SWC plugins should live in those projects, and instead consume the React compiler as a crate --------- Co-authored-by: mvitousek <mvitousek@devvm12588.pnb0.facebook.com>
1 parent b49e041 commit d3da200

105 files changed

Lines changed: 2724 additions & 19722 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

compiler/Cargo.lock

Lines changed: 103 additions & 1711 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compiler/crates/react_compiler/src/debug_print.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ impl<'a> DebugPrinter<'a> {
4343
self.fmt.line(&format!("fn_type: {:?}", func.fn_type));
4444
self.fmt.line(&format!("generator: {}", func.generator));
4545
self.fmt.line(&format!("is_async: {}", func.is_async));
46-
self.fmt.line(&format!("loc: {}", print::format_loc(&func.loc)));
46+
self.fmt
47+
.line(&format!("loc: {}", print::format_loc(&func.loc)));
4748

4849
// params
4950
self.fmt.line("params:");
@@ -83,7 +84,8 @@ impl<'a> DebugPrinter<'a> {
8384
self.fmt.line("aliasingEffects:");
8485
self.fmt.indent();
8586
for (i, eff) in effects.iter().enumerate() {
86-
self.fmt.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
87+
self.fmt
88+
.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
8789
}
8890
self.fmt.dedent();
8991
}
@@ -127,7 +129,8 @@ impl<'a> DebugPrinter<'a> {
127129
block: &BasicBlock,
128130
instructions: &[Instruction],
129131
) {
130-
self.fmt.line(&format!("bb{} ({}):", block_id.0, block.kind));
132+
self.fmt
133+
.line(&format!("bb{} ({}):", block_id.0, block.kind));
131134
self.fmt.indent();
132135

133136
// preds
@@ -223,13 +226,15 @@ impl<'a> DebugPrinter<'a> {
223226
self.fmt.line("effects:");
224227
self.fmt.indent();
225228
for (i, eff) in effects.iter().enumerate() {
226-
self.fmt.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
229+
self.fmt
230+
.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
227231
}
228232
self.fmt.dedent();
229233
}
230234
None => self.fmt.line("effects: null"),
231235
}
232-
self.fmt.line(&format!("loc: {}", print::format_loc(&instr.loc)));
236+
self.fmt
237+
.line(&format!("loc: {}", print::format_loc(&instr.loc)));
233238
self.fmt.dedent();
234239
self.fmt.line("}");
235240
}
@@ -346,14 +351,16 @@ impl<'a> DebugPrinter<'a> {
346351
self.fmt.line("Return {");
347352
self.fmt.indent();
348353
self.fmt.line(&format!("id: {}", id.0));
349-
self.fmt.line(&format!("returnVariant: {:?}", return_variant));
354+
self.fmt
355+
.line(&format!("returnVariant: {:?}", return_variant));
350356
self.fmt.format_place_field("value", value);
351357
match effects {
352358
Some(e) => {
353359
self.fmt.line("effects:");
354360
self.fmt.indent();
355361
for (i, eff) in e.iter().enumerate() {
356-
self.fmt.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
362+
self.fmt
363+
.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
357364
}
358365
self.fmt.dedent();
359366
}
@@ -402,10 +409,8 @@ impl<'a> DebugPrinter<'a> {
402409
self.fmt.line("}");
403410
}
404411
None => {
405-
self.fmt.line(&format!(
406-
"[{}] Default {{ block: bb{} }}",
407-
i, case.block.0
408-
));
412+
self.fmt
413+
.line(&format!("[{}] Default {{ block: bb{} }}", i, case.block.0));
409414
}
410415
}
411416
}
@@ -566,7 +571,8 @@ impl<'a> DebugPrinter<'a> {
566571
self.fmt.line("MaybeThrow {");
567572
self.fmt.indent();
568573
self.fmt.line(&format!("id: {}", id.0));
569-
self.fmt.line(&format!("continuation: bb{}", continuation.0));
574+
self.fmt
575+
.line(&format!("continuation: bb{}", continuation.0));
570576
self.fmt.line(&format!(
571577
"handler: {}",
572578
match handler {
@@ -579,7 +585,8 @@ impl<'a> DebugPrinter<'a> {
579585
self.fmt.line("effects:");
580586
self.fmt.indent();
581587
for (i, eff) in e.iter().enumerate() {
582-
self.fmt.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
588+
self.fmt
589+
.line(&format!("[{}] {}", i, self.fmt.format_effect(eff)));
583590
}
584591
self.fmt.dedent();
585592
}
@@ -686,10 +693,7 @@ pub fn format_errors(error: &CompilerError) -> String {
686693
/// Format an HIR function into a reactive PrintFormatter.
687694
/// This bridges the two debug printers so inner functions in FunctionExpression/ObjectMethod
688695
/// can be printed within the reactive function output.
689-
pub fn format_hir_function_into(
690-
reactive_fmt: &mut PrintFormatter,
691-
func: &HirFunction,
692-
) {
696+
pub fn format_hir_function_into(reactive_fmt: &mut PrintFormatter, func: &HirFunction) {
693697
// Create a temporary DebugPrinter that shares the same environment
694698
let mut printer = DebugPrinter {
695699
fmt: PrintFormatter {

compiler/crates/react_compiler/src/entrypoint/compile_result.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use react_compiler_ast::File;
12
use react_compiler_ast::expressions::Identifier as AstIdentifier;
23
use react_compiler_ast::patterns::PatternLike;
34
use react_compiler_ast::statements::BlockStatement;
4-
use react_compiler_ast::File;
55
use react_compiler_diagnostics::SourceLocation;
66
use react_compiler_hir::ReactFunctionType;
77
use serde::Serialize;
@@ -30,7 +30,12 @@ pub struct LoggerPosition {
3030

3131
impl LoggerSourceLocation {
3232
/// Create from a diagnostics SourceLocation, adding index and filename.
33-
pub fn from_loc(loc: &SourceLocation, filename: Option<&str>, start_index: Option<u32>, end_index: Option<u32>) -> Self {
33+
pub fn from_loc(
34+
loc: &SourceLocation,
35+
filename: Option<&str>,
36+
start_index: Option<u32>,
37+
end_index: Option<u32>,
38+
) -> Self {
3439
Self {
3540
start: LoggerPosition {
3641
line: loc.start.line,

compiler/crates/react_compiler/src/entrypoint/imports.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ use react_compiler_ast::declarations::{
1212
};
1313
use react_compiler_ast::expressions::{CallExpression, Expression, Identifier};
1414
use react_compiler_ast::literals::StringLiteral;
15-
use react_compiler_ast::patterns::{ObjectPattern, ObjectPatternProp, ObjectPatternProperty, PatternLike};
15+
use react_compiler_ast::patterns::{
16+
ObjectPattern, ObjectPatternProp, ObjectPatternProperty, PatternLike,
17+
};
1618
use react_compiler_ast::scope::ScopeInfo;
1719
use react_compiler_ast::statements::{
1820
Statement, VariableDeclaration, VariableDeclarationKind, VariableDeclarator,
1921
};
2022
use react_compiler_ast::{Program, SourceType};
21-
use react_compiler_diagnostics::{CompilerError, CompilerErrorDetail, ErrorCategory, Position, SourceLocation};
23+
use react_compiler_diagnostics::{
24+
CompilerError, CompilerErrorDetail, ErrorCategory, Position, SourceLocation,
25+
};
2226

2327
use super::compile_result::{DebugLogEntry, LoggerEvent, OrderedLogItem};
2428
use super::plugin_options::{CompilerTarget, PluginOptions};
@@ -238,7 +242,9 @@ impl ProgramContext {
238242

239243
/// Log a compilation event.
240244
pub fn log_event(&mut self, event: LoggerEvent) {
241-
self.ordered_log.push(OrderedLogItem::Event { event: event.clone() });
245+
self.ordered_log.push(OrderedLogItem::Event {
246+
event: event.clone(),
247+
});
242248
self.events.push(event);
243249
}
244250

@@ -280,8 +286,16 @@ pub fn validate_restricted_imports(
280286
)
281287
.with_description(format!("Import from module {}", import.source.value));
282288
detail.loc = import.base.loc.as_ref().map(|loc| SourceLocation {
283-
start: Position { line: loc.start.line, column: loc.start.column, index: loc.start.index },
284-
end: Position { line: loc.end.line, column: loc.end.column, index: loc.end.index },
289+
start: Position {
290+
line: loc.start.line,
291+
column: loc.start.column,
292+
index: loc.start.index,
293+
},
294+
end: Position {
295+
line: loc.end.line,
296+
column: loc.end.column,
297+
index: loc.end.index,
298+
},
285299
});
286300
error.push_error_detail(detail);
287301
}

compiler/crates/react_compiler/src/entrypoint/program.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,7 @@ fn find_directives_dynamic_gating<'a>(
235235
/// `^use memo if\(([^\)]*)\)$`: the condition may not contain `)` and the
236236
/// directive must end at the closing paren.
237237
fn parse_dynamic_gating_directive(value: &str) -> Option<&str> {
238-
let condition = value
239-
.strip_prefix("use memo if(")?
240-
.strip_suffix(')')?;
238+
let condition = value.strip_prefix("use memo if(")?.strip_suffix(')')?;
241239
if condition.contains(')') {
242240
return None;
243241
}
@@ -2198,11 +2196,12 @@ fn raw_node_references_identifier(value: &serde_json::Value, name: &str) -> bool
21982196
{
21992197
return true;
22002198
}
2201-
map.values().any(|v| raw_node_references_identifier(v, name))
2202-
}
2203-
serde_json::Value::Array(items) => {
2204-
items.iter().any(|v| raw_node_references_identifier(v, name))
2199+
map.values()
2200+
.any(|v| raw_node_references_identifier(v, name))
22052201
}
2202+
serde_json::Value::Array(items) => items
2203+
.iter()
2204+
.any(|v| raw_node_references_identifier(v, name)),
22062205
_ => false,
22072206
}
22082207
}

compiler/crates/react_compiler/src/entrypoint/suppression.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,48 @@ fn comment_data(comment: &Comment) -> &CommentData {
3838
/// Check if a comment value matches `eslint-disable-next-line <rule>` for any rule in `rule_names`.
3939
fn matches_eslint_disable_next_line(value: &str, rule_names: &[String]) -> bool {
4040
if let Some(rest) = value.strip_prefix("eslint-disable-next-line ") {
41-
return rule_names.iter().any(|name| rest.starts_with(name.as_str()));
41+
return rule_names
42+
.iter()
43+
.any(|name| rest.starts_with(name.as_str()));
4244
}
4345
// Also check with leading space (comment values often have leading whitespace)
4446
let trimmed = value.trim_start();
4547
if let Some(rest) = trimmed.strip_prefix("eslint-disable-next-line ") {
46-
return rule_names.iter().any(|name| rest.starts_with(name.as_str()));
48+
return rule_names
49+
.iter()
50+
.any(|name| rest.starts_with(name.as_str()));
4751
}
4852
false
4953
}
5054

5155
/// Check if a comment value matches `eslint-disable <rule>` for any rule in `rule_names`.
5256
fn matches_eslint_disable(value: &str, rule_names: &[String]) -> bool {
5357
if let Some(rest) = value.strip_prefix("eslint-disable ") {
54-
return rule_names.iter().any(|name| rest.starts_with(name.as_str()));
58+
return rule_names
59+
.iter()
60+
.any(|name| rest.starts_with(name.as_str()));
5561
}
5662
let trimmed = value.trim_start();
5763
if let Some(rest) = trimmed.strip_prefix("eslint-disable ") {
58-
return rule_names.iter().any(|name| rest.starts_with(name.as_str()));
64+
return rule_names
65+
.iter()
66+
.any(|name| rest.starts_with(name.as_str()));
5967
}
6068
false
6169
}
6270

6371
/// Check if a comment value matches `eslint-enable <rule>` for any rule in `rule_names`.
6472
fn matches_eslint_enable(value: &str, rule_names: &[String]) -> bool {
6573
if let Some(rest) = value.strip_prefix("eslint-enable ") {
66-
return rule_names.iter().any(|name| rest.starts_with(name.as_str()));
74+
return rule_names
75+
.iter()
76+
.any(|name| rest.starts_with(name.as_str()));
6777
}
6878
let trimmed = value.trim_start();
6979
if let Some(rest) = trimmed.strip_prefix("eslint-enable ") {
70-
return rule_names.iter().any(|name| rest.starts_with(name.as_str()));
80+
return rule_names
81+
.iter()
82+
.any(|name| rest.starts_with(name.as_str()));
7183
}
7284
false
7385
}
@@ -135,10 +147,7 @@ pub fn find_program_suppressions(
135147
}
136148

137149
// Check for Flow suppression (only if not already within a block)
138-
if flow_suppressions
139-
&& disable_comment.is_none()
140-
&& matches_flow_suppression(&data.value)
141-
{
150+
if flow_suppressions && disable_comment.is_none() && matches_flow_suppression(&data.value) {
142151
disable_comment = Some(data.clone());
143152
enable_comment = Some(data.clone());
144153
source = Some(SuppressionSource::Flow);

compiler/crates/react_compiler/src/entrypoint/validate_source_locations.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ use react_compiler_ast::expressions::{
1919
ObjectExpressionProperty,
2020
};
2121
use react_compiler_ast::patterns::PatternLike;
22-
use react_compiler_ast::statements::{ForInit, ForInOfLeft, Statement, VariableDeclaration};
22+
use react_compiler_ast::statements::{ForInOfLeft, ForInit, Statement, VariableDeclaration};
2323
use react_compiler_diagnostics::{
24-
CompilerDiagnostic, CompilerDiagnosticDetail, ErrorCategory,
25-
SourceLocation as DiagSourceLocation, Position as DiagPosition,
24+
CompilerDiagnostic, CompilerDiagnosticDetail, ErrorCategory, Position as DiagPosition,
25+
SourceLocation as DiagSourceLocation,
2626
};
2727
use react_compiler_hir::environment::Environment;
2828
use react_compiler_lowering::FunctionNode;
@@ -54,7 +54,10 @@ pub fn validate_source_locations(
5454
// (JS Map preserves insertion order, which is AST traversal order = source order)
5555
let mut sorted_entries: Vec<&ImportantLocation> = important_original.values().collect();
5656
sorted_entries.sort_by(|a, b| {
57-
a.loc.start.line.cmp(&b.loc.start.line)
57+
a.loc
58+
.start
59+
.line
60+
.cmp(&b.loc.start.line)
5861
.then(a.loc.start.column.cmp(&b.loc.start.column))
5962
// Outer nodes (larger spans) before inner nodes, matching depth-first traversal
6063
.then(b.loc.end.line.cmp(&a.loc.end.line))
@@ -85,12 +88,7 @@ pub fn validate_source_locations(
8588
if has_valid_node_type {
8689
report_missing_location(env, &entry.loc, node_type);
8790
} else {
88-
report_wrong_node_type(
89-
env,
90-
&entry.loc,
91-
node_type,
92-
generated_node_types,
93-
);
91+
report_wrong_node_type(env, &entry.loc, node_type, generated_node_types);
9492
}
9593
}
9694
}
@@ -228,15 +226,12 @@ fn important_expression_type(expr: &Expression) -> Option<&'static str> {
228226
fn is_manual_memoization(expr: &Expression) -> bool {
229227
if let Expression::CallExpression(call) = expr {
230228
match call.callee.as_ref() {
231-
Expression::Identifier(id) => {
232-
id.name == "useMemo" || id.name == "useCallback"
233-
}
229+
Expression::Identifier(id) => id.name == "useMemo" || id.name == "useCallback",
234230
Expression::MemberExpression(mem) => {
235231
if let (Expression::Identifier(obj), Expression::Identifier(prop)) =
236232
(mem.object.as_ref(), &*mem.property)
237233
{
238-
obj.name == "React"
239-
&& (prop.name == "useMemo" || prop.name == "useCallback")
234+
obj.name == "React" && (prop.name == "useMemo" || prop.name == "useCallback")
240235
} else {
241236
false
242237
}
@@ -679,8 +674,7 @@ fn collect_original_arrow_children(
679674
}
680675
match arrow.body.as_ref() {
681676
ArrowFunctionBody::BlockStatement(block) => {
682-
let is_single_return =
683-
block.body.len() == 1 && block.directives.is_empty();
677+
let is_single_return = block.body.len() == 1 && block.directives.is_empty();
684678
collect_original_block(&block.body, is_single_return, locations);
685679
}
686680
ArrowFunctionBody::Expression(expr) => {
@@ -741,10 +735,7 @@ fn collect_original_pattern(
741735
collect_original_pattern(&r.argument, locations);
742736
}
743737
PatternLike::MemberExpression(m) => {
744-
collect_original_expression(
745-
&Expression::MemberExpression(m.clone()),
746-
locations,
747-
);
738+
collect_original_expression(&Expression::MemberExpression(m.clone()), locations);
748739
}
749740
PatternLike::TSAsExpression(_)
750741
| PatternLike::TSSatisfiesExpression(_)
@@ -882,10 +873,7 @@ fn record_generated(
882873
}
883874
}
884875

885-
fn collect_generated_statement(
886-
stmt: &Statement,
887-
locations: &mut HashMap<String, HashSet<String>>,
888-
) {
876+
fn collect_generated_statement(stmt: &Statement, locations: &mut HashMap<String, HashSet<String>>) {
889877
// Record this statement's location
890878
let type_name = statement_type_name(stmt);
891879
record_generated(type_name, statement_loc(stmt), locations);

compiler/crates/react_compiler_ast/src/common.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ impl RawNode {
5555
/// RawNode text is valid JSON by construction, so failure here means a
5656
/// broken invariant, not bad input; fail loudly rather than degrade.
5757
pub fn parse_value(&self) -> serde_json::Value {
58-
from_json_str_unbounded(self.0.get())
59-
.expect("RawNode holds valid JSON by construction")
58+
from_json_str_unbounded(self.0.get()).expect("RawNode holds valid JSON by construction")
6059
}
6160

6261
/// The node's `"type"` field, without parsing the whole subtree into a Value.

compiler/crates/react_compiler_ast/src/jsx.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ pub struct JSXElement {
1414
#[serde(rename = "closingElement")]
1515
pub closing_element: Option<JSXClosingElement>,
1616
pub children: Vec<JSXChild>,
17-
#[serde(rename = "selfClosing", default, skip_serializing_if = "Option::is_none")]
17+
#[serde(
18+
rename = "selfClosing",
19+
default,
20+
skip_serializing_if = "Option::is_none"
21+
)]
1822
pub self_closing: Option<bool>,
1923
}
2024

0 commit comments

Comments
 (0)