@samitouri / QOS-React / commits / 90dd2084e2

ValidateNoCapitalizedCalls checks capitalized methods

Joe Savona committed Apr 3, 2024 at 08:02 UTC 90dd2084e2e3d3083a09cbf781e14ff2610cba6d
3 files changed +55
compiler/packages/babel-plugin-react-forget/src/Validation/ValidateNoCapitalizedCalls.ts
+22
@@ -27,6 +27,7 @@ export function validateNoCapitalizedCalls(fn: HIRFunction): void {
27 };
28
29 const capitalLoadGlobals = new Map<IdentifierId, string>();
30 + const capitalizedProperties = new Map<IdentifierId, string>();
31 for (const [, block] of fn.body.blocks) {
32 for (const { lvalue, value } of block.instructions) {
33 switch (value.kind) {
@@ -54,6 +55,27 @@ export function validateNoCapitalizedCalls(fn: HIRFunction): void {
55 suggestions: null,
56 });
57 }
58 + break;
59 + }
60 + case "PropertyLoad": {
61 + // Start conservative and disallow all capitalized method calls
62 + if (/^[A-Z]/.test(value.property)) {
63 + capitalizedProperties.set(lvalue.identifier.id, value.property);
64 + }
65 + break;
66 + }
67 + case "MethodCall": {
68 + const propertyIdentifier = value.property.identifier.id;
69 + const propertyName = capitalizedProperties.get(propertyIdentifier);
70 + if (propertyName != null) {
71 + CompilerError.throwInvalidReact({
72 + reason: `Capitalized method calls may be calling components that use hooks, which make them dangerous to memoize. Ensure there are no hook calls in the function and rename it to begin with a lowercase letter to fix this error`,
73 + description: `${propertyName} may be a component.`,
74 + loc: value.loc,
75 + suggestions: null,
76 + });
77 + }
78 + break;
79 }
80 }
81 }
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.capitalized-method-call.expect.md new
+27
@@ -0,0 +1,27 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +// @validateNoCapitalizedCalls
6 +function Component() {
7 + const x = someGlobal.SomeFunc();
8 +
9 + return x;
10 +}
11 +
12 +```
13 +
14 +
15 +## Error
16 +
17 +```
18 + 1 | // @validateNoCapitalizedCalls
19 + 2 | function Component() {
20 +> 3 | const x = someGlobal.SomeFunc();
21 + | ^^^^^^^^^^^^^^^^^^^^^ InvalidReact: Capitalized method calls may be calling components that use hooks, which make them dangerous to memoize. Ensure there are no hook calls in the function and rename it to begin with a lowercase letter to fix this error. SomeFunc may be a component. (3:3)
22 + 4 |
23 + 5 | return x;
24 + 6 | }
25 +```
26 +
27 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/error.capitalized-method-call.js new
+6
@@ -0,0 +1,6 @@
1 +// @validateNoCapitalizedCalls
2 +function Component() {
3 + const x = someGlobal.SomeFunc();
4 +
5 + return x;
6 +}