-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add getKeyName function to codegen Parser class (#35202)
Summary: This PR adds a `getKeyName` function to the codegen Parser class and implements it in the Flow and TypeScript parsers as requested on #34872. ## Changelog [Internal] [Added] - Add `getKeyName` function to codegen Parser class Pull Request resolved: #35202 Test Plan: Run `yarn jest react-native-codegen` and ensure CI is green ![image](https://user-images.githubusercontent.com/11707729/200028600-87e9c1d7-d56d-4cf7-bdbc-18bdf1b03fc5.png) Reviewed By: cipolleschi Differential Revision: D41081711 Pulled By: jacdebug fbshipit-source-id: 7ad2953a0e2f90f04d03270bda40d669d4d0d50a
- Loading branch information
1 parent
90b6735
commit f849f49
Showing
9 changed files
with
210 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/react-native-codegen/src/parsers/__tests__/parsers-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict-local | ||
* @format | ||
* @oncall react_native | ||
*/ | ||
|
||
'use-strict'; | ||
|
||
const { | ||
UnsupportedObjectPropertyTypeAnnotationParserError, | ||
} = require('../errors'); | ||
|
||
import {TypeScriptParser} from '../typescript/parser'; | ||
import {FlowParser} from '../flow/parser'; | ||
|
||
const hasteModuleName = 'moduleName'; | ||
describe('TypeScriptParser', () => { | ||
const parser = new FlowParser(); | ||
describe('getKeyName', () => { | ||
describe('when propertyOrIndex is ObjectTypeProperty', () => { | ||
it('returns property name', () => { | ||
const property = { | ||
type: 'ObjectTypeProperty', | ||
key: { | ||
name: 'propertyName', | ||
}, | ||
}; | ||
|
||
const expected = 'propertyName'; | ||
|
||
expect(parser.getKeyName(property, hasteModuleName)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('when propertyOrIndex is ObjectTypeIndexer', () => { | ||
it('returns indexer name', () => { | ||
const indexer = { | ||
type: 'ObjectTypeIndexer', | ||
id: { | ||
name: 'indexerName', | ||
}, | ||
}; | ||
|
||
const expected = 'indexerName'; | ||
|
||
expect(parser.getKeyName(indexer, hasteModuleName)).toEqual(expected); | ||
}); | ||
|
||
it('returns `key` if indexer has no name', () => { | ||
const indexer = { | ||
type: 'ObjectTypeIndexer', | ||
id: {}, | ||
}; | ||
|
||
const expected = 'key'; | ||
|
||
expect(parser.getKeyName(indexer, hasteModuleName)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('when propertyOrIndex is not ObjectTypeProperty or ObjectTypeIndexer', () => { | ||
it('throw UnsupportedObjectPropertyTypeAnnotationParserError', () => { | ||
const indexer = { | ||
type: 'EnumDeclaration', | ||
memberType: 'NumberTypeAnnotation', | ||
}; | ||
|
||
expect(() => parser.getKeyName(indexer, hasteModuleName)).toThrowError( | ||
UnsupportedObjectPropertyTypeAnnotationParserError, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('FlowParser', () => { | ||
const parser = new TypeScriptParser(); | ||
describe('getKeyName', () => { | ||
describe('when propertyOrIndex is TSPropertySignature', () => { | ||
it('returns property name', () => { | ||
const property = { | ||
type: 'TSPropertySignature', | ||
key: { | ||
name: 'propertyName', | ||
}, | ||
}; | ||
|
||
const expected = 'propertyName'; | ||
|
||
expect(parser.getKeyName(property, hasteModuleName)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('when propertyOrIndex is TSIndexSignature', () => { | ||
it('returns indexer name', () => { | ||
const indexer = { | ||
type: 'TSIndexSignature', | ||
parameters: [ | ||
{ | ||
name: 'indexerName', | ||
}, | ||
], | ||
}; | ||
|
||
const expected = 'indexerName'; | ||
|
||
expect(parser.getKeyName(indexer, hasteModuleName)).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('when propertyOrIndex is not TSPropertySignature or TSIndexSignature', () => { | ||
it('throw UnsupportedObjectPropertyTypeAnnotationParserError', () => { | ||
const indexer = { | ||
type: 'TSEnumDeclaration', | ||
memberType: 'NumberTypeAnnotation', | ||
}; | ||
|
||
expect(() => parser.getKeyName(indexer, hasteModuleName)).toThrowError( | ||
UnsupportedObjectPropertyTypeAnnotationParserError, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters