-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ function Foo() { | |
return <div /> | ||
} | ||
|
||
Baz.Foo = () => <div /> | ||
|
||
/** | ||
* Description! | ||
* | ||
|
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
122 changes: 122 additions & 0 deletions
122
packages/gatsby-transformer-react-docgen/src/displayNameHandler.js
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,122 @@ | ||
/* @flow */ | ||
/** Forked from https://github.com/nerdlabs/react-docgen-displayname-handler/blob/master/source/index.js */ | ||
|
||
import path from "path" | ||
import recast from "recast" | ||
import { utils } from "react-docgen" | ||
const { getMemberValuePath, getNameOrValue } = utils | ||
|
||
const { | ||
types: { namedTypes: types }, | ||
} = recast | ||
|
||
const DEFAULT_NAME = `UnknownComponent` | ||
|
||
type Documentation = Map<string, string> | ||
type NodePath = any | ||
|
||
function getNameFromPath(path: NodePath): ?string { | ||
var node = path.node | ||
switch (node.type) { | ||
case types.Identifier.name: | ||
case types.Literal.name: | ||
return getNameOrValue(path) | ||
case types.MemberExpression.name: | ||
return utils | ||
.getMembers(path) | ||
.reduce( | ||
(name, { path, computed }) => | ||
computed && getNameFromPath(path) | ||
? name | ||
: `${name}.${getNameFromPath(path) || ``}`, | ||
getNameFromPath(path.get(`object`)) | ||
) | ||
default: | ||
return null | ||
} | ||
} | ||
|
||
function getStaticDisplayName(path: NodePath): ?string { | ||
let displayName = null | ||
const staticMember = getMemberValuePath(path, `displayName`) | ||
if (staticMember && types.Literal.check(staticMember.node)) { | ||
displayName = getNameOrValue(staticMember) | ||
} | ||
|
||
return displayName || null | ||
} | ||
|
||
function getNodeIdentifier(path: NodePath): ?string { | ||
let displayName = null | ||
if ( | ||
types.FunctionExpression.check(path.node) || | ||
types.FunctionDeclaration.check(path.node) || | ||
types.ClassExpression.check(path.node) || | ||
types.ClassDeclaration.check(path.node) | ||
) { | ||
displayName = getNameFromPath(path.get(`id`)) | ||
} | ||
|
||
return displayName || null | ||
} | ||
|
||
function getVariableIdentifier(path: NodePath): ?string { | ||
let displayName = null | ||
let searchPath = path | ||
|
||
while (searchPath !== null) { | ||
if (types.VariableDeclarator.check(searchPath.node)) { | ||
displayName = getNameFromPath(searchPath.get(`id`)) | ||
break | ||
} | ||
if ( | ||
types.AssignmentExpression.check(searchPath.node) && | ||
!utils.isExportsOrModuleAssignment(searchPath) | ||
) { | ||
displayName = getNameFromPath(searchPath.get(`left`)) | ||
break | ||
} | ||
searchPath = searchPath.parentPath | ||
} | ||
|
||
return displayName || null | ||
} | ||
|
||
function getNameFromFilePath(filePath: string = ``): ?string { | ||
let displayName = null | ||
|
||
const filename = path.basename(filePath, path.extname(filePath)) | ||
if (filename === `index`) { | ||
const parts = path.dirname(filePath).split(path.sep) | ||
displayName = parts[parts.length - 1] | ||
} else { | ||
displayName = filename | ||
} | ||
|
||
return displayName | ||
.charAt(0) | ||
.toUpperCase() | ||
.concat(displayName.slice(1)) | ||
.replace(/-([a-z])/, (_, match) => match.toUpperCase()) | ||
} | ||
|
||
export default function createDisplayNameHandler( | ||
filePath: string | ||
): (docs: Documentation, path: NodePath) => void { | ||
return function displayNameHandler( | ||
documentation: Documentation, | ||
path: NodePath | ||
): void { | ||
let displayName: ?string = [ | ||
getStaticDisplayName, | ||
getNodeIdentifier, | ||
getVariableIdentifier, | ||
].reduce((name, getDisplayName) => name || getDisplayName(path), ``) | ||
|
||
if (!displayName) { | ||
displayName = getNameFromFilePath(filePath) | ||
} | ||
|
||
documentation.set(`displayName`, displayName || DEFAULT_NAME) | ||
} | ||
} |
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