babel-plugin-typescript-to-flow
Advanced tools
Comparing version 0.11.0 to 0.11.1
@@ -264,2 +264,6 @@ "use strict"; | ||
} | ||
if (t.isTSConditionalType(node)) { | ||
//todo: warn | ||
return t.anyTypeAnnotation(); | ||
} | ||
throw new Error(`Unsupported flow type TSType(type=${node.type})`); | ||
@@ -266,0 +270,0 @@ } |
@@ -42,3 +42,3 @@ "use strict"; | ||
return { | ||
name: 'babel-plugin-flow-to-typescript', | ||
name: 'babel-plugin-typescript-to-flow', | ||
visitor, | ||
@@ -45,0 +45,0 @@ manipulateOptions(_babel, parserOpts) { |
@@ -29,17 +29,4 @@ "use strict"; | ||
const replaceWith_1 = require("../utils/replaceWith"); | ||
const convertTSType_1 = require("../converters/convertTSType"); | ||
const convertFunctionTypeAnnotation_1 = require("../converters/convertFunctionTypeAnnotation"); | ||
const convertKey_1 = require("../converters/convertKey"); | ||
const convertClassTypeDeclaration_1 = require("../converters/convertClassTypeDeclaration"); | ||
const converted = new WeakSet(); | ||
function convertMemberExpressionToQualifiedTypeIdentifier(id) { | ||
if (t.isIdentifier(id)) | ||
return id; | ||
if (!t.isIdentifier(id.object) && !t.isMemberExpression(id.object)) { | ||
throw new Error('not implemented'); | ||
} | ||
if (!t.isIdentifier(id.property)) { | ||
throw new Error('not implemented'); | ||
} | ||
return t.qualifiedTypeIdentifier(id.property, convertMemberExpressionToQualifiedTypeIdentifier(id.object)); | ||
} | ||
function transformClassDeclaration(path, isAmbientContext) { | ||
@@ -52,83 +39,3 @@ const node = path.node; | ||
if (node.declare || isAmbientContext) { | ||
const id = node.id; | ||
let typeParameters = null; | ||
if (node.typeParameters) { | ||
if (!t.isTSTypeParameterDeclaration(node.typeParameters)) { | ||
throw new Error(`TSTypeParameterDeclaration is expected, but got ${node.typeParameters.type} instead`); | ||
} | ||
typeParameters = convertTSTypeParameterDeclaration_1.convertTSTypeParameterDeclaration(node.typeParameters); | ||
} | ||
let _extends = []; | ||
if (node.superClass) { | ||
if (!t.isIdentifier(node.superClass) && | ||
!t.isMemberExpression(node.superClass)) { | ||
throw new Error(`Identifier or MemberExpression is expected, but got ${node.superClass.type} instead`); | ||
} | ||
if (node.superTypeParameters && | ||
!t.isTSTypeParameterInstantiation(node.superTypeParameters)) { | ||
throw new Error(`TSTypeParameterInstantiation is expected, but got ${node.superClass.type} instead`); | ||
} | ||
_extends = [ | ||
t.interfaceExtends(convertMemberExpressionToQualifiedTypeIdentifier(node.superClass), node.superTypeParameters | ||
? convertTSTypeParameterInstantiation_1.convertTSTypeParameterInstantiation(node.superTypeParameters) | ||
: null), | ||
]; | ||
} | ||
const properties = []; | ||
const indexers = []; | ||
const callProperties = []; | ||
const internalSlots = []; | ||
for (const member of node.body.body) { | ||
if (t.isClassProperty(member)) { | ||
const key = convertKey_1.convertKey(member.key); | ||
const prop = t.objectTypeProperty( | ||
// @ts-ignore | ||
key, convertTSType_1.convertTSType(member.typeAnnotation.typeAnnotation)); | ||
prop.static = member.static; | ||
prop.optional = !!member.optional; | ||
if (member.readonly) { | ||
prop.variance = t.variance('plus'); | ||
} | ||
properties.push(prop); | ||
} | ||
else if (t.isTSDeclareMethod(member)) { | ||
const key = t.isIdentifier(member.key) || t.isStringLiteral(member.key) | ||
? member.key | ||
: t.isNumericLiteral(member.key) | ||
? t.stringLiteral(member.key.value + '') | ||
: null; | ||
const { typeParams, parameters, rest, returnType, } = convertFunctionTypeAnnotation_1.convertFunctionTypeAnnotation(member); | ||
const prop = t.objectTypeProperty( | ||
// @ts-ignore | ||
key, t.functionTypeAnnotation(typeParams, parameters, rest, returnType ? returnType : t.anyTypeAnnotation())); | ||
prop.static = member.static; | ||
prop.kind = | ||
member.kind === 'get' || member.kind === 'set' ? member.kind : 'init'; | ||
// @ts-ignore | ||
prop.method = true; | ||
properties.push(prop); | ||
} | ||
else { | ||
throw new Error('todo:'); | ||
} | ||
} | ||
const body = t.objectTypeAnnotation(properties, indexers, callProperties, internalSlots); | ||
const replacement = t.declareClass(id, typeParameters, _extends, body); | ||
if (node.implements) { | ||
replacement.implements = node.implements.map(impl => { | ||
if (!t.isTSExpressionWithTypeArguments(impl)) { | ||
throw new Error(`TSExpressionWithTypeArguments is expected, but got ${impl.type} instead`); | ||
} | ||
const id = convertTSEntityName_1.convertTSEntityName(impl.expression); | ||
const typeParameters = impl.typeParameters | ||
? convertTSTypeParameterInstantiation_1.convertTSTypeParameterInstantiation(impl.typeParameters) | ||
: null; | ||
if (!t.isIdentifier(id)) { | ||
// todo: create alias for the interface to be added to implements | ||
throw new Error('not implemented, flow does not allow class to implement interface with qualified type name'); | ||
} | ||
return t.classImplements(id, typeParameters); | ||
}); | ||
} | ||
replaceWith_1.replaceWith(path, replacement); | ||
replaceWith_1.replaceWith(path, convertClassTypeDeclaration_1.convertClassTypeDeclaration(node)); | ||
return; | ||
@@ -135,0 +42,0 @@ } |
import * as t from '@babel/types'; | ||
import { NodePath } from '@babel/traverse'; | ||
export declare function ClassDeclaration(path: NodePath<t.ClassDeclaration>): void; | ||
import { PluginPass } from '../types'; | ||
export declare function ClassDeclaration(path: NodePath<t.ClassDeclaration>, state: PluginPass): void; | ||
//# sourceMappingURL=ClassDeclaration.d.ts.map |
@@ -5,6 +5,7 @@ "use strict"; | ||
const transformClassDeclaration_1 = require("../transforms/transformClassDeclaration"); | ||
function ClassDeclaration(path) { | ||
transformClassDeclaration_1.transformClassDeclaration(path); | ||
function ClassDeclaration(path, state) { | ||
const isAmbientContext = state.opts.isAmbientContext; | ||
transformClassDeclaration_1.transformClassDeclaration(path, isAmbientContext); | ||
} | ||
exports.ClassDeclaration = ClassDeclaration; | ||
//# sourceMappingURL=ClassDeclaration.js.map |
@@ -26,2 +26,3 @@ "use strict"; | ||
const replaceWith_1 = require("../utils/replaceWith"); | ||
const convertClassTypeDeclaration_1 = require("../converters/convertClassTypeDeclaration"); | ||
function ExportNamedDeclaration(path) { | ||
@@ -48,2 +49,6 @@ const srcDeclaration = path.node.declaration; | ||
} | ||
else if (t.isClassDeclaration(srcDeclaration)) { | ||
const replacement = t.declareExportDeclaration(convertClassTypeDeclaration_1.convertClassTypeDeclaration(srcDeclaration)); | ||
replaceWith_1.replaceWith(path, replacement); | ||
} | ||
else if (srcDeclaration && srcDeclaration.declare) { | ||
@@ -50,0 +55,0 @@ srcDeclaration.declare = false; |
@@ -30,3 +30,2 @@ "use strict"; | ||
function Program(path, state) { | ||
// todo: pass this in plugin options | ||
const isAmbientContext = state.opts.isAmbientContext; | ||
@@ -33,0 +32,0 @@ for (const st of path.get('body')) { |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.11.0", | ||
"version": "0.11.1", | ||
"scripts": { | ||
@@ -54,3 +54,3 @@ "cover": "jest --coverage", | ||
}, | ||
"gitHead": "82ba4e8eed33771ef0fbec2266c103bc7e1cc95a" | ||
"gitHead": "3609b21bc2e6371f43498549981cf2af07c109a9" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
132996
150
1596