|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its 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 | + * @flow strict-local |
| 8 | + * @format |
| 9 | + */ |
| 10 | + |
| 11 | +'use strict'; |
| 12 | + |
| 13 | +const invariant = require('invariant'); |
| 14 | +const {ParserError} = require('../errors'); |
| 15 | + |
| 16 | +class MisnamedModuleFlowInterfaceParserError extends ParserError { |
| 17 | + constructor(hasteModuleName: string, id: $FlowFixMe) { |
| 18 | + super( |
| 19 | + hasteModuleName, |
| 20 | + id, |
| 21 | + `All Flow interfaces extending TurboModule must be called 'Spec'. Please rename Flow interface '${id.name}' to 'Spec'.`, |
| 22 | + ); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +class ModuleFlowInterfaceNotFoundParserError extends ParserError { |
| 27 | + constructor(hasteModuleName: string, ast: $FlowFixMe) { |
| 28 | + super( |
| 29 | + hasteModuleName, |
| 30 | + ast, |
| 31 | + 'No Flow interfaces extending TurboModule were detected in this NativeModule spec.', |
| 32 | + ); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +class MoreThanOneModuleFlowInterfaceParserError extends ParserError { |
| 37 | + constructor( |
| 38 | + hasteModuleName: string, |
| 39 | + flowModuleInterfaces: $ReadOnlyArray<$FlowFixMe>, |
| 40 | + names: $ReadOnlyArray<string>, |
| 41 | + ) { |
| 42 | + const finalName = names[names.length - 1]; |
| 43 | + const allButLastName = names.slice(0, -1); |
| 44 | + const quote = (x: string) => `'${x}'`; |
| 45 | + |
| 46 | + const nameStr = |
| 47 | + allButLastName.map(quote).join(', ') + ', and ' + quote(finalName); |
| 48 | + |
| 49 | + super( |
| 50 | + hasteModuleName, |
| 51 | + flowModuleInterfaces, |
| 52 | + `Every NativeModule spec file must declare exactly one NativeModule Flow interface. This file declares ${names.length}: ${nameStr}. Please remove the extraneous Flow interface declarations.`, |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class UnsupportedModulePropertyParserError extends ParserError { |
| 58 | + constructor( |
| 59 | + hasteModuleName: string, |
| 60 | + propertyValue: $FlowFixMe, |
| 61 | + propertyName: string, |
| 62 | + invalidPropertyValueType: string, |
| 63 | + ) { |
| 64 | + super( |
| 65 | + hasteModuleName, |
| 66 | + propertyValue, |
| 67 | + `Flow interfaces extending TurboModule must only contain 'FunctionTypeAnnotation's. Property '${propertyName}' refers to a '${invalidPropertyValueType}'.`, |
| 68 | + ); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class UnsupportedFlowTypeAnnotationParserError extends ParserError { |
| 73 | + +typeAnnotationType: string; |
| 74 | + constructor(hasteModuleName: string, typeAnnotation: $FlowFixMe) { |
| 75 | + super( |
| 76 | + hasteModuleName, |
| 77 | + typeAnnotation, |
| 78 | + `Flow type annotation '${typeAnnotation.type}' is unsupported in NativeModule specs.`, |
| 79 | + ); |
| 80 | + |
| 81 | + this.typeAnnotationType = typeAnnotation.type; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +class UnsupportedFlowGenericParserError extends ParserError { |
| 86 | + +genericName: string; |
| 87 | + constructor(hasteModuleName: string, genericTypeAnnotation: $FlowFixMe) { |
| 88 | + const genericName = genericTypeAnnotation.id.name; |
| 89 | + super( |
| 90 | + hasteModuleName, |
| 91 | + genericTypeAnnotation, |
| 92 | + `Unrecognized generic type '${genericName}' in NativeModule spec.`, |
| 93 | + ); |
| 94 | + |
| 95 | + this.genericName = genericName; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +class IncorrectlyParameterizedFlowGenericParserError extends ParserError { |
| 100 | + +genericName: string; |
| 101 | + +numTypeParameters: number; |
| 102 | + |
| 103 | + constructor(hasteModuleName: string, genericTypeAnnotation: $FlowFixMe) { |
| 104 | + if (genericTypeAnnotation.typeParameters == null) { |
| 105 | + super( |
| 106 | + hasteModuleName, |
| 107 | + genericTypeAnnotation, |
| 108 | + `Generic '${genericTypeAnnotation.id.name}' must have type parameters.`, |
| 109 | + ); |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + if ( |
| 114 | + genericTypeAnnotation.typeParameters.type === |
| 115 | + 'TypeParameterInstantiation' && |
| 116 | + genericTypeAnnotation.typeParameters.params.length !== 1 |
| 117 | + ) { |
| 118 | + super( |
| 119 | + hasteModuleName, |
| 120 | + genericTypeAnnotation.typeParameters, |
| 121 | + `Generic '${genericTypeAnnotation.id.name}' must have exactly one type parameter.`, |
| 122 | + ); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + invariant( |
| 127 | + false, |
| 128 | + "Couldn't create IncorrectlyParameterizedFlowGenericParserError", |
| 129 | + ); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Array parsing errors |
| 135 | + */ |
| 136 | + |
| 137 | +class UnsupportedArrayElementTypeAnnotationParserError extends ParserError { |
| 138 | + constructor( |
| 139 | + hasteModuleName: string, |
| 140 | + arrayElementTypeAST: $FlowFixMe, |
| 141 | + arrayType: 'Array' | '$ReadOnlyArray', |
| 142 | + invalidArrayElementType: string, |
| 143 | + ) { |
| 144 | + super( |
| 145 | + hasteModuleName, |
| 146 | + arrayElementTypeAST, |
| 147 | + `${arrayType} element types cannot be '${invalidArrayElementType}'.`, |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Object parsing errors |
| 154 | + */ |
| 155 | + |
| 156 | +class UnsupportedObjectPropertyTypeAnnotationParserError extends ParserError { |
| 157 | + constructor( |
| 158 | + hasteModuleName: string, |
| 159 | + propertyAST: $FlowFixMe, |
| 160 | + invalidPropertyType: string, |
| 161 | + ) { |
| 162 | + let message = `'ObjectTypeAnnotation' cannot contain '${invalidPropertyType}'.`; |
| 163 | + |
| 164 | + if (invalidPropertyType === 'ObjectTypeSpreadProperty') { |
| 165 | + message = "Object spread isn't supported in 'ObjectTypeAnnotation's."; |
| 166 | + } |
| 167 | + |
| 168 | + super(hasteModuleName, propertyAST, message); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +class UnsupportedObjectPropertyValueTypeAnnotationParserError extends ParserError { |
| 173 | + constructor( |
| 174 | + hasteModuleName: string, |
| 175 | + propertyValueAST: $FlowFixMe, |
| 176 | + propertyName: string, |
| 177 | + invalidPropertyValueType: string, |
| 178 | + ) { |
| 179 | + super( |
| 180 | + hasteModuleName, |
| 181 | + propertyValueAST, |
| 182 | + `Object property '${propertyName}' cannot have type '${invalidPropertyValueType}'.`, |
| 183 | + ); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Function parsing errors |
| 189 | + */ |
| 190 | + |
| 191 | +class UnnamedFunctionParamParserError extends ParserError { |
| 192 | + constructor(functionParam: $FlowFixMe, hasteModuleName: string) { |
| 193 | + super( |
| 194 | + hasteModuleName, |
| 195 | + functionParam, |
| 196 | + 'All function parameters must be named.', |
| 197 | + ); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +class UnsupportedFunctionParamTypeAnnotationParserError extends ParserError { |
| 202 | + constructor( |
| 203 | + hasteModuleName: string, |
| 204 | + flowParamTypeAnnotation: $FlowFixMe, |
| 205 | + paramName: string, |
| 206 | + invalidParamType: string, |
| 207 | + ) { |
| 208 | + super( |
| 209 | + hasteModuleName, |
| 210 | + flowParamTypeAnnotation, |
| 211 | + `Function parameter '${paramName}' cannot have type '${invalidParamType}'.`, |
| 212 | + ); |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +class UnsupportedFunctionReturnTypeAnnotationParserError extends ParserError { |
| 217 | + constructor( |
| 218 | + hasteModuleName: string, |
| 219 | + flowReturnTypeAnnotation: $FlowFixMe, |
| 220 | + invalidReturnType: string, |
| 221 | + ) { |
| 222 | + super( |
| 223 | + hasteModuleName, |
| 224 | + flowReturnTypeAnnotation, |
| 225 | + `Function return cannot have type '${invalidReturnType}'.`, |
| 226 | + ); |
| 227 | + } |
| 228 | +} |
| 229 | + |
| 230 | +class UnusedModuleFlowInterfaceParserError extends ParserError { |
| 231 | + constructor(hasteModuleName: string, flowInterface: $FlowFixMe) { |
| 232 | + super( |
| 233 | + hasteModuleName, |
| 234 | + flowInterface, |
| 235 | + "Unused NativeModule spec. Please load the NativeModule by calling TurboModuleRegistry.get<Spec>('<moduleName>').", |
| 236 | + ); |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +class MoreThanOneModuleRegistryCallsParserError extends ParserError { |
| 241 | + constructor( |
| 242 | + hasteModuleName: string, |
| 243 | + flowCallExpressions: $FlowFixMe, |
| 244 | + numCalls: number, |
| 245 | + ) { |
| 246 | + super( |
| 247 | + hasteModuleName, |
| 248 | + flowCallExpressions, |
| 249 | + `Every NativeModule spec file must contain exactly one NativeModule load. This file contains ${numCalls}. Please simplify this spec file, splitting it as necessary, to remove the extraneous loads.`, |
| 250 | + ); |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +class UntypedModuleRegistryCallParserError extends ParserError { |
| 255 | + constructor( |
| 256 | + hasteModuleName: string, |
| 257 | + flowCallExpression: $FlowFixMe, |
| 258 | + methodName: string, |
| 259 | + moduleName: string, |
| 260 | + ) { |
| 261 | + super( |
| 262 | + hasteModuleName, |
| 263 | + flowCallExpression, |
| 264 | + `Please type this NativeModule load: TurboModuleRegistry.${methodName}<Spec>('${moduleName}').`, |
| 265 | + ); |
| 266 | + } |
| 267 | +} |
| 268 | + |
| 269 | +class IncorrectModuleRegistryCallTypeParameterParserError extends ParserError { |
| 270 | + constructor( |
| 271 | + hasteModuleName: string, |
| 272 | + flowTypeArguments: $FlowFixMe, |
| 273 | + methodName: string, |
| 274 | + moduleName: string, |
| 275 | + ) { |
| 276 | + super( |
| 277 | + hasteModuleName, |
| 278 | + flowTypeArguments, |
| 279 | + `Please change these type arguments to reflect TurboModuleRegistry.${methodName}<Spec>('${moduleName}').`, |
| 280 | + ); |
| 281 | + } |
| 282 | +} |
| 283 | + |
| 284 | +class IncorrectModuleRegistryCallArityParserError extends ParserError { |
| 285 | + constructor( |
| 286 | + hasteModuleName: string, |
| 287 | + flowCallExpression: $FlowFixMe, |
| 288 | + methodName: string, |
| 289 | + incorrectArity: number, |
| 290 | + ) { |
| 291 | + super( |
| 292 | + hasteModuleName, |
| 293 | + flowCallExpression, |
| 294 | + `Please call TurboModuleRegistry.${methodName}<Spec>() with exactly one argument. Detected ${incorrectArity}.`, |
| 295 | + ); |
| 296 | + } |
| 297 | +} |
| 298 | + |
| 299 | +class IncorrectModuleRegistryCallArgumentTypeParserError extends ParserError { |
| 300 | + constructor( |
| 301 | + hasteModuleName: string, |
| 302 | + flowArgument: $FlowFixMe, |
| 303 | + methodName: string, |
| 304 | + type: string, |
| 305 | + ) { |
| 306 | + const a = /[aeiouy]/.test(type.toLowerCase()) ? 'an' : 'a'; |
| 307 | + super( |
| 308 | + hasteModuleName, |
| 309 | + flowArgument, |
| 310 | + `Please call TurboModuleRegistry.${methodName}<Spec>() with a string literal. Detected ${a} '${type}'`, |
| 311 | + ); |
| 312 | + } |
| 313 | +} |
| 314 | + |
| 315 | +module.exports = { |
| 316 | + IncorrectlyParameterizedFlowGenericParserError, |
| 317 | + MisnamedModuleFlowInterfaceParserError, |
| 318 | + ModuleFlowInterfaceNotFoundParserError, |
| 319 | + MoreThanOneModuleFlowInterfaceParserError, |
| 320 | + UnnamedFunctionParamParserError, |
| 321 | + UnsupportedArrayElementTypeAnnotationParserError, |
| 322 | + UnsupportedFlowGenericParserError, |
| 323 | + UnsupportedFlowTypeAnnotationParserError, |
| 324 | + UnsupportedFunctionParamTypeAnnotationParserError, |
| 325 | + UnsupportedFunctionReturnTypeAnnotationParserError, |
| 326 | + UnsupportedModulePropertyParserError, |
| 327 | + UnsupportedObjectPropertyTypeAnnotationParserError, |
| 328 | + UnsupportedObjectPropertyValueTypeAnnotationParserError, |
| 329 | + UnusedModuleFlowInterfaceParserError, |
| 330 | + MoreThanOneModuleRegistryCallsParserError, |
| 331 | + UntypedModuleRegistryCallParserError, |
| 332 | + IncorrectModuleRegistryCallTypeParameterParserError, |
| 333 | + IncorrectModuleRegistryCallArityParserError, |
| 334 | + IncorrectModuleRegistryCallArgumentTypeParserError, |
| 335 | +}; |
0 commit comments