Disallow object spreads in ObjectTypeAnnotations

Summary:
We don't currently support object spreads in `ObjectTypeAnnotation`s. This diff adds an explicit error for the case in the parser, so that when people use object spreads in ObjectTypeAnnotations, they get feedback from the linter rule and parser.

Previously, the Linter would crash, and the parser would fail, but the error wouldn't be immediately obvious.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D24543650

fbshipit-source-id: 76f389c72f858ee6281c5aff5ce797f3be685096
This commit is contained in:
Ramanpreet Nara 2020-10-26 16:20:24 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 09d4cb7b9f
Коммит f1f9cef9d3
2 изменённых файлов: 31 добавлений и 5 удалений

Просмотреть файл

@ -133,6 +133,22 @@ class UnsupportedArrayElementTypeAnnotationParserError extends ParserError {
*/
class UnsupportedObjectPropertyTypeAnnotationParserError extends ParserError {
constructor(
hasteModuleName: string,
propertyAST: $FlowFixMe,
invalidPropertyType: string,
) {
let message = `'ObjectTypeAnnotation' cannot contain '${invalidPropertyType}'.`;
if (invalidPropertyType === 'ObjectTypeSpreadProperty') {
message = "Object spread isn't supported in 'ObjectTypeAnnotation's.";
}
super(hasteModuleName, propertyAST, message);
}
}
class UnsupportedObjectPropertyValueTypeAnnotationParserError extends ParserError {
constructor(
hasteModuleName: string,
propertyValueAST: $FlowFixMe,
@ -202,4 +218,5 @@ module.exports = {
UnsupportedFunctionReturnTypeAnnotationParserError,
UnsupportedModulePropertyParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
};

Просмотреть файл

@ -40,6 +40,7 @@ const {
UnsupportedFunctionReturnTypeAnnotationParserError,
UnsupportedModulePropertyParserError,
UnsupportedObjectPropertyTypeAnnotationParserError,
UnsupportedObjectPropertyValueTypeAnnotationParserError,
} = require('./errors.js');
const invariant = require('invariant');
@ -207,9 +208,17 @@ function translateTypeAnnotation(
type: 'ObjectTypeAnnotation',
properties: (typeAnnotation.properties: Array<$FlowFixMe>)
.map<?NativeModuleObjectTypeAnnotationPropertySchema>(property => {
const {optional, key} = property;
return guard(() => {
if (property.type !== 'ObjectTypeProperty') {
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
hasteModuleName,
property,
property.type,
);
}
const {optional, key} = property;
const [
propertyTypeAnnotation,
isPropertyNullable,
@ -224,7 +233,7 @@ function translateTypeAnnotation(
);
if (propertyTypeAnnotation.type === 'FunctionTypeAnnotation') {
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
hasteModuleName,
property.value,
property.key,
@ -233,7 +242,7 @@ function translateTypeAnnotation(
}
if (propertyTypeAnnotation.type === 'VoidTypeAnnotation') {
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
hasteModuleName,
property.value,
property.key,
@ -242,7 +251,7 @@ function translateTypeAnnotation(
}
if (propertyTypeAnnotation.type === 'PromiseTypeAnnotation') {
throw new UnsupportedObjectPropertyTypeAnnotationParserError(
throw new UnsupportedObjectPropertyValueTypeAnnotationParserError(
hasteModuleName,
property.value,
property.key,