Skip to content
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

fix(gatsby-transformer-documentationjs): handle free floating jsdocs #13058

Merged
merged 2 commits into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Object {
Object {
"name": "ObjectType",
"type": "NameExpression",
"typeDef___NODE": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"}]",
"typeDef___NODE": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"}] line 12",
},
Object {
"name": "Object",
Expand All @@ -21,19 +21,19 @@ Object {
exports[`transformer-react-doc-gen: onCreateNode Complex example should handle typedefs should handle type applications 1`] = `
Object {
"children": Array [
"documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"},{\\"fieldName\\":\\"properties\\",\\"fieldIndex\\":0}]--DocumentationJSComponentDescription--comment.description",
"documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"},{\\"fieldName\\":\\"properties\\",\\"fieldIndex\\":0}] line 3--DocumentationJSComponentDescription--comment.description",
],
"commentNumber": null,
"description___NODE": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"},{\\"fieldName\\":\\"properties\\",\\"fieldIndex\\":0}]--DocumentationJSComponentDescription--comment.description",
"id": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"},{\\"fieldName\\":\\"properties\\",\\"fieldIndex\\":0}]",
"description___NODE": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"},{\\"fieldName\\":\\"properties\\",\\"fieldIndex\\":0}] line 3--DocumentationJSComponentDescription--comment.description",
"id": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"},{\\"fieldName\\":\\"properties\\",\\"fieldIndex\\":0}] line 3",
"internal": Object {
"contentDigest": "content-digest",
"type": "DocumentationJs",
},
"level": 1,
"name": "ready",
"optional": false,
"parent": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"}]",
"parent": "documentationJS node_1 path #[{\\"name\\":\\"ObjectType\\",\\"kind\\":\\"typedef\\"}] line 12",
"type": Object {
"applications": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,3 @@
exports.apple = paramName => {
console.log(`hi`)
}

/**
* Description of callback type
* @callback CallbackType
* @param {string} message A message to split
* @returns {string[]} Splitted message
*/

/**
* More complex example
*/
exports.complex = {
/**
* Description of object type
* @typedef {Object} ObjectType
* @property {Promise<any>} ready Returns promise that resolves when instance is ready to use
* @property {Object} nested This is nested object
* @property {string} nested.foo This is field in nested object
* @property {number} [nested.optional] This is optional field in nested object
* @property {CallbackType} nested.callback This is function in nested object
*/

/**
* Description of function
* @type {(ObjectType|Object)}
*/
object: {
ready: new Promise(),
nested: {
foo: `bar`,
callback: message => {
return message.split(`,`)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Description of callback type
* @callback CallbackType
* @param {string} message A message to split
* @returns {string[]} Splitted message
*/

/**
* More complex example
*/
exports.complex = {
/**
* Description of object type
* @typedef {Object} ObjectType
* @property {Promise<any>} ready Returns promise that resolves when instance is ready to use
* @property {Object} nested This is nested object
* @property {string} nested.foo This is field in nested object
* @property {number} [nested.optional] This is optional field in nested object
* @property {CallbackType} nested.callback This is function in nested object
*/

/**
* Description of function
* @type {(ObjectType|Object)}
*/
object: {
ready: new Promise(),
nested: {
foo: `bar`,
callback: message => {
return message.split(`,`)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* This is a free-floating comment
*/

/**
* This is a function
*/
const fn = () => {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ describe(`transformer-react-doc-gen: onCreateNode`, () => {
const createNodeId = jest.fn(id => id)
const createContentDigest = jest.fn().mockReturnValue(`content-digest`)

const node = {
id: `node_1`,
children: [],
absolutePath: path.join(__dirname, `fixtures`, `code.js`),
internal: {
mediaType: `application/javascript`,
type: `File`,
},
const getFileNode = fixture => {
return {
id: `node_1`,
children: [],
absolutePath: path.join(__dirname, `fixtures`, fixture),
internal: {
mediaType: `application/javascript`,
type: `File`,
},
}
}

const actions = {
Expand Down Expand Up @@ -44,11 +46,11 @@ describe(`transformer-react-doc-gen: onCreateNode`, () => {
)
}

beforeAll(async () => {
await run(node)
})

describe(`Simple example`, () => {
beforeAll(async () => {
await run(getFileNode(`code.js`))
})

it(`creates doc json apple node`, () => {
const appleNode = createdNodes.find(node => node.name === `apple`)
expect(appleNode).toBeDefined()
Expand Down Expand Up @@ -113,9 +115,19 @@ describe(`transformer-react-doc-gen: onCreateNode`, () => {
})
)
})

it(`doesn't create multiple nodes with same id`, () => {
Object.values(groupBy(createdNodes, `id`)).forEach(nodes =>
expect(nodes.length).toBe(1)
)
})
})

describe(`Complex example`, () => {
beforeAll(async () => {
await run(getFileNode(`complex-example.js`))
})

let callbackNode, typedefNode

it(`should create top-level node for callback`, () => {
Expand Down Expand Up @@ -232,6 +244,24 @@ describe(`transformer-react-doc-gen: onCreateNode`, () => {
expect(typeElement.typeDef___NODE).toBe(typedefNode.id)
})
})

it(`doesn't create multiple nodes with same id`, () => {
Object.values(groupBy(createdNodes, `id`)).forEach(nodes =>
expect(nodes.length).toBe(1)
)
})
})

describe(`Free floating comments`, () => {
beforeAll(async () => {
await run(getFileNode(`free-floating.js`))
})

it(`doesn't create multiple nodes with same id`, () => {
Object.values(groupBy(createdNodes, `id`)).forEach(nodes =>
expect(nodes.length).toBe(1)
)
})
})

describe(`Sanity checks`, () => {
Expand Down Expand Up @@ -262,7 +292,7 @@ describe(`transformer-react-doc-gen: onCreateNode`, () => {
await run({ internal: { mediaType: `application/javascript` } })
expect(createdNodes.length).toBe(0)

await run(node)
await run(getFileNode(`code.js`))
expect(createdNodes.length).toBeGreaterThan(0)
})
})
Expand Down
12 changes: 10 additions & 2 deletions packages/gatsby-transformer-documentationjs/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ const stringifyMarkdownAST = (node = ``) => {
}
}

const docId = (parentId, docsJson) =>
`documentationJS ${parentId} path #${JSON.stringify(docsJson.path)}`
const docId = (parentId, docsJson) => {
const lineNumber = docsJson.loc
? docsJson.loc.start.line
: docsJson.lineNumber

return `documentationJS ${parentId} path #${JSON.stringify(
docsJson.path
)} line ${lineNumber}`
}

const descriptionId = (parentId, name) =>
`${parentId}--DocumentationJSComponentDescription--${name}`

Expand Down