Skip to content

Add no-atomics-pause rule #251

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 4 commits 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
8 changes: 8 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
54 changes: 54 additions & 0 deletions docs/rules/no-atomics-pause.md
Original file line number Diff line number Diff line change
@@ -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

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
- ✅ 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:

<eslint-playground type="bad">

```js
/*eslint es-x/no-atomics-pause: [error] */
Atomics.pause()
```

</eslint-playground>

## 🔧 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
1 change: 1 addition & 0 deletions lib/configs/flat/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions lib/configs/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
35 changes: 35 additions & 0 deletions lib/rules/no-atomics-pause.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 `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" },
})
},
}
29 changes: 29 additions & 0 deletions lib/util/type-checker/es-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,35 @@ const WELLKNOWN_GLOBALS = {
SQRT2: { type: "Number" },
},
},
Atomics: {
type: "Object",
/** @type {Record<import('./types').AtomicsProperty,TypeInfo|undefined>} */
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<import('./types').ObjectPrototypeProperty, TypeInfo>} */
Expand Down
3 changes: 3 additions & 0 deletions lib/util/type-checker/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,6 @@ export type TypedArrayPrototypeProperty = Exclude<
ExcludePrototypeProperty | "BYTES_PER_ELEMENT"
>;
export type MathProperty = Exclude<keyof typeof Math, ExcludeProperty>;
export type AtomicsProperty =
| Exclude<keyof typeof Atomics, ExcludeProperty>
| "pause";
2 changes: 2 additions & 0 deletions lib/util/well-known-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ const atomicsProperties = new Set([
"notify",
"xor",
// [ %Symbol.toStringTag% ]

"pause",
])

const jsonProperties = new Set([
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-atomics-pause.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-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."],
},
],
})
2 changes: 1 addition & 1 deletion tests/lib/rules/no-atomics-waitasync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down