UNPKG

flow-immutable-models

Version:

Generates model classes from Flow types using Immutable.js

42 lines (38 loc) 1.86 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRequiredTypeStatement = getRequiredTypeStatement; exports.getFullTypeStatement = getFullTypeStatement; exports.getFromJSTypeStatement = getFromJSTypeStatement; exports.getOptionalTypeStatement = getOptionalTypeStatement; function getRequiredTypeStatement(j, className, defaultValues, typeProperties) { if (!defaultValues) { return null; } var defaultPropNames = defaultValues.init.properties.map(function (p) { return p.key.name; }); var requiredProperties = typeProperties.filter(function (p) { return !defaultPropNames.includes(p.key.name); }); return j.typeAlias(j.identifier(className + 'RequiredArguments'), null, j.objectTypeAnnotation(requiredProperties)); } function getFullTypeStatement(j, className, defaultValues) { if (!defaultValues) { return null; } return j.typeAlias(j.identifier(className + 'FullType'), null, j.intersectionTypeAnnotation([j.genericTypeAnnotation(j.identifier(className + 'OptionalArguments'), null), j.genericTypeAnnotation(j.identifier(className + 'RequiredArguments'), null)])); } function getFromJSTypeStatement(j, className, defaultValues) { if (!defaultValues) { return null; } return j.typeAlias(j.identifier(className + 'FromJSType'), null, j.intersectionTypeAnnotation([j.genericTypeAnnotation(j.identifier('$Shape'), j.typeParameterInstantiation([j.genericTypeAnnotation(j.identifier(className + 'FullType'), null)])), j.genericTypeAnnotation(j.identifier(className + 'RequiredArguments'), null)])); } function getOptionalTypeStatement(j, className, defaultValues) { if (!defaultValues) { return null; } return j.typeAlias(j.identifier(className + 'OptionalArguments'), null, j.typeofTypeAnnotation(j.genericTypeAnnotation(j.identifier(defaultValues.id.name), null))); }