diff --git a/.changeset/no-math-sumprecise.md b/.changeset/no-math-sumprecise.md new file mode 100644 index 00000000..99c7d75c --- /dev/null +++ b/.changeset/no-math-sumprecise.md @@ -0,0 +1,5 @@ +--- +"eslint-plugin-es-x": minor +--- + +Add `es-x/no-math-sumprecise` rule diff --git a/docs/rules/no-math-sumprecise.md b/docs/rules/no-math-sumprecise.md new file mode 100644 index 00000000..493a0360 --- /dev/null +++ b/docs/rules/no-math-sumprecise.md @@ -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: + + + +```js +/*eslint es-x/no-math-sumprecise: error */ +Math.sumPrecise([1e20, 0.1, -1e20]); +``` + + + +## 🔧 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. diff --git a/lib/rules/no-math-sumprecise.js b/lib/rules/no-math-sumprecise.js new file mode 100644 index 00000000..be9125e0 --- /dev/null +++ b/lib/rules/no-math-sumprecise.js @@ -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" }, + }) + }, +} diff --git a/lib/util/well-known-properties.js b/lib/util/well-known-properties.js index e57adfa4..d3f68e19 100644 --- a/lib/util/well-known-properties.js +++ b/lib/util/well-known-properties.js @@ -248,6 +248,7 @@ const mathProperties = new Set([ "sin", "sinh", "sqrt", + "sumPrecise", "tan", "tanh", "trunc", diff --git a/scripts/new-rule.js b/scripts/new-rule.js index c33f755b..8729bce3 100644 --- a/scripts/new-rule.js +++ b/scripts/new-rule.js @@ -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"] diff --git a/tests/lib/rules/no-math-sumprecise.js b/tests/lib/rules/no-math-sumprecise.js new file mode 100644 index 00000000..94cf66c3 --- /dev/null +++ b/tests/lib/rules/no-math-sumprecise.js @@ -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."], + }, + ], +})