-
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.
* Finish docgen plugin * WIP * Switch to jest config, clean up paths * More inference fixes :| * Finish plugin * rm lock * better polymoprhic value detection
- Loading branch information
1 parent
a0370a9
commit 5f9232d
Showing
19 changed files
with
579 additions
and
197 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const path = require(`path`) | ||
const glob = require(`glob`) | ||
|
||
const pkgs = glob.sync(`./packages/*`).map(p => p.replace(/^\./, `<rootDir>`)) | ||
|
||
const distDirs = pkgs.map(p => path.join(p, `dist`)) | ||
|
||
module.exports = { | ||
notify: true, | ||
verbose: true, | ||
roots: pkgs, | ||
modulePathIgnorePatterns: distDirs, | ||
coveragePathIgnorePatterns: distDirs, | ||
testPathIgnorePatterns: [`/dist/`, `/node_modules/`, `__tests__/fixtures`], | ||
} |
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
70 changes: 70 additions & 0 deletions
70
packages/gatsby-transformer-react-docgen/__tests__/fixtures/classes.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,70 @@ | ||
/* eslint-disable */ | ||
import React from 'react' | ||
|
||
const Baz = () => <div /> | ||
|
||
const Buz = function() { | ||
return <div /> | ||
} | ||
|
||
function Foo() { | ||
return <div /> | ||
} | ||
|
||
class Bar extends React.Component { | ||
static propTypes = { | ||
/** | ||
* An object hash of field errors for the form. | ||
*/ | ||
objProp: React.PropTypes.object, | ||
|
||
reqProp: React.PropTypes.object.isRequired, | ||
|
||
/** | ||
* Callback **that** is called when a validation error occurs. | ||
*/ | ||
funcProp: React.PropTypes.func, | ||
|
||
stringProp: React.PropTypes.string, | ||
|
||
boolProp: React.PropTypes.bool, | ||
|
||
'aria-property': React.PropTypes.string, | ||
|
||
enumProp: React.PropTypes.oneOf([true, 'john', 5, null, Infinity]), | ||
|
||
otherProp: React.PropTypes.instanceOf(Message), | ||
|
||
shapeProp: React.PropTypes.shape({ | ||
setter: React.PropTypes.func, | ||
name: React.PropTypes.string, | ||
}), | ||
|
||
unionProp: React.PropTypes.oneOfType([ | ||
React.PropTypes.func, | ||
React.PropTypes.string, | ||
]), | ||
|
||
reqUnionProp: React.PropTypes.oneOfType([ | ||
React.PropTypes.func, | ||
React.PropTypes.string, | ||
]).isRequired, | ||
|
||
customProp(props, name, componentName) { | ||
return React.PropTypes.any.isRequired(props, name, componentName) | ||
}, | ||
|
||
customIdentifier: someValidator, | ||
|
||
customCallExpression: someValidator(), | ||
} | ||
render() { | ||
return <Foo /> | ||
} | ||
} | ||
|
||
const Qux = React.createClass({ | ||
render() { | ||
return <Foo /> | ||
}, | ||
}) |
5 changes: 5 additions & 0 deletions
5
packages/gatsby-transformer-react-docgen/__tests__/fixtures/unnamed.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,5 @@ | ||
export default class extends React.Component { | ||
render() { | ||
return <div /> | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
packages/gatsby-transformer-react-docgen/__tests__/on-node-create.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,97 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { groupBy } from 'lodash' | ||
import onNodeCreate from '../src/on-node-create' | ||
|
||
const readFile = file => | ||
new Promise((y, n) => { | ||
fs.readFile( | ||
path.join(__dirname, `fixtures`, file), | ||
`utf8`, | ||
(err, content) => (err ? n(err) : y(content)) | ||
) | ||
}) | ||
|
||
describe(`transformer-react-doc-gen: onNodeCreate`, () => { | ||
let loadNodeContent, boundActionCreators, node, createdNodes, updatedNodes | ||
let run = (node, opts = {}) => | ||
onNodeCreate( | ||
{ | ||
node, | ||
loadNodeContent, | ||
boundActionCreators, | ||
}, | ||
opts | ||
) | ||
|
||
beforeEach(() => { | ||
createdNodes = [] | ||
updatedNodes = [] | ||
node = { | ||
mediaType: `application/javascript`, | ||
children: [], | ||
id: `node_1`, | ||
__fixture: `classes.js`, | ||
} | ||
loadNodeContent = jest.fn(node => readFile(node.__fixture)) | ||
boundActionCreators = { | ||
createNode: jest.fn(n => createdNodes.push(n)), | ||
updateNode: jest.fn(n => updatedNodes.push(n)), | ||
} | ||
}) | ||
|
||
it(`should only process javascript nodes`, () => { | ||
loadNodeContent = jest.fn(() => new Promise(() => {})) | ||
|
||
expect(run({ mediaType: `text/x-foo` })).toBeNull() | ||
expect(run({ mediaType: `application/javascript` })).toBeDefined() | ||
|
||
expect(loadNodeContent.mock.calls).toHaveLength(1) | ||
}) | ||
|
||
it(`should extract all components in a file`, async () => { | ||
await run(node) | ||
|
||
let types = groupBy(createdNodes, `type`) | ||
expect(types.ComponentMetadata).toHaveLength(5) | ||
}) | ||
|
||
it(`should give all components a name`, async () => { | ||
await run(node) | ||
|
||
let types = groupBy(createdNodes, `type`) | ||
expect(types.ComponentMetadata.every(c => c.displayName)).toBe(true) | ||
}) | ||
|
||
it(`should infer a name`, async () => { | ||
node.__fixture = `unnamed.js` | ||
node.absolutePath = path.join(__dirname, `UnnamedExport`) | ||
await run(node) | ||
|
||
expect(createdNodes[0].displayName).toEqual(`UnnamedExport`) | ||
}) | ||
|
||
it(`should extract all propTypes`, async () => { | ||
await run(node) | ||
|
||
let types = groupBy(createdNodes, `type`) | ||
expect(types.ComponentProp).toHaveLength(14) | ||
}) | ||
|
||
it(`should extract create description nodes with markdown types`, async () => { | ||
await run(node) | ||
let types = groupBy(createdNodes, `type`) | ||
expect( | ||
types.ComponentDescription.every(d => d.mediaType === `text/x-markdown`) | ||
).toBe(true) | ||
}) | ||
|
||
it(`should allow specifying handlers`, async () => { | ||
let handler = jest.fn() | ||
await run(node, { | ||
handlers: [handler], | ||
}) | ||
|
||
expect(!!handler.mock.calls.length).toBe(true) | ||
}) | ||
}) |
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
97 changes: 97 additions & 0 deletions
97
packages/gatsby-transformer-react-docgen/src/extend-node-type.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,97 @@ | ||
const { | ||
GraphQLBoolean, | ||
GraphQLObjectType, | ||
GraphQLList, | ||
GraphQLString, | ||
GraphQLNonNull, | ||
} = require(`graphql`) | ||
const GraphQLJSON = require(`graphql-type-json`) | ||
const { stripIndent, oneLine } = require(`common-tags`) | ||
|
||
const PropDefaultValue = new GraphQLObjectType({ | ||
name: `PropDefaultValue`, | ||
fields: () => ({ | ||
value: { type: GraphQLString }, | ||
computed: { type: GraphQLBoolean }, | ||
}), | ||
}) | ||
|
||
const PropTypeValue = new GraphQLObjectType({ | ||
name: `PropTypeValue`, | ||
fields: () => ({ | ||
name: { type: new GraphQLNonNull(GraphQLString) }, | ||
value: { type: GraphQLJSON }, | ||
raw: { type: GraphQLString }, | ||
}), | ||
}) | ||
|
||
const Method = new GraphQLObjectType({ | ||
name: `ComponentMethod`, | ||
fields: () => ({ | ||
name: { type: new GraphQLNonNull(GraphQLString) }, | ||
docblock: { type: GraphQLString }, | ||
modifiers: { | ||
type: new GraphQLList(GraphQLString), | ||
description: oneLine` | ||
Modifiers describing the kind and sort of method e.g. "static", | ||
"generator", or "async". | ||
`, | ||
}, | ||
params: { | ||
type: new GraphQLList( | ||
new GraphQLObjectType({ | ||
name: `ComponentMethodParams`, | ||
fields: () => ({ | ||
name: { type: GraphQLString }, | ||
type: { type: GraphQLJSON }, | ||
}), | ||
}) | ||
), | ||
}, | ||
returns: { type: new GraphQLList(GraphQLJSON) }, | ||
}), | ||
}) | ||
|
||
function extendComponents() { | ||
return { | ||
composes: { | ||
type: new GraphQLList(new GraphQLNonNull(GraphQLString)), | ||
description: stripIndent` | ||
${oneLine` | ||
A list of additional modules "spread" into this component's | ||
propTypes such as:`} | ||
propTypes = { | ||
name: PropTypes.string, | ||
...AnotherComponent.propTypes, | ||
} | ||
`, | ||
}, | ||
methods: { | ||
type: new GraphQLList(Method), | ||
description: `Component methods`, | ||
}, | ||
} | ||
} | ||
|
||
function extendProp() { | ||
return { | ||
defaultValue: { type: PropDefaultValue }, | ||
required: { | ||
type: new GraphQLNonNull(GraphQLBoolean), | ||
description: oneLine` | ||
Describes whether or not the propType is required, i.e. not \`null\` | ||
`, | ||
}, | ||
type: { | ||
type: new GraphQLNonNull(PropTypeValue), | ||
resolve: s => s._propType, | ||
}, | ||
} | ||
} | ||
|
||
export default ({ type }) => { | ||
if (type.name === `ComponentProp`) return extendProp() | ||
if (type.name === `ComponentMetadata`) return extendComponents() | ||
return {} | ||
} |
Oops, something went wrong.