From c37d6aa58a89dab93aadbfd560f59c5bb7fae6c1 Mon Sep 17 00:00:00 2001 From: Bart Ledoux Date: Sun, 10 Feb 2019 16:49:24 -0600 Subject: [PATCH 1/3] feat: add a few options to parse functions add custom handlers for template and script --- src/main.ts | 21 +++++++++++++++------ src/parse-script.ts | 2 +- src/parse.ts | 41 +++++++++++++++++++++++++++++++++++------ 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/main.ts b/src/main.ts index 29f65f1..b75516d 100755 --- a/src/main.ts +++ b/src/main.ts @@ -1,20 +1,29 @@ import { ComponentDoc, Documentation } from './Documentation' -import { parseFile, parseSource as parseSourceLocal } from './parse' +import { parseFile, ParseOptions, parseSource as parseSourceLocal } from './parse' -export { ComponentDoc } +export { ComponentDoc, ParseOptions } -export function parse(filePath: string, aliases?: { [alias: string]: string }): ComponentDoc { +export function parse( + filePath: string, + opts?: ParseOptions | { [alias: string]: string }, +): ComponentDoc { const doc = new Documentation() - parseFile(doc, { filePath, aliases }) + const options: ParseOptions = isParserOptions(opts) ? opts : { filePath, aliases: opts } + parseFile(doc, options) return doc.toObject() } export function parseSource( source: string, filePath: string, - aliases?: { [alias: string]: string }, + opts?: ParseOptions | { [alias: string]: string }, ): ComponentDoc { const doc = new Documentation() - parseSourceLocal(doc, source, { filePath, aliases }) + const options: ParseOptions = isParserOptions(opts) ? opts : { filePath, aliases: opts } + parseSourceLocal(doc, source, options) return doc.toObject() } + +function isParserOptions(opts: any): opts is ParseOptions { + return !!opts && !!opts.aliases +} diff --git a/src/parse-script.ts b/src/parse-script.ts index ba8b420..c931094 100644 --- a/src/parse-script.ts +++ b/src/parse-script.ts @@ -13,7 +13,7 @@ import { ParseOptions } from './parse' const ERROR_MISSING_DEFINITION = 'No suitable component definition found' -type Handler = ( +export type Handler = ( doc: Documentation, componentDefinition: NodePath, ast: bt.File, diff --git a/src/parse.ts b/src/parse.ts index e7be496..fff54ce 100755 --- a/src/parse.ts +++ b/src/parse.ts @@ -2,9 +2,9 @@ import * as fs from 'fs' import * as path from 'path' import { parseComponent, SFCDescriptor } from 'vue-template-compiler' import { Documentation } from './Documentation' -import parseScript from './parse-script' -import parseTemplate from './parse-template' -import handlers from './script-handlers' +import parseScript, { Handler as ScriptHandler } from './parse-script' +import parseTemplate, { Handler as TemplateHandler } from './parse-template' +import scriptHandlers from './script-handlers' import templateHandlers from './template-handlers' import cacher from './utils/cacher' @@ -12,9 +12,32 @@ const ERROR_EMPTY_DOCUMENT = 'The passed source is empty' export interface ParseOptions { filePath: string + /** + * In what language is the component written + * @default undefined - let the system decide + */ lang?: 'ts' | 'js' + /** + * Which exported variables should be looked at + * @default undefined - means treat all dependencies + */ nameFilter?: string[] + /** + * What alias should be replaced in requires and imports + */ aliases?: { [alias: string]: string } + /** + * What directories should be searched when resolving modules + */ + modules?: string[] + /** + * Handlers that will be added at the end of the script analysis + */ + addScriptHandlers?: ScriptHandler[] + /** + * Handlers that will be added at the end of the template analysis + */ + addTemplateHandlers?: TemplateHandler[] } /** @@ -50,7 +73,13 @@ export function parseSource(documentation: Documentation, source: string, opt: P // get slots and props from template if (parts && parts.template) { - parseTemplate(parts.template, documentation, templateHandlers, opt.filePath) + const addTemplateHandlers: TemplateHandler[] = opt.addTemplateHandlers || [] + parseTemplate( + parts.template, + documentation, + [...templateHandlers, ...addTemplateHandlers], + opt.filePath, + ) } const scriptSource = parts ? (parts.script ? parts.script.content : undefined) : source @@ -60,8 +89,8 @@ export function parseSource(documentation: Documentation, source: string, opt: P /\.tsx?$/i.test(path.extname(opt.filePath)) ? 'ts' : 'js' - - parseScript(scriptSource, documentation, handlers, opt) + const addScriptHandlers: ScriptHandler[] = opt.addScriptHandlers || [] + parseScript(scriptSource, documentation, [...scriptHandlers, ...addScriptHandlers], opt) } if (!documentation.get('displayName')) { From 1893362ad76f8105c9358508cd19ab05271db197 Mon Sep 17 00:00:00 2001 From: Bart Ledoux Date: Sun, 10 Feb 2019 17:04:37 -0600 Subject: [PATCH 2/3] feat: resolve.module implementation --- src/main.ts | 20 +++++++++++-------- src/parse.ts | 7 +++++-- src/script-handlers/extendsHandler.ts | 2 +- src/script-handlers/mixinsHandler.ts | 2 +- src/utils/makePathResolver.ts | 6 +++++- src/utils/resolvePathFrom.ts | 4 ++-- tests/components/mixin-resolved/button.vue | 1 + .../mixin-resolved/resolved-mixin.test.ts | 5 ++++- 8 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/main.ts b/src/main.ts index b75516d..6ffd570 100755 --- a/src/main.ts +++ b/src/main.ts @@ -1,14 +1,16 @@ import { ComponentDoc, Documentation } from './Documentation' -import { parseFile, ParseOptions, parseSource as parseSourceLocal } from './parse' +import { DocGenOptions, parseFile, ParseOptions, parseSource as parseSourceLocal } from './parse' -export { ComponentDoc, ParseOptions } +export { ComponentDoc, DocGenOptions } export function parse( filePath: string, - opts?: ParseOptions | { [alias: string]: string }, + opts?: DocGenOptions | { [alias: string]: string }, ): ComponentDoc { const doc = new Documentation() - const options: ParseOptions = isParserOptions(opts) ? opts : { filePath, aliases: opts } + const options: ParseOptions = isOptionsObject(opts) + ? { ...opts, filePath } + : { filePath, alias: opts } parseFile(doc, options) return doc.toObject() } @@ -16,14 +18,16 @@ export function parse( export function parseSource( source: string, filePath: string, - opts?: ParseOptions | { [alias: string]: string }, + opts?: DocGenOptions | { [alias: string]: string }, ): ComponentDoc { const doc = new Documentation() - const options: ParseOptions = isParserOptions(opts) ? opts : { filePath, aliases: opts } + const options: ParseOptions = isOptionsObject(opts) + ? { ...opts, filePath } + : { filePath, alias: opts } parseSourceLocal(doc, source, options) return doc.toObject() } -function isParserOptions(opts: any): opts is ParseOptions { - return !!opts && !!opts.aliases +function isOptionsObject(opts: any): opts is DocGenOptions { + return !!opts && !!opts.alias } diff --git a/src/parse.ts b/src/parse.ts index fff54ce..a80f89f 100755 --- a/src/parse.ts +++ b/src/parse.ts @@ -10,13 +10,16 @@ import cacher from './utils/cacher' const ERROR_EMPTY_DOCUMENT = 'The passed source is empty' -export interface ParseOptions { +export interface ParseOptions extends DocGenOptions { filePath: string /** * In what language is the component written * @default undefined - let the system decide */ lang?: 'ts' | 'js' +} + +export interface DocGenOptions { /** * Which exported variables should be looked at * @default undefined - means treat all dependencies @@ -25,7 +28,7 @@ export interface ParseOptions { /** * What alias should be replaced in requires and imports */ - aliases?: { [alias: string]: string } + alias?: { [alias: string]: string } /** * What directories should be searched when resolving modules */ diff --git a/src/script-handlers/extendsHandler.ts b/src/script-handlers/extendsHandler.ts index 40c451d..7497df9 100644 --- a/src/script-handlers/extendsHandler.ts +++ b/src/script-handlers/extendsHandler.ts @@ -31,7 +31,7 @@ export default function extendsHandler( const originalDirName = path.dirname(opt.filePath) - const pathResolver = makePathResolver(originalDirName, opt.aliases) + const pathResolver = makePathResolver(originalDirName, opt.alias) resolveImmediatelyExportedRequire(pathResolver, extendsFilePath) diff --git a/src/script-handlers/mixinsHandler.ts b/src/script-handlers/mixinsHandler.ts index ceb2462..83dc2e3 100644 --- a/src/script-handlers/mixinsHandler.ts +++ b/src/script-handlers/mixinsHandler.ts @@ -21,7 +21,7 @@ export default function mixinsHandler( ) { const originalDirName = path.dirname(opt.filePath) - const pathResolver = makePathResolver(originalDirName, opt.aliases) + const pathResolver = makePathResolver(originalDirName, opt.alias) // filter only mixins const mixinVariableNames = getMixinsVariableNames(componentDefinition) diff --git a/src/utils/makePathResolver.ts b/src/utils/makePathResolver.ts index 41592a6..328f44d 100644 --- a/src/utils/makePathResolver.ts +++ b/src/utils/makePathResolver.ts @@ -4,7 +4,11 @@ import resolvePathFrom from '../utils/resolvePathFrom' export default function makePathResolver( refDirName: string, aliases?: { [alias: string]: string }, + modules?: string[], ): (filePath: string, originalDirNameOverride?: string) => string { return (filePath: string, originalDirNameOverride?: string): string => - resolvePathFrom(resolveAliases(filePath, aliases || {}), originalDirNameOverride || refDirName) + resolvePathFrom(resolveAliases(filePath, aliases || {}), [ + originalDirNameOverride || refDirName, + ...(modules || []), + ]) } diff --git a/src/utils/resolvePathFrom.ts b/src/utils/resolvePathFrom.ts index 8470794..4adcab8 100644 --- a/src/utils/resolvePathFrom.ts +++ b/src/utils/resolvePathFrom.ts @@ -1,12 +1,12 @@ const SUFFIXES = ['', '.js', '.ts', '.vue', '/index.js', '/index.ts'] -export default function resolvePathFrom(path: string, from: string): string { +export default function resolvePathFrom(path: string, from: string[]): string { let finalPath = '' SUFFIXES.forEach(s => { if (!finalPath.length) { try { finalPath = require.resolve(`${path}${s}`, { - paths: [from], + paths: from, }) } catch (e) { // eat the error diff --git a/tests/components/mixin-resolved/button.vue b/tests/components/mixin-resolved/button.vue index 3955931..cb2cab8 100644 --- a/tests/components/mixin-resolved/button.vue +++ b/tests/components/mixin-resolved/button.vue @@ -8,6 +8,7 @@