diff --git a/docs/rules/index.md b/docs/rules/index.md index a087ad9c..2658d1a2 100644 --- a/docs/rules/index.md +++ b/docs/rules/index.md @@ -4,6 +4,14 @@ This plugin provides the following rules. - 🔧 mark means that the `--fix` option on the [command line](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) can automatically fix some of the problems reported by the rule. +## ES2026 + +There is a config that enables the rules in this category: [`no-new-in-esnext`] + +| Rule ID | Description | | +|:--------|:------------|:--:| +| [es-x/no-atomics-pause](./no-atomics-pause.md) | disallow the `Atomics.pause` method. | | + ## ES2025 There is a config that enables the rules in this category: [`no-new-in-esnext`] diff --git a/docs/rules/no-atomics-pause.md b/docs/rules/no-atomics-pause.md new file mode 100644 index 00000000..dc506da8 --- /dev/null +++ b/docs/rules/no-atomics-pause.md @@ -0,0 +1,54 @@ +--- +title: "es-x/no-atomics-pause" +description: "disallow the `Atomics.pause` method" +--- + +# es-x/no-atomics-pause +> disallow the `Atomics.pause` method + +- ❗ ***This rule has not been released yet.*** +- ✅ The following configurations enable this rule: [no-new-in-esnext] + +This rule reports ES2026 [`Atomics.pause` method](https://github.com/tc39/proposal-atomics-microwait) as errors. + +## 💡 Examples + +⛔ Examples of **incorrect** code for this rule: + + + +```js +/*eslint es-x/no-atomics-pause: [error] */ +Atomics.pause() +``` + + + +## 🔧 Options + +This rule has an option. + +```jsonc +{ + "rules": { + "es-x/no-atomics-pause": [ + "error", + { + "allowTestedProperty": false + } + ] + } +} +``` + +### allowTestedProperty: boolean + +Configure the allowTestedProperty mode for only this rule. +This is prior to the `settings['es-x'].allowTestedProperty` setting. + +## 📚 References + +- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-atomics-pause.js) +- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-atomics-pause.js) + +[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext diff --git a/lib/configs/flat/no-new-in-esnext.js b/lib/configs/flat/no-new-in-esnext.js index e5cccf75..5a9d439a 100644 --- a/lib/configs/flat/no-new-in-esnext.js +++ b/lib/configs/flat/no-new-in-esnext.js @@ -11,6 +11,7 @@ module.exports = { }, }, rules: { + "es-x/no-atomics-pause": "error", "es-x/no-dataview-prototype-getfloat16-setfloat16": "error", "es-x/no-dynamic-import-options": "error", "es-x/no-float16array": "error", diff --git a/lib/configs/no-new-in-esnext.js b/lib/configs/no-new-in-esnext.js index 9dd343d4..0cfc8b7b 100644 --- a/lib/configs/no-new-in-esnext.js +++ b/lib/configs/no-new-in-esnext.js @@ -7,6 +7,7 @@ module.exports = { plugins: ["es-x"], rules: { + "es-x/no-atomics-pause": "error", "es-x/no-dataview-prototype-getfloat16-setfloat16": "error", "es-x/no-dynamic-import-options": "error", "es-x/no-float16array": "error", diff --git a/lib/index.js b/lib/index.js index 03cc4988..a57434f9 100644 --- a/lib/index.js +++ b/lib/index.js @@ -179,6 +179,7 @@ module.exports = { "no-async-functions": require("./rules/no-async-functions"), "no-async-iteration": require("./rules/no-async-iteration"), "no-atomics": require("./rules/no-atomics"), + "no-atomics-pause": require("./rules/no-atomics-pause"), "no-atomics-waitasync": require("./rules/no-atomics-waitasync"), "no-bigint": require("./rules/no-bigint"), "no-binary-numeric-literals": require("./rules/no-binary-numeric-literals"), diff --git a/lib/rules/no-atomics-pause.js b/lib/rules/no-atomics-pause.js new file mode 100644 index 00000000..9af6f564 --- /dev/null +++ b/lib/rules/no-atomics-pause.js @@ -0,0 +1,35 @@ +"use strict" + +const { + defineStaticPropertiesHandler, +} = require("../util/define-static-properties-handler") + +module.exports = { + meta: { + docs: { + description: "disallow the `Atomics.pause` method.", + category: "ES2026", + recommended: false, + url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-atomics-pause.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, { + Atomics: { pause: "function" }, + }) + }, +} diff --git a/lib/util/type-checker/es-types.js b/lib/util/type-checker/es-types.js index 3ad23c49..aa934226 100644 --- a/lib/util/type-checker/es-types.js +++ b/lib/util/type-checker/es-types.js @@ -434,6 +434,35 @@ const WELLKNOWN_GLOBALS = { SQRT2: { type: "Number" }, }, }, + Atomics: { + type: "Object", + /** @type {Record} */ + properties: { + add: RETURN_NUMBER, + and: RETURN_NUMBER, + compareExchange: RETURN_NUMBER, + exchange: RETURN_NUMBER, + isLockFree: RETURN_BOOLEAN, + load: RETURN_NUMBER, + notify: RETURN_NUMBER, + or: RETURN_NUMBER, + store: RETURN_NUMBER, + sub: RETURN_NUMBER, + wait: RETURN_STRING, + waitAsync: { + type: "Function", + return: { + type: "Object", + properties: { + async: { type: "Boolean" }, + // value: { type: "Promise" or "String" }, + }, + }, + }, + xor: RETURN_NUMBER, + pause: { type: "Function" }, + }, + }, } /** @type {Record} */ diff --git a/lib/util/type-checker/types.d.ts b/lib/util/type-checker/types.d.ts index 7158fd7b..3da65bd4 100644 --- a/lib/util/type-checker/types.d.ts +++ b/lib/util/type-checker/types.d.ts @@ -215,3 +215,6 @@ export type TypedArrayPrototypeProperty = Exclude< ExcludePrototypeProperty | "BYTES_PER_ELEMENT" >; export type MathProperty = Exclude; +export type AtomicsProperty = + | Exclude + | "pause"; diff --git a/lib/util/well-known-properties.js b/lib/util/well-known-properties.js index 0f5a3426..d120d7f8 100644 --- a/lib/util/well-known-properties.js +++ b/lib/util/well-known-properties.js @@ -797,6 +797,8 @@ const atomicsProperties = new Set([ "notify", "xor", // [ %Symbol.toStringTag% ] + + "pause", ]) const jsonProperties = new Set([ diff --git a/tests/lib/rules/no-atomics-pause.js b/tests/lib/rules/no-atomics-pause.js new file mode 100644 index 00000000..41cfd107 --- /dev/null +++ b/tests/lib/rules/no-atomics-pause.js @@ -0,0 +1,14 @@ +"use strict" + +const RuleTester = require("../../tester") +const rule = require("../../../lib/rules/no-atomics-pause.js") + +new RuleTester().run("no-atomics-pause", rule, { + valid: ["Atomics", "Atomics.wait", "let Atomics = 0; Atomics.pause"], + invalid: [ + { + code: "Atomics.pause", + errors: ["ES2026 'Atomics.pause' method is forbidden."], + }, + ], +}) diff --git a/tests/lib/rules/no-atomics-waitasync.js b/tests/lib/rules/no-atomics-waitasync.js index 4e877a0b..1fad7b72 100644 --- a/tests/lib/rules/no-atomics-waitasync.js +++ b/tests/lib/rules/no-atomics-waitasync.js @@ -4,7 +4,7 @@ const RuleTester = require("../../tester") const rule = require("../../../lib/rules/no-atomics-waitasync.js") new RuleTester().run("no-atomics-waitasync", rule, { - valid: ["Atomics", "Atomics.wait", "let Atomics = 0; Atomics.watiAsync"], + valid: ["Atomics", "Atomics.wait", "let Atomics = 0; Atomics.waitAsync"], invalid: [ { code: "Atomics.waitAsync",