@samitouri / QOS-React-2 / commits / 95fe4ed0ed

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 22:44 UTC 95fe4ed0ed2cafb16f6a12667487f7d5151404e4
1 file changed +32 -8
compiler/forget/src/MiddleEnd/SketchyCodeCheck.ts
+32 -8
@@ -45,6 +45,24 @@ const ALLOWED_CAPITALIZED_STDLIB_FUNCTIONS = new Set([
45 "URIError",
46 ]);
47
48 +function getStaticCallee(expression: t.Node): string | null {
49 + if (t.isIdentifier(expression)) {
50 + return expression.name;
51 + }
52 + if (
53 + t.isMemberExpression(expression) ||
54 + t.isOptionalMemberExpression(expression)
55 + ) {
56 + const { object, property } = expression;
57 + const objectName = getStaticCallee(object);
58 + const propertyName = getStaticCallee(property);
59 + if (objectName && propertyName) {
60 + return `${objectName}.${propertyName}`;
61 + }
62 + }
63 + return null;
64 +}
65 +
66 export function run(
67 _irFunc: IR.Func,
68 func: NodePath<t.Function>,
@@ -53,11 +71,14 @@ export function run(
71 const funcBody = func.get("body");
72
73 if (context.opts.flags.bailOnCapitalizedFunctionCalls) {
56 - function isValidFunctionCallName(name: string): boolean {
74 + function isValidFunctionCallName(
75 + functionName: string,
76 + fullName: string
77 + ): boolean {
78 return (
58 - ALLOWED_CAPITALIZED_STDLIB_FUNCTIONS.has(name) ||
59 - context.opts.allowedCapitalizedUserFunctions.has(name) ||
60 - !/^[A-Z]/.test(name)
79 + ALLOWED_CAPITALIZED_STDLIB_FUNCTIONS.has(fullName) ||
80 + context.opts.allowedCapitalizedUserFunctions.has(fullName) ||
81 + !/^[A-Z]/.test(functionName)
82 );
83 }
84
@@ -73,7 +94,7 @@ export function run(
94 return;
95 }
96 const name = callee.node.name;
76 - if (!isValidFunctionCallName(name)) {
97 + if (!isValidFunctionCallName(name, name)) {
98 context.bailout("BailOnCapitalizedFunctionCalls", {
99 code: "E0018",
100 path: callee,
@@ -84,9 +105,12 @@ export function run(
105 t.isMemberExpression(callee.node) ||
106 t.isOptionalMemberExpression(callee.node)
107 ) {
87 - const { object, property } = callee.node;
88 - if (t.isIdentifier(object) && t.isIdentifier(property)) {
89 - if (!isValidFunctionCallName(`${object.name}.${property.name}`)) {
108 + if (t.isIdentifier(callee.node.property)) {
109 + const fullName = getStaticCallee(callee.node);
110 + if (
111 + fullName &&
112 + !isValidFunctionCallName(callee.node.property.name, fullName)
113 + ) {
114 context.bailout("BailOnCapitalizedFunctionCalls", {
115 code: "E0018",
116 path: callee,