@sanity/groq-lint 0.0.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/LICENSE +21 -0
- package/README.md +158 -0
- package/dist/chunk-5C74HJYX.js +1034 -0
- package/dist/chunk-5C74HJYX.js.map +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +220 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +103 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/schema.d.ts +61 -0
- package/dist/schema.js +57 -0
- package/dist/schema.js.map +1 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Sanity.io
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# @sanity/groq-lint
|
|
2
|
+
|
|
3
|
+
GROQ query linter for catching performance issues and best practice violations.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @sanity/groq-lint
|
|
9
|
+
# or
|
|
10
|
+
pnpm add @sanity/groq-lint
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### CLI
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Lint a query directly
|
|
19
|
+
npx @sanity/groq-lint -q '*[author->name == "Bob"]'
|
|
20
|
+
|
|
21
|
+
# Lint files
|
|
22
|
+
npx @sanity/groq-lint 'src/**/*.ts'
|
|
23
|
+
|
|
24
|
+
# With schema for schema-aware rules
|
|
25
|
+
npx @sanity/groq-lint 'src/**/*.ts' --schema schema.json
|
|
26
|
+
|
|
27
|
+
# JSON output for CI
|
|
28
|
+
npx @sanity/groq-lint --json 'src/**/*.ts'
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Programmatic API
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
import { lint, initLinter } from '@sanity/groq-lint'
|
|
35
|
+
|
|
36
|
+
// Optional: Initialize WASM for better performance
|
|
37
|
+
await initLinter()
|
|
38
|
+
|
|
39
|
+
// Lint a query
|
|
40
|
+
const result = lint('*[author->name == "Bob"]')
|
|
41
|
+
|
|
42
|
+
if (result.findings.length > 0) {
|
|
43
|
+
for (const finding of result.findings) {
|
|
44
|
+
console.log(`${finding.ruleId}: ${finding.message}`)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### With Configuration
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { lint } from '@sanity/groq-lint'
|
|
53
|
+
|
|
54
|
+
const result = lint(query, {
|
|
55
|
+
config: {
|
|
56
|
+
rules: {
|
|
57
|
+
'deep-pagination': false, // Disable specific rule
|
|
58
|
+
'large-pages': { enabled: true }, // Configure rule
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### With Schema (Schema-Aware Rules)
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { lint } from '@sanity/groq-lint'
|
|
68
|
+
import type { SchemaType } from 'groq-js'
|
|
69
|
+
|
|
70
|
+
const schema: SchemaType = {
|
|
71
|
+
// Your Sanity schema
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const result = lint(query, { schema })
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Rules
|
|
78
|
+
|
|
79
|
+
| Rule | Severity | Description |
|
|
80
|
+
| ------------------------------ | -------- | ------------------------------------------------------ |
|
|
81
|
+
| `join-in-filter` | error | Dereference (`->`) inside filter prevents optimization |
|
|
82
|
+
| `join-to-get-id` | warning | Using `->` to get `_id` (use `._ref` instead) |
|
|
83
|
+
| `computed-value-in-filter` | error | Computed values in filter prevent optimization |
|
|
84
|
+
| `match-on-id` | info | Using `match` on `_id` with wildcard |
|
|
85
|
+
| `order-on-expr` | error | Ordering on computed values |
|
|
86
|
+
| `deep-pagination` | warning | Large offset in slice (>=1000) |
|
|
87
|
+
| `deep-pagination-param` | warning | Pagination offset from parameter |
|
|
88
|
+
| `large-pages` | warning | Fetching >100 results from index 0 |
|
|
89
|
+
| `non-literal-comparison` | error | Comparing two non-literal expressions |
|
|
90
|
+
| `repeated-dereference` | info | Multiple `->` on same attribute in projection |
|
|
91
|
+
| `count-in-correlated-subquery` | info | `count()` on correlated subqueries |
|
|
92
|
+
| `very-large-query` | error | Query exceeds 10KB |
|
|
93
|
+
| `extremely-large-query` | error | Query exceeds 100KB |
|
|
94
|
+
| `many-joins` | warning | Query has >10 dereference operators |
|
|
95
|
+
|
|
96
|
+
## WASM Acceleration
|
|
97
|
+
|
|
98
|
+
This package uses WASM-compiled Rust for maximum performance on pure GROQ rules. Call `initLinter()` once at startup to enable WASM:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { initLinter, lint } from '@sanity/groq-lint'
|
|
102
|
+
|
|
103
|
+
// Initialize WASM (optional but recommended)
|
|
104
|
+
const wasmAvailable = await initLinter()
|
|
105
|
+
console.log(`WASM available: ${wasmAvailable}`)
|
|
106
|
+
|
|
107
|
+
// lint() automatically uses WASM for supported rules
|
|
108
|
+
const result = lint(query)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Hybrid Architecture
|
|
112
|
+
|
|
113
|
+
The linter uses a hybrid approach:
|
|
114
|
+
|
|
115
|
+
- **WASM rules**: Pure GROQ rules run via high-performance Rust/WASM
|
|
116
|
+
- **TypeScript rules**: Schema-aware rules run via TypeScript
|
|
117
|
+
|
|
118
|
+
This gives you the best of both worlds: raw performance for syntax-only checks and full schema integration for semantic checks.
|
|
119
|
+
|
|
120
|
+
### Force TypeScript
|
|
121
|
+
|
|
122
|
+
To force TypeScript rules (useful for debugging):
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
const result = lint(query, { forceTs: true })
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## API Reference
|
|
129
|
+
|
|
130
|
+
### `initLinter(): Promise<boolean>`
|
|
131
|
+
|
|
132
|
+
Initialize the WASM linter. Returns `true` if WASM is available.
|
|
133
|
+
|
|
134
|
+
### `lint(query: string, options?: LintOptions): LintResult`
|
|
135
|
+
|
|
136
|
+
Lint a GROQ query.
|
|
137
|
+
|
|
138
|
+
```typescript
|
|
139
|
+
interface LintOptions {
|
|
140
|
+
config?: LinterConfig // Rule configuration
|
|
141
|
+
schema?: SchemaType // Schema for schema-aware rules
|
|
142
|
+
forceTs?: boolean // Force TypeScript rules
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
interface LintResult {
|
|
146
|
+
query: string
|
|
147
|
+
findings: Finding[]
|
|
148
|
+
parseError?: string
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### `lintMany(queries: string[], options?: LintOptions): LintResult[]`
|
|
153
|
+
|
|
154
|
+
Lint multiple queries.
|
|
155
|
+
|
|
156
|
+
## License
|
|
157
|
+
|
|
158
|
+
MIT
|