oxlint-plugin-effect 0.0.0
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/.effect-lsp.json +60 -0
- package/.oxlintrc.json +33 -0
- package/package.json +51 -0
- package/scripts/add-rule.ts +87 -0
- package/scripts/codegen.ts +108 -0
- package/src/index.ts +9 -0
- package/src/plugin.ts +16 -0
- package/src/presets/core.ts +36 -0
- package/src/presets/effect-native.ts +41 -0
- package/src/presets/full.ts +84 -0
- package/src/presets/index.ts +3 -0
- package/src/rules/_effect-context.ts +28 -0
- package/src/rules/index.ts +83 -0
- package/src/rules/no-arrow-ladder.ts +71 -0
- package/src/rules/no-async-function.ts +48 -0
- package/src/rules/no-console-in-effect.ts +46 -0
- package/src/rules/no-date-in-effect.ts +61 -0
- package/src/rules/no-dynamic-import.ts +13 -0
- package/src/rules/no-effect-as.ts +14 -0
- package/src/rules/no-effect-async.ts +14 -0
- package/src/rules/no-effect-bind.ts +13 -0
- package/src/rules/no-effect-do.ts +14 -0
- package/src/rules/no-effect-fn-generator.ts +59 -0
- package/src/rules/no-effect-gen-adapter.ts +51 -0
- package/src/rules/no-effect-map-void.ts +81 -0
- package/src/rules/no-effect-never.ts +14 -0
- package/src/rules/no-effect-orElse-ladder.ts +80 -0
- package/src/rules/no-effect-succeed-string.ts +53 -0
- package/src/rules/no-effect-succeed-variable.ts +51 -0
- package/src/rules/no-effect-succeed-void.ts +64 -0
- package/src/rules/no-effect-sync-console.ts +92 -0
- package/src/rules/no-effect-sync-wrapper.ts +68 -0
- package/src/rules/no-effect-type-alias.ts +72 -0
- package/src/rules/no-effect-wrapper-alias.ts +98 -0
- package/src/rules/no-extends-native-error.ts +55 -0
- package/src/rules/no-fetch-in-effect.ts +47 -0
- package/src/rules/no-flatmap-ladder.ts +79 -0
- package/src/rules/no-fromnullable-coalesce.ts +63 -0
- package/src/rules/no-global-console.ts +17 -0
- package/src/rules/no-global-date.ts +20 -0
- package/src/rules/no-global-fetch.ts +14 -0
- package/src/rules/no-global-random.ts +13 -0
- package/src/rules/no-global-timers.ts +17 -0
- package/src/rules/no-if-statement.ts +16 -0
- package/src/rules/no-iife-wrapper.ts +52 -0
- package/src/rules/no-inline-runtime-provide.ts +42 -0
- package/src/rules/no-instanceof-schema.ts +43 -0
- package/src/rules/no-json-in-effect.ts +45 -0
- package/src/rules/no-json-parse.ts +18 -0
- package/src/rules/no-manual-effect-channels.ts +59 -0
- package/src/rules/no-match-effect-branch.ts +83 -0
- package/src/rules/no-match-void-branch.ts +73 -0
- package/src/rules/no-nested-effect-call.ts +59 -0
- package/src/rules/no-nested-effect-gen.ts +44 -0
- package/src/rules/no-nested-pipe.ts +76 -0
- package/src/rules/no-new-error.ts +17 -0
- package/src/rules/no-new-promise.ts +14 -0
- package/src/rules/no-node-builtin-import.ts +70 -0
- package/src/rules/no-option-as.ts +13 -0
- package/src/rules/no-option-boolean-normalization.ts +89 -0
- package/src/rules/no-platform-globals.ts +117 -0
- package/src/rules/no-process-env-in-effect.ts +39 -0
- package/src/rules/no-process-env.ts +13 -0
- package/src/rules/no-random-in-effect.ts +39 -0
- package/src/rules/no-return-in-arrow.ts +77 -0
- package/src/rules/no-return-null.ts +47 -0
- package/src/rules/no-run-in-effect.ts +18 -0
- package/src/rules/no-runtime-run-fork.ts +14 -0
- package/src/rules/no-string-sentinel-const.ts +50 -0
- package/src/rules/no-switch-statement.ts +13 -0
- package/src/rules/no-ternary.ts +32 -0
- package/src/rules/no-throw-in-effect-gen.ts +42 -0
- package/src/rules/no-throw-statement.ts +13 -0
- package/src/rules/no-timers-in-effect.ts +47 -0
- package/src/rules/no-try-catch-in-effect-gen.ts +42 -0
- package/src/rules/no-try-catch.ts +14 -0
- package/src/rules/no-unnecessary-arrow-block.ts +52 -0
- package/src/rules/no-unnecessary-effect-gen.ts +85 -0
- package/src/rules/no-unnecessary-pipe.ts +46 -0
- package/src/vendor/effect-oxlint/AST.ts +516 -0
- package/src/vendor/effect-oxlint/Comment.ts +86 -0
- package/src/vendor/effect-oxlint/Diagnostic.ts +199 -0
- package/src/vendor/effect-oxlint/Plugin.ts +71 -0
- package/src/vendor/effect-oxlint/Rule.ts +590 -0
- package/src/vendor/effect-oxlint/RuleContext.ts +144 -0
- package/src/vendor/effect-oxlint/Scope.ts +177 -0
- package/src/vendor/effect-oxlint/SourceCode.ts +377 -0
- package/src/vendor/effect-oxlint/Testing.ts +1227 -0
- package/src/vendor/effect-oxlint/Token.ts +131 -0
- package/src/vendor/effect-oxlint/Visitor.ts +287 -0
- package/src/vendor/effect-oxlint/index.ts +88 -0
- package/tests/ban-rules.test.ts +252 -0
- package/tests/context-rules.test.ts +281 -0
- package/tests/pattern-rules.test.ts +258 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AST pattern matching helpers returning `Option` for safe composition.
|
|
3
|
+
*
|
|
4
|
+
* Every matcher receives a raw ESTree node and returns `Option<NarrowedType>`
|
|
5
|
+
* so callers can chain with `Option.map`, `Option.flatMap`, etc.
|
|
6
|
+
*
|
|
7
|
+
* @since 0.1.0
|
|
8
|
+
*/
|
|
9
|
+
import type { ESTree } from '@oxlint/plugins';
|
|
10
|
+
import * as Arr from 'effect/Array';
|
|
11
|
+
import { dual, pipe } from 'effect/Function';
|
|
12
|
+
import * as Option from 'effect/Option';
|
|
13
|
+
import * as P from 'effect/Predicate';
|
|
14
|
+
import * as Result from 'effect/Result';
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// Internal helpers
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
/** @internal */
|
|
21
|
+
const isIdentifier = (
|
|
22
|
+
node: unknown
|
|
23
|
+
): node is ESTree.IdentifierName | ESTree.IdentifierReference =>
|
|
24
|
+
P.isObject(node) &&
|
|
25
|
+
'type' in node &&
|
|
26
|
+
node['type'] === 'Identifier' &&
|
|
27
|
+
'name' in node &&
|
|
28
|
+
P.isString(node['name']);
|
|
29
|
+
|
|
30
|
+
/** @internal */
|
|
31
|
+
const identifierName = (node: unknown): Option.Option<string> =>
|
|
32
|
+
isIdentifier(node) ? Option.some(node.name) : Option.none();
|
|
33
|
+
|
|
34
|
+
/** @internal */
|
|
35
|
+
const isStaticMember = (
|
|
36
|
+
node: ESTree.MemberExpression
|
|
37
|
+
): node is ESTree.StaticMemberExpression =>
|
|
38
|
+
!node.computed && node.property.type !== 'PrivateIdentifier';
|
|
39
|
+
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
// Member expression matching
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Match a `MemberExpression` of the form `obj.prop` where `obj` is an
|
|
46
|
+
* identifier with the given name and `prop` matches one of the given
|
|
47
|
+
* property names.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* // Match JSON.parse or JSON.stringify
|
|
52
|
+
* AST.matchMember(node, 'JSON', ['parse', 'stringify'])
|
|
53
|
+
* ```
|
|
54
|
+
*
|
|
55
|
+
* @since 0.1.0
|
|
56
|
+
*/
|
|
57
|
+
export const matchMember: {
|
|
58
|
+
(
|
|
59
|
+
obj: string,
|
|
60
|
+
prop: string | ReadonlyArray<string>
|
|
61
|
+
): (
|
|
62
|
+
node: ESTree.MemberExpression
|
|
63
|
+
) => Option.Option<ESTree.StaticMemberExpression>;
|
|
64
|
+
(
|
|
65
|
+
node: ESTree.MemberExpression,
|
|
66
|
+
obj: string,
|
|
67
|
+
prop: string | ReadonlyArray<string>
|
|
68
|
+
): Option.Option<ESTree.StaticMemberExpression>;
|
|
69
|
+
} = dual(
|
|
70
|
+
3,
|
|
71
|
+
(
|
|
72
|
+
node: ESTree.MemberExpression,
|
|
73
|
+
obj: string,
|
|
74
|
+
prop: string | ReadonlyArray<string>
|
|
75
|
+
): Option.Option<ESTree.StaticMemberExpression> => {
|
|
76
|
+
if (!isStaticMember(node)) return Option.none();
|
|
77
|
+
const props = P.isString(prop) ? [prop] : prop;
|
|
78
|
+
return pipe(
|
|
79
|
+
identifierName(node.object),
|
|
80
|
+
Option.filter((name) => name === obj),
|
|
81
|
+
Option.flatMap(() => identifierName(node.property)),
|
|
82
|
+
Option.filter((name) => Arr.contains(props, name)),
|
|
83
|
+
Option.map(() => node)
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Check whether a `MemberExpression` is `obj.prop`.
|
|
90
|
+
*
|
|
91
|
+
* Pure boolean predicate — use `matchMember` when you need the narrowed node.
|
|
92
|
+
*
|
|
93
|
+
* @since 0.1.0
|
|
94
|
+
*/
|
|
95
|
+
export const isMember: {
|
|
96
|
+
(
|
|
97
|
+
obj: string,
|
|
98
|
+
prop: string | ReadonlyArray<string>
|
|
99
|
+
): (node: ESTree.MemberExpression) => boolean;
|
|
100
|
+
(
|
|
101
|
+
node: ESTree.MemberExpression,
|
|
102
|
+
obj: string,
|
|
103
|
+
prop: string | ReadonlyArray<string>
|
|
104
|
+
): boolean;
|
|
105
|
+
} = dual(
|
|
106
|
+
3,
|
|
107
|
+
(
|
|
108
|
+
node: ESTree.MemberExpression,
|
|
109
|
+
obj: string,
|
|
110
|
+
prop: string | ReadonlyArray<string>
|
|
111
|
+
): boolean => Option.isSome(matchMember(node, obj, prop))
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
// ---------------------------------------------------------------------------
|
|
115
|
+
// Call expression matching
|
|
116
|
+
// ---------------------------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Match a `CallExpression` whose callee is `obj.prop(...)`.
|
|
120
|
+
*
|
|
121
|
+
* Returns the call expression narrowed to confirm its callee is a
|
|
122
|
+
* static member expression.
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```ts
|
|
126
|
+
* AST.matchCallOf(node, 'Effect', 'gen')
|
|
127
|
+
* AST.matchCallOf(node, 'Effect', ['fn', 'fnUntraced'])
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* @since 0.1.0
|
|
131
|
+
*/
|
|
132
|
+
export const matchCallOf: {
|
|
133
|
+
(
|
|
134
|
+
obj: string,
|
|
135
|
+
prop: string | ReadonlyArray<string>
|
|
136
|
+
): (node: ESTree.CallExpression) => Option.Option<ESTree.CallExpression>;
|
|
137
|
+
(
|
|
138
|
+
node: ESTree.CallExpression,
|
|
139
|
+
obj: string,
|
|
140
|
+
prop: string | ReadonlyArray<string>
|
|
141
|
+
): Option.Option<ESTree.CallExpression>;
|
|
142
|
+
} = dual(
|
|
143
|
+
3,
|
|
144
|
+
(
|
|
145
|
+
node: ESTree.CallExpression,
|
|
146
|
+
obj: string,
|
|
147
|
+
prop: string | ReadonlyArray<string>
|
|
148
|
+
): Option.Option<ESTree.CallExpression> =>
|
|
149
|
+
node.callee.type === 'MemberExpression'
|
|
150
|
+
? pipe(
|
|
151
|
+
matchMember(node.callee, obj, prop),
|
|
152
|
+
Option.map(() => node)
|
|
153
|
+
)
|
|
154
|
+
: Option.none()
|
|
155
|
+
);
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Boolean predicate: is this `CallExpression` a call of `obj.prop(...)`?
|
|
159
|
+
*
|
|
160
|
+
* @since 0.1.0
|
|
161
|
+
*/
|
|
162
|
+
export const isCallOf: {
|
|
163
|
+
(
|
|
164
|
+
obj: string,
|
|
165
|
+
prop: string | ReadonlyArray<string>
|
|
166
|
+
): (node: ESTree.CallExpression) => boolean;
|
|
167
|
+
(
|
|
168
|
+
node: ESTree.CallExpression,
|
|
169
|
+
obj: string,
|
|
170
|
+
prop: string | ReadonlyArray<string>
|
|
171
|
+
): boolean;
|
|
172
|
+
} = dual(
|
|
173
|
+
3,
|
|
174
|
+
(
|
|
175
|
+
node: ESTree.CallExpression,
|
|
176
|
+
obj: string,
|
|
177
|
+
prop: string | ReadonlyArray<string>
|
|
178
|
+
): boolean => Option.isSome(matchCallOf(node, obj, prop))
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
// Import matching
|
|
183
|
+
// ---------------------------------------------------------------------------
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Match an `ImportDeclaration` whose source matches a string or predicate.
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```ts
|
|
190
|
+
* AST.matchImport(node, 'node:fs')
|
|
191
|
+
* AST.matchImport(node, (src) => src.startsWith('node:'))
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* @since 0.1.0
|
|
195
|
+
*/
|
|
196
|
+
export const matchImport: {
|
|
197
|
+
(
|
|
198
|
+
source: string | ((source: string) => boolean)
|
|
199
|
+
): (
|
|
200
|
+
node: ESTree.ImportDeclaration
|
|
201
|
+
) => Option.Option<ESTree.ImportDeclaration>;
|
|
202
|
+
(
|
|
203
|
+
node: ESTree.ImportDeclaration,
|
|
204
|
+
source: string | ((source: string) => boolean)
|
|
205
|
+
): Option.Option<ESTree.ImportDeclaration>;
|
|
206
|
+
} = dual(
|
|
207
|
+
2,
|
|
208
|
+
(
|
|
209
|
+
node: ESTree.ImportDeclaration,
|
|
210
|
+
source: string | ((source: string) => boolean)
|
|
211
|
+
): Option.Option<ESTree.ImportDeclaration> => {
|
|
212
|
+
const src = node.source.value;
|
|
213
|
+
const matches = P.isString(source) ? src === source : source(src);
|
|
214
|
+
return matches ? Option.some(node) : Option.none();
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Boolean predicate: does this `ImportDeclaration` import from the given source?
|
|
220
|
+
*
|
|
221
|
+
* @since 0.1.0
|
|
222
|
+
*/
|
|
223
|
+
export const isImport: {
|
|
224
|
+
(
|
|
225
|
+
source: string | ((source: string) => boolean)
|
|
226
|
+
): (node: ESTree.ImportDeclaration) => boolean;
|
|
227
|
+
(
|
|
228
|
+
node: ESTree.ImportDeclaration,
|
|
229
|
+
source: string | ((source: string) => boolean)
|
|
230
|
+
): boolean;
|
|
231
|
+
} = dual(
|
|
232
|
+
2,
|
|
233
|
+
(
|
|
234
|
+
node: ESTree.ImportDeclaration,
|
|
235
|
+
source: string | ((source: string) => boolean)
|
|
236
|
+
): boolean => Option.isSome(matchImport(node, source))
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
// ---------------------------------------------------------------------------
|
|
240
|
+
// Identifier extraction
|
|
241
|
+
// ---------------------------------------------------------------------------
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Extract the callee name from a `CallExpression` when the callee is a
|
|
245
|
+
* bare identifier (e.g. `fetch(...)`).
|
|
246
|
+
*
|
|
247
|
+
* @since 0.1.0
|
|
248
|
+
*/
|
|
249
|
+
export const calleeName = (
|
|
250
|
+
node: ESTree.CallExpression
|
|
251
|
+
): Option.Option<string> => identifierName(node.callee);
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Extract the object and property names from a static `MemberExpression`.
|
|
255
|
+
*
|
|
256
|
+
* Returns `Option<readonly [objectName, propertyName]>`.
|
|
257
|
+
*
|
|
258
|
+
* @since 0.1.0
|
|
259
|
+
*/
|
|
260
|
+
export const memberNames = (
|
|
261
|
+
node: ESTree.MemberExpression
|
|
262
|
+
): Option.Option<readonly [obj: string, prop: string]> =>
|
|
263
|
+
node.computed
|
|
264
|
+
? Option.none()
|
|
265
|
+
: pipe(
|
|
266
|
+
identifierName(node.object),
|
|
267
|
+
Option.flatMap((obj) =>
|
|
268
|
+
pipe(
|
|
269
|
+
identifierName(node.property),
|
|
270
|
+
Option.map((prop) => [obj, prop] as const)
|
|
271
|
+
)
|
|
272
|
+
)
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Extract the import source string from an `ImportDeclaration`.
|
|
277
|
+
*
|
|
278
|
+
* @since 0.1.0
|
|
279
|
+
*/
|
|
280
|
+
export const importSource = (node: ESTree.ImportDeclaration): string =>
|
|
281
|
+
node.source.value;
|
|
282
|
+
|
|
283
|
+
// ---------------------------------------------------------------------------
|
|
284
|
+
// Object expression helpers
|
|
285
|
+
// ---------------------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Collect the statically-known key names from an `ObjectExpression`.
|
|
289
|
+
*
|
|
290
|
+
* Spread elements and computed properties are ignored.
|
|
291
|
+
*
|
|
292
|
+
* @since 0.1.0
|
|
293
|
+
*/
|
|
294
|
+
export const objectKeys = (
|
|
295
|
+
node: ESTree.ObjectExpression
|
|
296
|
+
): ReadonlyArray<string> =>
|
|
297
|
+
pipe(
|
|
298
|
+
node.properties,
|
|
299
|
+
Arr.filterMap((p) => {
|
|
300
|
+
if (p.type !== 'Property') return Result.fail(undefined);
|
|
301
|
+
return pipe(
|
|
302
|
+
identifierName(p.key),
|
|
303
|
+
Option.orElse(() =>
|
|
304
|
+
p.key.type === 'Literal' && P.isString(p.key.value)
|
|
305
|
+
? Option.some(p.key.value)
|
|
306
|
+
: Option.none()
|
|
307
|
+
),
|
|
308
|
+
Result.fromOption(() => undefined)
|
|
309
|
+
);
|
|
310
|
+
})
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Check whether an `ObjectExpression` has a property with the given key.
|
|
315
|
+
*
|
|
316
|
+
* @since 0.1.0
|
|
317
|
+
*/
|
|
318
|
+
export const objectHasKey: {
|
|
319
|
+
(key: string): (node: ESTree.ObjectExpression) => boolean;
|
|
320
|
+
(node: ESTree.ObjectExpression, key: string): boolean;
|
|
321
|
+
} = dual(2, (node: ESTree.ObjectExpression, key: string): boolean =>
|
|
322
|
+
Arr.contains(objectKeys(node), key)
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Get the value expression for a given key in an `ObjectExpression`.
|
|
327
|
+
*
|
|
328
|
+
* @since 0.1.0
|
|
329
|
+
*/
|
|
330
|
+
export const objectGetValue: {
|
|
331
|
+
(
|
|
332
|
+
key: string
|
|
333
|
+
): (node: ESTree.ObjectExpression) => Option.Option<ESTree.Expression>;
|
|
334
|
+
(
|
|
335
|
+
node: ESTree.ObjectExpression,
|
|
336
|
+
key: string
|
|
337
|
+
): Option.Option<ESTree.Expression>;
|
|
338
|
+
} = dual(
|
|
339
|
+
2,
|
|
340
|
+
(
|
|
341
|
+
node: ESTree.ObjectExpression,
|
|
342
|
+
key: string
|
|
343
|
+
): Option.Option<ESTree.Expression> =>
|
|
344
|
+
pipe(
|
|
345
|
+
node.properties,
|
|
346
|
+
Arr.findFirst(
|
|
347
|
+
(p): p is ESTree.ObjectProperty =>
|
|
348
|
+
p.type === 'Property' &&
|
|
349
|
+
(identifierName(p.key).pipe(
|
|
350
|
+
Option.map((n) => n === key),
|
|
351
|
+
Option.getOrElse(() => false)
|
|
352
|
+
) ||
|
|
353
|
+
(p.key.type === 'Literal' && p.key.value === key))
|
|
354
|
+
),
|
|
355
|
+
Option.map((p) => p.value)
|
|
356
|
+
)
|
|
357
|
+
);
|
|
358
|
+
|
|
359
|
+
// ---------------------------------------------------------------------------
|
|
360
|
+
// Node narrowing
|
|
361
|
+
// ---------------------------------------------------------------------------
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Narrow an AST node to a specific `type` string, returning `Option<Node>`.
|
|
365
|
+
*
|
|
366
|
+
* This is a safe alternative to casting — returns `Option.none()` if the
|
|
367
|
+
* node's `type` doesn't match.
|
|
368
|
+
*
|
|
369
|
+
* @example
|
|
370
|
+
* ```ts
|
|
371
|
+
* AST.narrow(node, 'Identifier') // Option<Node & { type: "Identifier" }>
|
|
372
|
+
* AST.narrow(node, 'CallExpression') // Option<Node & { type: "CallExpression" }>
|
|
373
|
+
* ```
|
|
374
|
+
*
|
|
375
|
+
* @since 0.2.0
|
|
376
|
+
*/
|
|
377
|
+
/** @internal Type guard: does the node's `type` match the literal? */
|
|
378
|
+
const hasType = <T extends string>(
|
|
379
|
+
node: ESTree.Node,
|
|
380
|
+
type: T
|
|
381
|
+
): node is ESTree.Node & { readonly type: T } => node.type === type;
|
|
382
|
+
|
|
383
|
+
export const narrow: {
|
|
384
|
+
<T extends string>(
|
|
385
|
+
type: T
|
|
386
|
+
): (node: ESTree.Node) => Option.Option<ESTree.Node & { readonly type: T }>;
|
|
387
|
+
<T extends string>(
|
|
388
|
+
node: ESTree.Node,
|
|
389
|
+
type: T
|
|
390
|
+
): Option.Option<ESTree.Node & { readonly type: T }>;
|
|
391
|
+
} = dual(
|
|
392
|
+
2,
|
|
393
|
+
<T extends string>(
|
|
394
|
+
node: ESTree.Node,
|
|
395
|
+
type: T
|
|
396
|
+
): Option.Option<ESTree.Node & { readonly type: T }> =>
|
|
397
|
+
hasType(node, type) ? Option.some(node) : Option.none()
|
|
398
|
+
);
|
|
399
|
+
|
|
400
|
+
// ---------------------------------------------------------------------------
|
|
401
|
+
// Member path extraction
|
|
402
|
+
// ---------------------------------------------------------------------------
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Extract the full member path from a (possibly chained) `MemberExpression`.
|
|
406
|
+
*
|
|
407
|
+
* Walks `a.b.c` → `['a', 'b', 'c']`. Returns `Option.none()` if any
|
|
408
|
+
* segment is computed or non-identifier.
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```ts
|
|
412
|
+
* // node is `Effect.gen` → Some(['Effect', 'gen'])
|
|
413
|
+
* AST.memberPath(node)
|
|
414
|
+
* // node is `a.b.c.d` → Some(['a', 'b', 'c', 'd'])
|
|
415
|
+
* AST.memberPath(node)
|
|
416
|
+
* // node is `a[b].c` → None (computed segment)
|
|
417
|
+
* AST.memberPath(node)
|
|
418
|
+
* ```
|
|
419
|
+
*
|
|
420
|
+
* @since 0.2.0
|
|
421
|
+
*/
|
|
422
|
+
export const memberPath = (
|
|
423
|
+
node: ESTree.MemberExpression
|
|
424
|
+
): Option.Option<Arr.NonEmptyReadonlyArray<string>> => {
|
|
425
|
+
/** @internal Collect property names from right to left. */
|
|
426
|
+
const collect = (
|
|
427
|
+
current: ESTree.Expression | ESTree.PrivateIdentifier,
|
|
428
|
+
acc: ReadonlyArray<string>
|
|
429
|
+
): Option.Option<Arr.NonEmptyReadonlyArray<string>> => {
|
|
430
|
+
if (
|
|
431
|
+
!P.isObject(current) ||
|
|
432
|
+
!('type' in current) ||
|
|
433
|
+
current.type !== 'MemberExpression'
|
|
434
|
+
) {
|
|
435
|
+
return pipe(
|
|
436
|
+
identifierName(current),
|
|
437
|
+
Option.map(
|
|
438
|
+
(rootName) =>
|
|
439
|
+
[
|
|
440
|
+
rootName,
|
|
441
|
+
...acc
|
|
442
|
+
] satisfies Arr.NonEmptyReadonlyArray<string>
|
|
443
|
+
)
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
if (current.computed) return Option.none();
|
|
447
|
+
return pipe(
|
|
448
|
+
identifierName(current.property),
|
|
449
|
+
Option.flatMap((propName) =>
|
|
450
|
+
collect(current.object, [propName, ...acc])
|
|
451
|
+
)
|
|
452
|
+
);
|
|
453
|
+
};
|
|
454
|
+
return collect(node, []);
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
// ---------------------------------------------------------------------------
|
|
458
|
+
// Ancestor / parent helpers
|
|
459
|
+
// ---------------------------------------------------------------------------
|
|
460
|
+
|
|
461
|
+
/** @internal Type guard for objects with a string `type` and optional `parent`. */
|
|
462
|
+
const isASTShape = (
|
|
463
|
+
value: unknown
|
|
464
|
+
): value is { readonly type: string; readonly parent?: unknown } =>
|
|
465
|
+
P.isObject(value) && 'type' in value && P.isString(value['type']);
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Walk the `.parent` chain and return the first ancestor whose `type`
|
|
469
|
+
* matches the given string.
|
|
470
|
+
*
|
|
471
|
+
* Returns the ancestor as an opaque record — callers should narrow
|
|
472
|
+
* via `type` checks or other AST helpers rather than casting.
|
|
473
|
+
*
|
|
474
|
+
* @since 0.1.0
|
|
475
|
+
*/
|
|
476
|
+
export const findAncestor: {
|
|
477
|
+
(
|
|
478
|
+
type: string
|
|
479
|
+
): (node: {
|
|
480
|
+
readonly parent?: unknown;
|
|
481
|
+
}) => Option.Option<{ readonly type: string; readonly parent?: unknown }>;
|
|
482
|
+
(
|
|
483
|
+
node: { readonly parent?: unknown },
|
|
484
|
+
type: string
|
|
485
|
+
): Option.Option<{ readonly type: string; readonly parent?: unknown }>;
|
|
486
|
+
} = dual(
|
|
487
|
+
2,
|
|
488
|
+
(
|
|
489
|
+
node: { readonly parent?: unknown },
|
|
490
|
+
type: string
|
|
491
|
+
): Option.Option<{ readonly type: string; readonly parent?: unknown }> => {
|
|
492
|
+
const walk = (
|
|
493
|
+
current: unknown
|
|
494
|
+
): Option.Option<{
|
|
495
|
+
readonly type: string;
|
|
496
|
+
readonly parent?: unknown;
|
|
497
|
+
}> => {
|
|
498
|
+
if (!isASTShape(current)) return Option.none();
|
|
499
|
+
if (current.type === type) return Option.some(current);
|
|
500
|
+
return walk(current.parent);
|
|
501
|
+
};
|
|
502
|
+
return walk(node.parent);
|
|
503
|
+
}
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Check whether any ancestor of the node has the given `type`.
|
|
508
|
+
*
|
|
509
|
+
* @since 0.1.0
|
|
510
|
+
*/
|
|
511
|
+
export const hasAncestor: {
|
|
512
|
+
(type: string): (node: { readonly parent?: unknown }) => boolean;
|
|
513
|
+
(node: { readonly parent?: unknown }, type: string): boolean;
|
|
514
|
+
} = dual(2, (node: { readonly parent?: unknown }, type: string): boolean =>
|
|
515
|
+
Option.isSome(findAncestor(node, type))
|
|
516
|
+
);
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Comment type predicates and helpers for Effect-first lint rules.
|
|
3
|
+
*
|
|
4
|
+
* Oxlint comments have a `type` of `"Line"`, `"Block"`, or `"Shebang"`
|
|
5
|
+
* and a `value` string (the comment text without delimiters).
|
|
6
|
+
*
|
|
7
|
+
* @since 0.2.0
|
|
8
|
+
*/
|
|
9
|
+
import type { Comment } from '@oxlint/plugins';
|
|
10
|
+
import * as Str from 'effect/String';
|
|
11
|
+
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
// Type predicates
|
|
14
|
+
// ---------------------------------------------------------------------------
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Check whether a comment is a line comment (`// ...`).
|
|
18
|
+
*
|
|
19
|
+
* @since 0.2.0
|
|
20
|
+
*/
|
|
21
|
+
export const isLine = (comment: Comment): boolean => comment.type === 'Line';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Check whether a comment is a block comment (`/* ... */`).
|
|
25
|
+
*
|
|
26
|
+
* @since 0.2.0
|
|
27
|
+
*/
|
|
28
|
+
export const isBlock = (comment: Comment): boolean => comment.type === 'Block';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Check whether a comment is a shebang (`#!/usr/bin/env node`).
|
|
32
|
+
*
|
|
33
|
+
* @since 0.2.0
|
|
34
|
+
*/
|
|
35
|
+
export const isShebang = (comment: Comment): boolean =>
|
|
36
|
+
comment.type === 'Shebang';
|
|
37
|
+
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
// Content helpers
|
|
40
|
+
// ---------------------------------------------------------------------------
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get the text content of a comment (without delimiters).
|
|
44
|
+
*
|
|
45
|
+
* @since 0.2.0
|
|
46
|
+
*/
|
|
47
|
+
export const text = (comment: Comment): string => comment.value;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Check whether a comment is a JSDoc comment (`/** ... */`).
|
|
51
|
+
*
|
|
52
|
+
* A JSDoc comment is a block comment whose value starts with `*`.
|
|
53
|
+
*
|
|
54
|
+
* @since 0.2.0
|
|
55
|
+
*/
|
|
56
|
+
export const isJSDoc = (comment: Comment): boolean =>
|
|
57
|
+
comment.type === 'Block' && Str.startsWith('*')(comment.value);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Check whether a comment is an eslint/oxlint disable directive.
|
|
61
|
+
*
|
|
62
|
+
* Matches line comments like `// eslint-disable-next-line ...`
|
|
63
|
+
* and block comments like `/* eslint-disable ... */`.
|
|
64
|
+
*
|
|
65
|
+
* @since 0.2.0
|
|
66
|
+
*/
|
|
67
|
+
export const isDisableDirective = (comment: Comment): boolean => {
|
|
68
|
+
const trimmed = Str.trim(comment.value);
|
|
69
|
+
return (
|
|
70
|
+
Str.startsWith('eslint-disable')(trimmed) ||
|
|
71
|
+
Str.startsWith('oxlint-disable')(trimmed)
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Check whether a comment is an eslint/oxlint enable directive.
|
|
77
|
+
*
|
|
78
|
+
* @since 0.2.0
|
|
79
|
+
*/
|
|
80
|
+
export const isEnableDirective = (comment: Comment): boolean => {
|
|
81
|
+
const trimmed = Str.trim(comment.value);
|
|
82
|
+
return (
|
|
83
|
+
Str.startsWith('eslint-enable')(trimmed) ||
|
|
84
|
+
Str.startsWith('oxlint-enable')(trimmed)
|
|
85
|
+
);
|
|
86
|
+
};
|