esdoc / esdoc-plugins

MIT License
139 stars 74 forks source link

Class decorator with object argument - `SyntaxError: Invalid regular expression` #36

Open loopmode opened 7 years ago

loopmode commented 7 years ago

Using

  "dependencies": {
    "esdoc": "^1.0.3",
    "esdoc-ecmascript-proposal-plugin": "^1.0.0",
    "esdoc-jsx-plugin": "^1.0.0",
    "esdoc-standard-plugin": "^1.0.0"
  }

When using a class decorator with an object as argument, I run into this error:

SyntaxError: Invalid regular expression: /[~]Logger
  level: 'debug'
})$/: Unmatched ')'
    at RegExp (<anonymous>)
    at ClassDocBuilder._findByName (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Builder\DocBuilder.js:116:20)
    at ClassDocBuilder._buildDocLinkHTML (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Builder\DocBuilder.js:768:22)
    at ClassDocBuilder._buildDecoratorHTML (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Builder\DocBuilder.js:1012:25)
    at ClassDocBuilder._buildClassDoc (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Builder\ClassDocBuilder.js:87:32)
    at ClassDocBuilder.exec (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Builder\ClassDocBuilder.js:31:32)
    at Plugin._exec (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Plugin.js:93:63)
    at Plugin.onPublish (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc-publish-html-plugin\out\src\Plugin.js:71:10)
    at Plugin._execHandler (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc\out\src\Plugin\Plugin.js:55:26)
    at Plugin.onPublish (D:\Projects\xai-jbm\jovi-r3r-experiments\docs\esdoc\node_modules\esdoc\out\src\Plugin\Plugin.js:154:10)

The stack trace is from this source code:

@Logger('Raycasting', { level: 0 })
export default class Raycasting { ... }

In general:

function Decorator() {}

// works:
@Decorator()
export default class MyClass { }

// works:
@Logger('foo')
export default class MyClass { }

// works:
const options = { level: 'debug' };
@Logger('foo', options )
export default class MyClass { }

// does not work:
@Logger({ level: 'debug' })
export default class MyClass { }

// does not work:
@Logger('foo', { level: 'debug' })
export default class MyClass { }
loopmode commented 7 years ago

Important: The error only happens using esdoc@1.0.3 - it does not happen using @1.0.2 or @1.01

trbm1 commented 7 years ago

I have traced this error to: https://github.com/esdoc/esdoc-plugins/blob/master/esdoc-publish-html-plugin/src/Builder/DocBuilder.js

Line: https://github.com/esdoc/esdoc-plugins/blob/378f3dddfdf2693cdb08ea392e5a5bb03571342e/esdoc-publish-html-plugin/src/Builder/DocBuilder.js#L101

It's one of the Regular Expressions parsing the decorators. Here is a link at which Match 1 and 3 are as intended. Match 2 shows why the error exists. http://rubular.com/r/6UdWsNAQS1

Who solves the RegEx, solves the bug. I'd prefer decorates would allow Simple Types, JSON Types and even functions in classic or Arrow (ES6) way.

trbm1 commented 7 years ago

An additional problem I found with regards to parsing Decorators originates from the core of ESDOC. See issue: https://github.com/esdoc/esdoc/issues/471