@samitouri / QOS-React-2 / commits / 4712017744

Include `Utils.Component()` in BailOnCapitalizedFunctionCalls

`Utils.Component()` wasn't caught by the bailout, and there are such usage on WWW Fixes #671

Tianyu Yao committed Oct 17, 2022 at 14:41 UTC 4712017744735dbb28ca069433bb3f1565ad2cbd
1 file changed +24 -8
compiler/forget/src/MiddleEnd/SketchyCodeCheck.ts
+24 -8
@@ -53,17 +53,18 @@ export function run(
53 const funcBody = func.get("body");
54
55 if (context.opts.flags.bailOnCapitalizedFunctionCalls) {
56 + function isValidFunctionCallName(name: string): boolean {
57 + return (
58 + ALLOWED_CAPITALIZED_STDLIB_FUNCTIONS.has(name) ||
59 + context.opts.allowedCapitalizedUserFunctions.has(name) ||
60 + !/^[A-Z]/.test(name)
61 + );
62 + }
63 +
64 funcBody.traverse({
65 CallExpression(path) {
66 const callee = path.get("callee");
67 if (t.isIdentifier(callee.node)) {
60 - const name = callee.node.name;
61 - if (
62 - ALLOWED_CAPITALIZED_STDLIB_FUNCTIONS.has(name) ||
63 - context.opts.allowedCapitalizedUserFunctions.has(name)
64 - ) {
65 - return;
66 - }
68 // Allow `Module().method()`;
69 if (
70 t.isMemberExpression(path.parent) ||
@@ -71,13 +72,28 @@ export function run(
72 ) {
73 return;
74 }
74 - if (/^[A-Z]/.test(name)) {
75 + const name = callee.node.name;
76 + if (!isValidFunctionCallName(name)) {
77 context.bailout("BailOnCapitalizedFunctionCalls", {
78 code: "E0018",
79 path: callee,
80 context: null,
81 });
82 }
83 + } else if (
84 + t.isMemberExpression(callee.node) ||
85 + t.isOptionalMemberExpression(callee.node)
86 + ) {
87 + const { object, property } = callee.node;
88 + if (t.isIdentifier(object) && t.isIdentifier(property)) {
89 + if (!isValidFunctionCallName(`${object.name}.${property.name}`)) {
90 + context.bailout("BailOnCapitalizedFunctionCalls", {
91 + code: "E0018",
92 + path: callee,
93 + context: null,
94 + });
95 + }
96 + }
97 }
98 },
99 });