@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/dist/schema.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// src/schema.ts
|
|
2
|
+
import { readSchema } from "@sanity/codegen";
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
async function loadSchema(path) {
|
|
6
|
+
return readSchema(path);
|
|
7
|
+
}
|
|
8
|
+
var SCHEMA_CANDIDATES = [
|
|
9
|
+
"schema.json",
|
|
10
|
+
"sanity.schema.json",
|
|
11
|
+
".sanity/schema.json",
|
|
12
|
+
"studio/schema.json"
|
|
13
|
+
];
|
|
14
|
+
function findSchemaPath(root = process.cwd()) {
|
|
15
|
+
for (const candidate of SCHEMA_CANDIDATES) {
|
|
16
|
+
const fullPath = resolve(root, candidate);
|
|
17
|
+
if (existsSync(fullPath)) {
|
|
18
|
+
return fullPath;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
async function tryLoadSchema(root = process.cwd()) {
|
|
24
|
+
const schemaPath = findSchemaPath(root);
|
|
25
|
+
if (!schemaPath) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
return await loadSchema(schemaPath);
|
|
30
|
+
} catch {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function getDocumentTypes(schema) {
|
|
35
|
+
return schema.filter((item) => item.type === "document").map((doc) => doc.name);
|
|
36
|
+
}
|
|
37
|
+
function getDocumentByType(schema, typeName) {
|
|
38
|
+
return schema.find(
|
|
39
|
+
(item) => item.type === "document" && item.name === typeName
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
function getFieldsForType(schema, typeName) {
|
|
43
|
+
const doc = getDocumentByType(schema, typeName);
|
|
44
|
+
if (!doc) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
return Object.keys(doc.attributes);
|
|
48
|
+
}
|
|
49
|
+
export {
|
|
50
|
+
findSchemaPath,
|
|
51
|
+
getDocumentByType,
|
|
52
|
+
getDocumentTypes,
|
|
53
|
+
getFieldsForType,
|
|
54
|
+
loadSchema,
|
|
55
|
+
tryLoadSchema
|
|
56
|
+
};
|
|
57
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/schema.ts"],"sourcesContent":["/**\n * Schema loading utilities for schema-aware GROQ linting.\n *\n * Uses @sanity/codegen's readSchema to load schema.json files\n * generated by `sanity schema extract`.\n */\n\nimport { readSchema } from '@sanity/codegen'\nimport type { SchemaType } from 'groq-js'\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\n/**\n * Load a schema from a JSON file path.\n *\n * @param path - Path to schema.json file\n * @returns The parsed schema\n * @throws If the file doesn't exist or is invalid\n */\nexport async function loadSchema(path: string): Promise<SchemaType> {\n return readSchema(path)\n}\n\n/**\n * Common locations to look for schema.json files\n */\nconst SCHEMA_CANDIDATES = [\n 'schema.json',\n 'sanity.schema.json',\n '.sanity/schema.json',\n 'studio/schema.json',\n]\n\n/**\n * Auto-discover a schema.json file in a project directory.\n *\n * Looks in common locations for schema files generated by\n * `sanity schema extract`.\n *\n * @param root - Project root directory (defaults to cwd)\n * @returns Path to schema file, or null if not found\n */\nexport function findSchemaPath(root: string = process.cwd()): string | null {\n for (const candidate of SCHEMA_CANDIDATES) {\n const fullPath = resolve(root, candidate)\n if (existsSync(fullPath)) {\n return fullPath\n }\n }\n return null\n}\n\n/**\n * Try to load a schema from common locations.\n *\n * @param root - Project root directory (defaults to cwd)\n * @returns The schema if found, or null\n */\nexport async function tryLoadSchema(root: string = process.cwd()): Promise<SchemaType | null> {\n const schemaPath = findSchemaPath(root)\n if (!schemaPath) {\n return null\n }\n\n try {\n return await loadSchema(schemaPath)\n } catch {\n return null\n }\n}\n\n/**\n * Get document type names from a schema.\n *\n * @param schema - The schema to extract document types from\n * @returns Array of document type names\n */\nexport function getDocumentTypes(schema: SchemaType): string[] {\n return schema\n .filter((item): item is Extract<typeof item, { type: 'document' }> => item.type === 'document')\n .map((doc) => doc.name)\n}\n\n/**\n * Get a document definition by name from a schema.\n *\n * @param schema - The schema to search\n * @param typeName - The document type name (e.g., 'post')\n * @returns The document definition, or undefined\n */\nexport function getDocumentByType(\n schema: SchemaType,\n typeName: string\n): Extract<SchemaType[number], { type: 'document' }> | undefined {\n return schema.find(\n (item): item is Extract<typeof item, { type: 'document' }> =>\n item.type === 'document' && item.name === typeName\n )\n}\n\n/**\n * Get field names for a document type.\n *\n * @param schema - The schema to search\n * @param typeName - The document type name\n * @returns Array of field names, or empty array if type not found\n */\nexport function getFieldsForType(schema: SchemaType, typeName: string): string[] {\n const doc = getDocumentByType(schema, typeName)\n if (!doc) {\n return []\n }\n return Object.keys(doc.attributes)\n}\n"],"mappings":";AAOA,SAAS,kBAAkB;AAE3B,SAAS,kBAAkB;AAC3B,SAAS,eAAe;AASxB,eAAsB,WAAW,MAAmC;AAClE,SAAO,WAAW,IAAI;AACxB;AAKA,IAAM,oBAAoB;AAAA,EACxB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAWO,SAAS,eAAe,OAAe,QAAQ,IAAI,GAAkB;AAC1E,aAAW,aAAa,mBAAmB;AACzC,UAAM,WAAW,QAAQ,MAAM,SAAS;AACxC,QAAI,WAAW,QAAQ,GAAG;AACxB,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;AAQA,eAAsB,cAAc,OAAe,QAAQ,IAAI,GAA+B;AAC5F,QAAM,aAAa,eAAe,IAAI;AACtC,MAAI,CAAC,YAAY;AACf,WAAO;AAAA,EACT;AAEA,MAAI;AACF,WAAO,MAAM,WAAW,UAAU;AAAA,EACpC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAQO,SAAS,iBAAiB,QAA8B;AAC7D,SAAO,OACJ,OAAO,CAAC,SAA6D,KAAK,SAAS,UAAU,EAC7F,IAAI,CAAC,QAAQ,IAAI,IAAI;AAC1B;AASO,SAAS,kBACd,QACA,UAC+D;AAC/D,SAAO,OAAO;AAAA,IACZ,CAAC,SACC,KAAK,SAAS,cAAc,KAAK,SAAS;AAAA,EAC9C;AACF;AASO,SAAS,iBAAiB,QAAoB,UAA4B;AAC/E,QAAM,MAAM,kBAAkB,QAAQ,QAAQ;AAC9C,MAAI,CAAC,KAAK;AACR,WAAO,CAAC;AAAA,EACV;AACA,SAAO,OAAO,KAAK,IAAI,UAAU;AACnC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sanity/groq-lint",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "GROQ linting engine, rules, and CLI for Sanity Lint",
|
|
5
|
+
"author": "Sanity.io <hello@sanity.io>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sanity-io/sanity-lint.git",
|
|
10
|
+
"directory": "packages/groq-lint"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./schema": {
|
|
19
|
+
"types": "./dist/schema.d.ts",
|
|
20
|
+
"import": "./dist/schema.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"main": "./dist/index.js",
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"bin": {
|
|
26
|
+
"groq-lint": "./dist/cli.js"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@sanity/codegen": "^5.1.0",
|
|
33
|
+
"groq-js": "^1.14.0",
|
|
34
|
+
"@sanity/groq-wasm": "0.0.1",
|
|
35
|
+
"@sanity/lint-core": "0.0.1"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"tsup": "^8.3.5",
|
|
39
|
+
"typescript": "^5.7.2",
|
|
40
|
+
"vitest": "^2.1.8"
|
|
41
|
+
},
|
|
42
|
+
"keywords": [
|
|
43
|
+
"sanity",
|
|
44
|
+
"groq",
|
|
45
|
+
"lint",
|
|
46
|
+
"linter"
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "tsup",
|
|
53
|
+
"dev": "tsup --watch",
|
|
54
|
+
"typecheck": "tsc --noEmit",
|
|
55
|
+
"clean": "rm -rf dist"
|
|
56
|
+
}
|
|
57
|
+
}
|