2

I have an eslint rule rule cropping up in my code dissuading me from using Math.pow in favor of the ES6 alternative exponentiation operator. In react native I am trying to calculate the distance between touch gestures by calculating the x/y using the pythagorean theorem. The calculation requires I square the x/y of the touches in order to get the distance between the users two fingers. I first used this way:

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

And I receive the eslint rule:

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

I changed the code to the following:

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

and now every Jest test suite is failing with this error:

  ● 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 *

I need to know how to transform these characters so they do not break my tests. Here is the current Jest setup I have implemented in my package,json:

"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"
    ]
  }

I have disabled the lint rule on that line for now as the only solution online I have found, is to install a dev dependency just for this. If someone could guide the way I would be very thankful.

brass monkey
  • 5,841
  • 10
  • 36
  • 61
Erik Grosskurth
  • 3,762
  • 5
  • 29
  • 44

1 Answers1

0

as you mentioned in https://2ality.com/2016/02/exponentiation-operator.html, it's final in es2016, so just set eslint rule "parseOptions" like below:

"parserOptions": {
    "ecmaVersion": 2016
}
thinkkevin
  • 191
  • 2
  • 4