Skip to content

Commit

Permalink
Support alternative syntax for exact object
Browse files Browse the repository at this point in the history
Sometimes `{ [any]: empty }` is used as a workaround instead of `{||}`.

See:
- facebook/flow#2977
- facebook/flow#4582
  • Loading branch information
skovhus committed Jan 7, 2018
1 parent 123aa74 commit 11acd6a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* @flow */

export const input = `
type Demo = {
foo?: number;
[any]: empty;
};
`;

export const expected = `
import t from "flow-runtime";
const Demo = t.type("Demo", t.exactObject(
t.property("foo", t.number(), true)
));
`;
13 changes: 11 additions & 2 deletions packages/babel-plugin-flow-runtime/src/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,12 +762,21 @@ converters.ObjectTypeAnnotation = (context: ConversionContext, path: NodePath):
[[], new Map(), new Map()]
);

const isEmptyObjectTypeIndexer = (path: NodePath) => path.node.type === 'ObjectTypeIndexer'
&& path.node.key.type === 'AnyTypeAnnotation'
&& path.node.value.type === 'EmptyTypeAnnotation';

const indexers = path.get('indexers');

const body = [
...path.get('callProperties'),
...properties,
...path.get('indexers')
...indexers.filter(path => !isEmptyObjectTypeIndexer(path))
];
return context.call(path.node.exact ? 'exactObject' : 'object', ...body.map(item => convert(context, item)));

const isExactObject = path.node.exact || indexers.some(path => isEmptyObjectTypeIndexer(path));

return context.call(isExactObject ? 'exactObject' : 'object', ...body.map(item => convert(context, item)));
};

converters.ObjectTypeSpreadProperty = (context: ConversionContext, path: NodePath): Node => {
Expand Down

1 comment on commit 11acd6a

@jedwards1211
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skovhus do you mind if I close this since $Shape works instead?

Please sign in to comment.