babel-plugin-typescript-to-flow
Advanced tools
Comparing version 0.11.1 to 0.11.2
@@ -125,2 +125,18 @@ "use strict"; | ||
} | ||
else if (t.isIdentifier(typeName) && | ||
typeName.name === 'Omit' && | ||
flowTypeParameters && | ||
flowTypeParameters.params.length === 2 && | ||
(t.isStringLiteralTypeAnnotation(flowTypeParameters.params[1]) || | ||
(t.isUnionTypeAnnotation(flowTypeParameters.params[1]) && | ||
flowTypeParameters.params[1].types.every(type => t.isStringLiteralTypeAnnotation(type))))) { | ||
// special case when Omit can be directly replaced by `$Rest` | ||
return t.genericTypeAnnotation(t.identifier('$Rest'), t.typeParameterInstantiation([ | ||
flowTypeParameters.params[0], | ||
t.objectTypeAnnotation((t.isStringLiteralTypeAnnotation(flowTypeParameters.params[1]) | ||
? [flowTypeParameters.params[1]] | ||
: flowTypeParameters.params[1] | ||
.types).map(type => t.objectTypeProperty(t.identifier(type.value), t.anyTypeAnnotation())), [], [], []), | ||
])); | ||
} | ||
else { | ||
@@ -127,0 +143,0 @@ return t.genericTypeAnnotation(typeName, flowTypeParameters); |
import * as t from '@babel/types'; | ||
import { NodePath } from '@babel/traverse'; | ||
import { PluginPass } from '../types'; | ||
export declare function Program(path: NodePath<t.Program>, state: PluginPass): void; | ||
export declare const Program: { | ||
enter(path: NodePath<t.Program>, state: PluginPass): void; | ||
exit(path: NodePath<t.Program>, state: PluginPass): void; | ||
}; | ||
//# sourceMappingURL=Program.d.ts.map |
@@ -21,2 +21,5 @@ "use strict"; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -30,29 +33,58 @@ exports.Program = void 0; | ||
const convertTSDeclareFunction_1 = require("../converters/convertTSDeclareFunction"); | ||
function Program(path, state) { | ||
const isAmbientContext = state.opts.isAmbientContext; | ||
for (const st of path.get('body')) { | ||
const node = st.node; | ||
// todo: isTSTypeAliasDeclaration on NodePath | ||
if (t.isTSTypeAliasDeclaration(node)) { | ||
const replacement = convertTSTypeAliasDeclaration_1.convertTSTypeAliasDeclaration(node, isAmbientContext); | ||
replaceWith_1.replaceWith(st, replacement); | ||
const helperTypes_1 = __importDefault(require("../helperTypes")); | ||
exports.Program = { | ||
enter(path, state) { | ||
const isAmbientContext = state.opts.isAmbientContext; | ||
for (const st of path.get('body')) { | ||
const node = st.node; | ||
// todo: isTSTypeAliasDeclaration on NodePath | ||
if (t.isTSTypeAliasDeclaration(node)) { | ||
const replacement = convertTSTypeAliasDeclaration_1.convertTSTypeAliasDeclaration(node, isAmbientContext); | ||
replaceWith_1.replaceWith(st, replacement); | ||
} | ||
// todo: isClassDeclaration on NodePath | ||
if (t.isClassDeclaration(node)) { | ||
transformClassDeclaration_1.transformClassDeclaration(st, isAmbientContext); | ||
} | ||
// todo: isTSInterfaceDeclaration on NodePath | ||
if (t.isTSInterfaceDeclaration(node)) { | ||
replaceWith_1.replaceWith(st, convertTSInterfaceDeclaration_1.convertTSInterfaceDeclaration(node, isAmbientContext)); | ||
} | ||
if (t.isTSDeclareFunction(node)) { | ||
if (!isAmbientContext && !node.declare) { | ||
throw path.buildCodeFrameError('unexpected TSDeclareFunction'); | ||
} | ||
const replacement = convertTSDeclareFunction_1.convertTSDeclareFunction(node); | ||
replaceWith_1.replaceWith(st, replacement); | ||
} | ||
} | ||
// todo: isClassDeclaration on NodePath | ||
if (t.isClassDeclaration(node)) { | ||
transformClassDeclaration_1.transformClassDeclaration(st, isAmbientContext); | ||
}, | ||
exit(path, state) { | ||
const usedHelperTypes = new Set(); | ||
path.traverse({ | ||
GenericTypeAnnotation(annotationNodePath) { | ||
const node = annotationNodePath.node; | ||
if (t.isIdentifier(node.id)) { | ||
const name = node.id.name; | ||
// @ts-expect-error | ||
if (helperTypes_1.default[name]) { | ||
usedHelperTypes.add(name); | ||
} | ||
} | ||
}, | ||
}); | ||
const body = path.get('body'); | ||
const imports = body.filter(st => st.isImportDeclaration()); | ||
let after; | ||
if (imports.length > 0) { | ||
after = imports[imports.length - 1]; | ||
} | ||
// todo: isTSInterfaceDeclaration on NodePath | ||
if (t.isTSInterfaceDeclaration(node)) { | ||
replaceWith_1.replaceWith(st, convertTSInterfaceDeclaration_1.convertTSInterfaceDeclaration(node, isAmbientContext)); | ||
} | ||
if (t.isTSDeclareFunction(node)) { | ||
if (!isAmbientContext && !node.declare) { | ||
throw path.buildCodeFrameError('unexpected TSDeclareFunction'); | ||
} | ||
const replacement = convertTSDeclareFunction_1.convertTSDeclareFunction(node); | ||
replaceWith_1.replaceWith(st, replacement); | ||
} | ||
} | ||
} | ||
exports.Program = Program; | ||
usedHelperTypes.forEach(helperName => { | ||
if (after) | ||
after.insertAfter(helperTypes_1.default[helperName]); | ||
else | ||
body[0].insertBefore(helperTypes_1.default[helperName]); | ||
}); | ||
}, | ||
}; | ||
//# sourceMappingURL=Program.js.map |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "0.11.1", | ||
"version": "0.11.2", | ||
"scripts": { | ||
@@ -54,3 +54,3 @@ "cover": "jest --coverage", | ||
}, | ||
"gitHead": "3609b21bc2e6371f43498549981cf2af07c109a9" | ||
"gitHead": "950d353ab20bafcef431d6da0a9d1c1ad174c025" | ||
} |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
142661
154
1687