Skip to content
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

Add rules for Intl.DurationFormat #236

Open
wants to merge 2 commits 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
26 changes: 26 additions & 0 deletions docs/configs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,32 @@ export default [
}
```

## no-new-in-esnext-intl-api

disallow the new stuff to be planned for the next yearly ECMAScript Intl API (ECMA-402) snapshot.\
⚠️ This config will be changed in the minor versions of this plugin.

### [Config (Flat Config)]

eslint.config.js:

```js
import pluginESx from "eslint-plugin-es-x"
export default [
pluginESx.configs['flat/no-new-in-esnext-intl-api']
]
```

### [Legacy Config]

.eslintrc.*:

```json
{
"extends": ["plugin:es-x/no-new-in-esnext-intl-api"],
}
```

## no-new-in-es2024

disallow new stuff in ES2024.
Expand Down
11 changes: 11 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
| [es-x/no-set-prototype-union](./no-set-prototype-union.md) | disallow the `Set.prototype.union` method. | |
| [es-x/no-trailing-dynamic-import-commas](./no-trailing-dynamic-import-commas.md) | disallow trailing commas in `import()`. | 🔧 |

## ES2025 Intl API

There is a config that enables the rules in this category: [`no-new-in-esnext-intl-api`]

| Rule ID | Description | |
|:--------|:------------|:--:|
| [es-x/no-intl-durationformat](./no-intl-durationformat.md) | disallow the `Intl.DurationFormat` object. | |

## ES2024

There are multiple configs that enable all rules in this category: [`no-new-in-es2024`], [`restrict-to-es3`], [`restrict-to-es5`], [`restrict-to-es2015`], [`restrict-to-es2016`], [`restrict-to-es2017`], [`restrict-to-es2018`], [`restrict-to-es2019`], [`restrict-to-es2020`], [`restrict-to-es2021`], [`restrict-to-es2022`], and [`restrict-to-es2023`]
Expand Down Expand Up @@ -406,6 +414,8 @@ Rules in this category are not included in any preset.
| [es-x/no-nonstandard-intl-datetimeformat-prototype-properties](./no-nonstandard-intl-datetimeformat-prototype-properties.md) | disallow non-standard properties on Intl.DateTimeFormat instance. | |
| [es-x/no-nonstandard-intl-displaynames-properties](./no-nonstandard-intl-displaynames-properties.md) | disallow non-standard static properties on `Intl.DisplayNames` class. | |
| [es-x/no-nonstandard-intl-displaynames-prototype-properties](./no-nonstandard-intl-displaynames-prototype-properties.md) | disallow non-standard properties on Intl.DisplayNames instance. | |
| [es-x/no-nonstandard-intl-durationformat-properties](./no-nonstandard-intl-durationformat-properties.md) | disallow non-standard static properties on `Intl.DurationFormat` class. | |
| [es-x/no-nonstandard-intl-durationformat-prototype-properties](./no-nonstandard-intl-durationformat-prototype-properties.md) | disallow non-standard properties on Intl.DurationFormat instance. | |
| [es-x/no-nonstandard-intl-listformat-properties](./no-nonstandard-intl-listformat-properties.md) | disallow non-standard static properties on `Intl.ListFormat` class. | |
| [es-x/no-nonstandard-intl-listformat-prototype-properties](./no-nonstandard-intl-listformat-prototype-properties.md) | disallow non-standard properties on Intl.ListFormat instance. | |
| [es-x/no-nonstandard-intl-locale-properties](./no-nonstandard-intl-locale-properties.md) | disallow non-standard static properties on `Intl.Locale` class. | |
Expand Down Expand Up @@ -463,6 +473,7 @@ Rules in this category are not included in any preset.
| [es-x/no-string-prototype-iswellformed-towellformed](./no-string-prototype-iswellformed-towellformed.md) | [es-x/no-string-prototype-iswellformed](./no-string-prototype-iswellformed.md), [es-x/no-string-prototype-towellformed](./no-string-prototype-towellformed.md) |

[`no-new-in-esnext`]: ../configs/index.md#no-new-in-esnext
[`no-new-in-esnext-intl-api`]: ../configs/index.md#no-new-in-esnext-intl-api
[`no-new-in-es2024`]: ../configs/index.md#no-new-in-es2024
[`restrict-to-es2023`]: ../configs/index.md#restrict-to-es2023
[`restrict-to-es2023-intl-api`]: ../configs/index.md#restrict-to-es2023-intl-api
Expand Down
38 changes: 38 additions & 0 deletions docs/rules/no-intl-durationformat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: "es-x/no-intl-durationformat"
description: "disallow the `Intl.DurationFormat` object"
---

# es-x/no-intl-durationformat
> disallow the `Intl.DurationFormat` object

- ❗ <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-intl-api]

This rule reports ES2025 Intl API `Intl.DurationFormat` object as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-intl-durationformat: error */
const df = new Intl.DurationFormat("fr-FR", { style: "long" });

df.format({
hours: 1,
minutes: 46,
seconds: 40,
});
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-intl-durationformat.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-intl-durationformat.js)

[no-new-in-esnext-intl-api]: ../configs/index.md#no-new-in-esnext-intl-api
50 changes: 50 additions & 0 deletions docs/rules/no-nonstandard-intl-durationformat-properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---
title: "es-x/no-nonstandard-intl-durationformat-properties"
description: "disallow non-standard static properties on `Intl.DurationFormat` class"
---

# es-x/no-nonstandard-intl-durationformat-properties
> disallow non-standard static properties on `Intl.DurationFormat` class

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

This rule reports non-standard static properties on `Intl.DurationFormat` class as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-nonstandard-intl-durationformat-properties: error */
Intl.DurationFormat.unknown();
```

