forked from motiz88/react-docgen
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial support for importing prop types
- Loading branch information
1 parent
f784504
commit 00b3d35
Showing
16 changed files
with
394 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
import React, { Component } from 'react'; | ||
|
||
interface Props { | ||
export interface Props { | ||
foo: string | ||
} | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import React, { Component } from 'react'; | ||
import { Props as ImportedProps } from './component_27'; | ||
|
||
export default interface ExtendedProps extends ImportedProps { | ||
bar: number | ||
} | ||
|
||
/** | ||
* This is a typescript component with imported prop types | ||
*/ | ||
export function ImportedComponent(props: ImportedProps) { | ||
return <h1>Hello world</h1>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
*/ | ||
|
||
import React, { Component } from 'react'; | ||
import ExtendedProps from './component_28'; | ||
|
||
/** | ||
* This is a typescript component with imported prop types | ||
*/ | ||
export function ImportedExtendedComponent(props: ExtendedProps) { | ||
return <h1>Hello world</h1>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Button from './component_6'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export function CustomButton({color, ...otherProps}) { | ||
return <Button {...otherProps} style={{color}} />; | ||
} | ||
|
||
CustomButton.propTypes = { | ||
...Button.propTypes, | ||
color: PropTypes.string | ||
}; | ||
|
||
export const sharedProps = Button.propTypes; |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {CustomButton, sharedProps} from './component_30'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export function SuperCustomButton({color, ...otherProps}) { | ||
return <CustomButton {...otherProps} style={{color}} />; | ||
} | ||
|
||
SuperCustomButton.propTypes = sharedProps; | ||
export {sharedProps}; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {SuperCustomButton, sharedProps} from './component_31'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export function SuperDuperCustomButton({color, ...otherProps}) { | ||
return <SuperCustomButton {...otherProps} style={{color}} />; | ||
} | ||
|
||
SuperDuperCustomButton.propTypes = sharedProps; |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import types from 'ast-types'; | ||
import { traverseShallow } from './traverse'; | ||
import resolve from 'resolve'; | ||
import { dirname } from 'path'; | ||
import buildParser, { type Options } from '../babelParser'; | ||
import fs from 'fs'; | ||
|
||
const { namedTypes: t, NodePath } = types; | ||
|
||
export default function resolveImportedValue(path: NodePath, name: string) { | ||
t.ImportDeclaration.assert(path.node); | ||
|
||
// Bail if no filename was provided for the current source file. | ||
// Also never traverse into react itself. | ||
const source = path.node.source.value; | ||
const options = getOptions(path); | ||
if (!options || !options.filename || source === 'react') { | ||
return null; | ||
} | ||
|
||
// Resolve the imported module using the Node resolver | ||
const basedir = dirname(options.filename); | ||
let resolvedSource; | ||
|
||
try { | ||
resolvedSource = resolve.sync(source, { | ||
basedir, | ||
extensions: ['.js', '.jsx', '.ts', '.tsx'], | ||
}); | ||
} catch (err) { | ||
return null; | ||
} | ||
|
||
// Read and parse the code | ||
// TODO: cache and reuse | ||
const code = fs.readFileSync(resolvedSource, 'utf8'); | ||
const parseOptions: Options = { | ||
...options, | ||
parserOptions: {}, | ||
filename: resolvedSource, | ||
}; | ||
|
||
const parser = buildParser(parseOptions); | ||
const ast = parser.parse(code); | ||
return findExportedValue(ast.program, name); | ||
} | ||
|
||
// Find the root Program node, which we attached our options too in babelParser.js | ||
function getOptions(path: NodePath): Options { | ||
while (!t.Program.check(path.node)) { | ||
path = path.parentPath; | ||
} | ||
|
||
return path.node.options || {}; | ||
} | ||
|
||
// Traverses the program looking for an export that matches the requested name | ||
function findExportedValue(ast, name) { | ||
let resultPath: ?NodePath = null; | ||
|
||
traverseShallow(ast, { | ||
visitExportNamedDeclaration(path: NodePath) { | ||
const { declaration, specifiers } = path.node; | ||
if (declaration && declaration.id && declaration.id.name === name) { | ||
resultPath = path.get('declaration'); | ||
} else if (declaration && declaration.declarations) { | ||
path.get('declaration', 'declarations').each((declPath: NodePath) => { | ||
const decl = declPath.node; | ||
// TODO: ArrayPattern and ObjectPattern | ||
if ( | ||
t.Identifier.check(decl.id) && | ||
decl.id.name === name && | ||
decl.init | ||
) { | ||
resultPath = declPath.get('init'); | ||
} | ||
}); | ||
} else if (specifiers) { | ||
path.get('specifiers').each((specifierPath: NodePath) => { | ||
if (specifierPath.node.exported.name === name) { | ||
resultPath = specifierPath.get('local'); | ||
} | ||
}); | ||
} | ||
|
||
return false; | ||
}, | ||
visitExportDefaultDeclaration(path: NodePath) { | ||
if (name === 'default') { | ||
resultPath = path.get('declaration'); | ||
} | ||
|
||
return false; | ||
}, | ||
// TODO: visitExportAllDeclaration | ||
}); | ||
|
||
return resultPath; | ||
} |
Oops, something went wrong.