|
11 | 11 |
|
12 | 12 | 'use-strict';
|
13 | 13 |
|
14 |
| -import {assertGenericTypeAnnotationHasExactlyOneTypeParameter} from '../parsers-commons'; |
15 |
| -import type {ParserType} from '../errors'; |
16 |
| -const { |
| 14 | +import { |
| 15 | + assertGenericTypeAnnotationHasExactlyOneTypeParameter, |
| 16 | + isObjectProperty, |
| 17 | + parseObjectProperty, |
17 | 18 | wrapNullable,
|
18 | 19 | unwrapNullable,
|
19 | 20 | emitUnionTypeAnnotation,
|
20 |
| -} = require('../parsers-commons.js'); |
21 |
| -const {UnsupportedUnionTypeAnnotationParserError} = require('../errors'); |
| 21 | +} from '../parsers-commons'; |
| 22 | +import type {ParserType} from '../errors'; |
22 | 23 | import type {UnionTypeAnnotationMemberType} from '../../CodegenSchema';
|
| 24 | + |
| 25 | +const { |
| 26 | + UnsupportedUnionTypeAnnotationParserError, |
| 27 | + UnsupportedObjectPropertyTypeAnnotationParserError, |
| 28 | +} = require('../errors'); |
| 29 | + |
23 | 30 | import {MockedParser} from '../parserMock';
|
24 | 31 |
|
25 | 32 | const parser = new MockedParser();
|
26 | 33 |
|
| 34 | +const flowTranslateTypeAnnotation = require('../flow/modules/index'); |
| 35 | +const typeScriptTranslateTypeAnnotation = require('../typescript/modules/index'); |
| 36 | + |
27 | 37 | describe('wrapNullable', () => {
|
28 | 38 | describe('when nullable is true', () => {
|
29 | 39 | it('returns nullable type annotation', () => {
|
@@ -189,6 +199,200 @@ describe('assertGenericTypeAnnotationHasExactlyOneTypeParameter', () => {
|
189 | 199 | });
|
190 | 200 | });
|
191 | 201 |
|
| 202 | +describe('isObjectProperty', () => { |
| 203 | + const propertyStub = { |
| 204 | + /* type: 'notObjectTypeProperty', */ |
| 205 | + typeAnnotation: { |
| 206 | + typeAnnotation: 'wrongTypeAnnotation', |
| 207 | + }, |
| 208 | + value: 'wrongValue', |
| 209 | + name: 'wrongName', |
| 210 | + }; |
| 211 | + |
| 212 | + describe("when 'language' is 'Flow'", () => { |
| 213 | + const language: ParserType = 'Flow'; |
| 214 | + it("returns 'true' if 'property.type' is 'ObjectTypeProperty'", () => { |
| 215 | + const result = isObjectProperty( |
| 216 | + { |
| 217 | + type: 'ObjectTypeProperty', |
| 218 | + ...propertyStub, |
| 219 | + }, |
| 220 | + language, |
| 221 | + ); |
| 222 | + expect(result).toEqual(true); |
| 223 | + }); |
| 224 | + |
| 225 | + it("returns 'true' if 'property.type' is 'ObjectTypeIndexer'", () => { |
| 226 | + const result = isObjectProperty( |
| 227 | + { |
| 228 | + type: 'ObjectTypeIndexer', |
| 229 | + ...propertyStub, |
| 230 | + }, |
| 231 | + language, |
| 232 | + ); |
| 233 | + expect(result).toEqual(true); |
| 234 | + }); |
| 235 | + |
| 236 | + it("returns 'false' if 'property.type' is not 'ObjectTypeProperty' or 'ObjectTypeIndexer'", () => { |
| 237 | + const result = isObjectProperty( |
| 238 | + { |
| 239 | + type: 'notObjectTypeProperty', |
| 240 | + ...propertyStub, |
| 241 | + }, |
| 242 | + language, |
| 243 | + ); |
| 244 | + expect(result).toEqual(false); |
| 245 | + }); |
| 246 | + }); |
| 247 | + |
| 248 | + describe("when 'language' is 'TypeScript'", () => { |
| 249 | + const language: ParserType = 'TypeScript'; |
| 250 | + it("returns 'true' if 'property.type' is 'TSPropertySignature'", () => { |
| 251 | + const result = isObjectProperty( |
| 252 | + { |
| 253 | + type: 'TSPropertySignature', |
| 254 | + ...propertyStub, |
| 255 | + }, |
| 256 | + language, |
| 257 | + ); |
| 258 | + expect(result).toEqual(true); |
| 259 | + }); |
| 260 | + |
| 261 | + it("returns 'true' if 'property.type' is 'TSIndexSignature'", () => { |
| 262 | + const result = isObjectProperty( |
| 263 | + { |
| 264 | + type: 'TSIndexSignature', |
| 265 | + ...propertyStub, |
| 266 | + }, |
| 267 | + language, |
| 268 | + ); |
| 269 | + expect(result).toEqual(true); |
| 270 | + }); |
| 271 | + |
| 272 | + it("returns 'false' if 'property.type' is not 'TSPropertySignature' or 'TSIndexSignature'", () => { |
| 273 | + const result = isObjectProperty( |
| 274 | + { |
| 275 | + type: 'notTSPropertySignature', |
| 276 | + ...propertyStub, |
| 277 | + }, |
| 278 | + language, |
| 279 | + ); |
| 280 | + expect(result).toEqual(false); |
| 281 | + }); |
| 282 | + }); |
| 283 | +}); |
| 284 | + |
| 285 | +describe('parseObjectProperty', () => { |
| 286 | + const moduleName = 'testModuleName'; |
| 287 | + const types = {['wrongName']: 'wrongType'}; |
| 288 | + const aliasMap = {}; |
| 289 | + const tryParse = () => null; |
| 290 | + const cxxOnly = false; |
| 291 | + const nullable = true; |
| 292 | + |
| 293 | + describe("when 'language' is 'Flow'", () => { |
| 294 | + const language: ParserType = 'Flow'; |
| 295 | + it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'ObjectTypeProperty' or 'ObjectTypeIndexer'.", () => { |
| 296 | + const property = { |
| 297 | + type: 'notObjectTypeProperty', |
| 298 | + typeAnnotation: { |
| 299 | + type: 'notObjectTypeProperty', |
| 300 | + typeAnnotation: 'wrongTypeAnnotation', |
| 301 | + }, |
| 302 | + value: 'wrongValue', |
| 303 | + name: 'wrongName', |
| 304 | + }; |
| 305 | + const expected = new UnsupportedObjectPropertyTypeAnnotationParserError( |
| 306 | + moduleName, |
| 307 | + property, |
| 308 | + property.type, |
| 309 | + language, |
| 310 | + ); |
| 311 | + expect(() => |
| 312 | + parseObjectProperty( |
| 313 | + property, |
| 314 | + moduleName, |
| 315 | + types, |
| 316 | + aliasMap, |
| 317 | + tryParse, |
| 318 | + cxxOnly, |
| 319 | + language, |
| 320 | + nullable, |
| 321 | + flowTranslateTypeAnnotation, |
| 322 | + ), |
| 323 | + ).toThrow(expected); |
| 324 | + }); |
| 325 | + }); |
| 326 | + |
| 327 | + describe("when 'language' is 'TypeScript'", () => { |
| 328 | + const language: ParserType = 'TypeScript'; |
| 329 | + it("throws an 'UnsupportedObjectPropertyTypeAnnotationParserError' error if 'property.type' is not 'TSPropertySignature' or 'TSIndexSignature'.", () => { |
| 330 | + const property = { |
| 331 | + type: 'notTSPropertySignature', |
| 332 | + typeAnnotation: { |
| 333 | + typeAnnotation: 'wrongTypeAnnotation', |
| 334 | + }, |
| 335 | + value: 'wrongValue', |
| 336 | + name: 'wrongName', |
| 337 | + }; |
| 338 | + const expected = new UnsupportedObjectPropertyTypeAnnotationParserError( |
| 339 | + moduleName, |
| 340 | + property, |
| 341 | + property.type, |
| 342 | + language, |
| 343 | + ); |
| 344 | + expect(() => |
| 345 | + parseObjectProperty( |
| 346 | + property, |
| 347 | + moduleName, |
| 348 | + types, |
| 349 | + aliasMap, |
| 350 | + tryParse, |
| 351 | + cxxOnly, |
| 352 | + language, |
| 353 | + nullable, |
| 354 | + typeScriptTranslateTypeAnnotation, |
| 355 | + ), |
| 356 | + ).toThrow(expected); |
| 357 | + }); |
| 358 | + |
| 359 | + it("returns a 'NativeModuleBaseTypeAnnotation' object with 'typeAnnotation.type' equal to 'GenericObjectTypeAnnotation', if 'property.type' is 'TSIndexSignature'.", () => { |
| 360 | + const property = { |
| 361 | + type: 'TSIndexSignature', |
| 362 | + typeAnnotation: { |
| 363 | + type: 'TSIndexSignature', |
| 364 | + typeAnnotation: 'TSIndexSignature', |
| 365 | + }, |
| 366 | + key: { |
| 367 | + name: 'testKeyName', |
| 368 | + }, |
| 369 | + value: 'wrongValue', |
| 370 | + name: 'wrongName', |
| 371 | + parameters: [{name: 'testName'}], |
| 372 | + }; |
| 373 | + const result = parseObjectProperty( |
| 374 | + property, |
| 375 | + moduleName, |
| 376 | + types, |
| 377 | + aliasMap, |
| 378 | + tryParse, |
| 379 | + cxxOnly, |
| 380 | + language, |
| 381 | + nullable, |
| 382 | + typeScriptTranslateTypeAnnotation, |
| 383 | + ); |
| 384 | + const expected = { |
| 385 | + name: 'testName', |
| 386 | + optional: false, |
| 387 | + typeAnnotation: wrapNullable(nullable, { |
| 388 | + type: 'GenericObjectTypeAnnotation', |
| 389 | + }), |
| 390 | + }; |
| 391 | + expect(result).toEqual(expected); |
| 392 | + }); |
| 393 | + }); |
| 394 | +}); |
| 395 | + |
192 | 396 | describe('emitUnionTypeAnnotation', () => {
|
193 | 397 | const hasteModuleName = 'SampleTurboModule';
|
194 | 398 |
|
|
0 commit comments