Skip to content

Commit 6139683

Browse files
committed
fix: Disallow null in CreateFilterParams["context"]
1 parent 3224735 commit 6139683

7 files changed

Lines changed: 61 additions & 130 deletions

File tree

eslint.config.js

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import tseslint from "typescript-eslint";
88

99
import allowNullInOptionalParameter from "./eslint/allow-null-in-optional-parameter.js";
1010
import namingConvention from "./eslint/naming-convention.js";
11-
import noOptional from "./eslint/no-optional.js";
1211

1312
export default tseslint.config(
1413
eslint.configs.recommended,
@@ -33,7 +32,6 @@ export default tseslint.config(
3332
masto: {
3433
rules: {
3534
"allow-null-in-optional-parameter": allowNullInOptionalParameter,
36-
"no-optional": noOptional,
3735
"naming-convention": namingConvention,
3836
},
3937
},
@@ -78,19 +76,13 @@ export default tseslint.config(
7876
},
7977
},
8078
},
81-
// {
82-
// files: ["src/mastodon/{rest,oauth,streaming}/**/*.ts"],
83-
// rules: {
84-
// "masto/naming-convention": ["warn"],
85-
// "masto/allow-null-in-optional-parameter": ["warn"],
86-
// },
87-
// },
88-
// {
89-
// files: ["src/mastodon/entities/**/*.ts"],
90-
// rules: {
91-
// "masto/no-optional": ["warn"],
92-
// },
93-
// },
79+
{
80+
files: ["src/mastodon/{rest,oauth,streaming}/**/*.ts"],
81+
rules: {
82+
// "masto/naming-convention": ["warn"],
83+
"masto/allow-null-in-optional-parameter": ["warn"],
84+
},
85+
},
9486
{
9587
files: [
9688
"vitest.config.ts",

eslint/allow-null-in-optional-parameter.js

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,48 @@ export default {
44
fixable: "code",
55
schema: [],
66
messages: {
7-
default: "Always use `T | null` for optional property",
7+
optionalWithoutNull: "Always use `T | null` for optional property",
8+
nullWithoutOptional: "Mark nullable parameter as optional",
89
},
910
},
1011

1112
create(context) {
1213
return {
1314
TSPropertySignature(node) {
14-
if (!node.optional) {
15-
return;
16-
}
17-
18-
if (node.typeAnnotation.type !== "TSTypeAnnotation") {
19-
return;
20-
}
15+
if (node.optional) {
16+
if (node.typeAnnotation.type !== "TSTypeAnnotation") {
17+
return;
18+
}
2119

22-
if (
23-
node.typeAnnotation.typeAnnotation.type !== "TSUnionType" ||
24-
node.typeAnnotation.typeAnnotation.types.every(
25-
(type) => type.type !== "TSNullKeyword",
26-
)
27-
) {
28-
context.report({
29-
node,
30-
messageId: "default",
31-
fix: function* (fixer) {
32-
yield fixer.insertTextAfter(node.typeAnnotation, " | null");
33-
},
34-
});
20+
if (
21+
node.typeAnnotation.typeAnnotation.type !== "TSUnionType" ||
22+
node.typeAnnotation.typeAnnotation.types.every(
23+
(type) => type.type !== "TSNullKeyword",
24+
)
25+
) {
26+
context.report({
27+
node,
28+
messageId: "optionalWithoutNull",
29+
fix: function* (fixer) {
30+
yield fixer.insertTextAfter(node.typeAnnotation, " | null");
31+
},
32+
});
33+
}
34+
} else {
35+
if (
36+
node.typeAnnotation.typeAnnotation.type === "TSUnionType" &&
37+
node.typeAnnotation.typeAnnotation.types.some(
38+
(type) => type.type === "TSNullKeyword",
39+
)
40+
) {
41+
context.report({
42+
node,
43+
messageId: "nullWithoutOptional",
44+
fix: function* (fixer) {
45+
yield fixer.insertTextAfter(node.key, "?");
46+
},
47+
});
48+
}
3549
}
3650
},
3751
};

eslint/allow-null-in-optional-parameter.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,22 @@ ruleTester.run("allow-null-in-optional-parameter", rule, {
3030
}
3131
`,
3232
},
33+
{
34+
code: `
35+
interface CreateAccountParams {
36+
email: string | null;
37+
}
38+
`,
39+
errors: [
40+
{
41+
message: "Mark nullable parameter as optional",
42+
},
43+
],
44+
output: `
45+
interface CreateAccountParams {
46+
email?: string | null;
47+
}
48+
`,
49+
},
3350
],
3451
});

eslint/no-optional.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

eslint/no-optional.spec.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/mastodon/rest/v1/filters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export interface CreateFilterParams {
1010
* Array of enumerable strings `home`, `notifications`, `public`, `thread`.
1111
* At least one context must be specified.
1212
*/
13-
readonly context: readonly FilterContext[] | null;
13+
readonly context: readonly FilterContext[];
1414
/** Should the server irreversibly drop matching entities from home and notifications? */
1515
readonly irreversible?: boolean | null;
1616
/** Consider word boundaries? */

src/mastodon/rest/v2/filters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface CreateFilterParams {
1515
/** String. The name of the filter group. */
1616
readonly title: string;
1717
/** Array of String. Where the filter should be applied. Specify at least one of home, notifications, public, thread, account. */
18-
readonly context: readonly FilterContext[] | null;
18+
readonly context: readonly FilterContext[];
1919
/** String. The policy to be applied when the filter is matched. Specify warn or hide. */
2020
readonly filterAction?: FilterAction | null;
2121
/** Integer. How many seconds from now should the filter expire? */

0 commit comments

Comments
 (0)