| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {CompilerErrorDetail, EnvironmentConfig} from '..'; |
| 9 | import {ErrorCategory} from '../CompilerError'; |
| 10 | import {HIRFunction, IdentifierId} from '../HIR'; |
| 11 | import {DEFAULT_GLOBALS} from '../HIR/Globals'; |
| 12 | |
| 13 | export function validateNoCapitalizedCalls(fn: HIRFunction): void { |
| 14 | const envConfig: EnvironmentConfig = fn.env.config; |
| 15 | const ALLOW_LIST = new Set([ |
| 16 | ...DEFAULT_GLOBALS.keys(), |
| 17 | ...(envConfig.validateNoCapitalizedCalls ?? []), |
| 18 | ]); |
| 19 | const isAllowed = (name: string): boolean => { |
| 20 | return ALLOW_LIST.has(name); |
| 21 | }; |
| 22 | |
| 23 | const capitalLoadGlobals = new Map<IdentifierId, string>(); |
| 24 | const capitalizedProperties = new Map<IdentifierId, string>(); |
| 25 | const reason = |
| 26 | 'Capitalized functions are reserved for components, which must be invoked with JSX. If this is a component, render it with JSX. Otherwise, ensure that it has no hook calls and rename it to begin with a lowercase letter. Alternatively, if you know for a fact that this function is not a component, you can allowlist it via the compiler config'; |
| 27 | for (const [, block] of fn.body.blocks) { |
| 28 | for (const {lvalue, value} of block.instructions) { |
| 29 | switch (value.kind) { |
| 30 | case 'LoadGlobal': { |
| 31 | if ( |
| 32 | value.binding.name != '' && |
| 33 | /^[A-Z]/.test(value.binding.name) && |
| 34 | // We don't want to flag CONSTANTS() |
| 35 | !(value.binding.name.toUpperCase() === value.binding.name) && |
| 36 | !isAllowed(value.binding.name) |
| 37 | ) { |
| 38 | capitalLoadGlobals.set(lvalue.identifier.id, value.binding.name); |
| 39 | } |
| 40 | |
| 41 | break; |
| 42 | } |
| 43 | case 'CallExpression': { |
| 44 | const calleeIdentifier = value.callee.identifier.id; |
| 45 | const calleeName = capitalLoadGlobals.get(calleeIdentifier); |
| 46 | if (calleeName != null) { |
| 47 | fn.env.recordError( |
| 48 | new CompilerErrorDetail({ |
| 49 | category: ErrorCategory.CapitalizedCalls, |
| 50 | reason, |
| 51 | description: `${calleeName} may be a component`, |
| 52 | loc: value.loc, |
| 53 | suggestions: null, |
| 54 | }), |
| 55 | ); |
| 56 | continue; |
| 57 | } |
| 58 | break; |
| 59 | } |
| 60 | case 'PropertyLoad': { |
| 61 | // Start conservative and disallow all capitalized method calls |
| 62 | if ( |
| 63 | typeof value.property === 'string' && |
| 64 | /^[A-Z]/.test(value.property) |
| 65 | ) { |
| 66 | capitalizedProperties.set(lvalue.identifier.id, value.property); |
| 67 | } |
| 68 | break; |
| 69 | } |
| 70 | case 'MethodCall': { |
| 71 | const propertyIdentifier = value.property.identifier.id; |
| 72 | const propertyName = capitalizedProperties.get(propertyIdentifier); |
| 73 | if (propertyName != null) { |
| 74 | fn.env.recordError( |
| 75 | new CompilerErrorDetail({ |
| 76 | category: ErrorCategory.CapitalizedCalls, |
| 77 | reason, |
| 78 | description: `${propertyName} may be a component`, |
| 79 | loc: value.loc, |
| 80 | suggestions: null, |
| 81 | }), |
| 82 | ); |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |