@avelonjs/bailiff 0.8.6 → 0.8.7
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/README.md +4 -2
- package/package.json +1 -1
- package/src/model-evidence.ts +170 -0
- package/src/rules.ts +17 -42
package/README.md
CHANGED
|
@@ -59,8 +59,8 @@ const rows = await DB.raw().rpc('match_documents', { embedding })
|
|
|
59
59
|
| `require-ward` | Every model has a ward | error |
|
|
60
60
|
| `no-raw-outside-drivers` | `driver.raw()` only in `app/Drivers/` or behind a reasoned disable | error |
|
|
61
61
|
| `no-hand-edited-generated` | Generated paths keep the generator banner | error |
|
|
62
|
-
| `no-bun-in-core` | `@avelonjs/core` and `@avelonjs/orm` may not import `bun:*`
|
|
63
|
-
| `no-model-across-boundary` | `view()`
|
|
62
|
+
| `no-bun-in-core` | `@avelonjs/core` and `@avelonjs/orm` may not import `bun:*` | error |
|
|
63
|
+
| `no-model-across-boundary` | `view()` rejects props with evidence of a raw model instance | error |
|
|
64
64
|
| `require-request` | A write action validates through a Request | error |
|
|
65
65
|
| `no-fake-transaction` | Sequential writes wrapped in try/catch are not a transaction | error |
|
|
66
66
|
| `relation-depth` | `.with()` chains cannot exceed driver `maxRelationDepth` | error |
|
|
@@ -71,6 +71,8 @@ const rows = await DB.raw().rpc('match_documents', { embedding })
|
|
|
71
71
|
| `no-any-public` | `any` in an exported signature | error |
|
|
72
72
|
| `require-disable-reason` | Every disable names a rule and a reason after `--` | error |
|
|
73
73
|
|
|
74
|
+
`no-model-across-boundary` follows local bindings to model types, constructors, and ORM calls that return models. It uses the `Models/` import convention and local `Model` subclasses. Plain data and serializer results can pass directly to `view()`. The rule does not resolve the return types of imported helpers.
|
|
75
|
+
|
|
74
76
|
`no-any-public` autofixes `any` to `unknown`. The other error rules fail the build; you suppress one only with a reason that becomes a searchable list of everywhere the framework was not good enough.
|
|
75
77
|
|
|
76
78
|
## Escape Hatches
|
package/package.json
CHANGED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { posix } from 'node:path'
|
|
2
|
+
import type { parseForESLint } from '@typescript-eslint/parser'
|
|
3
|
+
import type { Rule, SourceCode } from 'eslint'
|
|
4
|
+
import { posixPath } from './paths'
|
|
5
|
+
|
|
6
|
+
type ParserScope = ReturnType<typeof parseForESLint>['scopeManager']['scopes'][number]
|
|
7
|
+
type Definition = ParserScope['variables'][number]['defs'][number]
|
|
8
|
+
type Expression = NonNullable<Extract<Definition, { type: 'Variable' }>['node']['init']>
|
|
9
|
+
type Identifier = Extract<Expression, { type: 'Identifier' }>
|
|
10
|
+
type TypeNode = NonNullable<Identifier['typeAnnotation']>['typeAnnotation']
|
|
11
|
+
type Call = Extract<Expression, { type: 'CallExpression' }>
|
|
12
|
+
|
|
13
|
+
const modelFactories = new Set(['find', 'findOrFail', 'create'])
|
|
14
|
+
const modelQueryResults = new Set(['first', 'get', 'paginate'])
|
|
15
|
+
const modelInstanceResults = new Set(['fill', 'save', 'update', 'restore'])
|
|
16
|
+
|
|
17
|
+
function memberCall(node: Call) {
|
|
18
|
+
const { callee } = node
|
|
19
|
+
if (callee.type !== 'MemberExpression') return undefined
|
|
20
|
+
if (!callee.computed && callee.property.type === 'Identifier') {
|
|
21
|
+
return { receiver: callee.object, method: callee.property.name }
|
|
22
|
+
}
|
|
23
|
+
if (
|
|
24
|
+
callee.computed &&
|
|
25
|
+
callee.property.type === 'Literal' &&
|
|
26
|
+
typeof callee.property.value === 'string'
|
|
27
|
+
) {
|
|
28
|
+
return { receiver: callee.object, method: callee.property.value }
|
|
29
|
+
}
|
|
30
|
+
return undefined
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function modelEvidence(source: SourceCode, filename: string) {
|
|
34
|
+
function definition(node: Identifier): Definition | undefined {
|
|
35
|
+
const scope = source.getScope(node as Rule.Node) as unknown as ParserScope
|
|
36
|
+
return scope.references.find(({ identifier }) => identifier === node)?.resolved?.defs[0]
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function modelClass(node: Expression, seen = new Set<Definition>()): boolean {
|
|
40
|
+
if (node.type !== 'Identifier') return false
|
|
41
|
+
const def = definition(node)
|
|
42
|
+
if (def === undefined || seen.has(def)) return false
|
|
43
|
+
seen.add(def)
|
|
44
|
+
if (def.type === 'ImportBinding' && def.parent.type === 'ImportDeclaration') {
|
|
45
|
+
const specifier = def.parent.source.value
|
|
46
|
+
if (specifier === '@avelonjs/orm') {
|
|
47
|
+
return (
|
|
48
|
+
def.node.type === 'ImportSpecifier' &&
|
|
49
|
+
def.node.imported.type === 'Identifier' &&
|
|
50
|
+
def.node.imported.name === 'Model'
|
|
51
|
+
)
|
|
52
|
+
}
|
|
53
|
+
const path = specifier.startsWith('.')
|
|
54
|
+
? posix.resolve(posix.dirname(posixPath(filename)), specifier)
|
|
55
|
+
: specifier
|
|
56
|
+
return /(?:^|\/)Models(?:\/|$)/.test(path) && def.node.type !== 'ImportNamespaceSpecifier'
|
|
57
|
+
}
|
|
58
|
+
if (def.type === 'ClassName' && def.node.superClass !== null) {
|
|
59
|
+
return modelClass(def.node.superClass, seen)
|
|
60
|
+
}
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function modelType(node: TypeNode | undefined): boolean {
|
|
65
|
+
switch (node?.type) {
|
|
66
|
+
case 'TSTypeReference':
|
|
67
|
+
if (node.typeName.type !== 'Identifier') return false
|
|
68
|
+
if (modelClass(node.typeName)) return true
|
|
69
|
+
return (
|
|
70
|
+
['Array', 'ReadonlyArray', 'Promise'].includes(node.typeName.name) &&
|
|
71
|
+
modelType(node.typeArguments?.params[0])
|
|
72
|
+
)
|
|
73
|
+
case 'TSArrayType':
|
|
74
|
+
return modelType(node.elementType)
|
|
75
|
+
case 'TSTypeOperator':
|
|
76
|
+
return node.operator === 'readonly' && modelType(node.typeAnnotation)
|
|
77
|
+
case 'TSUnionType':
|
|
78
|
+
return node.types.some(modelType)
|
|
79
|
+
default:
|
|
80
|
+
return false
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function modelQuery(node: Expression, seen = new Set<Definition>()): boolean {
|
|
85
|
+
if (modelClass(node)) return true
|
|
86
|
+
if (node.type === 'Identifier') {
|
|
87
|
+
const def = definition(node)
|
|
88
|
+
if (
|
|
89
|
+
def?.type !== 'Variable' ||
|
|
90
|
+
def.parent.kind !== 'const' ||
|
|
91
|
+
def.node.id.type !== 'Identifier' ||
|
|
92
|
+
def.node.init === null ||
|
|
93
|
+
seen.has(def)
|
|
94
|
+
)
|
|
95
|
+
return false
|
|
96
|
+
seen.add(def)
|
|
97
|
+
return modelQuery(def.node.init, seen)
|
|
98
|
+
}
|
|
99
|
+
if (node.type !== 'CallExpression') return false
|
|
100
|
+
const call = memberCall(node)
|
|
101
|
+
return call !== undefined && modelQuery(call.receiver, seen)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function containsModel(
|
|
105
|
+
node: Call['arguments'][number] | null | undefined,
|
|
106
|
+
seen = new Set<Definition>(),
|
|
107
|
+
): boolean {
|
|
108
|
+
switch (node?.type) {
|
|
109
|
+
case 'Identifier': {
|
|
110
|
+
const def = definition(node)
|
|
111
|
+
if (def === undefined || seen.has(def)) return false
|
|
112
|
+
const next = new Set(seen).add(def)
|
|
113
|
+
if (def.type === 'Parameter' && def.name.type === 'Identifier') {
|
|
114
|
+
return modelType(def.name.typeAnnotation?.typeAnnotation)
|
|
115
|
+
}
|
|
116
|
+
if (def.type === 'Variable' && def.node.id.type === 'Identifier') {
|
|
117
|
+
return (
|
|
118
|
+
modelType(def.node.id.typeAnnotation?.typeAnnotation) ||
|
|
119
|
+
(def.parent.kind === 'const' && containsModel(def.node.init, next))
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
return false
|
|
123
|
+
}
|
|
124
|
+
case 'NewExpression':
|
|
125
|
+
return modelClass(node.callee)
|
|
126
|
+
case 'CallExpression': {
|
|
127
|
+
if (node.callee.type === 'Identifier') {
|
|
128
|
+
const def = definition(node.callee)
|
|
129
|
+
if (def?.type === 'FunctionName') {
|
|
130
|
+
return modelType(def.node.returnType?.typeAnnotation)
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const call = memberCall(node)
|
|
134
|
+
if (call === undefined) return false
|
|
135
|
+
const { receiver, method } = call
|
|
136
|
+
return (
|
|
137
|
+
(modelFactories.has(method) && modelClass(receiver)) ||
|
|
138
|
+
(modelQueryResults.has(method) && modelQuery(receiver)) ||
|
|
139
|
+
(modelInstanceResults.has(method) && containsModel(receiver, seen))
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
case 'AwaitExpression':
|
|
143
|
+
return containsModel(node.argument, seen)
|
|
144
|
+
case 'ChainExpression':
|
|
145
|
+
case 'TSAsExpression':
|
|
146
|
+
case 'TSNonNullExpression':
|
|
147
|
+
case 'TSSatisfiesExpression':
|
|
148
|
+
case 'TSTypeAssertion':
|
|
149
|
+
return containsModel(node.expression, seen)
|
|
150
|
+
case 'ConditionalExpression':
|
|
151
|
+
return containsModel(node.consequent, seen) || containsModel(node.alternate, seen)
|
|
152
|
+
case 'ArrayExpression':
|
|
153
|
+
return node.elements.some((element) => containsModel(element, seen))
|
|
154
|
+
case 'ObjectExpression':
|
|
155
|
+
return node.properties.some(
|
|
156
|
+
(property) =>
|
|
157
|
+
property.type === 'Property' &&
|
|
158
|
+
property.method === false &&
|
|
159
|
+
property.kind === 'init' &&
|
|
160
|
+
containsModel(property.value as Expression, seen),
|
|
161
|
+
)
|
|
162
|
+
case 'SpreadElement':
|
|
163
|
+
return containsModel(node.argument, seen)
|
|
164
|
+
default:
|
|
165
|
+
return false
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return (props: unknown): boolean => containsModel(props as Call['arguments'][number])
|
|
170
|
+
}
|
package/src/rules.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
wardExists,
|
|
25
25
|
type FileLayer,
|
|
26
26
|
} from './paths'
|
|
27
|
+
import { modelEvidence } from './model-evidence'
|
|
27
28
|
import { reasons, type RuleName } from './reasons'
|
|
28
29
|
import { disableComments, report } from './suppress'
|
|
29
30
|
|
|
@@ -332,48 +333,22 @@ const noBunInCore = ruleModule('no-bun-in-core', (context, layer) => ({
|
|
|
332
333
|
},
|
|
333
334
|
}))
|
|
334
335
|
|
|
335
|
-
const noModelAcrossBoundary = ruleModule('no-model-across-boundary', (context) =>
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
.
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
if (property.type !== 'Property') continue
|
|
352
|
-
const value = property.value
|
|
353
|
-
if (value?.type === 'Identifier') {
|
|
354
|
-
report(
|
|
355
|
-
context,
|
|
356
|
-
'no-model-across-boundary',
|
|
357
|
-
node as Rule.Node,
|
|
358
|
-
`Pass a serializer to view(), not the raw value '${value.name}'.`,
|
|
359
|
-
)
|
|
360
|
-
}
|
|
361
|
-
if (
|
|
362
|
-
value?.type === 'CallExpression' &&
|
|
363
|
-
value.callee?.type === 'MemberExpression' &&
|
|
364
|
-
value.callee.property?.name !== 'make' &&
|
|
365
|
-
value.callee.property?.name !== 'collection'
|
|
366
|
-
) {
|
|
367
|
-
report(
|
|
368
|
-
context,
|
|
369
|
-
'no-model-across-boundary',
|
|
370
|
-
node as Rule.Node,
|
|
371
|
-
'Pass a serializer to view(), not a model instance.',
|
|
372
|
-
)
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
},
|
|
376
|
-
}))
|
|
336
|
+
const noModelAcrossBoundary = ruleModule('no-model-across-boundary', (context) => {
|
|
337
|
+
const containsModel = modelEvidence(context.sourceCode, context.filename)
|
|
338
|
+
return {
|
|
339
|
+
CallExpression(node) {
|
|
340
|
+
const { callee, arguments: args } = node as CallLike
|
|
341
|
+
if (callee?.type !== 'Identifier' || callee.name !== 'view') return
|
|
342
|
+
if (!containsModel(args?.[1])) return
|
|
343
|
+
report(
|
|
344
|
+
context,
|
|
345
|
+
'no-model-across-boundary',
|
|
346
|
+
node as Rule.Node,
|
|
347
|
+
'Pass a serializer to view(), not a model instance.',
|
|
348
|
+
)
|
|
349
|
+
},
|
|
350
|
+
}
|
|
351
|
+
})
|
|
377
352
|
|
|
378
353
|
const requireRequest = ruleModule('require-request', (context, layer) => ({
|
|
379
354
|
MethodDefinition(node) {
|