oxlint-plugin-effect 0.1.0 → 0.2.1
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/dist/_virtual/_rolldown/runtime.js +13 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/plugin.d.ts +5 -0
- package/dist/plugin.js +18 -0
- package/dist/presets/core.d.ts +43 -0
- package/dist/presets/core.js +43 -0
- package/dist/presets/effect-native.d.ts +28 -0
- package/dist/presets/effect-native.js +28 -0
- package/dist/presets/full.d.ts +60 -0
- package/dist/presets/full.js +60 -0
- package/dist/presets/functional.d.ts +21 -0
- package/dist/presets/functional.js +21 -0
- package/dist/presets/index.d.ts +13 -0
- package/dist/presets/index.js +18 -0
- package/dist/presets/strict.d.ts +69 -0
- package/dist/presets/strict.js +69 -0
- package/dist/presets/v3.d.ts +15 -0
- package/dist/presets/v3.js +15 -0
- package/dist/rules/_effect-context.d.ts +19 -0
- package/dist/rules/_effect-context.js +22 -0
- package/dist/rules/index.d.ts +66 -0
- package/dist/rules/index.js +124 -0
- package/dist/rules/no-arrow-ladder.d.ts +6 -0
- package/dist/rules/no-arrow-ladder.js +61 -0
- package/dist/rules/no-async-function.d.ts +6 -0
- package/dist/rules/no-async-function.js +46 -0
- package/dist/rules/no-catch-all-to-map-error.d.ts +6 -0
- package/dist/rules/no-catch-all-to-map-error.js +67 -0
- package/dist/rules/no-effect-as.d.ts +6 -0
- package/dist/rules/no-effect-as.js +16 -0
- package/dist/rules/no-effect-async.d.ts +6 -0
- package/dist/rules/no-effect-async.js +16 -0
- package/dist/rules/no-effect-bind.d.ts +6 -0
- package/dist/rules/no-effect-bind.js +16 -0
- package/dist/rules/no-effect-do.d.ts +6 -0
- package/dist/rules/no-effect-do.js +16 -0
- package/dist/rules/no-effect-fn-generator.d.ts +6 -0
- package/dist/rules/no-effect-fn-generator.js +30 -0
- package/dist/rules/no-effect-gen-adapter.d.ts +6 -0
- package/dist/rules/no-effect-gen-adapter.js +30 -0
- package/dist/rules/no-effect-map-flatten.d.ts +6 -0
- package/dist/rules/no-effect-map-flatten.js +53 -0
- package/dist/rules/no-effect-map-void.d.ts +6 -0
- package/dist/rules/no-effect-map-void.js +40 -0
- package/dist/rules/no-effect-never.d.ts +6 -0
- package/dist/rules/no-effect-never.js +16 -0
- package/dist/rules/no-effect-orElse-ladder.d.ts +6 -0
- package/dist/rules/no-effect-orElse-ladder.js +53 -0
- package/dist/rules/no-effect-succeed-string.d.ts +6 -0
- package/dist/rules/no-effect-succeed-string.js +30 -0
- package/dist/rules/no-effect-succeed-variable.d.ts +6 -0
- package/dist/rules/no-effect-succeed-variable.js +30 -0
- package/dist/rules/no-effect-succeed-void.d.ts +6 -0
- package/dist/rules/no-effect-succeed-void.js +33 -0
- package/dist/rules/no-effect-sync-console.d.ts +6 -0
- package/dist/rules/no-effect-sync-console.js +63 -0
- package/dist/rules/no-effect-sync-wrapper.d.ts +6 -0
- package/dist/rules/no-effect-sync-wrapper.js +37 -0
- package/dist/rules/no-effect-type-alias.d.ts +6 -0
- package/dist/rules/no-effect-type-alias.js +52 -0
- package/dist/rules/no-effect-wrapper-alias.d.ts +6 -0
- package/dist/rules/no-effect-wrapper-alias.js +69 -0
- package/dist/rules/no-extends-native-error.d.ts +6 -0
- package/dist/rules/no-extends-native-error.js +41 -0
- package/dist/rules/no-flatmap-ladder.d.ts +6 -0
- package/dist/rules/no-flatmap-ladder.js +48 -0
- package/dist/rules/no-fromnullable-coalesce.d.ts +6 -0
- package/dist/rules/no-fromnullable-coalesce.js +36 -0
- package/dist/rules/no-global-error-in-catch.d.ts +6 -0
- package/dist/rules/no-global-error-in-catch.js +75 -0
- package/dist/rules/no-global-error-in-failure.d.ts +6 -0
- package/dist/rules/no-global-error-in-failure.js +54 -0
- package/dist/rules/no-globals.d.ts +6 -0
- package/dist/rules/no-globals.js +164 -0
- package/dist/rules/no-if-statement.d.ts +6 -0
- package/dist/rules/no-if-statement.js +18 -0
- package/dist/rules/no-iife-wrapper.d.ts +6 -0
- package/dist/rules/no-iife-wrapper.js +37 -0
- package/dist/rules/no-inline-provide.d.ts +6 -0
- package/dist/rules/no-inline-provide.js +31 -0
- package/dist/rules/no-instanceof-schema.d.ts +6 -0
- package/dist/rules/no-instanceof-schema.js +35 -0
- package/dist/rules/no-manual-effect-channels.d.ts +6 -0
- package/dist/rules/no-manual-effect-channels.js +40 -0
- package/dist/rules/no-match-effect-branch.d.ts +6 -0
- package/dist/rules/no-match-effect-branch.js +64 -0
- package/dist/rules/no-match-void-branch.d.ts +6 -0
- package/dist/rules/no-match-void-branch.js +50 -0
- package/dist/rules/no-multiple-effect-provide.d.ts +6 -0
- package/dist/rules/no-multiple-effect-provide.js +45 -0
- package/dist/rules/no-nested-effect-call.d.ts +6 -0
- package/dist/rules/no-nested-effect-call.js +97 -0
- package/dist/rules/no-nested-effect-gen.d.ts +6 -0
- package/dist/rules/no-nested-effect-gen.js +46 -0
- package/dist/rules/no-nested-pipe.d.ts +6 -0
- package/dist/rules/no-nested-pipe.js +38 -0
- package/dist/rules/no-new-error.d.ts +6 -0
- package/dist/rules/no-new-error.js +39 -0
- package/dist/rules/no-new-promise.d.ts +6 -0
- package/dist/rules/no-new-promise.js +38 -0
- package/dist/rules/no-node-builtin-import.d.ts +6 -0
- package/dist/rules/no-node-builtin-import.js +61 -0
- package/dist/rules/no-option-as.d.ts +6 -0
- package/dist/rules/no-option-as.js +16 -0
- package/dist/rules/no-option-boolean-normalization.d.ts +6 -0
- package/dist/rules/no-option-boolean-normalization.js +50 -0
- package/dist/rules/no-positional-log-error.d.ts +6 -0
- package/dist/rules/no-positional-log-error.js +52 -0
- package/dist/rules/no-redundant-schema-tag-identifier.d.ts +6 -0
- package/dist/rules/no-redundant-schema-tag-identifier.js +46 -0
- package/dist/rules/no-return-in-arrow.d.ts +6 -0
- package/dist/rules/no-return-in-arrow.js +41 -0
- package/dist/rules/no-return-nullish.d.ts +6 -0
- package/dist/rules/no-return-nullish.js +47 -0
- package/dist/rules/no-run-in-effect-gen.d.ts +6 -0
- package/dist/rules/no-run-in-effect-gen.js +48 -0
- package/dist/rules/no-runtime-run-fork.d.ts +6 -0
- package/dist/rules/no-runtime-run-fork.js +19 -0
- package/dist/rules/no-schema-struct-with-tag.d.ts +6 -0
- package/dist/rules/no-schema-struct-with-tag.js +48 -0
- package/dist/rules/no-schema-union-of-literals.d.ts +6 -0
- package/dist/rules/no-schema-union-of-literals.js +35 -0
- package/dist/rules/no-string-sentinel-const.d.ts +6 -0
- package/dist/rules/no-string-sentinel-const.js +38 -0
- package/dist/rules/no-switch-statement.d.ts +6 -0
- package/dist/rules/no-switch-statement.js +16 -0
- package/dist/rules/no-ternary.d.ts +6 -0
- package/dist/rules/no-ternary.js +30 -0
- package/dist/rules/no-throw-statement.d.ts +6 -0
- package/dist/rules/no-throw-statement.js +34 -0
- package/dist/rules/no-try-catch.d.ts +6 -0
- package/dist/rules/no-try-catch.js +34 -0
- package/dist/rules/no-unnecessary-arrow-block.d.ts +6 -0
- package/dist/rules/no-unnecessary-arrow-block.js +34 -0
- package/dist/rules/no-unnecessary-effect-gen.d.ts +6 -0
- package/dist/rules/no-unnecessary-effect-gen.js +35 -0
- package/dist/rules/no-unnecessary-pipe-chain.d.ts +6 -0
- package/dist/rules/no-unnecessary-pipe-chain.js +36 -0
- package/dist/rules/no-unnecessary-pipe.d.ts +6 -0
- package/dist/rules/no-unnecessary-pipe.js +29 -0
- package/dist/vendor/effect-oxlint/AST.d.ts +199 -0
- package/dist/vendor/effect-oxlint/AST.js +214 -0
- package/dist/vendor/effect-oxlint/Comment.d.ts +55 -0
- package/dist/vendor/effect-oxlint/Comment.js +67 -0
- package/dist/vendor/effect-oxlint/Diagnostic.d.ts +86 -0
- package/dist/vendor/effect-oxlint/Diagnostic.js +115 -0
- package/dist/vendor/effect-oxlint/Plugin.d.ts +38 -0
- package/dist/vendor/effect-oxlint/Plugin.js +45 -0
- package/dist/vendor/effect-oxlint/Rule.d.ts +218 -0
- package/dist/vendor/effect-oxlint/Rule.js +309 -0
- package/dist/vendor/effect-oxlint/RuleContext.d.ts +77 -0
- package/dist/vendor/effect-oxlint/RuleContext.js +74 -0
- package/dist/vendor/effect-oxlint/Scope.d.ts +100 -0
- package/dist/vendor/effect-oxlint/Scope.js +110 -0
- package/dist/vendor/effect-oxlint/SourceCode.d.ts +180 -0
- package/dist/vendor/effect-oxlint/SourceCode.js +203 -0
- package/dist/vendor/effect-oxlint/Testing.d.ts +510 -0
- package/dist/vendor/effect-oxlint/Testing.js +963 -0
- package/dist/vendor/effect-oxlint/Token.d.ts +98 -0
- package/dist/vendor/effect-oxlint/Token.js +103 -0
- package/dist/vendor/effect-oxlint/Visitor.d.ts +166 -0
- package/dist/vendor/effect-oxlint/Visitor.js +138 -0
- package/dist/vendor/effect-oxlint/index.d.ts +13 -0
- package/dist/vendor/effect-oxlint/index.js +12 -0
- package/package.json +24 -5
- package/.effect-lsp.json +0 -60
- package/.oxlintrc.json +0 -33
- package/CHANGELOG.md +0 -37
- package/scripts/add-rule.ts +0 -87
- package/scripts/codegen.ts +0 -108
- package/src/index.ts +0 -9
- package/src/plugin.ts +0 -16
- package/src/presets/core.ts +0 -47
- package/src/presets/effect-native.ts +0 -34
- package/src/presets/full.ts +0 -70
- package/src/presets/functional.ts +0 -18
- package/src/presets/index.ts +0 -5
- package/src/presets/strict.ts +0 -77
- package/src/rules/_effect-context.ts +0 -28
- package/src/rules/index.ts +0 -72
- package/src/rules/no-arrow-ladder.ts +0 -70
- package/src/rules/no-async-function.ts +0 -47
- package/src/rules/no-catch-all-to-map-error.ts +0 -89
- package/src/rules/no-effect-as.ts +0 -14
- package/src/rules/no-effect-async.ts +0 -14
- package/src/rules/no-effect-bind.ts +0 -13
- package/src/rules/no-effect-do.ts +0 -14
- package/src/rules/no-effect-fn-generator.ts +0 -58
- package/src/rules/no-effect-gen-adapter.ts +0 -50
- package/src/rules/no-effect-map-flatten.ts +0 -82
- package/src/rules/no-effect-map-void.ts +0 -80
- package/src/rules/no-effect-never.ts +0 -14
- package/src/rules/no-effect-orElse-ladder.ts +0 -79
- package/src/rules/no-effect-succeed-string.ts +0 -52
- package/src/rules/no-effect-succeed-variable.ts +0 -50
- package/src/rules/no-effect-succeed-void.ts +0 -63
- package/src/rules/no-effect-sync-console.ts +0 -91
- package/src/rules/no-effect-sync-wrapper.ts +0 -67
- package/src/rules/no-effect-type-alias.ts +0 -71
- package/src/rules/no-effect-wrapper-alias.ts +0 -95
- package/src/rules/no-extends-native-error.ts +0 -54
- package/src/rules/no-flatmap-ladder.ts +0 -77
- package/src/rules/no-fromnullable-coalesce.ts +0 -62
- package/src/rules/no-global-error-in-catch.ts +0 -104
- package/src/rules/no-global-error-in-failure.ts +0 -83
- package/src/rules/no-globals.ts +0 -178
- package/src/rules/no-if-statement.ts +0 -16
- package/src/rules/no-iife-wrapper.ts +0 -51
- package/src/rules/no-inline-provide.ts +0 -41
- package/src/rules/no-instanceof-schema.ts +0 -42
- package/src/rules/no-manual-effect-channels.ts +0 -58
- package/src/rules/no-match-effect-branch.ts +0 -82
- package/src/rules/no-match-void-branch.ts +0 -72
- package/src/rules/no-multiple-effect-provide.ts +0 -75
- package/src/rules/no-nested-effect-call.ts +0 -57
- package/src/rules/no-nested-effect-gen.ts +0 -43
- package/src/rules/no-nested-pipe.ts +0 -73
- package/src/rules/no-new-error.ts +0 -53
- package/src/rules/no-new-promise.ts +0 -50
- package/src/rules/no-node-builtin-import.ts +0 -68
- package/src/rules/no-option-as.ts +0 -13
- package/src/rules/no-option-boolean-normalization.ts +0 -88
- package/src/rules/no-positional-log-error.ts +0 -76
- package/src/rules/no-redundant-schema-tag-identifier.ts +0 -62
- package/src/rules/no-return-in-arrow.ts +0 -76
- package/src/rules/no-return-nullish.ts +0 -80
- package/src/rules/no-run-in-effect.ts +0 -18
- package/src/rules/no-runtime-run-fork.ts +0 -14
- package/src/rules/no-schema-struct-with-tag.ts +0 -64
- package/src/rules/no-schema-union-of-literals.ts +0 -48
- package/src/rules/no-string-sentinel-const.ts +0 -49
- package/src/rules/no-switch-statement.ts +0 -13
- package/src/rules/no-ternary.ts +0 -30
- package/src/rules/no-throw-statement.ts +0 -39
- package/src/rules/no-try-catch.ts +0 -39
- package/src/rules/no-unnecessary-arrow-block.ts +0 -51
- package/src/rules/no-unnecessary-effect-gen.ts +0 -83
- package/src/rules/no-unnecessary-pipe-chain.ts +0 -47
- package/src/rules/no-unnecessary-pipe.ts +0 -45
- package/src/vendor/effect-oxlint/AST.ts +0 -516
- package/src/vendor/effect-oxlint/Comment.ts +0 -86
- package/src/vendor/effect-oxlint/Diagnostic.ts +0 -199
- package/src/vendor/effect-oxlint/Plugin.ts +0 -71
- package/src/vendor/effect-oxlint/Rule.ts +0 -528
- package/src/vendor/effect-oxlint/RuleContext.ts +0 -144
- package/src/vendor/effect-oxlint/Scope.ts +0 -177
- package/src/vendor/effect-oxlint/SourceCode.ts +0 -377
- package/src/vendor/effect-oxlint/Testing.ts +0 -1227
- package/src/vendor/effect-oxlint/Token.ts +0 -131
- package/src/vendor/effect-oxlint/Visitor.ts +0 -287
- package/src/vendor/effect-oxlint/index.ts +0 -88
- package/tests/ban-rules.test.ts +0 -85
- package/tests/context-rules.test.ts +0 -198
- package/tests/pattern-rules.test.ts +0 -247
- package/tsconfig.json +0 -15
|
@@ -0,0 +1,963 @@
|
|
|
1
|
+
import { __exportAll } from "../../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { RuleContext, fromOxlintContext } from "./RuleContext.js";
|
|
3
|
+
import * as Arr from "effect/Array";
|
|
4
|
+
import * as Option from "effect/Option";
|
|
5
|
+
import * as P from "effect/Predicate";
|
|
6
|
+
import * as Effect from "effect/Effect";
|
|
7
|
+
import * as Layer from "effect/Layer";
|
|
8
|
+
//#region src/vendor/effect-oxlint/Testing.ts
|
|
9
|
+
var Testing_exports = /* @__PURE__ */ __exportAll({
|
|
10
|
+
arrowFn: () => arrowFn,
|
|
11
|
+
astNode: () => astNode,
|
|
12
|
+
binaryExpr: () => binaryExpr,
|
|
13
|
+
blockStmt: () => blockStmt,
|
|
14
|
+
boolLiteral: () => boolLiteral,
|
|
15
|
+
callExpr: () => callExpr,
|
|
16
|
+
callOfMember: () => callOfMember,
|
|
17
|
+
chainedMemberExpr: () => chainedMemberExpr,
|
|
18
|
+
classDecl: () => classDecl,
|
|
19
|
+
comment: () => comment,
|
|
20
|
+
computedMemberExpr: () => computedMemberExpr,
|
|
21
|
+
createMockContext: () => createMockContext,
|
|
22
|
+
doWhileStmt: () => doWhileStmt,
|
|
23
|
+
expectDiagnostics: () => expectDiagnostics,
|
|
24
|
+
expectNoDiagnostics: () => expectNoDiagnostics,
|
|
25
|
+
exportNamedDecl: () => exportNamedDecl,
|
|
26
|
+
exprStmt: () => exprStmt,
|
|
27
|
+
forInStmt: () => forInStmt,
|
|
28
|
+
forOfStmt: () => forOfStmt,
|
|
29
|
+
forStmt: () => forStmt,
|
|
30
|
+
id: () => id,
|
|
31
|
+
ifStmt: () => ifStmt,
|
|
32
|
+
importDecl: () => importDecl,
|
|
33
|
+
importDeclWithSpecifiers: () => importDeclWithSpecifiers,
|
|
34
|
+
importNamespaceSpecifier: () => importNamespaceSpecifier,
|
|
35
|
+
importSpecifier: () => importSpecifier,
|
|
36
|
+
interfaceDecl: () => interfaceDecl,
|
|
37
|
+
memberExpr: () => memberExpr,
|
|
38
|
+
messageIds: () => messageIds,
|
|
39
|
+
messages: () => messages,
|
|
40
|
+
methodDef: () => methodDef,
|
|
41
|
+
mockRuleContextLayer: () => mockRuleContextLayer,
|
|
42
|
+
newExpr: () => newExpr,
|
|
43
|
+
numLiteral: () => numLiteral,
|
|
44
|
+
objectExpr: () => objectExpr,
|
|
45
|
+
objectExprLiteralKeys: () => objectExprLiteralKeys,
|
|
46
|
+
objectExprWithSpread: () => objectExprWithSpread,
|
|
47
|
+
program: () => program,
|
|
48
|
+
propertyDef: () => propertyDef,
|
|
49
|
+
returnStmt: () => returnStmt,
|
|
50
|
+
runRule: () => runRule,
|
|
51
|
+
runRuleMulti: () => runRuleMulti,
|
|
52
|
+
scope: () => scope,
|
|
53
|
+
strLiteral: () => strLiteral,
|
|
54
|
+
switchStmt: () => switchStmt,
|
|
55
|
+
throwStmt: () => throwStmt,
|
|
56
|
+
token: () => token,
|
|
57
|
+
tryStmt: () => tryStmt,
|
|
58
|
+
tsAsExpr: () => tsAsExpr,
|
|
59
|
+
tsTypeLiteral: () => tsTypeLiteral,
|
|
60
|
+
tsTypeRef: () => tsTypeRef,
|
|
61
|
+
tsUnionType: () => tsUnionType,
|
|
62
|
+
typeAliasDecl: () => typeAliasDecl,
|
|
63
|
+
unaryExpr: () => unaryExpr,
|
|
64
|
+
varDecl: () => varDecl,
|
|
65
|
+
varDeclarator: () => varDeclarator,
|
|
66
|
+
variable: () => variable,
|
|
67
|
+
whileStmt: () => whileStmt,
|
|
68
|
+
withMockRuleContext: () => withMockRuleContext,
|
|
69
|
+
withParentChain: () => withParentChain,
|
|
70
|
+
yieldExpr: () => yieldExpr
|
|
71
|
+
});
|
|
72
|
+
/**
|
|
73
|
+
* Minimal AST node builders for tests.
|
|
74
|
+
*
|
|
75
|
+
* These produce mock objects satisfying the type shapes that AST/Visitor/Rule
|
|
76
|
+
* modules expect. They use boundary casts at the seam between test mock
|
|
77
|
+
* data and oxlint's strict branded types.
|
|
78
|
+
*
|
|
79
|
+
* @since 0.2.0
|
|
80
|
+
*/
|
|
81
|
+
/**
|
|
82
|
+
* Identifier: `{ type: "Identifier", name }`
|
|
83
|
+
*
|
|
84
|
+
* @since 0.2.0
|
|
85
|
+
*/
|
|
86
|
+
const id = (name) => ({
|
|
87
|
+
type: "Identifier",
|
|
88
|
+
name
|
|
89
|
+
});
|
|
90
|
+
/**
|
|
91
|
+
* MemberExpression: `obj.prop` (non-computed)
|
|
92
|
+
*
|
|
93
|
+
* @since 0.2.0
|
|
94
|
+
*/
|
|
95
|
+
const memberExpr = (obj, prop) => ({
|
|
96
|
+
type: "MemberExpression",
|
|
97
|
+
object: id(obj),
|
|
98
|
+
property: id(prop),
|
|
99
|
+
computed: false,
|
|
100
|
+
optional: false
|
|
101
|
+
});
|
|
102
|
+
/**
|
|
103
|
+
* MemberExpression: `obj[prop]` (computed)
|
|
104
|
+
*
|
|
105
|
+
* @since 0.2.0
|
|
106
|
+
*/
|
|
107
|
+
const computedMemberExpr = (obj, prop) => ({
|
|
108
|
+
type: "MemberExpression",
|
|
109
|
+
object: id(obj),
|
|
110
|
+
property: id(prop),
|
|
111
|
+
computed: true,
|
|
112
|
+
optional: false
|
|
113
|
+
});
|
|
114
|
+
/**
|
|
115
|
+
* Chained MemberExpression: `a.b.c` (non-computed)
|
|
116
|
+
*
|
|
117
|
+
* @since 0.2.0
|
|
118
|
+
*/
|
|
119
|
+
const chainedMemberExpr = (...names) => {
|
|
120
|
+
const [first, second, ...rest] = names;
|
|
121
|
+
const initial = memberExpr(first, second);
|
|
122
|
+
return Arr.reduce(rest, initial, (acc, name) => ({
|
|
123
|
+
type: "MemberExpression",
|
|
124
|
+
object: acc,
|
|
125
|
+
property: id(name),
|
|
126
|
+
computed: false,
|
|
127
|
+
optional: false
|
|
128
|
+
}));
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* CallExpression with bare identifier callee: `name(args)`
|
|
132
|
+
*
|
|
133
|
+
* @since 0.2.0
|
|
134
|
+
*/
|
|
135
|
+
const callExpr = (name, args = []) => ({
|
|
136
|
+
type: "CallExpression",
|
|
137
|
+
callee: id(name),
|
|
138
|
+
arguments: args
|
|
139
|
+
});
|
|
140
|
+
/**
|
|
141
|
+
* CallExpression with MemberExpression callee: `obj.prop(args)`
|
|
142
|
+
*
|
|
143
|
+
* @since 0.2.0
|
|
144
|
+
*/
|
|
145
|
+
const callOfMember = (obj, prop, args = []) => ({
|
|
146
|
+
type: "CallExpression",
|
|
147
|
+
callee: memberExpr(obj, prop),
|
|
148
|
+
arguments: args
|
|
149
|
+
});
|
|
150
|
+
/**
|
|
151
|
+
* ImportDeclaration: `import ... from "source"`
|
|
152
|
+
*
|
|
153
|
+
* @since 0.2.0
|
|
154
|
+
*/
|
|
155
|
+
const importDecl = (source) => ({
|
|
156
|
+
type: "ImportDeclaration",
|
|
157
|
+
source: {
|
|
158
|
+
type: "Literal",
|
|
159
|
+
value: source
|
|
160
|
+
},
|
|
161
|
+
specifiers: []
|
|
162
|
+
});
|
|
163
|
+
/**
|
|
164
|
+
* String literal: `{ type: "Literal", value }`
|
|
165
|
+
*
|
|
166
|
+
* @since 0.2.0
|
|
167
|
+
*/
|
|
168
|
+
const strLiteral = (value) => ({
|
|
169
|
+
type: "Literal",
|
|
170
|
+
value
|
|
171
|
+
});
|
|
172
|
+
/**
|
|
173
|
+
* Numeric literal: `{ type: "Literal", value }`
|
|
174
|
+
*
|
|
175
|
+
* @since 0.2.0
|
|
176
|
+
*/
|
|
177
|
+
const numLiteral = (value) => ({
|
|
178
|
+
type: "Literal",
|
|
179
|
+
value
|
|
180
|
+
});
|
|
181
|
+
/**
|
|
182
|
+
* Boolean literal: `{ type: "Literal", value }`
|
|
183
|
+
*
|
|
184
|
+
* @since 0.2.0
|
|
185
|
+
*/
|
|
186
|
+
const boolLiteral = (value) => ({
|
|
187
|
+
type: "Literal",
|
|
188
|
+
value
|
|
189
|
+
});
|
|
190
|
+
/**
|
|
191
|
+
* ObjectExpression with identifier-keyed properties.
|
|
192
|
+
*
|
|
193
|
+
* @since 0.2.0
|
|
194
|
+
*/
|
|
195
|
+
const objectExpr = (properties) => ({
|
|
196
|
+
type: "ObjectExpression",
|
|
197
|
+
properties: Arr.map(properties, (p) => ({
|
|
198
|
+
type: "Property",
|
|
199
|
+
key: id(p.key),
|
|
200
|
+
value: p.value ?? strLiteral("")
|
|
201
|
+
}))
|
|
202
|
+
});
|
|
203
|
+
/**
|
|
204
|
+
* ObjectExpression with string literal keys.
|
|
205
|
+
*
|
|
206
|
+
* @since 0.2.0
|
|
207
|
+
*/
|
|
208
|
+
const objectExprLiteralKeys = (properties) => ({
|
|
209
|
+
type: "ObjectExpression",
|
|
210
|
+
properties: Arr.map(properties, (p) => ({
|
|
211
|
+
type: "Property",
|
|
212
|
+
key: strLiteral(p.key),
|
|
213
|
+
value: p.value ?? strLiteral("")
|
|
214
|
+
}))
|
|
215
|
+
});
|
|
216
|
+
/**
|
|
217
|
+
* ObjectExpression with a SpreadElement.
|
|
218
|
+
*
|
|
219
|
+
* @since 0.2.0
|
|
220
|
+
*/
|
|
221
|
+
const objectExprWithSpread = (spreadArg) => ({
|
|
222
|
+
type: "ObjectExpression",
|
|
223
|
+
properties: [{
|
|
224
|
+
type: "SpreadElement",
|
|
225
|
+
argument: spreadArg
|
|
226
|
+
}]
|
|
227
|
+
});
|
|
228
|
+
/**
|
|
229
|
+
* ThrowStatement.
|
|
230
|
+
*
|
|
231
|
+
* @since 0.2.0
|
|
232
|
+
*/
|
|
233
|
+
const throwStmt = () => ({ type: "ThrowStatement" });
|
|
234
|
+
/**
|
|
235
|
+
* TryStatement.
|
|
236
|
+
*
|
|
237
|
+
* @since 0.2.0
|
|
238
|
+
*/
|
|
239
|
+
const tryStmt = () => ({ type: "TryStatement" });
|
|
240
|
+
/**
|
|
241
|
+
* ReturnStatement.
|
|
242
|
+
*
|
|
243
|
+
* @since 0.2.0
|
|
244
|
+
*/
|
|
245
|
+
const returnStmt = (argument) => ({
|
|
246
|
+
type: "ReturnStatement",
|
|
247
|
+
argument: argument ?? null
|
|
248
|
+
});
|
|
249
|
+
/**
|
|
250
|
+
* BlockStatement.
|
|
251
|
+
*
|
|
252
|
+
* @since 0.2.0
|
|
253
|
+
*/
|
|
254
|
+
const blockStmt = (body = []) => ({
|
|
255
|
+
type: "BlockStatement",
|
|
256
|
+
body: Array.from(body)
|
|
257
|
+
});
|
|
258
|
+
/**
|
|
259
|
+
* ArrowFunctionExpression.
|
|
260
|
+
*
|
|
261
|
+
* @since 0.2.0
|
|
262
|
+
*/
|
|
263
|
+
const arrowFn = (body, params = []) => ({
|
|
264
|
+
type: "ArrowFunctionExpression",
|
|
265
|
+
params: Array.from(params),
|
|
266
|
+
body: body ?? blockStmt(),
|
|
267
|
+
expression: false,
|
|
268
|
+
async: false
|
|
269
|
+
});
|
|
270
|
+
/**
|
|
271
|
+
* VariableDeclaration: `const/let/var name = init`
|
|
272
|
+
*
|
|
273
|
+
* @since 0.2.0
|
|
274
|
+
*/
|
|
275
|
+
const varDecl = (kind, name, init) => ({
|
|
276
|
+
type: "VariableDeclaration",
|
|
277
|
+
kind,
|
|
278
|
+
declarations: [{
|
|
279
|
+
type: "VariableDeclarator",
|
|
280
|
+
id: id(name),
|
|
281
|
+
init: init ?? null
|
|
282
|
+
}]
|
|
283
|
+
});
|
|
284
|
+
/**
|
|
285
|
+
* ExpressionStatement.
|
|
286
|
+
*
|
|
287
|
+
* @since 0.2.0
|
|
288
|
+
*/
|
|
289
|
+
const exprStmt = (expression) => ({
|
|
290
|
+
type: "ExpressionStatement",
|
|
291
|
+
expression
|
|
292
|
+
});
|
|
293
|
+
/**
|
|
294
|
+
* Program node.
|
|
295
|
+
*
|
|
296
|
+
* @since 0.2.0
|
|
297
|
+
*/
|
|
298
|
+
const program = (body = [], comments = []) => ({
|
|
299
|
+
type: "Program",
|
|
300
|
+
body: Array.from(body),
|
|
301
|
+
comments: Array.from(comments),
|
|
302
|
+
sourceType: "module"
|
|
303
|
+
});
|
|
304
|
+
/**
|
|
305
|
+
* IfStatement.
|
|
306
|
+
*
|
|
307
|
+
* All parameters are optional — `ifStmt()` produces a minimal
|
|
308
|
+
* `{ type: 'IfStatement' }` node suitable for enter/exit tracking.
|
|
309
|
+
*
|
|
310
|
+
* @since 0.2.0
|
|
311
|
+
*/
|
|
312
|
+
const ifStmt = (test, consequent, alternate) => ({
|
|
313
|
+
type: "IfStatement",
|
|
314
|
+
test: test ?? null,
|
|
315
|
+
consequent: consequent ?? null,
|
|
316
|
+
alternate: alternate ?? null
|
|
317
|
+
});
|
|
318
|
+
/**
|
|
319
|
+
* BinaryExpression.
|
|
320
|
+
*
|
|
321
|
+
* @since 0.2.0
|
|
322
|
+
*/
|
|
323
|
+
const binaryExpr = (operator, left, right) => ({
|
|
324
|
+
type: "BinaryExpression",
|
|
325
|
+
operator,
|
|
326
|
+
left,
|
|
327
|
+
right
|
|
328
|
+
});
|
|
329
|
+
/**
|
|
330
|
+
* NewExpression: `new callee(args)`
|
|
331
|
+
*
|
|
332
|
+
* When `callee` is a string it is auto-wrapped in `id()`, so
|
|
333
|
+
* `newExpr('Date')` is equivalent to `newExpr(id('Date'))`.
|
|
334
|
+
*
|
|
335
|
+
* @since 0.2.0
|
|
336
|
+
*/
|
|
337
|
+
const newExpr = (callee, args = []) => ({
|
|
338
|
+
type: "NewExpression",
|
|
339
|
+
callee: P.isString(callee) ? id(callee) : callee,
|
|
340
|
+
arguments: Array.from(args)
|
|
341
|
+
});
|
|
342
|
+
/**
|
|
343
|
+
* A generic AST node with type and optional parent pointer.
|
|
344
|
+
*
|
|
345
|
+
* @since 0.2.0
|
|
346
|
+
*/
|
|
347
|
+
const astNode = (type, parent) => ({
|
|
348
|
+
type,
|
|
349
|
+
parent
|
|
350
|
+
});
|
|
351
|
+
/**
|
|
352
|
+
* Build a parent chain from outermost → innermost.
|
|
353
|
+
*
|
|
354
|
+
* Returns the innermost node with `.parent` links to each ancestor.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* // Creates: FunctionDeclaration → BlockStatement → ThrowStatement
|
|
359
|
+
* withParentChain('FunctionDeclaration', 'BlockStatement', 'ThrowStatement')
|
|
360
|
+
* ```
|
|
361
|
+
*
|
|
362
|
+
* @since 0.2.0
|
|
363
|
+
*/
|
|
364
|
+
const withParentChain = (first, ...rest) => Arr.reduce(rest, astNode(first), (parent, type) => astNode(type, parent));
|
|
365
|
+
/**
|
|
366
|
+
* Mock Token.
|
|
367
|
+
*
|
|
368
|
+
* @since 0.2.0
|
|
369
|
+
*/
|
|
370
|
+
const token = (type, value) => ({
|
|
371
|
+
type,
|
|
372
|
+
value,
|
|
373
|
+
start: 0,
|
|
374
|
+
end: value.length,
|
|
375
|
+
range: [0, value.length],
|
|
376
|
+
loc: {
|
|
377
|
+
start: {
|
|
378
|
+
line: 1,
|
|
379
|
+
column: 0
|
|
380
|
+
},
|
|
381
|
+
end: {
|
|
382
|
+
line: 1,
|
|
383
|
+
column: value.length
|
|
384
|
+
}
|
|
385
|
+
},
|
|
386
|
+
regex: void 0
|
|
387
|
+
});
|
|
388
|
+
/**
|
|
389
|
+
* Mock Comment.
|
|
390
|
+
*
|
|
391
|
+
* @since 0.2.0
|
|
392
|
+
*/
|
|
393
|
+
const comment = (type, value) => ({
|
|
394
|
+
type,
|
|
395
|
+
value,
|
|
396
|
+
start: 0,
|
|
397
|
+
end: value.length + 4,
|
|
398
|
+
range: [0, value.length + 4],
|
|
399
|
+
loc: {
|
|
400
|
+
start: {
|
|
401
|
+
line: 1,
|
|
402
|
+
column: 0
|
|
403
|
+
},
|
|
404
|
+
end: {
|
|
405
|
+
line: 1,
|
|
406
|
+
column: value.length + 4
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
/**
|
|
411
|
+
* Mock Scope with minimal surface.
|
|
412
|
+
*
|
|
413
|
+
* @since 0.2.0
|
|
414
|
+
*/
|
|
415
|
+
const scope = (opts = {}) => {
|
|
416
|
+
const vars = Array.from(opts.variables ?? []);
|
|
417
|
+
const set = new Map(vars.map((v) => [v.name, v]));
|
|
418
|
+
return {
|
|
419
|
+
type: opts.type ?? "function",
|
|
420
|
+
isStrict: opts.isStrict ?? false,
|
|
421
|
+
upper: opts.upper ?? null,
|
|
422
|
+
childScopes: [],
|
|
423
|
+
variableScope: null,
|
|
424
|
+
block: {},
|
|
425
|
+
variables: vars,
|
|
426
|
+
set,
|
|
427
|
+
references: [],
|
|
428
|
+
through: [],
|
|
429
|
+
functionExpressionScope: false
|
|
430
|
+
};
|
|
431
|
+
};
|
|
432
|
+
/**
|
|
433
|
+
* Mock Variable.
|
|
434
|
+
*
|
|
435
|
+
* @since 0.2.0
|
|
436
|
+
*/
|
|
437
|
+
const variable = (name, opts = {}) => ({
|
|
438
|
+
name,
|
|
439
|
+
scope: {},
|
|
440
|
+
identifiers: [id(name)],
|
|
441
|
+
references: Array.from(opts.references ?? []),
|
|
442
|
+
defs: []
|
|
443
|
+
});
|
|
444
|
+
/**
|
|
445
|
+
* SwitchStatement.
|
|
446
|
+
*
|
|
447
|
+
* @since 0.2.0
|
|
448
|
+
*/
|
|
449
|
+
const switchStmt = () => ({
|
|
450
|
+
type: "SwitchStatement",
|
|
451
|
+
discriminant: null,
|
|
452
|
+
cases: []
|
|
453
|
+
});
|
|
454
|
+
/**
|
|
455
|
+
* ForStatement.
|
|
456
|
+
*
|
|
457
|
+
* @since 0.2.0
|
|
458
|
+
*/
|
|
459
|
+
const forStmt = () => ({
|
|
460
|
+
type: "ForStatement",
|
|
461
|
+
init: null,
|
|
462
|
+
test: null,
|
|
463
|
+
update: null,
|
|
464
|
+
body: blockStmt()
|
|
465
|
+
});
|
|
466
|
+
/**
|
|
467
|
+
* ForInStatement.
|
|
468
|
+
*
|
|
469
|
+
* @since 0.2.0
|
|
470
|
+
*/
|
|
471
|
+
const forInStmt = () => ({
|
|
472
|
+
type: "ForInStatement",
|
|
473
|
+
left: id("_"),
|
|
474
|
+
right: id("_"),
|
|
475
|
+
body: blockStmt()
|
|
476
|
+
});
|
|
477
|
+
/**
|
|
478
|
+
* ForOfStatement.
|
|
479
|
+
*
|
|
480
|
+
* @since 0.2.0
|
|
481
|
+
*/
|
|
482
|
+
const forOfStmt = () => ({
|
|
483
|
+
type: "ForOfStatement",
|
|
484
|
+
await: false,
|
|
485
|
+
left: id("_"),
|
|
486
|
+
right: id("_"),
|
|
487
|
+
body: blockStmt()
|
|
488
|
+
});
|
|
489
|
+
/**
|
|
490
|
+
* WhileStatement.
|
|
491
|
+
*
|
|
492
|
+
* @since 0.2.0
|
|
493
|
+
*/
|
|
494
|
+
const whileStmt = () => ({
|
|
495
|
+
type: "WhileStatement",
|
|
496
|
+
test: boolLiteral(true),
|
|
497
|
+
body: blockStmt()
|
|
498
|
+
});
|
|
499
|
+
/**
|
|
500
|
+
* DoWhileStatement.
|
|
501
|
+
*
|
|
502
|
+
* @since 0.2.0
|
|
503
|
+
*/
|
|
504
|
+
const doWhileStmt = () => ({
|
|
505
|
+
type: "DoWhileStatement",
|
|
506
|
+
test: boolLiteral(true),
|
|
507
|
+
body: blockStmt()
|
|
508
|
+
});
|
|
509
|
+
/**
|
|
510
|
+
* YieldExpression.
|
|
511
|
+
*
|
|
512
|
+
* @since 0.2.0
|
|
513
|
+
*/
|
|
514
|
+
const yieldExpr = (argument, delegate = false) => ({
|
|
515
|
+
type: "YieldExpression",
|
|
516
|
+
argument: argument ?? null,
|
|
517
|
+
delegate
|
|
518
|
+
});
|
|
519
|
+
/**
|
|
520
|
+
* UnaryExpression: `operator argument`
|
|
521
|
+
*
|
|
522
|
+
* @since 0.2.0
|
|
523
|
+
*/
|
|
524
|
+
const unaryExpr = (operator, argument) => ({
|
|
525
|
+
type: "UnaryExpression",
|
|
526
|
+
operator,
|
|
527
|
+
prefix: true,
|
|
528
|
+
argument
|
|
529
|
+
});
|
|
530
|
+
/**
|
|
531
|
+
* VariableDeclarator (standalone, without wrapping VariableDeclaration).
|
|
532
|
+
*
|
|
533
|
+
* @since 0.2.0
|
|
534
|
+
*/
|
|
535
|
+
const varDeclarator = (name, init) => ({
|
|
536
|
+
type: "VariableDeclarator",
|
|
537
|
+
id: id(name),
|
|
538
|
+
init: init ?? null
|
|
539
|
+
});
|
|
540
|
+
/**
|
|
541
|
+
* ExportNamedDeclaration.
|
|
542
|
+
*
|
|
543
|
+
* @since 0.2.0
|
|
544
|
+
*/
|
|
545
|
+
const exportNamedDecl = (declaration) => ({
|
|
546
|
+
type: "ExportNamedDeclaration",
|
|
547
|
+
declaration: declaration ?? null,
|
|
548
|
+
specifiers: [],
|
|
549
|
+
source: null
|
|
550
|
+
});
|
|
551
|
+
/**
|
|
552
|
+
* ImportDeclaration with specifiers: `import { a, b } from "source"`
|
|
553
|
+
*
|
|
554
|
+
* @since 0.2.0
|
|
555
|
+
*/
|
|
556
|
+
const importDeclWithSpecifiers = (source, specifiers, importKind = "value") => ({
|
|
557
|
+
type: "ImportDeclaration",
|
|
558
|
+
source: {
|
|
559
|
+
type: "Literal",
|
|
560
|
+
value: source
|
|
561
|
+
},
|
|
562
|
+
specifiers: Array.from(specifiers),
|
|
563
|
+
importKind
|
|
564
|
+
});
|
|
565
|
+
/**
|
|
566
|
+
* ImportSpecifier: `{ imported as local }`
|
|
567
|
+
*
|
|
568
|
+
* @since 0.2.0
|
|
569
|
+
*/
|
|
570
|
+
const importSpecifier = (imported, local, importKind = "value") => ({
|
|
571
|
+
type: "ImportSpecifier",
|
|
572
|
+
imported: id(imported),
|
|
573
|
+
local: id(local ?? imported),
|
|
574
|
+
importKind
|
|
575
|
+
});
|
|
576
|
+
/**
|
|
577
|
+
* ImportNamespaceSpecifier: `* as local`
|
|
578
|
+
*
|
|
579
|
+
* @since 0.2.0
|
|
580
|
+
*/
|
|
581
|
+
const importNamespaceSpecifier = (local) => ({
|
|
582
|
+
type: "ImportNamespaceSpecifier",
|
|
583
|
+
local: id(local)
|
|
584
|
+
});
|
|
585
|
+
/**
|
|
586
|
+
* TSAsExpression: `expr as Type`
|
|
587
|
+
*
|
|
588
|
+
* @since 0.2.0
|
|
589
|
+
*/
|
|
590
|
+
const tsAsExpr = (typeKind, parent) => ({
|
|
591
|
+
type: "TSAsExpression",
|
|
592
|
+
expression: id("_"),
|
|
593
|
+
typeAnnotation: { type: typeKind },
|
|
594
|
+
parent
|
|
595
|
+
});
|
|
596
|
+
/**
|
|
597
|
+
* TSUnionType: `A | B | C`
|
|
598
|
+
*
|
|
599
|
+
* @since 0.2.0
|
|
600
|
+
*/
|
|
601
|
+
const tsUnionType = (typeKinds) => ({
|
|
602
|
+
type: "TSUnionType",
|
|
603
|
+
types: Arr.map(typeKinds, (t) => ({ type: t }))
|
|
604
|
+
});
|
|
605
|
+
/**
|
|
606
|
+
* TSTypeReference: `TypeName`
|
|
607
|
+
*
|
|
608
|
+
* @since 0.2.0
|
|
609
|
+
*/
|
|
610
|
+
const tsTypeRef = (name) => ({
|
|
611
|
+
type: "TSTypeReference",
|
|
612
|
+
typeName: id(name),
|
|
613
|
+
typeArguments: null
|
|
614
|
+
});
|
|
615
|
+
/**
|
|
616
|
+
* TSTypeLiteral: `{ ... }` with N members.
|
|
617
|
+
*
|
|
618
|
+
* @since 0.2.0
|
|
619
|
+
*/
|
|
620
|
+
const tsTypeLiteral = (memberCount) => ({
|
|
621
|
+
type: "TSTypeLiteral",
|
|
622
|
+
members: Array.from({ length: memberCount }, () => ({ type: "TSPropertySignature" }))
|
|
623
|
+
});
|
|
624
|
+
/**
|
|
625
|
+
* TSInterfaceDeclaration: `interface Name { }`
|
|
626
|
+
*
|
|
627
|
+
* @since 0.2.0
|
|
628
|
+
*/
|
|
629
|
+
const interfaceDecl = (name) => ({
|
|
630
|
+
type: "TSInterfaceDeclaration",
|
|
631
|
+
id: {
|
|
632
|
+
type: "BindingIdentifier",
|
|
633
|
+
name
|
|
634
|
+
},
|
|
635
|
+
body: {
|
|
636
|
+
type: "TSInterfaceBody",
|
|
637
|
+
body: []
|
|
638
|
+
}
|
|
639
|
+
});
|
|
640
|
+
/**
|
|
641
|
+
* TSTypeAliasDeclaration: `type Name = ...`
|
|
642
|
+
*
|
|
643
|
+
* @since 0.2.0
|
|
644
|
+
*/
|
|
645
|
+
const typeAliasDecl = (name) => ({
|
|
646
|
+
type: "TSTypeAliasDeclaration",
|
|
647
|
+
id: {
|
|
648
|
+
type: "BindingIdentifier",
|
|
649
|
+
name
|
|
650
|
+
},
|
|
651
|
+
typeAnnotation: null
|
|
652
|
+
});
|
|
653
|
+
/**
|
|
654
|
+
* ClassDeclaration with optional superClass and body members.
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* ```ts
|
|
658
|
+
* // Simple: class Foo {}
|
|
659
|
+
* classDecl('Foo')
|
|
660
|
+
*
|
|
661
|
+
* // With super: class Foo extends Bar {}
|
|
662
|
+
* classDecl('Foo', { superClass: Testing.id('Bar') })
|
|
663
|
+
*
|
|
664
|
+
* // With members: class Foo { x; static y() {} }
|
|
665
|
+
* classDecl('Foo', {
|
|
666
|
+
* members: [Testing.propertyDef('x'), Testing.methodDef('y', true)]
|
|
667
|
+
* })
|
|
668
|
+
* ```
|
|
669
|
+
*
|
|
670
|
+
* @since 0.2.0
|
|
671
|
+
*/
|
|
672
|
+
const classDecl = (name, opts = {}) => ({
|
|
673
|
+
type: "ClassDeclaration",
|
|
674
|
+
id: {
|
|
675
|
+
type: "BindingIdentifier",
|
|
676
|
+
name
|
|
677
|
+
},
|
|
678
|
+
superClass: opts.superClass ?? null,
|
|
679
|
+
body: {
|
|
680
|
+
type: "ClassBody",
|
|
681
|
+
body: Array.from(opts.members ?? [])
|
|
682
|
+
},
|
|
683
|
+
decorators: []
|
|
684
|
+
});
|
|
685
|
+
/**
|
|
686
|
+
* PropertyDefinition: class field.
|
|
687
|
+
*
|
|
688
|
+
* @since 0.2.0
|
|
689
|
+
*/
|
|
690
|
+
const propertyDef = (name, isStatic = false) => ({
|
|
691
|
+
type: "PropertyDefinition",
|
|
692
|
+
key: id(name),
|
|
693
|
+
value: null,
|
|
694
|
+
computed: false,
|
|
695
|
+
static: isStatic,
|
|
696
|
+
decorators: []
|
|
697
|
+
});
|
|
698
|
+
/**
|
|
699
|
+
* MethodDefinition: class method.
|
|
700
|
+
*
|
|
701
|
+
* @since 0.2.0
|
|
702
|
+
*/
|
|
703
|
+
const methodDef = (name, isStatic = false) => ({
|
|
704
|
+
type: "MethodDefinition",
|
|
705
|
+
key: id(name),
|
|
706
|
+
kind: "method",
|
|
707
|
+
value: arrowFn(),
|
|
708
|
+
computed: false,
|
|
709
|
+
static: isStatic,
|
|
710
|
+
decorators: []
|
|
711
|
+
});
|
|
712
|
+
/**
|
|
713
|
+
* Create a mock oxlint `Context` and a diagnostics collector.
|
|
714
|
+
*
|
|
715
|
+
* The mock provides the minimal surface required by `RuleContext.fromOxlintContext`.
|
|
716
|
+
*
|
|
717
|
+
* @since 0.2.0
|
|
718
|
+
*/
|
|
719
|
+
const createMockContext = (opts = {}) => {
|
|
720
|
+
const diagnostics = [];
|
|
721
|
+
const filename = opts.filename ?? "/test/file.ts";
|
|
722
|
+
const cwd = opts.cwd ?? "/test";
|
|
723
|
+
const text = opts.sourceText ?? "";
|
|
724
|
+
const comments = Array.from(opts.comments ?? []);
|
|
725
|
+
const sourceCode = {
|
|
726
|
+
text,
|
|
727
|
+
ast: {
|
|
728
|
+
type: "Program",
|
|
729
|
+
body: [],
|
|
730
|
+
comments
|
|
731
|
+
},
|
|
732
|
+
getText() {
|
|
733
|
+
return text;
|
|
734
|
+
},
|
|
735
|
+
getAllComments() {
|
|
736
|
+
return comments;
|
|
737
|
+
},
|
|
738
|
+
getLocFromIndex(index) {
|
|
739
|
+
return {
|
|
740
|
+
line: 1,
|
|
741
|
+
column: index
|
|
742
|
+
};
|
|
743
|
+
},
|
|
744
|
+
getIndexFromLoc(loc) {
|
|
745
|
+
return loc.column;
|
|
746
|
+
},
|
|
747
|
+
getAncestors() {
|
|
748
|
+
return [];
|
|
749
|
+
},
|
|
750
|
+
getScope() {
|
|
751
|
+
return scope();
|
|
752
|
+
},
|
|
753
|
+
getDeclaredVariables() {
|
|
754
|
+
return [];
|
|
755
|
+
},
|
|
756
|
+
isGlobalReference() {
|
|
757
|
+
return false;
|
|
758
|
+
},
|
|
759
|
+
markVariableAsUsed() {
|
|
760
|
+
return false;
|
|
761
|
+
},
|
|
762
|
+
getFirstToken() {
|
|
763
|
+
return null;
|
|
764
|
+
},
|
|
765
|
+
getLastToken() {
|
|
766
|
+
return null;
|
|
767
|
+
},
|
|
768
|
+
getTokens() {
|
|
769
|
+
return [];
|
|
770
|
+
},
|
|
771
|
+
getTokenBefore() {
|
|
772
|
+
return null;
|
|
773
|
+
},
|
|
774
|
+
getTokenAfter() {
|
|
775
|
+
return null;
|
|
776
|
+
},
|
|
777
|
+
getTokensBetween() {
|
|
778
|
+
return [];
|
|
779
|
+
},
|
|
780
|
+
getFirstTokenBetween() {
|
|
781
|
+
return null;
|
|
782
|
+
},
|
|
783
|
+
getTokenByRangeStart() {
|
|
784
|
+
return null;
|
|
785
|
+
},
|
|
786
|
+
getCommentsBefore() {
|
|
787
|
+
return [];
|
|
788
|
+
},
|
|
789
|
+
getCommentsAfter() {
|
|
790
|
+
return [];
|
|
791
|
+
},
|
|
792
|
+
getCommentsInside() {
|
|
793
|
+
return [];
|
|
794
|
+
},
|
|
795
|
+
commentsExistBetween() {
|
|
796
|
+
return false;
|
|
797
|
+
},
|
|
798
|
+
getJSDocComment() {
|
|
799
|
+
return null;
|
|
800
|
+
},
|
|
801
|
+
getNodeByRangeIndex() {
|
|
802
|
+
return null;
|
|
803
|
+
},
|
|
804
|
+
getRange() {
|
|
805
|
+
return [0, 0];
|
|
806
|
+
},
|
|
807
|
+
getLoc() {
|
|
808
|
+
return {
|
|
809
|
+
start: {
|
|
810
|
+
line: 1,
|
|
811
|
+
column: 0
|
|
812
|
+
},
|
|
813
|
+
end: {
|
|
814
|
+
line: 1,
|
|
815
|
+
column: 0
|
|
816
|
+
}
|
|
817
|
+
};
|
|
818
|
+
},
|
|
819
|
+
getLines() {
|
|
820
|
+
return text.split("\n");
|
|
821
|
+
},
|
|
822
|
+
isSpaceBetween() {
|
|
823
|
+
return false;
|
|
824
|
+
}
|
|
825
|
+
};
|
|
826
|
+
return {
|
|
827
|
+
context: {
|
|
828
|
+
id: "effect/test-rule",
|
|
829
|
+
filename,
|
|
830
|
+
physicalFilename: filename,
|
|
831
|
+
cwd,
|
|
832
|
+
options: opts.options ?? [],
|
|
833
|
+
report(diagnostic) {
|
|
834
|
+
diagnostics.push({ diagnostic });
|
|
835
|
+
},
|
|
836
|
+
getFilename: () => filename,
|
|
837
|
+
getCwd: () => cwd,
|
|
838
|
+
sourceCode,
|
|
839
|
+
languageOptions: {
|
|
840
|
+
sourceType: "module",
|
|
841
|
+
ecmaVersion: 2024
|
|
842
|
+
},
|
|
843
|
+
settings: {},
|
|
844
|
+
getSourceCode: () => sourceCode,
|
|
845
|
+
getPhysicalFilename: () => filename,
|
|
846
|
+
parserOptions: {},
|
|
847
|
+
parserPath: void 0
|
|
848
|
+
},
|
|
849
|
+
diagnostics
|
|
850
|
+
};
|
|
851
|
+
};
|
|
852
|
+
/**
|
|
853
|
+
* Create a `Layer` that provides a mock `RuleContext` service.
|
|
854
|
+
*
|
|
855
|
+
* Use in `it.effect` tests that need to `yield*` visitor handlers
|
|
856
|
+
* (which carry `RuleContext` in their context type).
|
|
857
|
+
*
|
|
858
|
+
* @since 0.2.0
|
|
859
|
+
*/
|
|
860
|
+
const mockRuleContextLayer = (opts) => {
|
|
861
|
+
const { context } = createMockContext(opts);
|
|
862
|
+
return Layer.succeed(RuleContext, fromOxlintContext(context));
|
|
863
|
+
};
|
|
864
|
+
/**
|
|
865
|
+
* Provide a mock `RuleContext` to an effect for testing.
|
|
866
|
+
*
|
|
867
|
+
* @since 0.2.0
|
|
868
|
+
*/
|
|
869
|
+
const withMockRuleContext = (effect, opts) => Effect.provide(effect, mockRuleContextLayer(opts));
|
|
870
|
+
/** @internal Call a visitor handler on a mock AST node. */
|
|
871
|
+
const callHandler = (visitors, key, visitorNode) => {
|
|
872
|
+
const handler = visitors[key];
|
|
873
|
+
if (handler) handler(visitorNode);
|
|
874
|
+
};
|
|
875
|
+
/**
|
|
876
|
+
* Run a rule with a single visitor event and collect diagnostics.
|
|
877
|
+
*
|
|
878
|
+
* @since 0.2.0
|
|
879
|
+
*/
|
|
880
|
+
const runRule = (rule, visitor, visitorNode, opts) => {
|
|
881
|
+
const { context, diagnostics } = createMockContext(opts);
|
|
882
|
+
callHandler(rule.create(context), visitor, visitorNode);
|
|
883
|
+
return diagnostics;
|
|
884
|
+
};
|
|
885
|
+
/**
|
|
886
|
+
* Run a rule with multiple visitor/node events sequentially.
|
|
887
|
+
*
|
|
888
|
+
* Same context is shared so `Ref` state persists across calls.
|
|
889
|
+
*
|
|
890
|
+
* @since 0.2.0
|
|
891
|
+
*/
|
|
892
|
+
const runRuleMulti = (rule, pairs, opts) => {
|
|
893
|
+
const { context, diagnostics } = createMockContext(opts);
|
|
894
|
+
const visitors = rule.create(context);
|
|
895
|
+
Arr.forEach(pairs, ([visitor, visitorNode]) => {
|
|
896
|
+
callHandler(visitors, visitor, visitorNode);
|
|
897
|
+
});
|
|
898
|
+
return diagnostics;
|
|
899
|
+
};
|
|
900
|
+
/**
|
|
901
|
+
* Extract the diagnostic messages from a result array.
|
|
902
|
+
*
|
|
903
|
+
* Returns `Option.none()` when a diagnostic uses `messageId` instead
|
|
904
|
+
* of `message`, or when the message is `null`.
|
|
905
|
+
*
|
|
906
|
+
* @example
|
|
907
|
+
* ```ts
|
|
908
|
+
* import * as Option from 'effect/Option'
|
|
909
|
+
*
|
|
910
|
+
* const result = Testing.runRule(rule, 'ThrowStatement', Testing.throwStmt())
|
|
911
|
+
* expect(Testing.messages(result)).toEqual([Option.some('Use Effect.fail instead')])
|
|
912
|
+
* ```
|
|
913
|
+
*
|
|
914
|
+
* @since 0.2.0
|
|
915
|
+
*/
|
|
916
|
+
const messages = (result) => Arr.map(result, (r) => Option.fromNullishOr(r.diagnostic.message));
|
|
917
|
+
/**
|
|
918
|
+
* Extract the diagnostic messageIds from a result array.
|
|
919
|
+
*
|
|
920
|
+
* Returns `Option.none()` when a diagnostic uses `message` instead
|
|
921
|
+
* of `messageId`, or when the messageId is `null`.
|
|
922
|
+
*
|
|
923
|
+
* @since 0.2.0
|
|
924
|
+
*/
|
|
925
|
+
const messageIds = (result) => Arr.map(result, (r) => Option.fromNullishOr(r.diagnostic.messageId));
|
|
926
|
+
/**
|
|
927
|
+
* Assert that diagnostics match expected patterns.
|
|
928
|
+
*
|
|
929
|
+
* Each matcher is partially checked — only provided fields are compared.
|
|
930
|
+
* This allows flexible matching without specifying every field.
|
|
931
|
+
*
|
|
932
|
+
* @example
|
|
933
|
+
* ```ts
|
|
934
|
+
* Testing.expectDiagnostics(result, [
|
|
935
|
+
* { message: 'No throw in Effect.gen' },
|
|
936
|
+
* { messageId: 'noTryCatch' }
|
|
937
|
+
* ])
|
|
938
|
+
* ```
|
|
939
|
+
*
|
|
940
|
+
* @since 0.2.0
|
|
941
|
+
*/
|
|
942
|
+
const expectDiagnostics = (result, expected) => {
|
|
943
|
+
if (result.length !== expected.length) throw new Error(`Expected ${expected.length} diagnostics, got ${result.length}:\n` + Arr.join(Arr.map(result, (r) => ` - ${r.diagnostic.message ?? r.diagnostic.messageId ?? "(unknown)"}`), "\n"));
|
|
944
|
+
Arr.forEach(expected, (exp, i) => {
|
|
945
|
+
const actual = result[i];
|
|
946
|
+
if (actual === void 0) throw new Error(`Missing diagnostic at index ${i}`);
|
|
947
|
+
if (exp.message !== void 0 && actual.diagnostic.message !== exp.message) throw new Error(`Diagnostic ${i}: expected message "${exp.message}", got "${actual.diagnostic.message}"`);
|
|
948
|
+
if (exp.messageId !== void 0 && actual.diagnostic.messageId !== exp.messageId) throw new Error(`Diagnostic ${i}: expected messageId "${exp.messageId}", got "${actual.diagnostic.messageId}"`);
|
|
949
|
+
});
|
|
950
|
+
};
|
|
951
|
+
/**
|
|
952
|
+
* Assert that no diagnostics were reported.
|
|
953
|
+
*
|
|
954
|
+
* @since 0.2.0
|
|
955
|
+
*/
|
|
956
|
+
const expectNoDiagnostics = (result) => Arr.match(result, {
|
|
957
|
+
onEmpty: () => {},
|
|
958
|
+
onNonEmpty: (items) => {
|
|
959
|
+
throw new Error(`Expected no diagnostics, got ${items.length}:\n` + Arr.join(Arr.map(items, (r) => ` - ${r.diagnostic.message ?? r.diagnostic.messageId ?? "(unknown)"}`), "\n"));
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
//#endregion
|
|
963
|
+
export { Testing_exports, arrowFn, astNode, binaryExpr, blockStmt, boolLiteral, callExpr, callOfMember, chainedMemberExpr, classDecl, comment, computedMemberExpr, createMockContext, doWhileStmt, expectDiagnostics, expectNoDiagnostics, exportNamedDecl, exprStmt, forInStmt, forOfStmt, forStmt, id, ifStmt, importDecl, importDeclWithSpecifiers, importNamespaceSpecifier, importSpecifier, interfaceDecl, memberExpr, messageIds, messages, methodDef, mockRuleContextLayer, newExpr, numLiteral, objectExpr, objectExprLiteralKeys, objectExprWithSpread, program, propertyDef, returnStmt, runRule, runRuleMulti, scope, strLiteral, switchStmt, throwStmt, token, tryStmt, tsAsExpr, tsTypeLiteral, tsTypeRef, tsUnionType, typeAliasDecl, unaryExpr, varDecl, varDeclarator, variable, whileStmt, withMockRuleContext, withParentChain, yieldExpr };
|