【问题标题】:eslint rule forces exponentiation operator vs Math.pow causing ALL Jest test suites to fail with SyntaxError: Unexpected token *eslint 规则强制求幂运算符 vs Math.pow 导致所有 Jest 测试套件失败并出现 SyntaxError: Unexpected token *
【发布时间】:2019-03-02 14:59:26
【问题描述】:

我的代码中出现了一个 eslint 规则,阻止我使用 Math.pow 来支持 ES6 alternative exponentiation operator。在本机反应中,我试图通过使用勾股定理计算 x/y 来计算触摸手势之间的距离。该计算需要我将触摸的 x/y 平方,以获得用户两根手指之间的距离。我第一次用这种方式:

this.pinch = Math.sqrt((Math.pow((px1 - px2), 2)) + (Math.pow((py1 - py2), 2)));

我收到了 eslint 规则:

Math.pow is restricted from being used. Use the exponentiation operator (**) instead. (no-restricted-properties)

我把代码改成如下:

this.pinch = Math.sqrt(((px1 - px2) ** 2) + ((py1 - py2) ** 2));

现在每个 Jest 测试套件都因此错误而失败:

  ● Test suite failed to run

    /Users/egrosskurth/Documents/apps/cv-react-native/src/components/lists/dataTable/CvDataTableGrid.js:234
    _this3.pinchStart=Math.sqrt((px1-px2)**2+(py1-py2)**2);
                                          ^
    SyntaxError: Unexpected token *

我需要知道如何转换这些字符,以免它们破坏我的测试。这是我在我的包 json 中实现的当前 Jest 设置:

"devDependencies": {
    "babel-eslint": "8.2.1",
    "babel-jest": "22.0.4",
    "babel-plugin-transform-decorators-legacy": "1.3.4",
    "babel-preset-env": "1.7.0",
    "babel-preset-react-native": "4.0.0",
    "eslint": "4.15.0",
    "eslint-config-airbnb": "16.1.0",
    "eslint-plugin-filenames": "1.2.0",
    "eslint-plugin-import": "2.8.0",
    "eslint-plugin-jsx-a11y": "6.0.3",
    "eslint-plugin-react": "7.5.1",
    "husky": "0.14.3",
    "jest": "22.0.5",
    "jest-transform-stub": "1.0.0",
    "license-checker": "20.1.0",
    "lint-staged": "6.1.0",
    "react-test-renderer": "16.3.0-alpha.2",
    "reactotron-react-native": "2.0.0",
    "rimraf": "2.6.2"
  },
  "jest": {
    "collectCoverageFrom": [
      "<rootDir>/src/pages/**/*.{js,jsx}",
      "!<rootDir>/src/pages/**/*.styles.{js,jsx}",
      "!<rootDir>/src/pages/sda/**",
      "<rootDir>/src/components/**/*.{js,jsx}",
      "!<rootDir>/src/**/*.styles.{js,jsx}",
      "!**/node_modules/**",
      "!**/android/**",
      "!**/ios/**"
    ],
    "transform": {
      ".+\\.(css|styl|less|sass|scss|png|jpg|gif|GIF|ttf|woff|woff2)$": "jest-transform-stub",
      "^.+\\.jsx$": "babel-jest",
      "^.+\\.js$": "babel-jest"
    },
    "preset": "react-native",
    "setupFiles": [
      "./__tests__/__mocks__/node-modules/react-native-localization_mock.js",
      "./__tests__/setupJest.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/*.test.js",
      "<rootDir>/__tests__/**/*.test.js"
    ],
    "transformIgnorePatterns": [
      "node_modules/(?!mobx-react/native|react-native-view-shot)/"
    ]
  },
  "lint-staged": {
    "src/**/*.{js,json}": [
      "lint",
      "test:check",
      "git add"
    ]
  }

我现在已经禁用了该行的 lint 规则,因为我在网上找到的唯一解决方案是为此安装一个开发依赖项。如果有人能指导我将非常感激。

【问题讨论】:

    标签: unit-testing react-native jestjs eslint exponentiation


    【解决方案1】:

    正如你在https://2ality.com/2016/02/exponentiation-operator.html 中提到的,它在 es2016 中是最终的,所以只需设置 eslint 规则“parseOptions”,如下所示:

    "parserOptions": {
        "ecmaVersion": 2016
    }
    

    【讨论】:

    • 我应该把这条规则放在哪里?
    猜你喜欢
    • 2020-01-15
    • 2020-12-28
    • 2022-06-16
    • 1970-01-01
    • 2017-06-10
    • 2021-01-22
    • 2018-03-29
    • 2019-03-17
    • 2020-11-21
    相关资源
    最近更新 更多