Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.

Commit 109cd4e

Browse files
authored
feat: add parse options (#104)
* feat: add a few options to parse functions add custom handlers for template and script * feat: resolve.module implementation * doc: add some documentation fixes #102
1 parent 8ad6f95 commit 109cd4e

File tree

10 files changed

+97
-25
lines changed

10 files changed

+97
-25
lines changed

README.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,31 @@ var vueDocs = require('vue-docgen-api')
2929
var componentInfo = vueDocs.parse(filePath)
3030
```
3131

32-
or with typescript/es6
32+
or with typescript
3333

3434
```ts
3535
import { parse } from 'vue-docgen-api'
36-
var componentInfo = parse(filePath)
36+
var componentInfo = parse(filePath);
37+
var componentInfo = vueDocs.parse(filePath, {
38+
alias: {"@assets": path.resolve(__dirname, "src/assets")},
39+
resolve: [path.resolve(__dirname, "src")],
40+
addScriptHandler: [function(
41+
documentation: Documentation,
42+
componentDefinition: NodePath,
43+
astPath: bt.File,
44+
opt: ParseOptions){
45+
// handle custom code in script
46+
}],
47+
addTemplateHandler: [function(
48+
documentation: Documentation,
49+
templateAst: ASTElement,
50+
options: TemplateParserOptions){
51+
// handle custom directives here
52+
}]
53+
})
3754
```
3855

39-
### parse(filePath)
56+
### parse(filePath:string, options?: DocGenOptions)
4057

4158
| Parameter | Type | Description |
4259
| --------- | ------ | ------------- |

src/main.ts

+20-7
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
import { ComponentDoc, Documentation } from './Documentation'
2-
import { parseFile, parseSource as parseSourceLocal } from './parse'
2+
import { DocGenOptions, parseFile, ParseOptions, parseSource as parseSourceLocal } from './parse'
3+
export { ScriptHandler, TemplateHandler } from './parse'
4+
export { ComponentDoc, DocGenOptions, ParseOptions, Documentation }
35

4-
export { ComponentDoc }
5-
6-
export function parse(filePath: string, aliases?: { [alias: string]: string }): ComponentDoc {
6+
export function parse(
7+
filePath: string,
8+
opts?: DocGenOptions | { [alias: string]: string },
9+
): ComponentDoc {
710
const doc = new Documentation()
8-
parseFile(doc, { filePath, aliases })
11+
const options: ParseOptions = isOptionsObject(opts)
12+
? { ...opts, filePath }
13+
: { filePath, alias: opts }
14+
parseFile(doc, options)
915
return doc.toObject()
1016
}
1117

1218
export function parseSource(
1319
source: string,
1420
filePath: string,
15-
aliases?: { [alias: string]: string },
21+
opts?: DocGenOptions | { [alias: string]: string },
1622
): ComponentDoc {
1723
const doc = new Documentation()
18-
parseSourceLocal(doc, source, { filePath, aliases })
24+
const options: ParseOptions = isOptionsObject(opts)
25+
? { ...opts, filePath }
26+
: { filePath, alias: opts }
27+
parseSourceLocal(doc, source, options)
1928
return doc.toObject()
2029
}
30+
31+
function isOptionsObject(opts: any): opts is DocGenOptions {
32+
return !!opts && !!opts.alias
33+
}

src/parse-script.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ParseOptions } from './parse'
1313

1414
const ERROR_MISSING_DEFINITION = 'No suitable component definition found'
1515

16-
type Handler = (
16+
export type Handler = (
1717
doc: Documentation,
1818
componentDefinition: NodePath,
1919
ast: bt.File,

src/parse.ts

+42-8
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,47 @@ import * as fs from 'fs'
22
import * as path from 'path'
33
import { parseComponent, SFCDescriptor } from 'vue-template-compiler'
44
import { Documentation } from './Documentation'
5-
import parseScript from './parse-script'
6-
import parseTemplate from './parse-template'
7-
import handlers from './script-handlers'
5+
import parseScript, { Handler as ScriptHandler } from './parse-script'
6+
import parseTemplate, { Handler as TemplateHandler } from './parse-template'
7+
import scriptHandlers from './script-handlers'
88
import templateHandlers from './template-handlers'
99
import cacher from './utils/cacher'
1010

1111
const ERROR_EMPTY_DOCUMENT = 'The passed source is empty'
1212

13-
export interface ParseOptions {
13+
export { ScriptHandler, TemplateHandler }
14+
15+
export interface ParseOptions extends DocGenOptions {
1416
filePath: string
17+
/**
18+
* In what language is the component written
19+
* @default undefined - let the system decide
20+
*/
1521
lang?: 'ts' | 'js'
22+
}
23+
24+
export interface DocGenOptions {
25+
/**
26+
* Which exported variables should be looked at
27+
* @default undefined - means treat all dependencies
28+
*/
1629
nameFilter?: string[]
17-
aliases?: { [alias: string]: string }
30+
/**
31+
* What alias should be replaced in requires and imports
32+
*/
33+
alias?: { [alias: string]: string }
34+
/**
35+
* What directories should be searched when resolving modules
36+
*/
37+
modules?: string[]
38+
/**
39+
* Handlers that will be added at the end of the script analysis
40+
*/
41+
addScriptHandlers?: ScriptHandler[]
42+
/**
43+
* Handlers that will be added at the end of the template analysis
44+
*/
45+
addTemplateHandlers?: TemplateHandler[]
1846
}
1947

2048
/**
@@ -50,7 +78,13 @@ export function parseSource(documentation: Documentation, source: string, opt: P
5078

5179
// get slots and props from template
5280
if (parts && parts.template) {
53-
parseTemplate(parts.template, documentation, templateHandlers, opt.filePath)
81+
const addTemplateHandlers: TemplateHandler[] = opt.addTemplateHandlers || []
82+
parseTemplate(
83+
parts.template,
84+
documentation,
85+
[...templateHandlers, ...addTemplateHandlers],
86+
opt.filePath,
87+
)
5488
}
5589

5690
const scriptSource = parts ? (parts.script ? parts.script.content : undefined) : source
@@ -60,8 +94,8 @@ export function parseSource(documentation: Documentation, source: string, opt: P
6094
/\.tsx?$/i.test(path.extname(opt.filePath))
6195
? 'ts'
6296
: 'js'
63-
64-
parseScript(scriptSource, documentation, handlers, opt)
97+
const addScriptHandlers: ScriptHandler[] = opt.addScriptHandlers || []
98+
parseScript(scriptSource, documentation, [...scriptHandlers, ...addScriptHandlers], opt)
6599
}
66100

67101
if (!documentation.get('displayName')) {

src/script-handlers/extendsHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function extendsHandler(
3131

3232
const originalDirName = path.dirname(opt.filePath)
3333

34-
const pathResolver = makePathResolver(originalDirName, opt.aliases)
34+
const pathResolver = makePathResolver(originalDirName, opt.alias)
3535

3636
resolveImmediatelyExportedRequire(pathResolver, extendsFilePath)
3737

src/script-handlers/mixinsHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default function mixinsHandler(
2121
) {
2222
const originalDirName = path.dirname(opt.filePath)
2323

24-
const pathResolver = makePathResolver(originalDirName, opt.aliases)
24+
const pathResolver = makePathResolver(originalDirName, opt.alias)
2525

2626
// filter only mixins
2727
const mixinVariableNames = getMixinsVariableNames(componentDefinition)

src/utils/makePathResolver.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import resolvePathFrom from '../utils/resolvePathFrom'
44
export default function makePathResolver(
55
refDirName: string,
66
aliases?: { [alias: string]: string },
7+
modules?: string[],
78
): (filePath: string, originalDirNameOverride?: string) => string {
89
return (filePath: string, originalDirNameOverride?: string): string =>
9-
resolvePathFrom(resolveAliases(filePath, aliases || {}), originalDirNameOverride || refDirName)
10+
resolvePathFrom(resolveAliases(filePath, aliases || {}), [
11+
originalDirNameOverride || refDirName,
12+
...(modules || []),
13+
])
1014
}

src/utils/resolvePathFrom.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const SUFFIXES = ['', '.js', '.ts', '.vue', '/index.js', '/index.ts']
22

3-
export default function resolvePathFrom(path: string, from: string): string {
3+
export default function resolvePathFrom(path: string, from: string[]): string {
44
let finalPath = ''
55
SUFFIXES.forEach(s => {
66
if (!finalPath.length) {
77
try {
88
finalPath = require.resolve(`${path}${s}`, {
9-
paths: [from],
9+
paths: from,
1010
})
1111
} catch (e) {
1212
// eat the error

tests/components/mixin-resolved/button.vue

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<script>
1010
import { anotherMixin, myMixin } from '@mixins/index'
11+
import { multi, hidden } from 'multiMixin'
1112
1213
export default {
1314
mixins: [anotherMixin, myMixin],

tests/components/mixin-resolved/resolved-mixin.test.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ let docButton: ComponentDoc
88
describe('tests button', () => {
99
beforeAll(done => {
1010
docButton = parse(button, {
11-
'@mixins': path.resolve(__dirname, '../../mixins'),
11+
alias: {
12+
'@mixins': path.resolve(__dirname, '../../mixins'),
13+
},
14+
modules: [path.resolve(__dirname, '../../mixins')],
1215
})
1316
done()
1417
})

0 commit comments

Comments
 (0)