-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Expand file tree
/
Copy pathreact-compiler.js
More file actions
124 lines (105 loc) · 3.05 KB
/
react-compiler.js
File metadata and controls
124 lines (105 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* 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.
*/
const {transformFromAstSync} = require('@babel/core');
const {parse: babelParse} = require('@babel/parser');
const BabelPluginReactCompiler = require('babel-plugin-react-compiler').default;
const {
parsePluginOptions,
validateEnvironmentConfig,
} = require('babel-plugin-react-compiler');
const COMPILER_OPTIONS = {
noEmit: true,
panicThreshold: 'none',
environment: validateEnvironmentConfig({
validateRefAccessDuringRender: true,
validateNoSetStateInRender: true,
validateNoSetStateInEffects: true,
validateNoJSXInTryStatements: true,
validateNoImpureFunctionsInRender: true,
validateStaticComponents: true,
validateNoFreezingKnownMutableFunctions: true,
validateNoVoidUseMemo: true,
validateNoCapitalizedCalls: [],
validateHooksUsage: true,
validateNoDerivedComputationsInEffects: true,
}),
};
function hasRelevantCode(code) {
const functionPattern = /^(export\s+)?(default\s+)?function\s+\w+/m;
const arrowPattern =
/^(export\s+)?(const|let|var)\s+\w+\s*=\s*(\([^)]*\)|\w+)\s*=>/m;
const hasImports = /^import\s+/m.test(code);
return functionPattern.test(code) || arrowPattern.test(code) || hasImports;
}
function runReactCompiler(code, filename) {
const result = {
sourceCode: code,
events: [],
};
if (!hasRelevantCode(code)) {
return {...result, diagnostics: []};
}
const options = parsePluginOptions({
...COMPILER_OPTIONS,
});
options.logger = {
logEvent: (_, event) => {
if (event.kind === 'CompileError') {
const category = event.detail?.category;
if (category === 'Todo' || category === 'Invariant') {
return;
}
result.events.push(event);
}
},
};
try {
const ast = babelParse(code, {
sourceFilename: filename,
sourceType: 'module',
plugins: ['jsx', 'typescript'],
});
transformFromAstSync(ast, code, {
filename,
highlightCode: false,
retainLines: true,
plugins: [[BabelPluginReactCompiler, options]],
sourceType: 'module',
configFile: false,
babelrc: false,
});
} catch {
return {...result, diagnostics: []};
}
const diagnostics = [];
for (const event of result.events) {
if (event.kind !== 'CompileError') {
continue;
}
const detail = event.detail;
if (!detail) {
continue;
}
const loc =
typeof detail.primaryLocation === 'function'
? detail.primaryLocation()
: null;
if (loc == null || typeof loc === 'symbol') {
continue;
}
const message =
typeof detail.printErrorMessage === 'function'
? detail.printErrorMessage(result.sourceCode, {eslint: true})
: detail.description || 'Unknown React Compiler error';
diagnostics.push({detail, loc, message});
}
return {...result, diagnostics};
}
module.exports = {
hasRelevantCode,
runReactCompiler,
};