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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
167 changes: 154 additions & 13 deletions scripts/new-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ async function main(ruleId) {
const ruleFile = path.resolve(__dirname, `../lib/rules/${ruleId}.js`)
const testFile = path.resolve(__dirname, `../tests/lib/rules/${ruleId}.js`)
const docFile = path.resolve(__dirname, `../docs/rules/${ruleId}.md`)
const changesetFile = path.resolve(__dirname, `../.changeset/${ruleId}.md`)

prompts.intro("Create the new rule!")

Expand All @@ -47,10 +48,10 @@ async function main(ruleId) {
value: "global-object",
label: "The rule forbids the use of global objects.",
},
// {
// value: "static-properties",
// label: "The rule forbids the use of static properties.",
// },
{
value: "static-properties",
label: "The rule forbids the use of static properties.",
},
// {
// value: "prototype-properties",
// label: "The rule forbids the use of prototype properties.",
Expand Down Expand Up @@ -131,20 +132,32 @@ async function main(ruleId) {
.filter((s) => s)
}

const BUILDERS = {
"global-object": buildGlobalObjectRuleResources,
"static-properties": buildStaticPropertiesRuleResources,
"nonstandard-static-properties":
buildNonStandardStaticPropertiesRuleResources,
"nonstandard-prototype-properties":
buildNonStandardPrototypePropertiesRuleResources,
default: buildDefaultResources,
}

const resources =
kind === "global-object"
? buildGlobalObjectRuleResources(resourceOptions)
: kind === "nonstandard-static-properties"
? buildNonStandardStaticPropertiesRuleResources(resourceOptions)
: kind === "nonstandard-prototype-properties"
? buildNonStandardPrototypePropertiesRuleResources(
resourceOptions,
)
: buildDefaultResources(resourceOptions)
BUILDERS[kind]?.(resourceOptions) ??
buildDefaultResources(resourceOptions)

fs.writeFileSync(ruleFile, resources.rule)
fs.writeFileSync(testFile, resources.test)
fs.writeFileSync(docFile, resources.doc)
fs.writeFileSync(
changesetFile,
`---
"eslint-plugin-es-x": minor
---

Add \`es-x/${ruleId}\` rule
`,
)

cp.execSync(`code "${ruleFile}"`)
cp.execSync(`code "${testFile}"`)
Expand Down Expand Up @@ -243,6 +256,134 @@ let ${object.toLowerCase()} = new ${object}()
}
}

function buildStaticPropertiesRuleResources({ ruleId, object, properties }) {
const promptObject = globalThis[object]
const exampleProperty = promptObject
? Object.getOwnPropertyNames(promptObject)[0]
: "example"
const propertyType =
promptObject && promptObject[properties[0]]
? typeof promptObject[properties[0]]
: "function"
const kind =
propertyType === "function"
? ["method", "methods"]
: ["property", "properties"]
const propertiesString =
properties.length > 1 ? `{${properties.join(",")}}` : properties[0]
let propertiesName = `\`${object}.${properties[properties.length - 1]}\` ${kind[0]}`
if (properties.length > 1) {
propertiesName = `${properties
.slice(0, -1)
.map((p) => `\`${object}.${p}\``)
.join(
", ",
)}, and \`${object}.${properties[properties.length - 1]}\` ${kind[1]}`
}

return {
rule: `"use strict"

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

module.exports = {
meta: {
docs: {
description: "disallow the \`${object}.${propertiesString}\` ${kind[0]}",
category: "ES${maxESVersion}",
recommended: false,
url: "",
},
fixable: null,
messages: {
forbidden: "ES${maxESVersion} '{{name}}' ${kind[0]} is forbidden.",
},
schema: [
{
type: "object",
properties: {
allowTestedProperty: { type: "boolean" },
},
additionalProperties: false,
},
],
type: "problem",
},
create(context) {
return defineStaticPropertiesHandler(context, {
"${object}": { ${properties.map((p) => `"${p}": "${propertyType}"`).join(",\n")} },
})
},
}
`,
test: `"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/${ruleId}.js")

new RuleTester().run("${ruleId}", rule, {
valid: [
"${object}",
"${object}.${exampleProperty}",
${properties.map((p) => `"let ${object} = 0; ${object}.${p}"`).join(",\n")}
],
invalid: [
${properties
.map(
(p) => `{
code: "${object}.${p}",
errors: ["ES${maxESVersion} '${object}.${p}' ${kind[0]} is forbidden."],
}`,
)
.join(",\n")}
],
})
`,
doc: `# es-x/${ruleId}
>

This rule reports ES${maxESVersion} [${propertiesName}]($$LINK$$) as errors.

## 💡 Examples

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

<eslint-playground type="bad">

\`\`\`js
/*eslint es-x/${ruleId}: error */
${properties.map((p) => `${object}.${p}();`).join("\n")}
\`\`\`

</eslint-playground>

## 🔧 Options

This rule has an option.

\`\`\`jsonc
{
"rules": {
"es-x/${ruleId}": [
"error",
{
"allowTestedProperty": false
}
]
}
}
\`\`\`

### allowTestedProperty: boolean

Configure the allowTestedProperty mode for only this rule.
This is prior to the \`settings['es-x'].allowTestedProperty\` setting.
`,
}
}

function buildNonStandardStaticPropertiesRuleResources({ ruleId, object }) {
const camelObject = camelCase(object)
return {
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."],
},
],
})