|
| 1 | +import {NodePath} from '@babel/core'; |
| 2 | +import * as t from '@babel/types'; |
| 3 | + |
| 4 | +import { |
| 5 | + CompilerError, |
| 6 | + CompilerErrorDetailOptions, |
| 7 | + EnvironmentConfig, |
| 8 | + ErrorSeverity, |
| 9 | + Logger, |
| 10 | +} from '..'; |
| 11 | +import {getOrInsertWith} from '../Utils/utils'; |
| 12 | +import {Environment} from '../HIR'; |
| 13 | +import {DEFAULT_EXPORT} from '../HIR/Environment'; |
| 14 | + |
| 15 | +function throwInvalidReact( |
| 16 | + options: Omit<CompilerErrorDetailOptions, 'severity'>, |
| 17 | + {logger, filename}: TraversalState, |
| 18 | +): never { |
| 19 | + const detail: CompilerErrorDetailOptions = { |
| 20 | + ...options, |
| 21 | + severity: ErrorSeverity.InvalidReact, |
| 22 | + }; |
| 23 | + logger?.logEvent(filename, { |
| 24 | + kind: 'CompileError', |
| 25 | + fnLoc: null, |
| 26 | + detail, |
| 27 | + }); |
| 28 | + CompilerError.throw(detail); |
| 29 | +} |
| 30 | +function assertValidEffectImportReference( |
| 31 | + numArgs: number, |
| 32 | + paths: Array<NodePath<t.Node>>, |
| 33 | + context: TraversalState, |
| 34 | +): void { |
| 35 | + for (const path of paths) { |
| 36 | + const parent = path.parentPath; |
| 37 | + if (parent != null && parent.isCallExpression()) { |
| 38 | + const args = parent.get('arguments'); |
| 39 | + /** |
| 40 | + * Only error on untransformed references of the form `useMyEffect(...)` |
| 41 | + * or `moduleNamespace.useMyEffect(...)`, with matching argument counts. |
| 42 | + * TODO: do we also want a mode to also hard error on non-call references? |
| 43 | + */ |
| 44 | + if (args.length === numArgs) { |
| 45 | + const maybeErrorDiagnostic = matchCompilerDiagnostic( |
| 46 | + path, |
| 47 | + context.transformErrors, |
| 48 | + ); |
| 49 | + /** |
| 50 | + * Note that we cannot easily check the type of the first argument here, |
| 51 | + * as it may have already been transformed by the compiler (and not |
| 52 | + * memoized). |
| 53 | + */ |
| 54 | + throwInvalidReact( |
| 55 | + { |
| 56 | + reason: |
| 57 | + '[InferEffectDependencies] React Compiler is unable to infer dependencies of this effect. ' + |
| 58 | + 'This will break your build! ' + |
| 59 | + 'To resolve, either pass your own dependency array or fix reported compiler bailout diagnostics.', |
| 60 | + description: maybeErrorDiagnostic |
| 61 | + ? `(Bailout reason: ${maybeErrorDiagnostic})` |
| 62 | + : null, |
| 63 | + loc: parent.node.loc ?? null, |
| 64 | + }, |
| 65 | + context, |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function assertValidFireImportReference( |
| 73 | + paths: Array<NodePath<t.Node>>, |
| 74 | + context: TraversalState, |
| 75 | +): void { |
| 76 | + if (paths.length > 0) { |
| 77 | + const maybeErrorDiagnostic = matchCompilerDiagnostic( |
| 78 | + paths[0], |
| 79 | + context.transformErrors, |
| 80 | + ); |
| 81 | + throwInvalidReact( |
| 82 | + { |
| 83 | + reason: |
| 84 | + '[Fire] Untransformed reference to compiler-required feature. ' + |
| 85 | + 'Either remove this `fire` call or ensure it is successfully transformed by the compiler', |
| 86 | + description: maybeErrorDiagnostic |
| 87 | + ? `(Bailout reason: ${maybeErrorDiagnostic})` |
| 88 | + : null, |
| 89 | + loc: paths[0].node.loc ?? null, |
| 90 | + }, |
| 91 | + context, |
| 92 | + ); |
| 93 | + } |
| 94 | +} |
| 95 | +export default function validateNoUntransformedReferences( |
| 96 | + path: NodePath<t.Program>, |
| 97 | + filename: string | null, |
| 98 | + logger: Logger | null, |
| 99 | + env: EnvironmentConfig, |
| 100 | + transformErrors: Array<{fn: NodePath<t.Node>; error: CompilerError}>, |
| 101 | +): void { |
| 102 | + const moduleLoadChecks = new Map< |
| 103 | + string, |
| 104 | + Map<string, CheckInvalidReferenceFn> |
| 105 | + >(); |
| 106 | + if (env.enableFire) { |
| 107 | + /** |
| 108 | + * Error on any untransformed references to `fire` (e.g. including non-call |
| 109 | + * expressions) |
| 110 | + */ |
| 111 | + for (const module of Environment.knownReactModules) { |
| 112 | + const react = getOrInsertWith(moduleLoadChecks, module, () => new Map()); |
| 113 | + react.set('fire', assertValidFireImportReference); |
| 114 | + } |
| 115 | + } |
| 116 | + if (env.inferEffectDependencies) { |
| 117 | + for (const { |
| 118 | + function: {source, importSpecifierName}, |
| 119 | + numRequiredArgs, |
| 120 | + } of env.inferEffectDependencies) { |
| 121 | + const module = getOrInsertWith(moduleLoadChecks, source, () => new Map()); |
| 122 | + module.set( |
| 123 | + importSpecifierName, |
| 124 | + assertValidEffectImportReference.bind(null, numRequiredArgs), |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | + if (moduleLoadChecks.size > 0) { |
| 129 | + transformProgram(path, moduleLoadChecks, filename, logger, transformErrors); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +type TraversalState = { |
| 134 | + shouldInvalidateScopes: boolean; |
| 135 | + program: NodePath<t.Program>; |
| 136 | + logger: Logger | null; |
| 137 | + filename: string | null; |
| 138 | + transformErrors: Array<{fn: NodePath<t.Node>; error: CompilerError}>; |
| 139 | +}; |
| 140 | +type CheckInvalidReferenceFn = ( |
| 141 | + paths: Array<NodePath<t.Node>>, |
| 142 | + context: TraversalState, |
| 143 | +) => void; |
| 144 | + |
| 145 | +function validateImportSpecifier( |
| 146 | + specifier: NodePath<t.ImportSpecifier>, |
| 147 | + importSpecifierChecks: Map<string, CheckInvalidReferenceFn>, |
| 148 | + state: TraversalState, |
| 149 | +): void { |
| 150 | + const imported = specifier.get('imported'); |
| 151 | + const specifierName: string = |
| 152 | + imported.node.type === 'Identifier' |
| 153 | + ? imported.node.name |
| 154 | + : imported.node.value; |
| 155 | + const checkFn = importSpecifierChecks.get(specifierName); |
| 156 | + if (checkFn == null) { |
| 157 | + return; |
| 158 | + } |
| 159 | + if (state.shouldInvalidateScopes) { |
| 160 | + state.shouldInvalidateScopes = false; |
| 161 | + state.program.scope.crawl(); |
| 162 | + } |
| 163 | + |
| 164 | + const local = specifier.get('local'); |
| 165 | + const binding = local.scope.getBinding(local.node.name); |
| 166 | + CompilerError.invariant(binding != null, { |
| 167 | + reason: 'Expected binding to be found for import specifier', |
| 168 | + loc: local.node.loc ?? null, |
| 169 | + }); |
| 170 | + checkFn(binding.referencePaths, state); |
| 171 | +} |
| 172 | + |
| 173 | +function validateNamespacedImport( |
| 174 | + specifier: NodePath<t.ImportNamespaceSpecifier | t.ImportDefaultSpecifier>, |
| 175 | + importSpecifierChecks: Map<string, CheckInvalidReferenceFn>, |
| 176 | + state: TraversalState, |
| 177 | +): void { |
| 178 | + if (state.shouldInvalidateScopes) { |
| 179 | + state.shouldInvalidateScopes = false; |
| 180 | + state.program.scope.crawl(); |
| 181 | + } |
| 182 | + const local = specifier.get('local'); |
| 183 | + const binding = local.scope.getBinding(local.node.name); |
| 184 | + const defaultCheckFn = importSpecifierChecks.get(DEFAULT_EXPORT); |
| 185 | + |
| 186 | + CompilerError.invariant(binding != null, { |
| 187 | + reason: 'Expected binding to be found for import specifier', |
| 188 | + loc: local.node.loc ?? null, |
| 189 | + }); |
| 190 | + const filteredReferences = new Map< |
| 191 | + CheckInvalidReferenceFn, |
| 192 | + Array<NodePath<t.Node>> |
| 193 | + >(); |
| 194 | + for (const reference of binding.referencePaths) { |
| 195 | + if (defaultCheckFn != null) { |
| 196 | + getOrInsertWith(filteredReferences, defaultCheckFn, () => []).push( |
| 197 | + reference, |
| 198 | + ); |
| 199 | + } |
| 200 | + const parent = reference.parentPath; |
| 201 | + if ( |
| 202 | + parent != null && |
| 203 | + parent.isMemberExpression() && |
| 204 | + parent.get('object') === reference |
| 205 | + ) { |
| 206 | + if (parent.node.computed || parent.node.property.type !== 'Identifier') { |
| 207 | + continue; |
| 208 | + } |
| 209 | + const checkFn = importSpecifierChecks.get(parent.node.property.name); |
| 210 | + if (checkFn != null) { |
| 211 | + getOrInsertWith(filteredReferences, checkFn, () => []).push(parent); |
| 212 | + } |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + for (const [checkFn, references] of filteredReferences) { |
| 217 | + checkFn(references, state); |
| 218 | + } |
| 219 | +} |
| 220 | +function transformProgram( |
| 221 | + path: NodePath<t.Program>, |
| 222 | + |
| 223 | + moduleLoadChecks: Map<string, Map<string, CheckInvalidReferenceFn>>, |
| 224 | + filename: string | null, |
| 225 | + logger: Logger | null, |
| 226 | + transformErrors: Array<{fn: NodePath<t.Node>; error: CompilerError}>, |
| 227 | +): void { |
| 228 | + const traversalState: TraversalState = { |
| 229 | + shouldInvalidateScopes: true, |
| 230 | + program: path, |
| 231 | + filename, |
| 232 | + logger, |
| 233 | + transformErrors, |
| 234 | + }; |
| 235 | + path.traverse({ |
| 236 | + ImportDeclaration(path: NodePath<t.ImportDeclaration>) { |
| 237 | + const importSpecifierChecks = moduleLoadChecks.get( |
| 238 | + path.node.source.value, |
| 239 | + ); |
| 240 | + if (importSpecifierChecks == null) { |
| 241 | + return; |
| 242 | + } |
| 243 | + const specifiers = path.get('specifiers'); |
| 244 | + for (const specifier of specifiers) { |
| 245 | + if (specifier.isImportSpecifier()) { |
| 246 | + validateImportSpecifier( |
| 247 | + specifier, |
| 248 | + importSpecifierChecks, |
| 249 | + traversalState, |
| 250 | + ); |
| 251 | + } else { |
| 252 | + validateNamespacedImport( |
| 253 | + specifier as NodePath< |
| 254 | + t.ImportNamespaceSpecifier | t.ImportDefaultSpecifier |
| 255 | + >, |
| 256 | + importSpecifierChecks, |
| 257 | + traversalState, |
| 258 | + ); |
| 259 | + } |
| 260 | + } |
| 261 | + }, |
| 262 | + }); |
| 263 | +} |
| 264 | + |
| 265 | +function matchCompilerDiagnostic( |
| 266 | + badReference: NodePath<t.Node>, |
| 267 | + transformErrors: Array<{fn: NodePath<t.Node>; error: CompilerError}>, |
| 268 | +): string | null { |
| 269 | + for (const {fn, error} of transformErrors) { |
| 270 | + if (fn.isAncestor(badReference)) { |
| 271 | + return error.toString(); |
| 272 | + } |
| 273 | + } |
| 274 | + return null; |
| 275 | +} |
0 commit comments