Skip to content

Commit

Permalink
fix: πŸ› Fixes an issue when using object spread and $Exact
Browse files Browse the repository at this point in the history
Fixes an issue where documentationjs breaks on build when  using object
spread and Flow utility $Exact (i.g: ...Exact<Type>)

βœ… Closes: #1324
  • Loading branch information
andrewdelprete authored and tmcw committed Jun 4, 2020
1 parent 78db9a4 commit 106945c
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/infer/properties.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// properties
const typeAnnotation = require('../type_annotation');
const findTarget = require('./finders').findTarget;

Expand All @@ -8,8 +9,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 +30,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 handing 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 +77,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 handing 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

0 comments on commit 106945c

Please sign in to comment.