Skip to content

Commit

Permalink
Dmd now prints a warning if a malformed input data edge case is detec…
Browse files Browse the repository at this point in the history
…ted. #89

Fixes a 'maximum call stack size exceeded' error.
  • Loading branch information
75lb committed Aug 27, 2024
1 parent d44a3b7 commit 82941c3
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 12 deletions.
22 changes: 22 additions & 0 deletions bin/generate-partial-cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const FileSet = require('file-set')
const fs = require('fs')
const path = require('path')

const fileSet = new FileSet()

async function start () {
await fileSet.add('./partials/**/*.hbs')
const map = new Map()
for (const file of fileSet.files) {
map.set(
path.basename(file, '.hbs'),
fs.readFileSync(file, 'utf8') || ''
)
}
console.log('module.exports = ' + JSON.stringify(Array.from(map), null, ' '))
}
start()

/*
node bin/generate-partial-cache.js > partials/partial-cache.js
*/
16 changes: 13 additions & 3 deletions helpers/ddata.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const where = require('test-value').where
const flatten = require('reduce-flatten')
const state = require('../lib/state')

let malformedDataWarningIssued = false

/**
* ddata is a collection of handlebars helpers for working with the documentation data output by [jsdoc-parse](https://github.com/75lb/jsdoc-parse).
* @module
Expand Down Expand Up @@ -447,7 +449,6 @@ function _orphans (options) {
*/
function _identifiers (options) {
const query = {}

for (const prop in options.hash) {
if (/^-/.test(prop)) {
query[prop.replace(/^-/, '!')] = options.hash[prop]
Expand All @@ -472,6 +473,15 @@ return the identifiers which are a `memberof` this one. Exclude externals withou
*/
function _children (options) {
if (!this.id) return []
if (this.id === this.memberof) {
if (!malformedDataWarningIssued) {
console.warn('Jsdoc data looks malformed. Typically, this can be fixed by ensuring the sourcecode file has a `@module tag`. ')
console.warn('Please see the "Document an ES2015 module" section in the wiki')
console.warn('https://github.com/jsdoc2md/jsdoc-to-markdown/wiki')
malformedDataWarningIssued = true
}
return []
}
const min = options.hash.min
delete options.hash.min
options.hash.memberof = this.id
Expand Down Expand Up @@ -501,10 +511,10 @@ function descendants (options) {
const output = []
function iterate (childrenList) {
if (childrenList.length) {
childrenList.forEach(function (child) {
for (const child of childrenList) {
output.push(child)
iterate(_children.call(child, options))
})
}
}
}
iterate(_children.call(this, options))
Expand Down
6 changes: 4 additions & 2 deletions helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ function tableRow () {
const options = args.pop()
const cols = args
let output = ''
let data

if (options.data) {
var data = handlebars.createFrame(options.data)
data = handlebars.createFrame(options.data)
cols.forEach(function (col, index) {
const colNumber = index + 1
data['col' + colNumber] = containsData(rows, col)
Expand Down Expand Up @@ -305,12 +306,13 @@ function examples (options) {
if (this.examples) {
return this.examples.reduce(function (prev, example) {
const lines = example.split(/\r\n|\r|\n/)
let exampleLangSubtag

/* Process @lang */
const exampleLangOptions = ddata.option('example-lang', options)
let matches = lines[0].match(/@lang\s+(\w+)\s*/)
if (matches) {
var exampleLangSubtag = matches[1]
exampleLangSubtag = matches[1]
lines[0] = lines[0].replace(matches[0], '')
if (lines[0].length === 0) {
lines.splice(0, 1)
Expand Down
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const DmdOptions = require('./lib/dmd-options')
const dmdVersion = require('./package').version
const FileSet = require('file-set')
const os = require('os')
const partialCache = require('./partials/partial-cache.js')

/**
* Transforms doclet data into markdown documentation.
Expand Down Expand Up @@ -77,8 +78,10 @@ async function generate (templateData, options) {
state.templateData = templateData
state.options = options

/* register all dmd partials. */
await registerPartials(path.resolve(__dirname, 'partials', '**', '*.hbs'))
/* register all internal dmd partials. */
for (const [name, content] of partialCache) {
handlebars.registerPartial(name, content)
}

/* if plugins were specified, register the helpers/partials from them too */
if (options.plugin) {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"common-sequence": "^2.0.2",
"file-set": "^6.0.1",
"handlebars": "^4.7.8",
"marked": "^14.0.0",
"marked": "^14.1.0",
"object-get": "^2.1.1",
"reduce-flatten": "^3.0.1",
"reduce-unique": "^2.0.1",
Expand Down
Loading

0 comments on commit 82941c3

Please sign in to comment.