-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from pocka/fix/valid-code-generation
fix: Correct code injection
- Loading branch information
Showing
9 changed files
with
156 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
const jscodeshift = require('jscodeshift') | ||
|
||
/** | ||
* Inject docgen results into components. | ||
* @param content {string} - JS source code | ||
* @param infoList {object[]} - A list of docgen result | ||
* @return {string} - source code with docgen info | ||
*/ | ||
const inject = (content, infoList) => { | ||
// In order to inject info object to Component, we need to assign default | ||
// exported expression to a variable. | ||
const [normalizedCode, defaultExportAltName] = reDeclareDefaultExport( | ||
content, | ||
infoList | ||
) | ||
|
||
const infoInjectors = infoList | ||
.map(info => { | ||
const name = | ||
!info.exportName || info.exportName === 'default' | ||
? defaultExportAltName | ||
: info.exportName | ||
|
||
return `${name}.__docgenInfo = ${JSON.stringify(info)}` | ||
}) | ||
.join('\n') | ||
|
||
return normalizedCode + '\n' + infoInjectors | ||
} | ||
|
||
module.exports = inject | ||
|
||
/** | ||
* Replace default export with variable declaration and default export. | ||
* | ||
* ```js | ||
* export default { props: ['foo'] } | ||
* | ||
* // to ... | ||
* | ||
* const __vuedocgen_export_0 = { props: ['foo'] } | ||
* export default __vuedocgen_export_0 | ||
* ``` | ||
* | ||
* @param content {string} - JS source code | ||
* @param infoList {object[]} - A list of docgen result | ||
* @return {[string, string]} - Modified source code and variable name to inject info | ||
*/ | ||
const reDeclareDefaultExport = (content, infoList) => { | ||
if ( | ||
!infoList.some(info => !info.exportName || info.exportName === 'default') | ||
) { | ||
return [content, null] | ||
} | ||
|
||
const ast = jscodeshift(content) | ||
|
||
const defaultExportAltName = generateDefaultExportAltName(ast) | ||
|
||
ast.find(jscodeshift.ExportDefaultDeclaration).forEach(path => { | ||
const info = infoList.find( | ||
info => info.exportName === 'default' || !info.exportName | ||
) | ||
|
||
if (!info) { | ||
return | ||
} | ||
|
||
jscodeshift(path).replaceWith( | ||
jscodeshift.variableDeclaration('const', [ | ||
jscodeshift.variableDeclarator( | ||
jscodeshift.identifier(defaultExportAltName), | ||
path.value.declaration | ||
) | ||
]) | ||
) | ||
|
||
jscodeshift(path).insertAfter( | ||
jscodeshift.exportDefaultDeclaration( | ||
jscodeshift.identifier(defaultExportAltName) | ||
) | ||
) | ||
}) | ||
|
||
return [ast.toSource(), defaultExportAltName] | ||
} | ||
|
||
/** | ||
* Generate a name for default exported expression. | ||
* @param ast {object} | ||
* @return {string} - identifier | ||
*/ | ||
const generateDefaultExportAltName = (ast, i = 0) => { | ||
const name = `__vuedocgen_export_${i}` | ||
|
||
const idents = ast.find(jscodeshift.Identifier, node => node.name === name) | ||
|
||
return idents.length === 0 ? name : generateDefaultExportAltName(ast, i + 1) | ||
} |
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,14 @@ | ||
import Vue from 'vue' | ||
|
||
export default Vue.extend({ | ||
props: { | ||
/** | ||
* Foo the number. | ||
*/ | ||
foo: { | ||
type: Number, | ||
default: -1 | ||
} | ||
}, | ||
template: '<span>{{foo}}</span>' | ||
}) |
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