@react-native/codegen
Advanced tools
Comparing version 0.72.2 to 0.72.3
@@ -27,2 +27,4 @@ /** | ||
_require.IncorrectModuleRegistryCallTypeParameterParserError, | ||
IncorrectModuleRegistryCallArgumentTypeParserError = | ||
_require.IncorrectModuleRegistryCallArgumentTypeParserError, | ||
UnsupportedObjectPropertyValueTypeAnnotationParserError = | ||
@@ -253,2 +255,38 @@ _require.UnsupportedObjectPropertyValueTypeAnnotationParserError, | ||
} | ||
function throwIfIncorrectModuleRegistryCallArgument( | ||
nativeModuleName, | ||
callExpressionArg, | ||
methodName, | ||
) { | ||
if ( | ||
callExpressionArg.type !== 'StringLiteral' && | ||
callExpressionArg.type !== 'Literal' | ||
) { | ||
const type = callExpressionArg.type; | ||
throw new IncorrectModuleRegistryCallArgumentTypeParserError( | ||
nativeModuleName, | ||
callExpressionArg, | ||
methodName, | ||
type, | ||
); | ||
} | ||
} | ||
function throwIfPartialNotAnnotatingTypeParameter( | ||
typeAnnotation, | ||
types, | ||
parser, | ||
) { | ||
const annotatedElement = parser.extractAnnotatedElement( | ||
typeAnnotation, | ||
types, | ||
); | ||
if (!annotatedElement) { | ||
throw new Error('Partials only support annotating a type parameter.'); | ||
} | ||
} | ||
function throwIfPartialWithMoreParameter(typeAnnotation) { | ||
if (typeAnnotation.typeParameters.params.length !== 1) { | ||
throw new Error('Partials only support annotating exactly one parameter.'); | ||
} | ||
} | ||
module.exports = { | ||
@@ -268,2 +306,5 @@ throwIfModuleInterfaceIsMisnamed, | ||
throwIfArrayElementTypeAnnotationIsUnsupported, | ||
throwIfIncorrectModuleRegistryCallArgument, | ||
throwIfPartialNotAnnotatingTypeParameter, | ||
throwIfPartialWithMoreParameter, | ||
}; |
@@ -75,18 +75,16 @@ /** | ||
} | ||
const _require = require('../utils'), | ||
getTypes = _require.getTypes; | ||
const _require2 = require('./commands'), | ||
getCommands = _require2.getCommands; | ||
const _require3 = require('./events'), | ||
getEvents = _require3.getEvents; | ||
const _require4 = require('./extends'), | ||
getExtendsProps = _require4.getExtendsProps, | ||
removeKnownExtends = _require4.removeKnownExtends; | ||
const _require5 = require('./options'), | ||
getCommandOptions = _require5.getCommandOptions, | ||
getOptions = _require5.getOptions; | ||
const _require6 = require('./props'), | ||
getProps = _require6.getProps; | ||
const _require7 = require('./componentsUtils.js'), | ||
getProperties = _require7.getProperties; | ||
const _require = require('./commands'), | ||
getCommands = _require.getCommands; | ||
const _require2 = require('./events'), | ||
getEvents = _require2.getEvents; | ||
const _require3 = require('./extends'), | ||
getExtendsProps = _require3.getExtendsProps, | ||
removeKnownExtends = _require3.removeKnownExtends; | ||
const _require4 = require('./options'), | ||
getCommandOptions = _require4.getCommandOptions, | ||
getOptions = _require4.getOptions; | ||
const _require5 = require('./props'), | ||
getProps = _require5.getProps; | ||
const _require6 = require('./componentsUtils.js'), | ||
getProperties = _require6.getProperties; | ||
@@ -233,5 +231,3 @@ /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's | ||
// $FlowFixMe[signature-verification-failure] there's no flowtype for AST | ||
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's | ||
* LTI update could not be added via codemod */ | ||
function buildComponentSchema(ast) { | ||
function buildComponentSchema(ast, parser) { | ||
const _findComponentConfig = findComponentConfig(ast), | ||
@@ -243,3 +239,3 @@ componentName = _findComponentConfig.componentName, | ||
optionsExpression = _findComponentConfig.optionsExpression; | ||
const types = getTypes(ast); | ||
const types = parser.getTypes(ast); | ||
const propProperties = getProperties(propsTypeName, types); | ||
@@ -246,0 +242,0 @@ const commandOptions = getCommandOptions(commandOptionsExpression); |
@@ -148,4 +148,3 @@ /** | ||
const _require2 = require('../utils'), | ||
resolveTypeAnnotation = _require2.resolveTypeAnnotation, | ||
getTypes = _require2.getTypes; | ||
resolveTypeAnnotation = _require2.resolveTypeAnnotation; | ||
const _require3 = require('../../parsers-commons'), | ||
@@ -180,4 +179,2 @@ unwrapNullable = _require3.unwrapNullable, | ||
_require5.UnsupportedTypeAnnotationParserError, | ||
IncorrectModuleRegistryCallArgumentTypeParserError = | ||
_require5.IncorrectModuleRegistryCallArgumentTypeParserError, | ||
UnsupportedGenericParserError = _require5.UnsupportedGenericParserError; | ||
@@ -195,5 +192,10 @@ const _require6 = require('../../error-utils'), | ||
_require6.throwIfIncorrectModuleRegistryCallTypeParameterParserError, | ||
throwIfIncorrectModuleRegistryCallArgument = | ||
_require6.throwIfIncorrectModuleRegistryCallArgument, | ||
throwIfUntypedModule = _require6.throwIfUntypedModule, | ||
throwIfMoreThanOneModuleInterfaceParserError = | ||
_require6.throwIfMoreThanOneModuleInterfaceParserError; | ||
_require6.throwIfMoreThanOneModuleInterfaceParserError, | ||
throwIfPartialNotAnnotatingTypeParameter = | ||
_require6.throwIfPartialNotAnnotatingTypeParameter, | ||
throwIfPartialWithMoreParameter = _require6.throwIfPartialWithMoreParameter; | ||
const language = 'Flow'; | ||
@@ -294,14 +296,12 @@ function translateTypeAnnotation( | ||
case '$Partial': { | ||
if (typeAnnotation.typeParameters.params.length !== 1) { | ||
throw new Error( | ||
'Partials only support annotating exactly one parameter.', | ||
); | ||
} | ||
const annotatedElement = | ||
types[typeAnnotation.typeParameters.params[0].id.name]; | ||
if (!annotatedElement) { | ||
throw new Error( | ||
'Partials only support annotating a type parameter.', | ||
); | ||
} | ||
throwIfPartialWithMoreParameter(typeAnnotation); | ||
const annotatedElement = parser.extractAnnotatedElement( | ||
typeAnnotation, | ||
types, | ||
); | ||
throwIfPartialNotAnnotatingTypeParameter( | ||
typeAnnotation, | ||
types, | ||
parser, | ||
); | ||
const properties = annotatedElement.right.properties.map(prop => { | ||
@@ -451,10 +451,2 @@ return { | ||
} | ||
function isModuleInterface(node) { | ||
return ( | ||
node.type === 'InterfaceDeclaration' && | ||
node.extends.length === 1 && | ||
node.extends[0].type === 'InterfaceExtends' && | ||
node.extends[0].id.name === 'TurboModule' | ||
); | ||
} | ||
function buildModuleSchema( | ||
@@ -469,4 +461,6 @@ hasteModuleName, | ||
) { | ||
const types = getTypes(ast); | ||
const moduleSpecs = Object.values(types).filter(isModuleInterface); | ||
const types = parser.getTypes(ast); | ||
const moduleSpecs = Object.values(types).filter(t => | ||
parser.isModuleInterface(t), | ||
); | ||
throwIfModuleInterfaceNotFound( | ||
@@ -516,11 +510,7 @@ moduleSpecs.length, | ||
); | ||
if (callExpression.arguments[0].type !== 'Literal') { | ||
const type = callExpression.arguments[0].type; | ||
throw new IncorrectModuleRegistryCallArgumentTypeParserError( | ||
hasteModuleName, | ||
callExpression.arguments[0], | ||
methodName, | ||
type, | ||
); | ||
} | ||
throwIfIncorrectModuleRegistryCallArgument( | ||
hasteModuleName, | ||
callExpression.arguments[0], | ||
methodName, | ||
); | ||
const $moduleName = callExpression.arguments[0].value; | ||
@@ -527,0 +517,0 @@ throwIfUntypedModule( |
@@ -49,3 +49,3 @@ /** | ||
buildComponentSchema = _require3.buildComponentSchema; | ||
const _require4 = require('./components/schema'), | ||
const _require4 = require('../schema.js'), | ||
wrapComponentSchema = _require4.wrapComponentSchema; | ||
@@ -197,2 +197,53 @@ const _require5 = require('./modules'), | ||
} | ||
isModuleInterface(node) { | ||
return ( | ||
node.type === 'InterfaceDeclaration' && | ||
node.extends.length === 1 && | ||
node.extends[0].type === 'InterfaceExtends' && | ||
node.extends[0].id.name === 'TurboModule' | ||
); | ||
} | ||
extractAnnotatedElement(typeAnnotation, types) { | ||
return types[typeAnnotation.typeParameters.params[0].id.name]; | ||
} | ||
/** | ||
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias | ||
* declaration type. Unfortunately, we don't have those types, because flow-parser | ||
* generates them, and flow-parser is not type-safe. In the future, we should find | ||
* a way to get these types from our flow parser library. | ||
* | ||
* TODO(T71778680): Flow type AST Nodes | ||
*/ | ||
getTypes(ast) { | ||
return ast.body.reduce((types, node) => { | ||
if ( | ||
node.type === 'ExportNamedDeclaration' && | ||
node.exportKind === 'type' | ||
) { | ||
if ( | ||
node.declaration != null && | ||
(node.declaration.type === 'TypeAlias' || | ||
node.declaration.type === 'InterfaceDeclaration') | ||
) { | ||
types[node.declaration.id.name] = node.declaration; | ||
} | ||
} else if ( | ||
node.type === 'ExportNamedDeclaration' && | ||
node.exportKind === 'value' && | ||
node.declaration && | ||
node.declaration.type === 'EnumDeclaration' | ||
) { | ||
types[node.declaration.id.name] = node.declaration; | ||
} else if ( | ||
node.type === 'TypeAlias' || | ||
node.type === 'InterfaceDeclaration' || | ||
node.type === 'EnumDeclaration' | ||
) { | ||
types[node.id.name] = node; | ||
} | ||
return types; | ||
}, {}); | ||
} | ||
} | ||
@@ -199,0 +250,0 @@ module.exports = { |
@@ -13,39 +13,2 @@ /** | ||
/** | ||
* This FlowFixMe is supposed to refer to an InterfaceDeclaration or TypeAlias | ||
* declaration type. Unfortunately, we don't have those types, because flow-parser | ||
* generates them, and flow-parser is not type-safe. In the future, we should find | ||
* a way to get these types from our flow parser library. | ||
* | ||
* TODO(T71778680): Flow type AST Nodes | ||
*/ | ||
function getTypes(ast) { | ||
return ast.body.reduce((types, node) => { | ||
if (node.type === 'ExportNamedDeclaration' && node.exportKind === 'type') { | ||
if ( | ||
node.declaration != null && | ||
(node.declaration.type === 'TypeAlias' || | ||
node.declaration.type === 'InterfaceDeclaration') | ||
) { | ||
types[node.declaration.id.name] = node.declaration; | ||
} | ||
} else if ( | ||
node.type === 'ExportNamedDeclaration' && | ||
node.exportKind === 'value' && | ||
node.declaration && | ||
node.declaration.type === 'EnumDeclaration' | ||
) { | ||
types[node.declaration.id.name] = node.declaration; | ||
} else if ( | ||
node.type === 'TypeAlias' || | ||
node.type === 'InterfaceDeclaration' || | ||
node.type === 'EnumDeclaration' | ||
) { | ||
types[node.id.name] = node; | ||
} | ||
return types; | ||
}, {}); | ||
} | ||
// $FlowFixMe[unclear-type] there's no flowtype for ASTs | ||
@@ -122,3 +85,2 @@ | ||
resolveTypeAnnotation, | ||
getTypes, | ||
}; |
@@ -153,2 +153,16 @@ /** | ||
} | ||
isModuleInterface(node) { | ||
return ( | ||
node.type === 'InterfaceDeclaration' && | ||
node.extends.length === 1 && | ||
node.extends[0].type === 'InterfaceExtends' && | ||
node.extends[0].id.name === 'TurboModule' | ||
); | ||
} | ||
extractAnnotatedElement(typeAnnotation, types) { | ||
return types[typeAnnotation.typeParameters.params[0].id.name]; | ||
} | ||
getTypes(ast) { | ||
return {}; | ||
} | ||
} |
@@ -372,3 +372,3 @@ /** | ||
case 'component': { | ||
return wrapComponentSchema(buildComponentSchema(ast)); | ||
return wrapComponentSchema(buildComponentSchema(ast, parser)); | ||
} | ||
@@ -375,0 +375,0 @@ case 'module': { |
@@ -75,17 +75,15 @@ /** | ||
} | ||
const _require = require('../utils'), | ||
getTypes = _require.getTypes; | ||
const _require2 = require('./commands'), | ||
getCommands = _require2.getCommands; | ||
const _require3 = require('./events'), | ||
getEvents = _require3.getEvents; | ||
const _require4 = require('./extends'), | ||
categorizeProps = _require4.categorizeProps; | ||
const _require5 = require('./options'), | ||
getCommandOptions = _require5.getCommandOptions, | ||
getOptions = _require5.getOptions; | ||
const _require6 = require('./props'), | ||
getProps = _require6.getProps; | ||
const _require7 = require('./componentsUtils.js'), | ||
getProperties = _require7.getProperties; | ||
const _require = require('./commands'), | ||
getCommands = _require.getCommands; | ||
const _require2 = require('./events'), | ||
getEvents = _require2.getEvents; | ||
const _require3 = require('./extends'), | ||
categorizeProps = _require3.categorizeProps; | ||
const _require4 = require('./options'), | ||
getCommandOptions = _require4.getCommandOptions, | ||
getOptions = _require4.getOptions; | ||
const _require5 = require('./props'), | ||
getProps = _require5.getProps; | ||
const _require6 = require('./componentsUtils.js'), | ||
getProperties = _require6.getProperties; | ||
@@ -235,5 +233,3 @@ /* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's | ||
// $FlowFixMe[signature-verification-failure] TODO(T108222691): Use flow-types for @babel/parser | ||
/* $FlowFixMe[missing-local-annot] The type annotation(s) required by Flow's | ||
* LTI update could not be added via codemod */ | ||
function buildComponentSchema(ast) { | ||
function buildComponentSchema(ast, parser) { | ||
const _findComponentConfig = findComponentConfig(ast), | ||
@@ -245,3 +241,3 @@ componentName = _findComponentConfig.componentName, | ||
optionsExpression = _findComponentConfig.optionsExpression; | ||
const types = getTypes(ast); | ||
const types = parser.getTypes(ast); | ||
const propProperties = getProperties(propsTypeName, types); | ||
@@ -248,0 +244,0 @@ const commandOptions = getCommandOptions(commandOptionsExpression); |
@@ -152,4 +152,3 @@ /** | ||
const _require4 = require('../utils'), | ||
resolveTypeAnnotation = _require4.resolveTypeAnnotation, | ||
getTypes = _require4.getTypes; | ||
resolveTypeAnnotation = _require4.resolveTypeAnnotation; | ||
const _require5 = require('../../parsers-commons'), | ||
@@ -182,5 +181,3 @@ parseObjectProperty = _require5.parseObjectProperty, | ||
UnsupportedTypeAnnotationParserError = | ||
_require8.UnsupportedTypeAnnotationParserError, | ||
IncorrectModuleRegistryCallArgumentTypeParserError = | ||
_require8.IncorrectModuleRegistryCallArgumentTypeParserError; | ||
_require8.UnsupportedTypeAnnotationParserError; | ||
const _require9 = require('../../error-utils'), | ||
@@ -199,3 +196,8 @@ throwIfUntypedModule = _require9.throwIfUntypedModule, | ||
throwIfIncorrectModuleRegistryCallTypeParameterParserError = | ||
_require9.throwIfIncorrectModuleRegistryCallTypeParameterParserError; | ||
_require9.throwIfIncorrectModuleRegistryCallTypeParameterParserError, | ||
throwIfIncorrectModuleRegistryCallArgument = | ||
_require9.throwIfIncorrectModuleRegistryCallArgument, | ||
throwIfPartialNotAnnotatingTypeParameter = | ||
_require9.throwIfPartialNotAnnotatingTypeParameter, | ||
throwIfPartialWithMoreParameter = _require9.throwIfPartialWithMoreParameter; | ||
const language = 'TypeScript'; | ||
@@ -367,14 +369,12 @@ function translateObjectTypeAnnotation( | ||
case 'Partial': { | ||
if (typeAnnotation.typeParameters.params.length !== 1) { | ||
throw new Error( | ||
'Partials only support annotating exactly one parameter.', | ||
); | ||
} | ||
const annotatedElement = | ||
types[typeAnnotation.typeParameters.params[0].typeName.name]; | ||
if (!annotatedElement) { | ||
throw new Error( | ||
'Partials only support annotating a type parameter.', | ||
); | ||
} | ||
throwIfPartialWithMoreParameter(typeAnnotation); | ||
const annotatedElement = parser.extractAnnotatedElement( | ||
typeAnnotation, | ||
types, | ||
); | ||
throwIfPartialNotAnnotatingTypeParameter( | ||
typeAnnotation, | ||
types, | ||
parser, | ||
); | ||
const properties = annotatedElement.typeAnnotation.members.map( | ||
@@ -562,13 +562,2 @@ member => { | ||
} | ||
function isModuleInterface(node) { | ||
var _node$extends; | ||
return ( | ||
node.type === 'TSInterfaceDeclaration' && | ||
((_node$extends = node.extends) === null || _node$extends === void 0 | ||
? void 0 | ||
: _node$extends.length) === 1 && | ||
node.extends[0].type === 'TSExpressionWithTypeArguments' && | ||
node.extends[0].expression.name === 'TurboModule' | ||
); | ||
} | ||
function buildModuleSchema( | ||
@@ -583,4 +572,6 @@ hasteModuleName, | ||
) { | ||
const types = getTypes(ast); | ||
const moduleSpecs = Object.values(types).filter(isModuleInterface); | ||
const types = parser.getTypes(ast); | ||
const moduleSpecs = Object.values(types).filter(t => | ||
parser.isModuleInterface(t), | ||
); | ||
throwIfModuleInterfaceNotFound( | ||
@@ -630,11 +621,7 @@ moduleSpecs.length, | ||
); | ||
if (callExpression.arguments[0].type !== 'StringLiteral') { | ||
const type = callExpression.arguments[0].type; | ||
throw new IncorrectModuleRegistryCallArgumentTypeParserError( | ||
hasteModuleName, | ||
callExpression.arguments[0], | ||
methodName, | ||
type, | ||
); | ||
} | ||
throwIfIncorrectModuleRegistryCallArgument( | ||
hasteModuleName, | ||
callExpression.arguments[0], | ||
methodName, | ||
); | ||
const $moduleName = callExpression.arguments[0].value; | ||
@@ -641,0 +628,0 @@ throwIfUntypedModule( |
@@ -49,3 +49,3 @@ /** | ||
buildComponentSchema = _require3.buildComponentSchema; | ||
const _require4 = require('./components/schema'), | ||
const _require4 = require('../schema.js'), | ||
wrapComponentSchema = _require4.wrapComponentSchema; | ||
@@ -209,2 +209,46 @@ const _require5 = require('./modules'), | ||
} | ||
isModuleInterface(node) { | ||
var _node$extends; | ||
return ( | ||
node.type === 'TSInterfaceDeclaration' && | ||
((_node$extends = node.extends) === null || _node$extends === void 0 | ||
? void 0 | ||
: _node$extends.length) === 1 && | ||
node.extends[0].type === 'TSExpressionWithTypeArguments' && | ||
node.extends[0].expression.name === 'TurboModule' | ||
); | ||
} | ||
extractAnnotatedElement(typeAnnotation, types) { | ||
return types[typeAnnotation.typeParameters.params[0].typeName.name]; | ||
} | ||
/** | ||
* TODO(T108222691): Use flow-types for @babel/parser | ||
*/ | ||
getTypes(ast) { | ||
return ast.body.reduce((types, node) => { | ||
switch (node.type) { | ||
case 'ExportNamedDeclaration': { | ||
if (node.declaration) { | ||
switch (node.declaration.type) { | ||
case 'TSTypeAliasDeclaration': | ||
case 'TSInterfaceDeclaration': | ||
case 'TSEnumDeclaration': { | ||
types[node.declaration.id.name] = node.declaration; | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case 'TSTypeAliasDeclaration': | ||
case 'TSInterfaceDeclaration': | ||
case 'TSEnumDeclaration': { | ||
types[node.id.name] = node; | ||
break; | ||
} | ||
} | ||
return types; | ||
}, {}); | ||
} | ||
} | ||
@@ -211,0 +255,0 @@ module.exports = { |
@@ -16,32 +16,2 @@ /** | ||
/** | ||
* TODO(T108222691): Use flow-types for @babel/parser | ||
*/ | ||
function getTypes(ast) { | ||
return ast.body.reduce((types, node) => { | ||
switch (node.type) { | ||
case 'ExportNamedDeclaration': { | ||
if (node.declaration) { | ||
switch (node.declaration.type) { | ||
case 'TSTypeAliasDeclaration': | ||
case 'TSInterfaceDeclaration': | ||
case 'TSEnumDeclaration': { | ||
types[node.declaration.id.name] = node.declaration; | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case 'TSTypeAliasDeclaration': | ||
case 'TSInterfaceDeclaration': | ||
case 'TSEnumDeclaration': { | ||
types[node.id.name] = node; | ||
break; | ||
} | ||
} | ||
return types; | ||
}, {}); | ||
} | ||
// $FlowFixMe[unclear-type] Use flow-types for @babel/parser | ||
@@ -121,3 +91,2 @@ | ||
resolveTypeAnnotation, | ||
getTypes, | ||
}; |
{ | ||
"name": "@react-native/codegen", | ||
"version": "0.72.2", | ||
"version": "0.72.3", | ||
"description": "⚛️ Code generation tools for React Native", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/facebook/react-native/tree/HEAD/packages/react-native-codegen", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
1449347
191
23599