Skip to content

Commit

Permalink
Merge branch 'mixin-support' of https://github.com/roxburghm/vuese in…
Browse files Browse the repository at this point in the history
…to roxburghm-mixin-support
  • Loading branch information
HcySunYang committed Mar 21, 2019
2 parents 0fdbac1 + efcc1e2 commit 8a7183d
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/markdown-render/lib/genMarkdownTpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function(parserRes: ParserResult) {
templateStr += parserRes.events ? genBaseTemplate('events') : ''
templateStr += parserRes.slots ? genBaseTemplate('slots') : ''
templateStr += parserRes.methods ? genBaseTemplate('methods') : ''
templateStr += parserRes.mixIns ? genBaseTemplate('mixIns') : ''

return !forceGenerate && original === templateStr ? '' : templateStr
}
Expand Down
29 changes: 27 additions & 2 deletions packages/markdown-render/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ParserResult,
MixInResult,
PropsResult,
SlotResult,
EventResult,
Expand All @@ -14,13 +15,15 @@ interface RenderOptions {
slots: string[]
events: string[]
methods: string[]
mixIns: string[]
}

export interface RenderResult {
props?: string
slots?: string
events?: string
methods?: string
mixIns?: string
}

export class Render {
Expand All @@ -34,14 +37,15 @@ export class Render {
props: ['Name', 'Description', 'Type', 'Required', 'Default'],
events: ['Event Name', 'Description', 'Parameters'],
slots: ['Name', 'Description', 'Default Slot Content'],
methods: ['Method', 'Description', 'Parameters']
methods: ['Method', 'Description', 'Parameters'],
mixIns: ['MixIn']
},
this.options
)
}

render(): RenderResult {
const { props, slots, events, methods } = this.parserResult
const { props, slots, events, methods, mixIns } = this.parserResult
let md: RenderResult = {}
if (props) {
md.props = this.propRender(props)
Expand All @@ -55,6 +59,9 @@ export class Render {
if (methods) {
md.methods = this.methodRender(methods)
}
if (mixIns) {
md.mixIns = this.mixInRender(mixIns)
}

return md
}
Expand Down Expand Up @@ -209,6 +216,24 @@ export class Render {
return code
}

mixInRender(mixInsRes: MixInResult[]) {
const mixInsConfig = (this.options as RenderOptions).mixIns
let code = this.renderTabelHeader(mixInsConfig)
mixInsRes.forEach((mixIn: MixInResult) => {
const row: string[] = []
for (let i = 0; i < mixInsConfig.length; i++) {
if (mixInsConfig[i] === 'MixIn') {
row.push(mixIn.mixIn)
} else {
row.push('-')
}
}
code += this.renderTabelRow(row)
})

return code
}

renderTabelHeader(header: string[]): string {
const headerString = this.renderTabelRow(header)
const splitLine = this.renderSplitLine(header.length)
Expand Down
11 changes: 11 additions & 0 deletions packages/parser/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export interface MethodResult {
argumentsDesc?: string[]
}

export interface MixInResult {
mixIn: string
}

export type AttrsMap = {
[key: string]: string
}
Expand All @@ -63,6 +67,9 @@ export interface ParserOptions {
onSlot?: {
(slotRes: SlotResult): void
}
onMixIn?: {
(mixInRes: MixInResult): void
}
onName?: {
(name: string): void
}
Expand All @@ -76,6 +83,7 @@ export interface ParserResult {
props?: PropsResult[]
events?: EventResult[]
slots?: SlotResult[]
mixIns?: MixInResult[]
methods?: MethodResult[]
name?: string
componentDesc?: CommentResult
Expand Down Expand Up @@ -103,6 +111,9 @@ export function parser(
onSlot(slotRes: SlotResult) {
;(res.slots || (res.slots = [])).push(slotRes)
},
onMixIn(mixInRes: MixInResult) {
;(res.mixIns || (res.mixIns = [])).push(mixInRes)
},
onMethod(methodRes: MethodResult) {
;(res.methods || (res.methods = [])).push(methodRes)
},
Expand Down
15 changes: 14 additions & 1 deletion packages/parser/lib/parseJavascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ParserOptions,
EventResult,
MethodResult,
MixInResult,
SlotResult
} from './index'
import { getValueFromGenerate, isVueOption } from './helper'
Expand All @@ -28,7 +29,7 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {

rootPath.traverse({
ObjectProperty(path: NodePath<bt.ObjectProperty>) {
const { onProp, onMethod, onName, onSlot } = options
const { onProp, onMethod, onName, onSlot, onMixIn } = options
// Processing name
if (isVueOption(path, 'name')) {
let componentName = (path.node.value as bt.StringLiteral).value
Expand Down Expand Up @@ -71,6 +72,18 @@ export function parseJavascript(ast: bt.File, options: ParserOptions = {}) {
}
}

// Processing mixins
if (onMixIn && isVueOption(path, 'mixins')) {
const properties = (path.node.value as bt.ArrayExpression).elements

properties.forEach(mixIn => {
const result: MixInResult = {
mixIn: (mixIn as bt.Identifier).name
}
onMixIn(result)
})
}

// Processing methods
if (onMethod && isVueOption(path, 'methods')) {
const properties = (path.node
Expand Down

0 comments on commit 8a7183d

Please sign in to comment.