Skip to content

Add es-x/no-math-sumprecise rule #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/no-math-sumprecise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-es-x": minor
---

Add `es-x/no-math-sumprecise` rule
39 changes: 39 additions & 0 deletions docs/rules/no-math-sumprecise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# es-x/no-math-sumprecise
>

This rule reports ES2026 [`Math.sumPrecise` property](https://github.com/tc39/proposal-math-sum) as errors.

## 💡 Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad">

```js
/*eslint es-x/no-math-sumprecise: error */
Math.sumPrecise([1e20, 0.1, -1e20]);
```

</eslint-playground>

## 🔧 Options

This rule has an option.

```jsonc
{
"rules": {
"es-x/no-math-sumprecise": [
"error",
{
"allowTestedProperty": false
}
]
}
}
```

### allowTestedProperty: boolean

Configure the allowTestedProperty mode for only this rule.
This is prior to the `settings['es-x'].allowTestedProperty` setting.
35 changes: 35 additions & 0 deletions lib/rules/no-math-sumprecise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"use strict"

const {
defineStaticPropertiesHandler,
} = require("../util/define-static-properties-handler")

module.exports = {
meta: {
docs: {
description: "disallow the `Math.sumPrecise` method",
category: "ES2026",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-math-sumprecise.html",
},
fixable: null,
messages: {
forbidden: "ES2026 '{{name}}' method is forbidden.",
},
schema: [
{
type: "object",
properties: {
allowTestedProperty: { type: "boolean" },
},
additionalProperties: false,
},
],
type: "problem",
},
create(context) {
return defineStaticPropertiesHandler(context, {
Math: { sumPrecise: "function" },
})
},
}
1 change: 1 addition & 0 deletions lib/util/well-known-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ const mathProperties = new Set([
"sin",
"sinh",
"sqrt",
"sumPrecise",
"tan",
"tanh",
"trunc",
Expand Down
7 changes: 4 additions & 3 deletions scripts/new-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ function buildStaticPropertiesRuleResources({ ruleId, object, properties }) {
const exampleProperty = promptObject
? Object.getOwnPropertyNames(promptObject)[0]
: "example"
const propertyType = promptObject
? typeof promptObject[properties[0]]
: "function"
const propertyType =
promptObject && promptObject[properties[0]]
? typeof promptObject[properties[0]]
: "function"
const kind =
propertyType === "function"
? ["method", "methods"]
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-math-sumprecise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-math-sumprecise.js")

new RuleTester().run("no-math-sumprecise", rule, {
valid: ["Math", "Math.abs", "let Math = 0; Math.sumPrecise"],
invalid: [
{
code: "Math.sumPrecise",
errors: ["ES2026 'Math.sumPrecise' method is forbidden."],
},
],
})
Loading