-
Notifications
You must be signed in to change notification settings - Fork 444
eslint-factory: extend no-json-stringify-error to cover .then(_, onRejected) and pin scope boundaries #43724
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
Changes from all commits
182d65e
ad87461
eea6eb7
befce33
f6e9018
c916cb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,6 +183,91 @@ describe("no-json-stringify-error", () => { | |
| }); | ||
| }); | ||
|
|
||
| it("valid: p.catch(handler) named-reference is not flagged", () => { | ||
| cjsRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, { | ||
| valid: [`const handler = err => JSON.stringify(err); p.catch(handler);`], | ||
| invalid: [], | ||
| }); | ||
| }); | ||
|
|
||
| it("valid: p.then(ok, handler) named-reference rejection handler is not flagged", () => { | ||
| cjsRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, { | ||
| valid: [`function onRejected(err) { JSON.stringify(err); } p.then(ok, onRejected);`], | ||
| invalid: [], | ||
| }); | ||
| }); | ||
|
|
||
| it("invalid: JSON.stringify(err) in .then() second-argument rejection handler (arrow) is flagged", () => { | ||
| cjsRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, { | ||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `p.then(result => result, err => core.error(JSON.stringify(err)));`, | ||
| errors: [ | ||
| { | ||
| messageId: "jsonStringifyError", | ||
| data: { errorVar: "err" }, | ||
| suggestions: [ | ||
| { | ||
| messageId: "useGetErrorMessage", | ||
| data: { errorVar: "err" }, | ||
| output: `p.then(result => result, err => core.error(getErrorMessage(err)));`, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: `p.then(null, err => core.error(JSON.stringify(err, null, 2)));`, | ||
| errors: [ | ||
| { | ||
| messageId: "jsonStringifyError", | ||
| data: { errorVar: "err" }, | ||
| suggestions: [ | ||
| { | ||
| messageId: "useGetErrorMessage", | ||
| data: { errorVar: "err" }, | ||
| output: `p.then(null, err => core.error(getErrorMessage(err)));`, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("invalid: JSON.stringify(err) in .then() second-argument rejection handler (function) is flagged", () => { | ||
| cjsRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing test: 💡 Suggested fixAdd a case inside the existing {
code: `p.then(null, err => core.error(JSON.stringify(err, null, 2)));`,
errors: [{
messageId: "jsonStringifyError",
data: { errorVar: "err" },
suggestions: [{ messageId: "useGetErrorMessage", data: { errorVar: "err" }, output: `p.then(null, err => core.error(getErrorMessage(err)));` }],
}],
},The rule targets |
||
| valid: [], | ||
| invalid: [ | ||
| { | ||
| code: `p.then(null, function(err) { core.error(JSON.stringify(err)); });`, | ||
| errors: [ | ||
| { | ||
| messageId: "jsonStringifyError", | ||
| data: { errorVar: "err" }, | ||
| suggestions: [ | ||
| { | ||
| messageId: "useGetErrorMessage", | ||
| data: { errorVar: "err" }, | ||
| output: `p.then(null, function(err) { core.error(getErrorMessage(err)); });`, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
| it("valid: first argument of .then() (onFulfilled) is not treated as a rejection handler", () => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/tdd] The 💡 Suggested testit("valid: p.then(err => JSON.stringify(err)) — first argument is onFulfilled, not flagged", () => {
cjsRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, {
valid: [`p.then(err => core.info(JSON.stringify(err)));`],
invalid: [],
});
});Without this, a future refactor that accidentally checks argument index @copilot please address this. |
||
| cjsRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, { | ||
| valid: [`p.then(result => JSON.stringify(result));`, `p.then(function(result) { JSON.stringify(result); });`, `p.then(err => core.info(JSON.stringify(err)));`], | ||
| invalid: [], | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing ESM-mode test for 💡 Suggested fixAdd an ESM invalid case after the existing CJS tests: it("invalid: JSON.stringify(err) in .then() rejection handler is flagged (ESM)", () => {
esmRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, {
valid: [],
invalid: [
{
code: `p.then(ok, err => core.error(JSON.stringify(err)));`,
errors: [{ messageId: "jsonStringifyError", data: { errorVar: "err" }, suggestions: [{ messageId: "useGetErrorMessage", data: { errorVar: "err" }, output: `p.then(ok, err => core.error(getErrorMessage(err)));` }] }],
},
],
});
});Parser configuration differences between module and script mode can affect identifier binding. The suite already has |
||
| }); | ||
|
|
||
| it("invalid: works with ES module syntax", () => { | ||
| esmRuleTester.run("no-json-stringify-error", noJsonStringifyErrorRule, { | ||
| valid: [], | ||
|
|
@@ -203,6 +288,22 @@ describe("no-json-stringify-error", () => { | |
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: `p.then(ok, err => console.error(JSON.stringify(err)));`, | ||
| errors: [ | ||
| { | ||
| messageId: "jsonStringifyError", | ||
| data: { errorVar: "err" }, | ||
| suggestions: [ | ||
| { | ||
| messageId: "useGetErrorMessage", | ||
| data: { errorVar: "err" }, | ||
| output: `p.then(ok, err => console.error(getErrorMessage(err)));`, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd] The two
.then()invalid tests usecjsRuleTesteronly — there is no ESM-mode counterpart, unlike the existingtry/catchand.catch()tests which exercise both testers. If the ESM parser resolves method calls differently (e.g., in strict mode modules) the gap would go undetected.💡 Suggested addition
Add a minimal ESM-mode invalid test alongside the existing
it("invalid: works with ES module syntax")block:@copilot please address this.