Javascript SyntaxError:Strict模式不允许在词汇嵌套语句中声明函数
在Android从React Native 0.18升级到0.22后,我开始出现以下错误:Javascript SyntaxError:Strict模式不允许在词汇嵌套语句中声明函数,javascript,android,react-native,Javascript,Android,React Native,在Android从React Native 0.18升级到0.22后,我开始出现以下错误: Got JS Exception: SyntaxError: Strict mode does not allow function declarations in a lexically nested statement. 它会在启动应用程序时使应用程序崩溃,并且无法启动调试 正如所建议的,我已尝试手动删除所有“use strict”以及使用构建脚本: gulp.task('transform-and
Got JS Exception: SyntaxError: Strict mode does not allow function declarations in a lexically nested statement.
它会在启动应用程序时使应用程序崩溃,并且无法启动调试
正如所建议的,我已尝试手动删除所有“use strict”以及使用构建脚本:
gulp.task('transform-android', function() {
return gulp.src(config.tasks.androidjs.src)
.pipe(envify({NATIVE: true}))
.pipe(babel({
"stage": 0,
blacklist: ["useStrict"]
}))
.pipe(gulp.dest(path.join(config.root.dest, config.tasks.androidjs.dest)));
});
生成的代码中不再有“use strict”。
然而,我仍然得到完全相同的错误。
我使用的是babel v5.8.3
非常感谢您的帮助
这是我的package.json
{
"name": "xodo",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node_modules/react-native/packager/packager.sh"
},
"author": "",
"license": "ISC",
"dependencies": {
"events": "^1.1.0",
"firebase": "^2.3.1",
"flux": "^2.0.1",
"immutable": "3.7.3",
"jwt-decode": "^1.4.0",
"linkifyjs": "^2.0.0-beta.7",
"lodash": "^3.10.1",
"promise-queue": "^2.2.3",
"react": "^0.14.7",
"react-immutable-proptypes": "^1.5.1",
"react-native": "^0.22.2",
"react-native-invertible-scroll-view": "^0.2.0",
"reqwest": "^1.1.5",
"rsvp": "^3.0.18",
"unbounce": "^0.1.0",
"xmldom": "^0.1.22"
},
"devDependencies": {
"babel": "^5.8.3",
"del": "^2.2.0",
"gulp": "^3.9.0",
"gulp-babel": "^5.3.0",
"gulp-envify": "^1.0.0",
"gulp-less2js": "0.0.3",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-sequence": "^0.4.0",
"path": "^0.12.7"
}
}
下面是错误的完整跟踪:
Got JS Exception: SyntaxError: Strict mode does not allow function declarations in a lexically nested statement.
03-29 15:15:01.567 2047-2109/com.xodo.pdf.reader E/unknown:React: Exception in native call from JS
com.facebook.react.bridge.JSExecutionException: SyntaxError: Strict mode does not allow function declarations in a lexically nested statement. (http://10.0.3.2:8081/index.android.bundle?platform=android&dev=true&hot=false:32356)
at com.facebook.react.bridge.ReactBridge.loadScriptFromFile(Native Method)
at com.facebook.react.bridge.JSBundleLoader$2.loadScript(JSBundleLoader.java:58)
at com.facebook.react.bridge.CatalystInstanceImpl$2.call(CatalystInstanceImpl.java:146)
at com.facebook.react.bridge.CatalystInstanceImpl$2.call(CatalystInstanceImpl.java:137)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$1.run(MessageQueueThreadImpl.java:73)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
at android.os.Looper.loop(Looper.java:148)
at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:184)
at java.lang.Thread.run(Thread.java:818)
问题出现在一个核心文件中。拉取请求是为react native创建的,但您可以自己进行更改作为解决方法 打开node_modules\react native\Libraries\Core\InitializeCore.js第112行 将函数handleError(e,isFatal)更改为var handleError=函数(e,isFatal) 然后执行npm启动--“重置缓存”
您可以在上找到更多信息。这不是问题的直接答案,而是错误背后的一个概念或原因
当在块中而不是在函数体中定义内部函数时,它将给出SyntaxError。ES5不允许在块上声明函数(函数体除外)。另一方面,ES2015(ES6)放宽了这一限制。
这是ESLint规则,在if/else或for块中没有任何函数声明?我与React JS共享相同的后端代码,React JS有“use strict”,在浏览器上运行良好。只有在Android升级到RN v0.22时才会出现问题。我遇到了同样的问题,它发生在xmldom中(dom.js第17行)。如果你想确认,你可以下载包文件并检查第32356行。像魅力一样工作!谢谢。