@yasainet/eslint 0.0.69 → 0.0.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yasainet/eslint",
3
- "version": "0.0.69",
3
+ "version": "0.0.71",
4
4
  "description": "ESLint",
5
5
  "type": "module",
6
6
  "exports": {
@@ -56,6 +56,16 @@ const LATERAL_PATTERNS = {
56
56
  message:
57
57
  "entries cannot import other feature's entries (lateral violation)",
58
58
  },
59
+ {
60
+ group: [
61
+ "@/features/*/services/*",
62
+ "@/features/*/services",
63
+ "!@/features/shared/services/*",
64
+ "!@/features/shared/services",
65
+ ],
66
+ message:
67
+ "entries cannot import other feature's services. Use the same feature's service (1:1) or move orchestration into the service layer. `shared/services/*` is exempt for cross-cutting side effects (notifications etc.).",
68
+ },
59
69
  ],
60
70
  };
61
71
 
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Enforce 1:1 entry-to-service mapping for `**\/entries/*.ts` exports.
3
+ *
4
+ * Why: services are the orchestration layer (they may combine multiple queries
5
+ * and other features' queries). entries should be a thin wrapper that calls a
6
+ * single service function and normalizes the return shape into
7
+ * `{ data, error }`. If an entry calls more than one service, orchestration is
8
+ * leaking up into the entry layer.
9
+ *
10
+ * Detection rule:
11
+ *
12
+ * - For every exported async `FunctionDeclaration` in an entries file, count
13
+ * `CallExpression`s whose callee is a `MemberExpression` of the form
14
+ * `<binding>.<method>(...)` where `<binding>` matches the namespace import
15
+ * naming convention `*Service` (e.g. `articlesServerService`,
16
+ * `usersClientService`).
17
+ * - More than one such call inside the same exported function is an error.
18
+ *
19
+ * Exception (C-3):
20
+ *
21
+ * - Bindings starting with `shared` (e.g. `sharedDiscordService`,
22
+ * `sharedResendService`) are EXCLUDED from the count. These represent
23
+ * cross-cutting side-effect abstractions (Discord / Resend / Slack
24
+ * notifications) that don't fit the entry-service 1:1 model and are allowed
25
+ * to be invoked from entries directly.
26
+ *
27
+ * The rule reports the 2nd and later violations (the 1st call is permitted),
28
+ * so the fix surface is the redundant calls.
29
+ */
30
+
31
+ const SERVICE_BINDING_REGEX = /Service$/;
32
+
33
+ function isServiceCall(node) {
34
+ if (node?.type !== "CallExpression") return false;
35
+ const callee = node.callee;
36
+ if (callee.type !== "MemberExpression") return false;
37
+ const obj = callee.object;
38
+ if (obj.type !== "Identifier") return false;
39
+ if (!SERVICE_BINDING_REGEX.test(obj.name)) return false;
40
+ if (obj.name.startsWith("shared")) return false;
41
+ return true;
42
+ }
43
+
44
+ function collectServiceCalls(root) {
45
+ const calls = [];
46
+ function visit(node) {
47
+ if (!node || typeof node !== "object") return;
48
+ if (isServiceCall(node)) calls.push(node);
49
+ for (const key of Object.keys(node)) {
50
+ if (key === "parent" || key === "loc" || key === "range") continue;
51
+ const value = node[key];
52
+ if (Array.isArray(value)) {
53
+ for (const item of value) visit(item);
54
+ } else if (value && typeof value === "object" && "type" in value) {
55
+ visit(value);
56
+ }
57
+ }
58
+ }
59
+ visit(root);
60
+ return calls;
61
+ }
62
+
63
+ export const entrySingleServiceCallRule = {
64
+ meta: {
65
+ type: "problem",
66
+ docs: {
67
+ description:
68
+ "Enforce entries call at most one (non-shared) feature service per exported function.",
69
+ },
70
+ messages: {
71
+ multipleServiceCalls:
72
+ "entry '{{ funcName }}' calls more than one feature service ({{ count }} total). entries must be a thin wrapper that calls a single service. Move orchestration into the service layer. `shared/services/*` (e.g. `sharedDiscordService`) is exempt.",
73
+ },
74
+ schema: [],
75
+ },
76
+ create(context) {
77
+ return {
78
+ ExportNamedDeclaration(node) {
79
+ if (!node.declaration) return;
80
+ const decl = node.declaration;
81
+ if (decl.type !== "FunctionDeclaration") return;
82
+ if (!decl.async) return;
83
+ if (!decl.id) return;
84
+ const funcName = decl.id.name;
85
+ const calls = collectServiceCalls(decl.body);
86
+ if (calls.length <= 1) return;
87
+ for (let i = 1; i < calls.length; i++) {
88
+ context.report({
89
+ node: calls[i],
90
+ messageId: "multipleServiceCalls",
91
+ data: { funcName, count: String(calls.length) },
92
+ });
93
+ }
94
+ },
95
+ };
96
+ },
97
+ };
@@ -0,0 +1,213 @@
1
+ /**
2
+ * Enforce the canonical `{ data, error: { message } }` shape for FormState types.
3
+ *
4
+ * Targets `TSInterfaceDeclaration` and `TSTypeAliasDeclaration` whose name ends
5
+ * with `FormState`. Required shape:
6
+ *
7
+ * ```ts
8
+ * interface XxxFormState {
9
+ * data: T | null; // T may be any type; `data: null` literal also OK
10
+ * error: { message: string } | null;
11
+ * }
12
+ * ```
13
+ *
14
+ * Why this shape:
15
+ *
16
+ * - Single uniform shape lets entry / hook / UI be templated and lint-checked.
17
+ * - Forbids `code` / `status` / similar discriminator fields that tend to grow
18
+ * defensive branches (e.g. `error.code === "unauthenticated"`) which usually
19
+ * indicate a missing upstream guard (route gate, button conditional render),
20
+ * not a legitimate need for in-action discrimination.
21
+ * - Discriminated unions (`{ data: T; error: null } | { data: null; error: ... }`)
22
+ * are rejected for the same reason — they leak shape complexity into hooks
23
+ * (`useActionState<T | null>`), components (`state?.error`), and entries.
24
+ *
25
+ * Future extension when Stripe / payment libraries actually need `code` / `status`:
26
+ *
27
+ * - a) Add a `// allow-error-code: <reason>` comment opt-out to this rule, so
28
+ * individual FormStates can justify their extra field on the previous line.
29
+ * - b) Change the rule to whitelist a fixed set of allowed extra fields (enum
30
+ * of standard error codes shared across the codebase).
31
+ *
32
+ * Until that need materializes, this rule is strict (no opt-out) per YAGNI.
33
+ */
34
+
35
+ const FORM_STATE_SUFFIX = "FormState";
36
+
37
+ function isNullLiteralType(node) {
38
+ return node?.type === "TSNullKeyword";
39
+ }
40
+
41
+ function isStringType(node) {
42
+ return node?.type === "TSStringKeyword";
43
+ }
44
+
45
+ function isUnionContainingNull(node) {
46
+ if (node?.type !== "TSUnionType") return false;
47
+ return node.types.some(isNullLiteralType);
48
+ }
49
+
50
+ function getNonNullUnionMembers(unionNode) {
51
+ return unionNode.types.filter((t) => !isNullLiteralType(t));
52
+ }
53
+
54
+ function dataTypeAllowsNull(node) {
55
+ if (!node) return false;
56
+ if (isNullLiteralType(node)) return true;
57
+ return isUnionContainingNull(node);
58
+ }
59
+
60
+ function getErrorObjectExtraFields(node) {
61
+ if (node?.type !== "TSTypeLiteral") return [];
62
+ return node.members
63
+ .filter((m) => m.type === "TSPropertySignature")
64
+ .filter((m) => m.key?.type === "Identifier" && m.key.name !== "message")
65
+ .map((m) => m.key.name);
66
+ }
67
+
68
+ function isErrorMessageOnlyShape(node) {
69
+ if (node?.type !== "TSTypeLiteral") return false;
70
+ if (node.members.length !== 1) return false;
71
+ const member = node.members[0];
72
+ if (member.type !== "TSPropertySignature") return false;
73
+ if (member.key?.type !== "Identifier" || member.key.name !== "message") return false;
74
+ const typeAnn = member.typeAnnotation?.typeAnnotation;
75
+ return isStringType(typeAnn);
76
+ }
77
+
78
+ function checkBody(context, idNode, name, members) {
79
+ const props = new Map();
80
+ for (const m of members) {
81
+ if (m.type !== "TSPropertySignature") continue;
82
+ if (m.key?.type !== "Identifier") continue;
83
+ props.set(m.key.name, m);
84
+ }
85
+
86
+ const dataProp = props.get("data");
87
+ if (!dataProp) {
88
+ context.report({
89
+ node: idNode,
90
+ messageId: "dataMissing",
91
+ data: { name },
92
+ });
93
+ } else {
94
+ const dataType = dataProp.typeAnnotation?.typeAnnotation;
95
+ if (!dataTypeAllowsNull(dataType)) {
96
+ context.report({
97
+ node: dataProp,
98
+ messageId: "dataNotNullable",
99
+ data: { name },
100
+ });
101
+ }
102
+ }
103
+
104
+ const errorProp = props.get("error");
105
+ if (!errorProp) {
106
+ context.report({
107
+ node: idNode,
108
+ messageId: "errorMissing",
109
+ data: { name },
110
+ });
111
+ } else {
112
+ const errorType = errorProp.typeAnnotation?.typeAnnotation;
113
+ if (!isUnionContainingNull(errorType)) {
114
+ context.report({
115
+ node: errorProp,
116
+ messageId: "errorNotNullable",
117
+ data: { name },
118
+ });
119
+ } else {
120
+ const nonNull = getNonNullUnionMembers(errorType);
121
+ if (nonNull.length !== 1) {
122
+ context.report({
123
+ node: errorProp,
124
+ messageId: "errorWrongShape",
125
+ data: { name },
126
+ });
127
+ } else {
128
+ const errorObj = nonNull[0];
129
+ if (!isErrorMessageOnlyShape(errorObj)) {
130
+ const extras = getErrorObjectExtraFields(errorObj);
131
+ if (extras.length > 0) {
132
+ for (const extra of extras) {
133
+ context.report({
134
+ node: errorProp,
135
+ messageId: "errorExtraField",
136
+ data: { name, field: extra },
137
+ });
138
+ }
139
+ } else {
140
+ context.report({
141
+ node: errorProp,
142
+ messageId: "errorWrongShape",
143
+ data: { name },
144
+ });
145
+ }
146
+ }
147
+ }
148
+ }
149
+ }
150
+
151
+ for (const [propName, propNode] of props) {
152
+ if (propName !== "data" && propName !== "error") {
153
+ context.report({
154
+ node: propNode,
155
+ messageId: "extraProperty",
156
+ data: { name, field: propName },
157
+ });
158
+ }
159
+ }
160
+ }
161
+
162
+ export const formStateShapeRule = {
163
+ meta: {
164
+ type: "problem",
165
+ docs: {
166
+ description:
167
+ "Enforce { data, error: { message } } shape for FormState types.",
168
+ },
169
+ messages: {
170
+ dataMissing:
171
+ "FormState '{{ name }}' must have a `data` property (use `data: null` if there is no payload).",
172
+ dataNotNullable:
173
+ "FormState '{{ name }}' `data` must allow null (e.g. `data: T | null` or `data: null`).",
174
+ errorMissing:
175
+ "FormState '{{ name }}' must have an `error: { message: string } | null` property.",
176
+ errorNotNullable:
177
+ "FormState '{{ name }}' `error` must be nullable (`{ message: string } | null`).",
178
+ errorWrongShape:
179
+ "FormState '{{ name }}' `error` must be exactly `{ message: string } | null`.",
180
+ errorExtraField:
181
+ "FormState '{{ name }}' `error` object must contain only `message: string`. Forbidden field: '{{ field }}'. See form-state-shape rule docstring for the rationale and the future opt-out plan for Stripe-like cases.",
182
+ extraProperty:
183
+ "FormState '{{ name }}' must contain only `data` and `error`. Forbidden property: '{{ field }}'.",
184
+ discriminatedUnion:
185
+ "FormState '{{ name }}' must be a single interface or type literal, not a discriminated union.",
186
+ },
187
+ schema: [],
188
+ },
189
+ create(context) {
190
+ return {
191
+ TSInterfaceDeclaration(node) {
192
+ const name = node.id?.name;
193
+ if (!name?.endsWith(FORM_STATE_SUFFIX)) return;
194
+ checkBody(context, node.id, name, node.body.body);
195
+ },
196
+ TSTypeAliasDeclaration(node) {
197
+ const name = node.id?.name;
198
+ if (!name?.endsWith(FORM_STATE_SUFFIX)) return;
199
+ const ann = node.typeAnnotation;
200
+ if (ann?.type === "TSUnionType") {
201
+ context.report({
202
+ node: node.id,
203
+ messageId: "discriminatedUnion",
204
+ data: { name },
205
+ });
206
+ return;
207
+ }
208
+ if (ann?.type !== "TSTypeLiteral") return;
209
+ checkBody(context, node.id, name, ann.members);
210
+ },
211
+ };
212
+ },
213
+ };
@@ -1,6 +1,8 @@
1
+ import { entrySingleServiceCallRule } from "./entry-single-service-call.mjs";
1
2
  import { entryTemplateRule } from "./entry-template.mjs";
2
3
  import { featureNameRule } from "./feature-name.mjs";
3
4
  import { formStateNamingRule } from "./form-state-naming.mjs";
5
+ import { formStateShapeRule } from "./form-state-shape.mjs";
4
6
  import { importPathStyleRule } from "./import-path-style.mjs";
5
7
  import { layoutMainStructuralOnlyRule } from "./layout-main-structural-only.mjs";
6
8
  import { namespaceImportNameRule } from "./namespace-import-name.mjs";
@@ -14,9 +16,11 @@ import { supabaseSelectTypedColumnsRule } from "./supabase-select-typed-columns.
14
16
  /** Single plugin object to avoid ESLint "Cannot redefine plugin" errors. */
15
17
  export const localPlugin = {
16
18
  rules: {
19
+ "entry-single-service-call": entrySingleServiceCallRule,
17
20
  "entry-template": entryTemplateRule,
18
21
  "feature-name": featureNameRule,
19
22
  "form-state-naming": formStateNamingRule,
23
+ "form-state-shape": formStateShapeRule,
20
24
  "import-path-style": importPathStyleRule,
21
25
  "layout-main-structural-only": layoutMainStructuralOnlyRule,
22
26
  "namespace-import-name": namespaceImportNameRule,
@@ -151,6 +151,7 @@ export function createNamingConfigs(featureRoot, prefixLibMapping) {
151
151
  plugins: { local: localPlugin },
152
152
  rules: {
153
153
  "local/form-state-naming": "error",
154
+ "local/form-state-shape": "error",
154
155
  },
155
156
  },
156
157
  );
@@ -305,6 +306,15 @@ export function createNamingConfigs(featureRoot, prefixLibMapping) {
305
306
  },
306
307
  });
307
308
 
309
+ configs.push({
310
+ name: "naming/entry-single-service-call",
311
+ files: featuresGlob(featureRoot, "**/entries/*.ts"),
312
+ plugins: { local: localPlugin },
313
+ rules: {
314
+ "local/entry-single-service-call": "error",
315
+ },
316
+ });
317
+
308
318
  configs.push(
309
319
  {
310
320
  name: "naming/entries-shared",