Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(typescript) #3688

Merged

Expand Object definitions

Expand Object definitions for properties and parameters
  • Loading branch information...
outofmemoryagain committed Mar 21, 2019
commit dca588d6a43e209ab2740089d63a28c0f48f5ef9
@@ -24,27 +24,52 @@ module.exports.generate = function () {
})
}

function convertTypeVal (type) {
function getPropDeclaration (prop) {
const propDef = prop.def
var propType = getTypeVal(propDef)
// const propTypeVal = propType
return `${prop.name}${!propDef.required ? '?' : ''} : ${propType};`
}

function getObjectDeclaration (objectDef) {
const props = []
for (var prop in objectDef) {
const propDef = { name: toCamelCase(prop), def: objectDef[prop] }
props.push(getPropDeclaration(propDef))
}
return props
}

function convertTypeVal (type, def) {
const t = type.trim()
if (t === 'Array') {
return '[]'
}
else if (t === 'Any') {
return 'any'
}
else if (t === 'Component') {
return 'Vue'
}
else if (t === 'Object') {
return 'any'
if (def.definition) {
return `{ ${getObjectDeclaration(def.definition).join(' ')} }`
}
else {
return 'any'
}
}
return t
}

function getTypeVal (types) {
function getTypeVal (def) {
const types = def.type
if (Array.isArray(types)) {
const propTypes = types.map(convertTypeVal)
return propTypes.join(' | ')
return propTypes.join('|')
}
else {
return convertTypeVal(types)
return convertTypeVal(types, def)
}
}

@@ -82,9 +107,10 @@ module.exports.generate = function () {
// Write Props
for (var key in content.props) {
const propName = toCamelCase(key)
var propType = getTypeVal(content.props[key].type)
const propDef = content.props[key]
var propType = getTypeVal(propDef)
const propTypeVal = propType
contents.push(` ${propName}: ${propTypeVal};\n`)
contents.push(` ${propName}${!propDef.required ? '?' : ''} : ${propTypeVal};\n`)
}

// Write Methods
@@ -93,7 +119,9 @@ module.exports.generate = function () {
if (content.methods[methodKey].params) {
const params = []
for (var paramsKey in content.methods[methodKey].params) {
params.push(`${paramsKey}: any`)
const prarmName = toCamelCase(paramsKey)
const paramDef = content.methods[methodKey].params[paramsKey]
params.push(`${prarmName}${!paramDef.required ? '?' : ''}: ${getTypeVal(paramDef)}`)
}
contents.push(params.join(', '))
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.