JSDoc 3在严格模式下使用相同的名称解析错误getter / setter

时间:2013-09-17 22:49:48

标签: javascript jsdoc

编译以下代码以生成带有JSDoc 3的文档时出错。如果我删除第一行“user strict”;然后它就可以了。这是JSDoc中的错误,还是我遗漏了什么?

代码:

"use strict";

var BaseClass = require("./BaseClass").BaseClass;

/**
 * @class
 * @extends BaseClass
  */
 var MyClass = BaseClass.extend(
 /** @lends MyClass.prototype */
 {
     /**
      * Initializer.
      * @public
      */
     initialize:function () {
         this._id = -1;
     },

     /**
      * Property id
      * @public
      * @property {number} id
      */
     get id() {
         return this._id;
     },

     set id(id) {
         this._id = id;
     }
 }); 

 exports.MyClass = MyClass;

这是我看到的错误:

./jsdoc3/jsdoc -c ./conf.json
js: "/xxx/src/main.js", line 31: Property "id" already defined in this object literal.
js:     }
js: ....^
org.mozilla.javascript.EvaluatorException: Compilation produced 1 syntax errors.     (/xxx/src/main.js#1)
at org.mozilla.javascript.tools.ToolErrorReporter.runtimeError(ToolErrorReporter.java:144)
at org.mozilla.javascript.Parser.parse(Parser.java:596)
at org.mozilla.javascript.Parser.parse(Parser.java:505)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:161)
at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:247)
at org.mozilla.javascript.optimizer.OptRuntime.callN(OptRuntime.java:86)
at org.mozilla.javascript.gen.file__Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_node_modules_jsdoc_src_parser_js_18._c_anonymous_24(Unknown Source)
at org.mozilla.javascript.gen.file__Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_node_modules_jsdoc_src_parser_js_18.call(Unknown Source)
at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:76)
at org.mozilla.javascript.gen.file__Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_node_modules_jsdoc_src_parser_js_18._c_anonymous_2(Unknown Source)
at org.mozilla.javascript.gen.file__Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_node_modules_jsdoc_src_parser_js_18.call(Unknown Source)
at org.mozilla.javascript.optimizer.OptRuntime.call2(OptRuntime.java:76)
at org.mozilla.javascript.gen._Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_jsdoc_js_1._c_main_3(Unknown Source)
at org.mozilla.javascript.gen._Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_jsdoc_js_1.call(Unknown Source)
at org.mozilla.javascript.optimizer.OptRuntime.callName0(OptRuntime.java:108)
at org.mozilla.javascript.gen._Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_jsdoc_js_1._c_script_0(Unknown Source)
at org.mozilla.javascript.gen._Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_jsdoc_js_1.call(Unknown Source)
at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:426)
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3178)
at org.mozilla.javascript.gen._Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_jsdoc_js_1.call(Unknown Source)
at org.mozilla.javascript.gen._Users_ytakeda_Projects_Supernova_snova_docs_submodules_jsdoc3_jsdoc_js_1.exec(Unknown Source)
at org.mozilla.javascript.tools.shell.Main.evaluateScript(Main.java:654)
at org.mozilla.javascript.tools.shell.Main.processFileSecure(Main.java:552)
at org.mozilla.javascript.tools.shell.Main.processFile(Main.java:507)
at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:499)
at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:215)
at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:134)
at org.mozilla.javascript.Context.call(Context.java:521)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:535)
at org.mozilla.javascript.tools.shell.Main.exec(Main.java:198)
at org.mozilla.javascript.tools.shell.Main.main(Main.java:174)
make: *** [docs] Error 1

1 个答案:

答案 0 :(得分:2)

如上所述,此语法在JavaScript的严格模式下是合法的。我怀疑这是JSDoc使用的Mozilla Rhino版本中的解析器错误。

作为一种变通方法,您可以使用以下插件在JSDoc分析文件之前注释掉“use strict”声明:

exports.handlers = {
    beforeParse: function(e) {
        e.source = e.source.replace(/['"]use strict['"]/g, "//'use strict'");
    }
};