useExponentiationOperator
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/style/useExponentiationOperator¥Diagnostic Category:
lint/style/useExponentiationOperator -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 safe 修复程序。
¥This rule has a safe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
prefer-exponentiation-operator相同¥Same as
prefer-exponentiation-operator
-
¥How to configure
{ "linter": { "rules": { "style": { "useExponentiationOperator": "error" } } }}¥Description
禁止使用 Math.pow,而使用 ** 运算符。
¥Disallow the use of Math.pow in favor of the ** operator.
在 ES2016 中引入的中缀指数运算符 ** 是标准 Math.pow 函数的替代方案。中缀表示法被认为更具可读性,因此比函数表示法更可取。
¥Introduced in ES2016, the infix exponentiation operator ** is an alternative for the standard Math.pow function.
Infix notation is considered to be more readable and thus more preferable than the function notation.
¥Examples
¥Invalid
const foo = Math.pow(2, 8);code-block.js:1:13 lint/style/useExponentiationOperator FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use the ’**’ operator instead of ‘Math.pow’.
> 1 │ const foo = Math.pow(2, 8);
│ ^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Use the ’**’ operator instead of ‘Math.pow’.
1 │ - const·foo·=·Math.pow(2,·8);
1 │ + const·foo·=·2·**·8;
2 2 │
const bar = Math.pow(a, b);code-block.js:1:13 lint/style/useExponentiationOperator FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use the ’**’ operator instead of ‘Math.pow’.
> 1 │ const bar = Math.pow(a, b);
│ ^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Use the ’**’ operator instead of ‘Math.pow’.
1 │ - const·bar·=·Math.pow(a,·b);
1 │ + const·bar·=·a·**·b;
2 2 │
let baz = Math.pow(a + b, c + d);code-block.js:1:11 lint/style/useExponentiationOperator FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use the ’**’ operator instead of ‘Math.pow’.
> 1 │ let baz = Math.pow(a + b, c + d);
│ ^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Use the ’**’ operator instead of ‘Math.pow’.
1 │ - let·baz·=·Math.pow(a·+·b,·c·+·d);
1 │ + let·baz·=·(a·+·b)·**·(c·+·d);
2 2 │
let quux = Math.pow(-1, n);code-block.js:1:12 lint/style/useExponentiationOperator FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use the ’**’ operator instead of ‘Math.pow’.
> 1 │ let quux = Math.pow(-1, n);
│ ^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Use the ’**’ operator instead of ‘Math.pow’.
1 │ - let·quux·=·Math.pow(-1,·n);
1 │ + let·quux·=·(-1)·**·n;
2 2 │
¥Valid
const foo = 2 ** 8;
const bar = a ** b;
let baz = (a + b) ** (c + d);
let quux = (-1) ** n;¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号