Skip to content

Commit 5f1a721

Browse files
committed
Update local function rules
1 parent 41d9b16 commit 5f1a721

File tree

5 files changed

+76
-78
lines changed

5 files changed

+76
-78
lines changed

.pkgs/function-rules/rules/nullish-comparison.ts

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,59 @@ export interface nullishComparisonOptions {
99

1010
// TODO: Implement different enforce options
1111
export function nullishComparison(options?: nullishComparisonOptions) {
12-
return (context: Rule.RuleContext) =>
13-
defineRuleListener({
14-
BinaryExpression(node): void {
15-
if (node.operator === "===" || node.operator === "!==") {
16-
const offendingChild = [node.left, node.right].find(
17-
(child) =>
18-
(child.type === "Identifier"
19-
&& child.name === "undefined")
20-
|| (child.type === "Literal" && child.raw === "null"),
21-
);
12+
return (context: Rule.RuleContext): Rule.RuleListener => ({
13+
BinaryExpression(node): void {
14+
if (node.operator === "===" || node.operator === "!==") {
15+
const offendingChild = [node.left, node.right].find(
16+
(child) =>
17+
(child.type === "Identifier"
18+
&& child.name === "undefined")
19+
|| (child.type === "Literal" && child.raw === "null"),
20+
);
2221

23-
if (offendingChild == null) {
24-
return;
25-
}
22+
if (offendingChild == null) {
23+
return;
24+
}
2625

27-
const operatorToken = context.sourceCode.getFirstTokenBetween(
28-
node.left,
29-
node.right,
30-
(token) => token.value === node.operator,
31-
);
26+
const operatorToken = context.sourceCode.getFirstTokenBetween(
27+
node.left,
28+
node.right,
29+
(token) => token.value === node.operator,
30+
);
3231

33-
if (operatorToken == null) throw new Error("Can't get operator token");
32+
if (operatorToken == null) throw new Error("Can't get operator token");
3433

35-
const wasLeft = node.left === offendingChild;
34+
const wasLeft = node.left === offendingChild;
3635

37-
const nullishKind = offendingChild.type === "Identifier"
38-
? "undefined"
39-
: "null";
36+
const nullishKind = offendingChild.type === "Identifier"
37+
? "undefined"
38+
: "null";
4039

41-
const looseOperator = node.operator === "===" ? "==" : "!=";
40+
const looseOperator = node.operator === "===" ? "==" : "!=";
4241

43-
context.report({
44-
loc: wasLeft
45-
? {
46-
end: operatorToken.loc.end,
47-
start: node.left.loc!.start,
48-
}
49-
: {
50-
end: node.right.loc!.end,
51-
start: operatorToken.loc.start,
52-
},
53-
message:
54-
`Unexpected strict comparison ('${node.operator}') with '${nullishKind}'. In this codebase, we prefer to use loose equality as a general-purpose nullish check when possible.`,
55-
suggest: [
56-
{
57-
desc: `Use loose comparison ('${looseOperator} null') instead, to check both nullish values.`,
58-
fix: (fixer) => [
59-
fixer.replaceText(offendingChild, "null"),
60-
fixer.replaceText(operatorToken, looseOperator),
61-
],
62-
},
63-
],
64-
});
65-
}
66-
},
67-
});
42+
context.report({
43+
loc: wasLeft
44+
? {
45+
end: operatorToken.loc.end,
46+
start: node.left.loc!.start,
47+
}
48+
: {
49+
end: node.right.loc!.end,
50+
start: operatorToken.loc.start,
51+
},
52+
message:
53+
`Unexpected strict comparison ('${node.operator}') with '${nullishKind}'. In this codebase, we prefer to use loose equality as a general-purpose nullish check when possible.`,
54+
suggest: [
55+
{
56+
desc: `Use loose comparison ('${looseOperator} null') instead, to check both nullish values.`,
57+
fix: (fixer) => [
58+
fixer.replaceText(offendingChild, "null"),
59+
fixer.replaceText(operatorToken, looseOperator),
60+
],
61+
},
62+
],
63+
});
64+
}
65+
},
66+
});
6867
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import type { Rule } from "eslint";
2-
import { defineRuleListener } from "eslint-plugin-function-rule";
32

43
export interface templateExpressionOptions {
54
allowMultiline: boolean;
65
}
76

87
export function templateExpression(options?: templateExpressionOptions) {
98
const allowMultiline = options?.allowMultiline ?? false;
10-
return (context: Rule.RuleContext) =>
11-
defineRuleListener({
12-
TemplateLiteral(node) {
13-
if (!allowMultiline && node.loc?.start.line !== node.loc?.end.line) {
14-
context.report({
15-
node,
16-
message: "Avoid multiline template expressions.",
17-
});
18-
}
19-
},
20-
});
9+
return (context: Rule.RuleContext): Rule.RuleListener => ({
10+
TemplateLiteral(node) {
11+
if (!allowMultiline && node.loc?.start.line !== node.loc?.end.line) {
12+
context.report({
13+
node,
14+
message: "Avoid multiline template expressions.",
15+
});
16+
}
17+
},
18+
});
2119
}

eslint.config.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,17 @@ export default defineConfig([
5555
plugins: {
5656
"function-rule-1": defineRule(templateExpression()),
5757
"function-rule-2": defineRule(nullishComparison()),
58-
// custom: defineRule((context) => ({
59-
// TemplateLiteral(node) {
60-
// if (node.loc?.start.line !== node.loc?.end.line) {
61-
// context.report({
62-
// node,
63-
// message: "Avoid multiline template expressions.",
64-
// });
65-
// }
66-
// },
67-
// })),
58+
59+
custom: defineRule((context) => ({
60+
CallExpression(node) {
61+
if (context.sourceCode.getText(node.callee) === "Date.now") {
62+
context.report({
63+
node,
64+
message: "Don't use 'Date.now()'.",
65+
});
66+
}
67+
},
68+
})),
6869
},
6970
rules: {
7071
"fast-import/no-unused-exports": "off",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"update:website": "tsx ./scripts/update-website.ts"
5353
},
5454
"devDependencies": {
55-
"@effect/language-service": "^0.55.2",
55+
"@effect/language-service": "^0.55.3",
5656
"@effect/platform": "^0.93.0",
5757
"@effect/platform-node": "^0.100.0",
5858
"@eslint/compat": "^1.4.1",

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)