eslint 规则强制幂运算符与 Math.pow 导致所有 Jest 测试套件失败,并显示语法错误:意外的令牌 *



我的代码中出现了一个eslint规则规则,劝阻我不要使用Math.pow,而支持ES6替代幂运算符。在 react native 中,我试图通过使用勾股定理计算 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 *

我需要知道如何转换这些字符,这样它们就不会破坏我的测试。这是我在我的包中实现的当前 Jest 设置,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"
]
}

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

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

"parserOptions": {
"ecmaVersion": 2016
}

最新更新