簡體   English   中英

SyntaxError:嚴格模式不允許在詞法嵌套語句中使用函數聲明

[英]SyntaxError: Strict mode does not allow function declarations in a lexically nested statement

從React Native 0.18升級到0.22 for Android后,我開始收到以下錯誤:

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)

問題出現在其中一個核心文件中。 pull request是為react-native創建的,但您可以自己進行更改以作為變通方法

open node_modules \\ react-native \\ Libraries \\ Core \\ InitializeCore.js第112行

將函數handleError(e,isFatal)更改為var handleError = function(e,isFatal)

然后做npm start - --reset-cache

您可以在https://github.com/facebook/react-native/issues/11389找到更多信息

這不是您問題的直接答案,而是錯誤背后的概念或原因。

When an inner function is defined in a block rather than function body it will give SyntaxError. ES5 disallows function declarations on blocks (except function bodies). On the other hand, ES2015 (ES6) has relaxed this restriction.

這是ESLint規則no-inner-declaration

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM