jonschlinkert / parse-comments

Parse JavaScript code comments. Works with block and line comments, and should work with CSS, LESS, SASS, or any language with the same comment formats.
https://github.com/jonschlinkert
MIT License
66 stars 23 forks source link

tag.type problems #17

Open tunnckoCore opened 4 years ago

tunnckoCore commented 4 years ago

First, it would be cool if in the tag object there is also the whole non-parsed string of the types.

Second, the example case

/**
 * @param {Array<string>|string} options.include - glob patterns
 * @param {string|boolean} options.exclude - ignore patterns
 * @param {Function} options.hook - a hook function passed with [Context](#context)
 * @param {boolean} options.always - a boolean that makes `options.hook` to always be called
 * @param {Function} options.glob - a globbing library like [glob][], [fast-glob][], [tiny-glob][], defaults to `fast-glob`
 * @param {object} options.globOptions
 * @param {string[]} options.cacheLocation
 */

I always felt there's something wrong. Finally dug down and found it.

{ type: 'NameExpression', name: 'string' }
{
  type: 'UnionType',
  elements: [
    { type: 'NameExpression', name: 'string' },
    { type: 'NameExpression', name: 'boolean' }
  ]
}
{ type: 'NameExpression', name: 'Function' }
{ type: 'NameExpression', name: 'boolean' }
{ type: 'NameExpression', name: 'Function' }
{ type: 'NameExpression', name: 'object' }
{
  type: 'TypeApplication',
  expression: { type: 'NameExpression', name: 'Array' },
  applications: [ { type: 'NameExpression', name: 'string' } ]
}
{ type: 'NameExpression', name: 'Promise' }

as you can see, the first one is just NameExpression, which means it only gets the last of the type definition. If you reverse it from {Array<string>|string} to {string|Array<string>} the first result will be TypeApplication.

{
  type: 'TypeApplication',
  expression: { type: 'NameExpression', name: 'Array' },
  applications: [ { type: 'NameExpression', name: 'string' } ]
}

In short, the problem only when there is a TypeApplication type in a UnionType, because this works {string|boolean} okay.

I believe that the expected result should be

{
  type: 'UnionType',
  elements: [
    { type: 'NameExpression', name: 'string' },
    {
      type: 'TypeApplication',
      expression: { type: 'NameExpression', name: 'Array' },
      applications: [ { type: 'NameExpression', name: 'string' } ]
    }
  ]
}

@jonschlinkert seems like I continue to stack up more and more issues :laughing: :hear_no_evil: :heart: