Skip to content

Add rules for JSON.parse source text access #277

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-json-israwjson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-es-x": minor
---

Add `es-x/no-json-israwjson` rule
5 changes: 5 additions & 0 deletions .changeset/no-json-parse-reviver-context-parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-es-x": minor
---

Add `es-x/no-json-parse-reviver-context-parameter` rule
5 changes: 5 additions & 0 deletions .changeset/no-json-parse-with-source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-es-x": minor
---

Add `no-json-parse-with-source` config
5 changes: 5 additions & 0 deletions .changeset/no-json-rawjson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-es-x": minor
---

Add `es-x/no-json-rawjson` rule
26 changes: 26 additions & 0 deletions docs/configs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,32 @@ export default [

</details>

## no-json-parse-with-source

disallow proposal ES2026 [JSON.parse source text access](https://github.com/tc39/proposal-json-parse-with-source)\
⚠️ This config will be changed in the minor versions of this plugin.

This configs includes rules for [es-x/no-json-israwjson](../rules/no-json-israwjson.md), [es-x/no-json-parse-reviver-context-parameter](../rules/no-json-parse-reviver-context-parameter.md), and [es-x/no-json-rawjson](../rules/no-json-rawjson.md).

```js
import pluginESx from "eslint-plugin-es-x"
export default [
pluginESx.configs['flat/no-json-parse-with-source']
]
```

<details><summary> Legacy Config </summary>

.eslintrc.*:

```json
{
"extends": ["plugin:es-x/no-json-parse-with-source"],
}
```

</details>

## no-float16array

disallow proposal ES2025 [Float16Array](https://github.com/tc39/proposal-float16array)
Expand Down
3 changes: 3 additions & 0 deletions docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
| [es-x/no-asyncdisposablestack](./no-asyncdisposablestack.md) | disallow the `AsyncDisposableStack` class. | |
| [es-x/no-disposablestack](./no-disposablestack.md) | disallow the `DisposableStack` class. | |
| [es-x/no-error-iserror](./no-error-iserror.md) | disallow the `Error.isError` method. | |
| [es-x/no-json-israwjson](./no-json-israwjson.md) | disallow the `JSON.isRawJSON` method. | |
| [es-x/no-json-parse-reviver-context-parameter](./no-json-parse-reviver-context-parameter.md) | disallow the `context` parameter in `JSON.parse` reviver function. | |
| [es-x/no-json-rawjson](./no-json-rawjson.md) | disallow the `JSON.rawJSON` method. | |
| [es-x/no-suppressederror](./no-suppressederror.md) | disallow the `SuppressedError` class. | |
| [es-x/no-symbol-asyncdispose](./no-symbol-asyncdispose.md) | disallow the `Symbol.asyncDispose` property. | |
| [es-x/no-symbol-dispose](./no-symbol-dispose.md) | disallow the `Symbol.dispose` property. | |
Expand Down
55 changes: 55 additions & 0 deletions docs/rules/no-json-israwjson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "es-x/no-json-israwjson"
description: "disallow the `JSON.isRawJSON` method"
---

# es-x/no-json-israwjson
> disallow the `JSON.isRawJSON` 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-json-parse-with-source] and [no-new-in-esnext]

This rule reports ES2026 [`JSON.isRawJSON` method](https://github.com/tc39/proposal-json-parse-with-source) as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-json-israwjson: error */
JSON.isRawJSON(object);
```

</eslint-playground>

## 🔧 Options

This rule has an option.

```jsonc
{
"rules": {
"es-x/no-json-israwjson": [
"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-json-israwjson.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-json-israwjson.js)

[no-json-parse-with-source]: ../configs/index.md#no-json-parse-with-source
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext
41 changes: 41 additions & 0 deletions docs/rules/no-json-parse-reviver-context-parameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: "es-x/no-json-parse-reviver-context-parameter"
description: "disallow the `context` parameter in `JSON.parse` reviver function"
---

# es-x/no-json-parse-reviver-context-parameter
> disallow the `context` parameter in `JSON.parse` reviver function

- ❗ <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-json-parse-with-source] and [no-new-in-esnext]

This rule reports ES2026 [`JSON.parse` resolver `context` parameter](https://github.com/tc39/proposal-json-parse-with-source) as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-json-parse-reviver-context-parameter: error */
JSON.parse(
'{"key": "value"}',
(
key,
value,
context // This is the context parameter
) => {
return value;
});
```

</eslint-playground>

## 📚 References

- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-json-parse-reviver-context-parameter.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-json-parse-reviver-context-parameter.js)

[no-json-parse-with-source]: ../configs/index.md#no-json-parse-with-source
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext
55 changes: 55 additions & 0 deletions docs/rules/no-json-rawjson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: "es-x/no-json-rawjson"
description: "disallow the `JSON.rawJSON` method"
---

# es-x/no-json-rawjson
> disallow the `JSON.rawJSON` 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-json-parse-with-source] and [no-new-in-esnext]

This rule reports ES2026 [`JSON.rawJSON` method](https://github.com/tc39/proposal-json-parse-with-source) as errors.

## 💡 Examples

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

<eslint-playground type="bad">

```js
/*eslint es-x/no-json-rawjson: error */
JSON.rawJSON('123');
```

</eslint-playground>

## 🔧 Options

This rule has an option.

```jsonc
{
"rules": {
"es-x/no-json-rawjson": [
"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-json-rawjson.js)
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-json-rawjson.js)

[no-json-parse-with-source]: ../configs/index.md#no-json-parse-with-source
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext
18 changes: 18 additions & 0 deletions lib/configs/flat/no-json-parse-with-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* DON'T EDIT THIS FILE.
* This file was generated by "scripts/update-lib-flat-configs.js" script.
*/
"use strict"

module.exports = {
plugins: {
get "es-x"() {
return require("../../index.js")
},
},
rules: {
"es-x/no-json-israwjson": "error",
"es-x/no-json-parse-reviver-context-parameter": "error",
"es-x/no-json-rawjson": "error",
},
}
3 changes: 3 additions & 0 deletions lib/configs/flat/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module.exports = {
"es-x/no-asyncdisposablestack": "error",
"es-x/no-disposablestack": "error",
"es-x/no-error-iserror": "error",
"es-x/no-json-israwjson": "error",
"es-x/no-json-parse-reviver-context-parameter": "error",
"es-x/no-json-rawjson": "error",
"es-x/no-suppressederror": "error",
"es-x/no-symbol-asyncdispose": "error",
"es-x/no-symbol-dispose": "error",
Expand Down
14 changes: 14 additions & 0 deletions lib/configs/no-json-parse-with-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* DON'T EDIT THIS FILE.
* This file was generated by "scripts/update-lib-configs.js" script.
*/
"use strict"

module.exports = {
plugins: ["es-x"],
rules: {
"es-x/no-json-israwjson": "error",
"es-x/no-json-parse-reviver-context-parameter": "error",
"es-x/no-json-rawjson": "error",
},
}
3 changes: 3 additions & 0 deletions lib/configs/no-new-in-esnext.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ module.exports = {
"es-x/no-asyncdisposablestack": "error",
"es-x/no-disposablestack": "error",
"es-x/no-error-iserror": "error",
"es-x/no-json-israwjson": "error",
"es-x/no-json-parse-reviver-context-parameter": "error",
"es-x/no-json-rawjson": "error",
"es-x/no-suppressederror": "error",
"es-x/no-symbol-asyncdispose": "error",
"es-x/no-symbol-dispose": "error",
Expand Down
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module.exports = {
"flat/no-intl-numberformat-v3": require("./configs/flat/no-intl-numberformat-v3"),
"flat/no-is-usv-string": require("./configs/flat/no-is-usv-string"),
"flat/no-iterator-helpers": require("./configs/flat/no-iterator-helpers"),
"flat/no-json-parse-with-source": require("./configs/flat/no-json-parse-with-source"),
"flat/no-new-in-es5": require("./configs/flat/no-new-in-es5"),
"flat/no-new-in-es2015": require("./configs/flat/no-new-in-es2015"),
"flat/no-new-in-es2015-intl-api": require("./configs/flat/no-new-in-es2015-intl-api"),
Expand Down Expand Up @@ -79,6 +80,7 @@ module.exports = {
"no-intl-numberformat-v3": require("./configs/no-intl-numberformat-v3"),
"no-is-usv-string": require("./configs/no-is-usv-string"),
"no-iterator-helpers": require("./configs/no-iterator-helpers"),
"no-json-parse-with-source": require("./configs/no-json-parse-with-source"),
"no-new-in-es5": require("./configs/no-new-in-es5"),
"no-new-in-es2015": require("./configs/no-new-in-es2015"),
"no-new-in-es2015-intl-api": require("./configs/no-new-in-es2015-intl-api"),
Expand Down Expand Up @@ -258,7 +260,10 @@ module.exports = {
"no-iterator-prototype-take": require("./rules/no-iterator-prototype-take"),
"no-iterator-prototype-toarray": require("./rules/no-iterator-prototype-toarray"),
"no-json": require("./rules/no-json"),
"no-json-israwjson": require("./rules/no-json-israwjson"),
"no-json-modules": require("./rules/no-json-modules"),
"no-json-parse-reviver-context-parameter": require("./rules/no-json-parse-reviver-context-parameter"),
"no-json-rawjson": require("./rules/no-json-rawjson"),
"no-json-superset": require("./rules/no-json-superset"),
"no-keyword-properties": require("./rules/no-keyword-properties"),
"no-labelled-function-declarations": require("./rules/no-labelled-function-declarations"),
Expand Down
36 changes: 36 additions & 0 deletions lib/rules/no-json-israwjson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict"

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

module.exports = {
meta: {
docs: {
description: "disallow the `JSON.isRawJSON` method",
category: "ES2026",
proposal: "json-parse-with-source",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-json-israwjson.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, {
JSON: { isRawJSON: "function" },
})
},
}
54 changes: 54 additions & 0 deletions lib/rules/no-json-parse-reviver-context-parameter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"use strict"

const { ReferenceTracker, CALL } = require("@eslint-community/eslint-utils")

module.exports = {
meta: {
docs: {
description:
"disallow the `context` parameter in `JSON.parse` reviver function",
category: "ES2026",
proposal: "json-parse-with-source",
recommended: false,
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-json-parse-reviver-context-parameter.html",
},
fixable: null,
messages: {
forbidden:
"Unexpected context parameter in JSON.parse reviver function.",
},
schema: [],
type: "problem",
},
create(context) {
const sourceCode = context.sourceCode
return {
"Program:exit"(program) {
const tracker = new ReferenceTracker(
sourceCode.getScope(program),
)
for (const { node } of tracker.iterateGlobalReferences({
JSON: { parse: { [CALL]: true } },
})) {
if (node.type !== "CallExpression") {
continue
}
const reviver = node.arguments[1]
if (
!reviver ||
(reviver.type !== "FunctionExpression" &&
reviver.type !== "ArrowFunctionExpression")
) {
continue
}
if (reviver.params.length >= 3) {
context.report({
node: reviver.params[2],
messageId: "forbidden",
})
}
}
},
}
},
}
Loading
Loading