-
-
Notifications
You must be signed in to change notification settings - Fork 297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
infer displayName when not defined #59
Closed
+242
−51
Closed
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
b14d938
component name fallback implementation
NogsMPLS f1487ab
more defensive conditional
NogsMPLS 45c84f2
infer stateless component displayName
NogsMPLS 7880236
removing duplicate test
NogsMPLS cd711b2
use resolveName() for functional components
NogsMPLS 32636c5
turn resolveName into separate module
NogsMPLS caa435a
adding failing test
NogsMPLS 09d541c
Immediately Exported ES6 Modules passing tests
NogsMPLS 59e4f9c
fix extract displayname test
NogsMPLS 437a123
Merge remote-tracking branch 'upstream/master' into inferDisplayName
NogsMPLS 05e4e67
infer displayName on immediately exported commonJS exports
NogsMPLS 588daa5
add test, remove unnecessary import
NogsMPLS bab57a6
name tests correctly
NogsMPLS 60dfe63
Merge remote-tracking branch 'upstream/master' into inferDisplayName
NogsMPLS 5e684de
fix displayNameHandler tests
NogsMPLS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,22 +13,65 @@ | |
import type Documentation from '../Documentation'; | ||
|
||
import getMemberValuePath from '../utils/getMemberValuePath'; | ||
import resolveName from '../utils/resolveName'; | ||
import recast from 'recast'; | ||
import resolveToValue from '../utils/resolveToValue'; | ||
import isExportsOrModuleAssignment from '../utils/isExportsOrModuleAssignment'; | ||
import resolveExportDeclaration from '../utils/resolveExportDeclaration' | ||
|
||
var {types: {namedTypes: types}} = recast; | ||
|
||
function getOrInferDisplayName(path) { | ||
var displayNamePath = getMemberValuePath(path, 'displayName'); | ||
|
||
if (displayNamePath) { | ||
displayNamePath = resolveToValue(displayNamePath); | ||
if (!displayNamePath || !types.Literal.check(displayNamePath.node)) { | ||
return; | ||
} | ||
return displayNamePath.node.value; | ||
} else if (!displayNamePath && path.node.id) { | ||
return path.node.id.name; | ||
} else if (!displayNamePath && resolveName(path)) { | ||
return resolveName(path); | ||
} | ||
} | ||
|
||
export default function displayNameHandler( | ||
documentation: Documentation, | ||
path: NodePath | ||
) { | ||
var displayNamePath = getMemberValuePath(path, 'displayName'); | ||
if (!displayNamePath) { | ||
return; | ||
} | ||
displayNamePath = resolveToValue(displayNamePath); | ||
if (!displayNamePath || !types.Literal.check(displayNamePath.node)) { | ||
return; | ||
var displayName; | ||
|
||
//If not immediately exported via ES6 or CommonJS exports or an ExpressionStatement | ||
if (!types.ExportNamedDeclaration.check(path.node) && !isExportsOrModuleAssignment(path) && !types.ExpressionStatement.check(path.node)) { | ||
displayName = getOrInferDisplayName(path); | ||
|
||
//ES6 Exports | ||
} else if (types.ExportNamedDeclaration.check(path.node)) { | ||
var declarationPath; | ||
var declaration = path.node.declaration; | ||
|
||
if ( | ||
declaration.type === types.ClassDeclaration.name || | ||
declaration.type === types.FunctionDeclaration.name || | ||
declaration.type === types.FunctionExpression.name | ||
) { | ||
declarationPath = resolveExportDeclaration(path)[0]; | ||
|
||
} else if (declaration.type === types.VariableDeclaration.name) { | ||
|
||
declarationPath = resolveExportDeclaration(path)[0].parentPath.parentPath.parentPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't really like this solution, but I'm not sure of how else to get this value while keeping 'NodePath' type intact. |
||
} | ||
|
||
displayName = getOrInferDisplayName(declarationPath); | ||
|
||
//CommonJS export.X | ||
} else if (isExportsOrModuleAssignment(path)) { | ||
displayName = resolveToValue(path).get('expression', 'left', 'property', 'name').value; | ||
|
||
} else if (types.ExpressionStatement.check(path.node) && path.node.expression.id) { | ||
displayName = path.node.expression.id.name; | ||
} | ||
documentation.set('displayName', displayNamePath.node.value); | ||
documentation.set('displayName', displayName); | ||
} |
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,39 @@ | ||
import recast from 'recast'; | ||
|
||
var {types: {namedTypes: types}} = recast; | ||
|
||
export default function resolveName(path) { | ||
if (types.VariableDeclaration.check(path.node)) { | ||
var declarations = path.get('declarations'); | ||
if (declarations.value.length && declarations.value.length !== 1) { | ||
throw new TypeError( | ||
'Got unsupported VariableDeclaration. VariableDeclaration must only ' + | ||
'have a single VariableDeclarator. Got ' + declarations.value.length + | ||
' declarations.' | ||
); | ||
} | ||
var value = declarations.get(0, 'id', 'name').value; | ||
return value; | ||
} | ||
|
||
if (types.FunctionDeclaration.check(path.node)) { | ||
return path.get('id', 'name').value; | ||
} | ||
|
||
if ( | ||
types.FunctionExpression.check(path.node) || | ||
types.ArrowFunctionExpression.check(path.node) | ||
) { | ||
if (!types.VariableDeclarator.check(path.parent.node)) { | ||
return; // eslint-disable-line consistent-return | ||
} | ||
|
||
return path.parent.get('id', 'name').value; | ||
} | ||
|
||
throw new TypeError( | ||
'Attempted to resolveName for an unsupported path. resolveName accepts a ' + | ||
'VariableDeclaration, FunctionDeclaration, or FunctionExpression. Got "' + | ||
path.node.type + '".' | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this should be "dispayNameHandler".