Skip to content

Commit

Permalink
Merge pull request #3 from Kiikurage/dev-opaque
Browse files Browse the repository at this point in the history
support opaque type
  • Loading branch information
Kiikurage committed Apr 14, 2018
2 parents ae96055 + c4c5147 commit caeb1bb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -37,7 +37,7 @@ $ babel --plugins ${ABS_PATH_TO_THIS_DIR}/dist ${SRC_FLOW_FILE} -o ${DEST_TS_FIL
|| Exact type | `{\| a: A \|}` | `{ a: A }` |
|| Indexers | `{ [A]: B }` | `{ [a: A]: B }` |
|| Existential type | `Map<*, *>` | `Map<any, any>` |
| | Opaque types | `opaque type A = B` | `type A = B` |
| | Opaque types | `opaque type A = B` | `type A = B` |
|| Variance | `interface A { +b: B, -c: C }` | `interface A { readonly b: B, c: C }` |
|| Type parameter bounds | `function f<A: string>(a:A){}` | `function f<A extends string>(a:A){}` |
|| Cast | `(a: A)` | `(a as A)` |
Expand Down
17 changes: 17 additions & 0 deletions src/converters/convert_opaque_type.ts
@@ -0,0 +1,17 @@
import {NodePath} from '@babel/traverse';
import {
OpaqueType,
tsTypeAliasDeclaration,
TSTypeAliasDeclaration,
} from '@babel/types';
import {warnOnlyOnce} from '../util';
import {convertFlowType} from './convert_flow_type';


export function convertOpaqueType(path: NodePath<OpaqueType>): TSTypeAliasDeclaration {
if (path.node.supertype) warnOnlyOnce('Subtyping constraints in opaque type in Flow is ignored in TypeScript');
const tsNode = tsTypeAliasDeclaration(path.node.id, null, convertFlowType(path.get('impltype')));
tsNode.declare = false;

return tsNode;
}
4 changes: 3 additions & 1 deletion src/index.ts
@@ -1,5 +1,6 @@
import {buildPlugin} from './plugin';
import {ImportDeclaration} from './visitors/import_declaration';
import {OpaqueType} from './visitors/opaque_type';
import {TypeAnnotation} from './visitors/type_annotation';
import {TypeCastExpression} from './visitors/type_cast_expression';
import {TypeParameterDeclaration} from './visitors/type_parameter_declaration';
Expand All @@ -8,5 +9,6 @@ export = buildPlugin([
TypeAnnotation,
TypeParameterDeclaration,
ImportDeclaration,
TypeCastExpression
TypeCastExpression,
OpaqueType
]);
7 changes: 7 additions & 0 deletions src/visitors/opaque_type.ts
@@ -0,0 +1,7 @@
import {OpaqueType} from '@babel/types';
import {NodePath} from '@babel/traverse';
import {convertOpaqueType} from '../converters/convert_opaque_type';

export function OpaqueType(path: NodePath<OpaqueType>) {
path.replaceWith(convertOpaqueType(path));
}
16 changes: 16 additions & 0 deletions test/visitors/opaque_type.test.ts
@@ -0,0 +1,16 @@
import * as pluginTester from 'babel-plugin-tester';
import {buildPlugin} from '../../src/plugin';
import {OpaqueType} from '../../src/visitors/opaque_type';

pluginTester({
plugin: buildPlugin([OpaqueType]),
tests: [{
title: 'opaque type',
code: `opaque type A = B;`,
output: `type A = B;`
}, {
title: 'opaque type with super type',
code: `opaque type A: S = B;`,
output: `type A = B;`
}]
});

0 comments on commit caeb1bb

Please sign in to comment.