Skip to content

fix: 🐛 Fixes an issue when using object spread and $Exact #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/infer/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ function prefixedName(name, prefix) {
return name;
}

function isObjectSpreadAndExactUtilTypeProperty(property) {
return (
property.type === 'ObjectTypeSpreadProperty' &&
property.argument.id.name === '$Exact'
);
}

function propertyToDoc(property, prefix) {
let type;
let name;

if (property.type === 'ObjectTypeProperty') {
// flow
type = typeAnnotation(property.value);
Expand All @@ -20,7 +29,20 @@ function propertyToDoc(property, prefix) {
// typescript
type = typeAnnotation(property);
}
const name = property.key.name || property.key.value;

if (property.key) {
name = property.key.name || property.key.value;
}

// Special handling for { ...$Exact<Type> }
if (isObjectSpreadAndExactUtilTypeProperty(property)) {
name = property.argument.id.name;
type = {
type: 'NameExpression',
name: property.argument.typeParameters.params[0].id.name
};
}

if (property.optional) {
type = {
type: 'OptionalType',
Expand Down Expand Up @@ -54,7 +76,18 @@ function inferProperties(comment) {
) {
const properties = value.properties || value.members || value.body || [];
properties.forEach(function(property) {
if (!explicitProperties.has(prefixedName(property.key.name, prefix))) {
let name;

if (property.key) {
name = property.key.name;
}

// Special handling for { ...$Exact<Type> }
if (isObjectSpreadAndExactUtilTypeProperty(property)) {
name = property.argument.id.name;
}

if (!explicitProperties.has(prefixedName(name, prefix))) {
comment.properties = comment.properties.concat(
propertyToDoc(property, prefix)
);
Expand Down