Skip to content

Commit 1134e50

Browse files
committed
Add es-x/no-math-sumprecise rule
1 parent 821498f commit 1134e50

File tree

6 files changed

+98
-3
lines changed

6 files changed

+98
-3
lines changed

.changeset/no-math-sumprecise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-es-x": minor
3+
---
4+
5+
Add `es-x/no-math-sumprecise` rule

docs/rules/no-math-sumprecise.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# es-x/no-math-sumprecise
2+
>
3+
4+
This rule reports ES2026 [`Math.sumPrecise` property](https://github.com/tc39/proposal-math-sum) as errors.
5+
6+
## 💡 Examples
7+
8+
⛔ Examples of **incorrect** code for this rule:
9+
10+
<eslint-playground type="bad">
11+
12+
```js
13+
/*eslint es-x/no-math-sumprecise: error */
14+
Math.sumPrecise([1e20, 0.1, -1e20]);
15+
```
16+
17+
</eslint-playground>
18+
19+
## 🔧 Options
20+
21+
This rule has an option.
22+
23+
```jsonc
24+
{
25+
"rules": {
26+
"es-x/no-math-sumprecise": [
27+
"error",
28+
{
29+
"allowTestedProperty": false
30+
}
31+
]
32+
}
33+
}
34+
```
35+
36+
### allowTestedProperty: boolean
37+
38+
Configure the allowTestedProperty mode for only this rule.
39+
This is prior to the `settings['es-x'].allowTestedProperty` setting.

lib/rules/no-math-sumprecise.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use strict"
2+
3+
const {
4+
defineStaticPropertiesHandler,
5+
} = require("../util/define-static-properties-handler")
6+
7+
module.exports = {
8+
meta: {
9+
docs: {
10+
description: "disallow the `Math.sumPrecise` method",
11+
category: "ES2026",
12+
recommended: false,
13+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-math-sumprecise.html",
14+
},
15+
fixable: null,
16+
messages: {
17+
forbidden: "ES2026 '{{name}}' method is forbidden.",
18+
},
19+
schema: [
20+
{
21+
type: "object",
22+
properties: {
23+
allowTestedProperty: { type: "boolean" },
24+
},
25+
additionalProperties: false,
26+
},
27+
],
28+
type: "problem",
29+
},
30+
create(context) {
31+
return defineStaticPropertiesHandler(context, {
32+
Math: { sumPrecise: "function" },
33+
})
34+
},
35+
}

lib/util/well-known-properties.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ const mathProperties = new Set([
248248
"sin",
249249
"sinh",
250250
"sqrt",
251+
"sumPrecise",
251252
"tan",
252253
"tanh",
253254
"trunc",

scripts/new-rule.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,10 @@ function buildStaticPropertiesRuleResources({ ruleId, object, properties }) {
261261
const exampleProperty = promptObject
262262
? Object.getOwnPropertyNames(promptObject)[0]
263263
: "example"
264-
const propertyType = promptObject
265-
? typeof promptObject[properties[0]]
266-
: "function"
264+
const propertyType =
265+
promptObject && promptObject[properties[0]]
266+
? typeof promptObject[properties[0]]
267+
: "function"
267268
const kind =
268269
propertyType === "function"
269270
? ["method", "methods"]

tests/lib/rules/no-math-sumprecise.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use strict"
2+
3+
const RuleTester = require("../../tester")
4+
const rule = require("../../../lib/rules/no-math-sumprecise.js")
5+
6+
new RuleTester().run("no-math-sumprecise", rule, {
7+
valid: ["Math", "Math.abs", "let Math = 0; Math.sumPrecise"],
8+
invalid: [
9+
{
10+
code: "Math.sumPrecise",
11+
errors: ["ES2026 'Math.sumPrecise' method is forbidden."],
12+
},
13+
],
14+
})

0 commit comments

Comments
 (0)