</eslint-playground>

## 🔧 Options

This rule has an option.

```jsonc
{
"rules": {
"es-x/no-nonstandard-intl-durationformat-properties": [
"error",
{
"allow": []
}
]
}
}
```

### allow: string[]

An array of non-standard property names to allow.

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-nonstandard-intl-durationformat-properties.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-nonstandard-intl-durationformat-properties.js)
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
title: "es-x/no-nonstandard-intl-durationformat-prototype-properties"
description: "disallow non-standard properties on Intl.DurationFormat instance"
---

# es-x/no-nonstandard-intl-durationformat-prototype-properties
> disallow non-standard properties on Intl.DurationFormat instance

- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

This rule reports non-standard properties on Intl.DurationFormat instance as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-nonstandard-intl-durationformat-prototype-properties: error */
const df = new Intl.DurationFormat("fr-FR", { style: "long" });
df.unknown();
```

</eslint-playground>

## 🔧 Options

This rule has an option.

```jsonc
{
"rules": {
"es-x/no-nonstandard-intl-durationformat-prototype-properties": [
"error",
{
"allow": []
}
]
}
}
```

### allow: string[]

An array of non-standard property names to allow.

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-nonstandard-intl-durationformat-prototype-properties.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-nonstandard-intl-durationformat-prototype-properties.js)
2 changes: 1 addition & 1 deletion lib/configs/flat/no-new-in-esnext-intl-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ module.exports = {
return require("../../index.js")
},
},
rules: {},
rules: { "es-x/no-intl-durationformat": "error" },
}
5 changes: 4 additions & 1 deletion lib/configs/no-new-in-esnext-intl-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
*/
"use strict"

module.exports = { plugins: ["es-x"], rules: {} }
module.exports = {
plugins: ["es-x"],
rules: { "es-x/no-intl-durationformat": "error" },
}
3 changes: 3 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ module.exports = {
"no-intl-datetimeformat-prototype-formatrange": require("./rules/no-intl-datetimeformat-prototype-formatrange"),
"no-intl-datetimeformat-prototype-formattoparts": require("./rules/no-intl-datetimeformat-prototype-formattoparts"),
"no-intl-displaynames": require("./rules/no-intl-displaynames"),
"no-intl-durationformat": require("./rules/no-intl-durationformat"),
"no-intl-getcanonicallocales": require("./rules/no-intl-getcanonicallocales"),
"no-intl-listformat": require("./rules/no-intl-listformat"),
"no-intl-locale": require("./rules/no-intl-locale"),
Expand Down Expand Up @@ -287,6 +288,8 @@ module.exports = {
"no-nonstandard-intl-datetimeformat-prototype-properties": require("./rules/no-nonstandard-intl-datetimeformat-prototype-properties"),
"no-nonstandard-intl-displaynames-properties": require("./rules/no-nonstandard-intl-displaynames-properties"),
"no-nonstandard-intl-displaynames-prototype-properties": require("./rules/no-nonstandard-intl-displaynames-prototype-properties"),
"no-nonstandard-intl-durationformat-properties": require("./rules/no-nonstandard-intl-durationformat-properties"),
"no-nonstandard-intl-durationformat-prototype-properties": require("./rules/no-nonstandard-intl-durationformat-prototype-properties"),
"no-nonstandard-intl-listformat-properties": require("./rules/no-nonstandard-intl-listformat-properties"),
"no-nonstandard-intl-listformat-prototype-properties": require("./rules/no-nonstandard-intl-listformat-prototype-properties"),
"no-nonstandard-intl-locale-properties": require("./rules/no-nonstandard-intl-locale-properties"),
Expand Down
27 changes: 27 additions & 0 deletions lib/rules/no-intl-durationformat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict"

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

module.exports = {
meta: {
docs: {
description: "disallow the `Intl.DurationFormat` object.",
category: "ES2025-Intl-API",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-intl-durationformat.html",
},
fixable: null,
messages: {
forbidden: "ES2025 Intl API '{{name}}' object is forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
return defineStaticPropertiesHandler(context, {
Intl: ["DurationFormat"],
})
},
}
48 changes: 48 additions & 0 deletions lib/rules/no-nonstandard-intl-durationformat-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict"

const {
defineNonstandardStaticPropertiesHandler,
} = require("../util/define-nonstandard-static-properties-handler")
const {
intlDurationFormatProperties,
} = require("../util/well-known-properties")

module.exports = {
meta: {
docs: {
description:
"disallow non-standard static properties on `Intl.DurationFormat` class",
category: "nonstandard",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-nonstandard-intl-durationformat-properties.html",
},
fixable: null,
messages: {
forbidden: "Non-standard '{{name}}' property is forbidden.",
},
schema: [
{
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" },
uniqueItems: true,
},
},
additionalProperties: false,
},
],
type: "problem",
},
create(context) {
/** @type {Set<string>} */
const allows = new Set([
...(context.options[0]?.allow || []),
...intlDurationFormatProperties,
])
return defineNonstandardStaticPropertiesHandler(context, {
"Intl.DurationFormat": allows,
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"use strict"

const {
defineNonstandardPrototypePropertiesHandler,
} = require("../util/define-nonstandard-prototype-properties-handler")
const {
intlDurationFormatPrototypeProperties,
} = require("../util/well-known-properties")

module.exports = {
meta: {
docs: {
description:
"disallow non-standard properties on Intl.DurationFormat instance",
category: "nonstandard",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-nonstandard-intl-durationformat-prototype-properties.html",
},
fixable: null,
messages: {
forbidden: "Non-standard '{{name}}' property is forbidden.",
},
schema: [
{
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" },
uniqueItems: true,
},
},
additionalProperties: false,
},
],
type: "problem",
},
create(context) {
/** @type {Set<string>} */
const allows = new Set([
...(context.options[0]?.allow || []),
...intlDurationFormatPrototypeProperties,
])
return defineNonstandardPrototypePropertiesHandler(context, {
"Intl.DurationFormat": allows,
})
},
}
5 changes: 5 additions & 0 deletions lib/util/type-checker/es-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ const WELLKNOWN_GLOBALS = {
return: { type: "Intl.DisplayNames" },
prototypeType: "Intl.DisplayNames",
},
DurationFormat: {
type: "Function",
return: { type: "Intl.DurationFormat" },
prototypeType: "Intl.DurationFormat",
},
Locale: {
type: "Function",
return: { type: "Intl.Locale" },
Expand Down
3 changes: 2 additions & 1 deletion lib/util/type-checker/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type TypeName =
| "Intl.Segmenter"
| "Intl.DisplayNames"
| "Intl.Locale"
| "Intl.DurationFormat"
| "Promise"
| "RegExp"
| "String"
Expand Down Expand Up @@ -193,7 +194,7 @@ export type FinalizationRegistryPrototypeProperty = Exclude<
keyof FinalizationRegistry<any>,
ExcludePrototypeProperty
>;
export type IntlProperty = Exclude<keyof typeof Intl, ExcludeProperty>;
export type IntlProperty = Exclude<keyof typeof Intl, ExcludeProperty> | 'DurationFormat';
export type IteratorProperty = Exclude<keyof typeof Iterator, ExcludeProperty>;
export type IteratorPrototypeProperty = Exclude<
keyof IteratorObject<any>,
Expand Down
Loading
Loading