-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docz-core): add support for parse sections via ast
- Loading branch information
1 parent
c517f0e
commit 6db0cd8
Showing
9 changed files
with
94 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,82 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import * as t from 'babel-types' | ||
import { NodePath } from 'babel-traverse' | ||
import { File } from 'babel-types' | ||
import { parse } from 'babylon' | ||
import generate from 'babel-generator' | ||
import get from 'lodash.get' | ||
|
||
import * as paths from './config/paths' | ||
import { traverseAndAssign } from './utils/traverse' | ||
import { traverseAndAssign, traverseAndAssignEach } from './utils/traverse' | ||
import { isArrEqual } from './utils/helpers' | ||
import { format } from './utils/format' | ||
|
||
const convertToAst = (entry: string): File | null => { | ||
try { | ||
return parse(fs.readFileSync(entry, 'utf-8'), { | ||
plugins: ['jsx'], | ||
sourceType: 'module', | ||
}) | ||
} catch (err) { | ||
console.log(err) | ||
return null | ||
} | ||
} | ||
|
||
const getNameFromDoc = traverseAndAssign<any, string>({ | ||
assign: p => p.node.arguments[0].value, | ||
when: p => p.isCallExpression() && p.node.callee.name === 'doc', | ||
}) | ||
const hasImport = (p: NodePath<any>): boolean => | ||
t.isImportDeclaration(p) && get(p, 'node.source.value') === 'docz' | ||
|
||
const hasImport = (filepath: NodePath<any>): boolean => | ||
filepath.isImportDeclaration() && | ||
filepath.node && | ||
filepath.node.source && | ||
filepath.node.source.value === 'docz' | ||
|
||
const hasDocFn = (filepath: NodePath<any>): boolean => | ||
filepath.node.specifiers && | ||
filepath.node.specifiers.some( | ||
const hasDocFn = (p: NodePath<any>): boolean => | ||
p.node.specifiers && | ||
p.node.specifiers.some( | ||
(node: NodePath<any>) => | ||
t.isImportSpecifier(node) && node.imported.name === 'doc' | ||
) | ||
|
||
const checkImport = traverseAndAssign<NodePath<t.Node>, boolean>({ | ||
assign: () => true, | ||
when: p => hasImport(p) && hasDocFn(p), | ||
assign: () => true, | ||
}) | ||
|
||
export interface EntryConstructor { | ||
file: string | ||
src: string | ||
} | ||
const getNameFromDoc = traverseAndAssign<any, string>({ | ||
when: p => p.isCallExpression() && get(p, 'node.callee.name') === 'doc', | ||
assign: p => get(p, 'node.arguments[0].value'), | ||
}) | ||
|
||
const parseSections = traverseAndAssignEach<NodePath<t.Node>, string[]>({ | ||
when: 'MemberExpression', | ||
assign: p => { | ||
const name = get(p, 'node.property.name') | ||
const args = get(p, 'parentPath.node.arguments') | ||
|
||
if (name === 'section' && args && args.length > 0) { | ||
for (const arg of args) { | ||
if (arg.type !== 'StringLiteral') { | ||
const { code } = generate(arg.body, {}) | ||
const formatted: any = format(code) | ||
|
||
return formatted.slice(1, Infinity) | ||
} | ||
} | ||
} | ||
}, | ||
}) | ||
|
||
export class Entry { | ||
public static parseName(file: string): string | undefined | null { | ||
const ast = convertToAst(file) | ||
return ast && getNameFromDoc(ast) | ||
readonly [key: string]: any | ||
|
||
public static check(file: string): boolean | null { | ||
return checkImport(file) | ||
} | ||
|
||
public static parseName(file: string): string | null { | ||
return getNameFromDoc(file) | ||
} | ||
|
||
public static check(entry: string): boolean | undefined | null { | ||
const ast = convertToAst(entry) | ||
return ast && checkImport(ast) | ||
public static parseSections(file: string): string[] | null { | ||
const sections = parseSections(file) | ||
return sections && sections.reverse() | ||
} | ||
|
||
public name: string | undefined | ||
public filepath: string | ||
public name: string | null | ||
public sections: string[] | null | ||
|
||
constructor({ src, file }: EntryConstructor) { | ||
const ast = convertToAst(file) | ||
const name = ast ? getNameFromDoc(ast) : '' | ||
const filepath = path.relative(paths.root, file) | ||
constructor(file: string) { | ||
this.filepath = path.relative(paths.root, file) | ||
this.name = Entry.parseName(file) | ||
this.sections = Entry.parseSections(file) | ||
} | ||
|
||
this.name = name | ||
this.filepath = filepath | ||
public diff(entry: any): boolean { | ||
return ( | ||
this.name !== entry.name || !isArrEqual(this.sections, entry.sections) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
declare module 'art-template' | ||
declare module 'babel-file-loader' | ||
declare module 'babel-generator' | ||
declare module 'babel-collect-imports' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters