Skip to content

Commit d281f86

Browse files
committed
refactor: remove duplicate code
1 parent 707cdeb commit d281f86

File tree

10 files changed

+348
-362
lines changed

10 files changed

+348
-362
lines changed

.pkgs/eslint-plugin-local/dist/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import * as _typescript_eslint_utils_ts_eslint from '@typescript-eslint/utils/ts-eslint';
22

3+
type MessageID = "unexpectedComparison" | "useLooseComparisonSuggestion";
4+
35
declare const _default: {
46
readonly meta: {
57
readonly name: string;
@@ -8,7 +10,7 @@ declare const _default: {
810
readonly rules: {
911
readonly "avoid-multiline-template-expression": _typescript_eslint_utils_ts_eslint.RuleModule<"avoidMultilineTemplateExpression", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
1012
readonly "no-shadow-underscore": _typescript_eslint_utils_ts_eslint.RuleModule<"noShadowUnderscore", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
11-
readonly "prefer-eqeq-nullish-comparison": _typescript_eslint_utils_ts_eslint.RuleModule<"unexpectedComparison" | "useLooseComparisonSuggestion", [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
13+
readonly "prefer-eqeq-nullish-comparison": _typescript_eslint_utils_ts_eslint.RuleModule<MessageID, [], unknown, _typescript_eslint_utils_ts_eslint.RuleListener>;
1214
};
1315
};
1416

.pkgs/eslint-plugin-local/dist/index.js

Lines changed: 80 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,21 @@ var avoid_multiline_template_expression_default = createRule({
6363
schema: []
6464
},
6565
name: RULE_NAME,
66-
create(context) {
67-
return {
68-
TemplateLiteral: (node) => {
69-
if (AST.isMultiLine(node)) {
70-
context.report({
71-
messageId: "avoidMultilineTemplateExpression",
72-
node
73-
});
74-
}
75-
}
76-
};
77-
},
66+
create,
7867
defaultOptions: []
7968
});
69+
function create(context) {
70+
return {
71+
TemplateLiteral: (node) => {
72+
if (AST.isMultiLine(node)) {
73+
context.report({
74+
messageId: "avoidMultilineTemplateExpression",
75+
node
76+
});
77+
}
78+
}
79+
};
80+
}
8081

8182
// src/rules/no-shadow-underscore.ts
8283
var RULE_NAME2 = "no-shadow-underscore";
@@ -94,22 +95,23 @@ var no_shadow_underscore_default = createRule({
9495
schema: []
9596
},
9697
name: RULE_NAME2,
97-
create(context) {
98-
return {
99-
"Identifier[name='_']"(node) {
100-
const initialScope = context.sourceCode.getScope(node);
101-
const isFromImport = isInitializedFromSource("_", "@eslint-react/eff", initialScope);
102-
if (!isFromImport) {
103-
context.report({
104-
messageId: "noShadowUnderscore",
105-
node
106-
});
107-
}
108-
}
109-
};
110-
},
98+
create: create2,
11199
defaultOptions: []
112100
});
101+
function create2(context) {
102+
return {
103+
"Identifier[name='_']"(node) {
104+
const initialScope = context.sourceCode.getScope(node);
105+
const isFromImport = isInitializedFromSource("_", "@eslint-react/eff", initialScope);
106+
if (!isFromImport) {
107+
context.report({
108+
messageId: "noShadowUnderscore",
109+
node
110+
});
111+
}
112+
}
113+
};
114+
}
113115
var RULE_NAME3 = "prefer-eqeq-nullish-comparison";
114116
var prefer_eqeq_nullish_comparison_default = createRule({
115117
meta: {
@@ -126,59 +128,60 @@ var prefer_eqeq_nullish_comparison_default = createRule({
126128
schema: []
127129
},
128130
name: RULE_NAME3,
129-
create(context) {
130-
return {
131-
BinaryExpression(node) {
132-
if (node.operator === "===" || node.operator === "!==") {
133-
const offendingChild = [node.left, node.right].find(
134-
(child) => child.type === AST_NODE_TYPES.Identifier && child.name === "undefined" || child.type === AST_NODE_TYPES.Literal && child.raw === "null"
135-
);
136-
if (offendingChild == null) {
137-
return;
138-
}
139-
const operatorToken = nullThrows(
140-
context.sourceCode.getFirstTokenBetween(
141-
node.left,
142-
node.right,
143-
(token) => token.value === node.operator
144-
),
145-
NullThrowsReasons.MissingToken(node.operator, "binary expression")
146-
);
147-
const wasLeft = node.left === offendingChild;
148-
const nullishKind = offendingChild.type === AST_NODE_TYPES.Identifier ? "undefined" : "null";
149-
const looseOperator = node.operator === "===" ? "==" : "!=";
150-
context.report({
151-
messageId: "unexpectedComparison",
152-
data: {
153-
nullishKind,
154-
strictOperator: node.operator
155-
},
156-
loc: wasLeft ? {
157-
end: operatorToken.loc.end,
158-
start: node.left.loc.start
159-
} : {
160-
end: node.right.loc.end,
161-
start: operatorToken.loc.start
162-
},
163-
suggest: [
164-
{
165-
messageId: "useLooseComparisonSuggestion",
166-
data: {
167-
looseOperator
168-
},
169-
fix: (fixer) => [
170-
fixer.replaceText(offendingChild, "null"),
171-
fixer.replaceText(operatorToken, looseOperator)
172-
]
173-
}
174-
]
175-
});
176-
}
177-
}
178-
};
179-
},
131+
create: create3,
180132
defaultOptions: []
181133
});
134+
function create3(context) {
135+
return {
136+
BinaryExpression(node) {
137+
if (node.operator === "===" || node.operator === "!==") {
138+
const offendingChild = [node.left, node.right].find(
139+
(child) => child.type === AST_NODE_TYPES.Identifier && child.name === "undefined" || child.type === AST_NODE_TYPES.Literal && child.raw === "null"
140+
);
141+
if (offendingChild == null) {
142+
return;
143+
}
144+
const operatorToken = nullThrows(
145+
context.sourceCode.getFirstTokenBetween(
146+
node.left,
147+
node.right,
148+
(token) => token.value === node.operator
149+
),
150+
NullThrowsReasons.MissingToken(node.operator, "binary expression")
151+
);
152+
const wasLeft = node.left === offendingChild;
153+
const nullishKind = offendingChild.type === AST_NODE_TYPES.Identifier ? "undefined" : "null";
154+
const looseOperator = node.operator === "===" ? "==" : "!=";
155+
context.report({
156+
messageId: "unexpectedComparison",
157+
data: {
158+
nullishKind,
159+
strictOperator: node.operator
160+
},
161+
loc: wasLeft ? {
162+
end: operatorToken.loc.end,
163+
start: node.left.loc.start
164+
} : {
165+
end: node.right.loc.end,
166+
start: operatorToken.loc.start
167+
},
168+
suggest: [
169+
{
170+
messageId: "useLooseComparisonSuggestion",
171+
data: {
172+
looseOperator
173+
},
174+
fix: (fixer) => [
175+
fixer.replaceText(offendingChild, "null"),
176+
fixer.replaceText(operatorToken, looseOperator)
177+
]
178+
}
179+
]
180+
});
181+
}
182+
}
183+
};
184+
}
182185

183186
// src/index.ts
184187
var index_default = {

.pkgs/eslint-plugin-local/src/rules/avoid-multiline-template-expression.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as AST from "@eslint-react/ast";
2-
import type { RuleFeature } from "@eslint-react/shared";
2+
import type { RuleContext, RuleFeature } from "@eslint-react/shared";
3+
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
34
import type { CamelCase } from "string-ts";
45

56
import { createRule } from "../utils";
@@ -23,17 +24,19 @@ export default createRule<[], MessageID>({
2324
schema: [],
2425
},
2526
name: RULE_NAME,
26-
create(context) {
27-
return {
28-
TemplateLiteral: (node) => {
29-
if (AST.isMultiLine(node)) {
30-
context.report({
31-
messageId: "avoidMultilineTemplateExpression",
32-
node,
33-
});
34-
}
35-
},
36-
};
37-
},
27+
create,
3828
defaultOptions: [],
3929
});
30+
31+
export function create(context: RuleContext<MessageID, []>): RuleListener {
32+
return {
33+
TemplateLiteral: (node) => {
34+
if (AST.isMultiLine(node)) {
35+
context.report({
36+
messageId: "avoidMultilineTemplateExpression",
37+
node,
38+
});
39+
}
40+
},
41+
};
42+
}
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import type { RuleFeature } from "@eslint-react/shared";
1+
import type { RuleContext, RuleFeature } from "@eslint-react/shared";
22
import type { TSESTree } from "@typescript-eslint/types";
33
import type { CamelCase } from "string-ts";
44

55
import { createRule, isInitializedFromSource } from "../utils";
6+
import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
67

78
export const RULE_NAME = "no-shadow-underscore";
89

@@ -23,19 +24,21 @@ export default createRule<[], MessageID>({
2324
schema: [],
2425
},
2526
name: RULE_NAME,
26-
create(context) {
27-
return {
28-
"Identifier[name='_']"(node: TSESTree.Identifier & { name: "_" }) {
29-
const initialScope = context.sourceCode.getScope(node);
30-
const isFromImport = isInitializedFromSource("_", "@eslint-react/eff", initialScope);
31-
if (!isFromImport) {
32-
context.report({
33-
messageId: "noShadowUnderscore",
34-
node,
35-
});
36-
}
37-
},
38-
};
39-
},
27+
create,
4028
defaultOptions: [],
4129
});
30+
31+
export function create(context: RuleContext<MessageID, []>): RuleListener {
32+
return {
33+
"Identifier[name='_']"(node: TSESTree.Identifier & { name: "_" }) {
34+
const initialScope = context.sourceCode.getScope(node);
35+
const isFromImport = isInitializedFromSource("_", "@eslint-react/eff", initialScope);
36+
if (!isFromImport) {
37+
context.report({
38+
messageId: "noShadowUnderscore",
39+
node,
40+
});
41+
}
42+
},
43+
};
44+
}

0 commit comments

Comments
 (0)