Skip to content

Commit

Permalink
Fix data documentation to also work when data is method
Browse files Browse the repository at this point in the history
  • Loading branch information
josephoneill committed Jun 13, 2019
1 parent 4fc4a7d commit 53c3b7e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
16 changes: 16 additions & 0 deletions packages/parser/lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export function getValueFromGenerate(node: any) {
}

export function computesFromStore(node: any): boolean {
if (node === undefined) {
return false
}

let fromStore = false
if (bt.isObjectMethod(node)) {
fromStore = computesFromStore(node.body)
Expand All @@ -88,3 +92,15 @@ export function computesFromStore(node: any): boolean {

return fromStore
}

export function getLiteralValue(node: bt.Node): string {
let data = ''
if (
bt.isStringLiteral(node) ||
bt.isBooleanLiteral(node) ||
bt.isNumericLiteral(node)
) {
data = node.value.toString()
}
return data
}
37 changes: 36 additions & 1 deletion packages/parser/lib/parseJavascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
properties.forEach(node => {
const commentsRes: CommentResult = getComments(node)
const isFromStore: boolean = computesFromStore(node)

/*let type = ''
if (bt.isObjectMethod(node)) {
if (bt.isReturnStatement(node.body.body[0])) {
let hi = (node.body.body[0]
.type as bt.ReturnStatement).argument.type.toString()
}
}*/

// Collect only computed that have @vuese annotations
if (commentsRes.vuese) {
const result: ComputedResult = {
Expand All @@ -124,7 +133,6 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
})
}

// Processing data
if (
onData &&
isVueOption(path, 'data') &&
Expand Down Expand Up @@ -208,6 +216,7 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
}
},
ObjectMethod(path: NodePath<bt.ObjectMethod>) {
const { onData } = options
// @Component: functional component - `ctx.children` in the render function
if (
options.onSlot &&
Expand All @@ -216,6 +225,32 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
) {
determineChildren(path, options.onSlot)
}

// Data can be represented as a component or a method
if (onData && isVueOption(path, 'data')) {
path.node.body.body.forEach(body => {
if (bt.isReturnStatement(body)) {
const properties = (body.argument as bt.ObjectExpression).properties.filter(
n => bt.isObjectMethod(n) || bt.isObjectProperty(n)
) as (bt.ObjectProperty)[]

properties.forEach(node => {
const commentsRes: CommentResult = getComments(node)
// Collect only data that have @vuese annotations
if (commentsRes.vuese) {
const result: DataResult = {
name: node.key.name,
type: '',
describe: commentsRes.default,
default: ''
}
processDataValue(node.value, result)
onData(result)
}
})
}
})
}
},
CallExpression(path: NodePath<bt.CallExpression>) {
const node = path.node
Expand Down
13 changes: 1 addition & 12 deletions packages/parser/lib/processData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as bt from '@babel/types'
import { getLiteralValue } from './helper'
import { DataResult } from './index'

export function processDataValue(dataValueNode: bt.Node, result: DataResult) {
Expand Down Expand Up @@ -54,15 +55,3 @@ function literalToType(literal: string) {
.replace('Numeric', 'Number')
return type
}

function getLiteralValue(node: bt.Node): string {
let data = ''
if (
bt.isStringLiteral(node) ||
bt.isBooleanLiteral(node) ||
bt.isNumericLiteral(node)
) {
data = node.value.toString()
}
return data
}

0 comments on commit 53c3b7e

Please sign in to comment.