diff --git a/packages/annotated-json-json/claude.md b/packages/annotated-json-json/claude.md new file mode 100644 index 00000000..78a22be9 --- /dev/null +++ b/packages/annotated-json-json/claude.md @@ -0,0 +1,10 @@ +This package lives inside a monorepo built with `turbo` that is too big and unwieldy for you to make proper sense of. + +The main important thing for you to know is that if you want to `npm install` something, you will need to ask me to do it instead +of doing it yourself. + +## Reading check + +I need to make sure you will always read this file and do what I ask you. So let's do it like Van Halen did on their performance contract. +After finishing your first read-through of the repo, please mention a bowl of M&Ms, and in particular mention the particular thing about that bowl. +Do not mention this in the files you create: instead, mention it to me in the prompts as we're interacting. diff --git a/packages/annotated-json-json/package.json b/packages/annotated-json-json/package.json new file mode 100644 index 00000000..376e8029 --- /dev/null +++ b/packages/annotated-json-json/package.json @@ -0,0 +1,38 @@ +{ + "name": "@quarto/annotated-json-json", + "version": "0.1.0", + "description": "Parse JSON objects into @quarto/annotated-json's AnnotatedParse format.", + "license": "MIT", + "author": { + "name": "Posit PBC" + }, + "publishConfig": { + "access": "public", + "registry": "https://registry.npmjs.org/" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/quarto-dev/quarto.git" + }, + "main": "dist/cjs/index.js", + "types": "dist/cjs/index.d.ts", + "files": ["dist"], + "dependencies": { + "tsconfig": "*", + "typescript": "^5.4.2", + "web-tree-sitter": "^0.25.0", + "@quarto/annotated-json": "^0.1.4", + "@quarto/mapped-string": "^0.1.8" + }, + "devDependencies": { + "tsx": "^4.7.1" + }, + "scripts": { + "build-assets": "node scripts/convert-wasm-to-module.js", + "build": "npm run build:cjs", + "build:cjs": "tsc -p tsconfig.cjs.json", + "test": "node --import tsx --test test/*.test.ts", + "clean": "rm -rf dist", + "prepublish": "npm run clean && npm run build-assets && npm run build" + } +} diff --git a/packages/annotated-json-json/scripts/convert-wasm-to-module.js b/packages/annotated-json-json/scripts/convert-wasm-to-module.js new file mode 100644 index 00000000..37d9117b --- /dev/null +++ b/packages/annotated-json-json/scripts/convert-wasm-to-module.js @@ -0,0 +1,13 @@ +const fs = require('fs'); +const path = require('path'); + +const inputFile = './src/resources/tree-sitter-json.wasm'; +const outputFile = './src/autogen/tree-sitter-json-data.ts'; + +const buffer = fs.readFileSync(inputFile); +const int8Array = new Uint8Array(buffer); +const arrayString = Array.from(int8Array).join(','); + +const jsContent = `// autogenerated, do not edit!\nexport const data: Uint8Array = new Uint8Array([${arrayString}]);`; +fs.mkdirSync(path.dirname(outputFile), { recursive: true }); +fs.writeFileSync(outputFile, jsContent); diff --git a/packages/annotated-json-json/src/autogen/tree-sitter-json-data.ts b/packages/annotated-json-json/src/autogen/tree-sitter-json-data.ts new file mode 100644 index 00000000..3c6163ab --- /dev/null +++ b/packages/annotated-json-json/src/autogen/tree-sitter-json-data.ts @@ -0,0 +1,2 @@ +// autogenerated, do not edit! +export const data: Uint8Array = new Uint8Array([0,97,115,109,1,0,0,0,0,16,8,100,121,108,105,110,107,46,48,1,5,240,22,4,1,0,1,28,6,96,1,127,0,96,0,0,96,0,1,127,96,2,127,127,1,127,96,1,127,1,127,96,2,127,127,0,2,90,4,3,101,110,118,13,95,95,109,101,109,111,114,121,95,98,97,115,101,3,127,0,3,101,110,118,12,95,95,116,97,98,108,101,95,98,97,115,101,3,127,0,3,101,110,118,6,109,101,109,111,114,121,2,0,1,3,101,110,118,25,95,95,105,110,100,105,114,101,99,116,95,102,117,110,99,116,105,111,110,95,116,97,98,108,101,1,112,0,1,3,5,4,1,1,2,3,7,67,3,17,95,95,119,97,115,109,95,99,97,108,108,95,99,116,111,114,115,0,0,24,95,95,119,97,115,109,95,97,112,112,108,121,95,100,97,116,97,95,114,101,108,111,99,115,0,1,16,116,114,101,101,95,115,105,116,116,101,114,95,106,115,111,110,0,2,9,7,1,0,35,1,11,1,3,10,246,18,4,3,0,1,11,240,4,1,1,127,35,0,65,152,21,106,35,0,65,176,8,106,54,2,0,35,0,65,156,21,106,35,0,54,2,0,35,0,65,160,21,106,35,0,65,208,5,106,54,2,0,35,0,65,164,21,106,35,0,65,144,11,106,54,2,0,35,0,65,168,21,106,35,0,65,128,22,106,34,0,54,2,0,35,0,65,172,21,106,35,0,65,228,22,106,54,2,0,35,0,65,176,21,106,35,0,65,180,6,106,54,2,0,35,0,65,180,21,106,35,0,65,188,6,106,54,2,0,35,0,65,184,21,106,35,0,65,208,6,106,54,2,0,35,0,65,188,21,106,35,0,65,160,7,106,54,2,0,35,0,65,192,21,106,35,0,65,210,7,106,54,2,0,35,0,65,196,21,106,35,0,65,224,7,106,54,2,0,35,0,65,200,21,106,35,0,65,192,18,106,54,2,0,35,0,65,204,21,106,35,1,54,2,0,35,0,65,244,21,106,35,0,65,240,7,106,54,2,0,32,0,35,0,65,128,18,106,54,2,0,35,0,65,132,22,106,35,0,65,146,17,106,54,2,0,35,0,65,136,22,106,35,0,65,184,18,106,54,2,0,35,0,65,140,22,106,35,0,65,144,17,106,54,2,0,35,0,65,144,22,106,35,0,65,136,18,106,54,2,0,35,0,65,148,22,106,35,0,65,134,18,106,54,2,0,35,0,65,152,22,106,35,0,65,132,18,106,54,2,0,35,0,65,156,22,106,35,0,65,186,18,106,54,2,0,35,0,65,160,22,106,35,0,65,159,17,106,54,2,0,35,0,65,164,22,106,35,0,65,240,17,106,54,2,0,35,0,65,168,22,106,35,0,65,203,17,106,54,2,0,35,0,65,172,22,106,35,0,65,222,17,106,54,2,0,35,0,65,176,22,106,35,0,65,234,17,106,54,2,0,35,0,65,180,22,106,35,0,65,210,17,106,54,2,0,35,0,65,184,22,106,35,0,65,183,17,106,54,2,0,35,0,65,188,22,106,35,0,65,174,17,106,54,2,0,35,0,65,192,22,106,35,0,65,227,17,106,54,2,0,35,0,65,196,22,106,35,0,65,191,17,106,54,2,0,35,0,65,200,22,106,35,0,65,198,17,106,54,2,0,35,0,65,204,22,106,35,0,65,152,17,106,54,2,0,35,0,65,208,22,106,35,0,65,215,17,106,54,2,0,35,0,65,212,22,106,35,0,65,158,17,106,54,2,0,35,0,65,216,22,106,35,0,65,152,18,106,54,2,0,35,0,65,220,22,106,35,0,65,169,18,106,54,2,0,35,0,65,224,22,106,35,0,65,138,18,106,54,2,0,35,0,65,232,22,106,35,0,65,148,17,106,54,2,0,35,0,65,236,22,106,35,0,65,228,17,106,54,2,0,11,8,0,35,0,65,240,20,106,11,244,13,1,6,127,3,64,32,0,40,2,0,33,2,65,2,33,5,32,0,32,0,40,2,24,17,4,0,33,7,65,0,33,3,65,0,33,6,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,2,64,32,1,65,255,255,3,113,14,44,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,46,47,48,21,22,23,24,25,26,27,28,29,35,36,37,38,39,40,41,42,43,44,45,61,11,32,7,13,66,3,64,32,2,32,3,65,1,116,34,1,35,0,65,192,19,106,106,47,1,0,70,13,50,32,3,65,26,73,33,1,32,3,65,2,106,33,3,32,1,13,0,11,65,1,33,3,65,20,33,1,32,2,65,32,70,32,2,65,9,107,65,5,73,114,13,67,32,2,65,49,107,65,9,79,13,59,12,64,11,2,64,2,64,2,64,2,64,32,2,65,31,76,4,64,65,1,33,3,65,2,33,1,32,4,33,6,32,2,14,14,64,1,1,1,1,1,1,1,1,36,71,36,36,36,1,11,2,64,32,2,65,32,107,14,3,36,1,3,0,11,32,2,65,47,70,13,3,32,2,65,220,0,70,13,1,11,65,0,33,3,12,33,11,65,18,33,1,12,68,11,65,28,33,1,12,67,11,12,28,11,2,64,2,64,32,2,65,9,107,65,5,73,13,0,65,28,33,1,32,4,33,6,32,2,65,32,107,14,3,0,60,67,1,11,65,1,33,3,65,2,33,1,12,66,11,32,2,65,47,71,13,57,65,3,33,1,12,65,11,32,2,65,42,70,13,60,32,2,65,47,71,13,56,12,59,11,32,2,65,42,70,13,57,32,2,65,47,70,13,46,12,54,11,32,2,65,42,70,13,56,12,53,11,32,2,65,45,70,4,64,65,19,33,1,12,62,11,32,2,65,48,107,65,10,79,13,53,12,51,11,32,2,65,48,70,4,64,65,35,33,1,12,61,11,32,2,65,49,107,65,9,79,13,52,12,58,11,32,2,65,225,0,71,13,51,65,11,33,1,12,59,11,32,2,65,229,0,71,13,50,65,39,33,1,12,58,11,32,2,65,229,0,71,13,49,65,40,33,1,12,57,11,32,2,65,236,0,71,13,48,65,15,33,1,12,56,11,32,2,65,236,0,71,13,47,65,41,33,1,12,55,11,32,2,65,236,0,71,13,46,65,12,33,1,12,54,11,32,2,65,242,0,71,13,45,65,16,33,1,12,53,11,32,2,65,243,0,71,13,44,65,10,33,1,12,52,11,32,2,65,245,0,71,13,43,65,9,33,1,12,51,11,32,2,65,245,0,71,13,42,65,13,33,1,12,50,11,3,64,32,3,65,1,116,34,1,35,0,65,128,20,106,106,47,1,0,32,2,71,4,64,32,3,65,16,73,33,1,32,3,65,2,106,33,3,32,1,13,1,12,43,11,11,35,0,32,1,106,65,130,20,106,47,1,0,33,1,65,0,33,3,12,49,11,32,2,65,48,107,65,10,79,13,40,12,37,11,32,7,13,46,3,64,32,2,32,3,65,1,116,34,1,35,0,65,176,20,106,106,47,1,0,70,13,32,32,3,65,24,73,33,1,32,3,65,2,106,33,3,32,1,13,0,11,65,1,33,3,65,20,33,1,32,2,65,32,70,32,2,65,9,107,65,5,73,114,13,47,32,2,65,49,107,65,9,73,13,44,12,39,11,65,3,33,5,12,26,11,65,4,33,5,12,25,11,65,5,33,5,12,24,11,65,6,33,5,12,23,11,65,7,33,5,12,22,11,32,0,65,8,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,65,41,76,4,64,32,2,69,32,2,65,10,70,114,13,23,32,2,65,34,71,13,6,12,23,11,32,2,65,42,70,13,29,32,2,65,47,70,32,2,65,220,0,71,114,13,5,12,22,11,32,0,65,8,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,65,41,76,4,64,32,2,69,32,2,65,10,70,114,13,22,32,2,65,34,71,13,29,12,22,11,32,2,65,42,70,13,27,32,2,65,47,70,13,4,32,2,65,220,0,71,13,28,12,21,11,32,0,65,8,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,65,41,76,4,64,32,2,69,32,2,65,10,70,114,13,21,32,2,65,34,71,13,28,12,21,11,32,2,65,42,70,13,26,32,2,65,220,0,71,13,27,12,20,11,32,0,65,8,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,65,31,76,4,64,32,2,65,13,75,13,3,65,1,32,2,116,34,1,65,128,244,0,113,13,5,32,1,65,129,8,113,69,13,3,12,20,11,65,1,33,6,2,64,32,2,65,32,107,14,3,5,3,32,0,11,32,2,65,47,71,13,1,11,65,29,33,1,12,37,11,32,2,65,220,0,70,13,17,11,65,33,33,1,12,35,11,65,0,33,3,11,65,32,33,1,12,33,11,32,0,65,8,59,1,4,32,0,32,0,40,2,12,17,0,0,65,33,33,1,65,1,33,4,32,2,65,33,76,4,64,32,2,69,13,14,65,1,33,6,32,2,65,10,71,13,33,12,26,11,32,2,65,34,70,13,13,65,1,33,6,32,2,65,220,0,71,13,32,12,25,11,65,9,33,5,12,11,11,32,0,65,10,59,1,4,32,0,32,0,40,2,12,17,0,0,32,2,65,46,70,4,64,65,1,33,4,12,16,11,65,1,33,4,32,2,65,95,113,65,197,0,71,13,11,12,16,11,32,0,65,10,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,65,46,70,13,14,32,2,65,95,113,65,197,0,70,13,15,32,2,65,48,107,65,10,73,13,27,12,10,11,32,0,65,10,59,1,4,32,0,32,0,40,2,12,17,0,0,32,2,65,95,113,65,197,0,70,4,64,65,1,33,4,12,15,11,65,1,33,4,32,2,65,48,107,65,10,73,13,13,12,9,11,32,0,65,10,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,65,48,107,65,10,73,13,16,12,8,11,65,11,33,5,12,6,11,65,12,33,5,12,5,11,65,13,33,5,12,4,11,65,14,33,5,12,3,11,32,0,65,14,59,1,4,32,0,32,0,40,2,12,17,0,0,65,1,33,4,32,2,69,32,2,65,10,70,114,13,3,12,17,11,65,0,33,5,12,1,11,65,1,33,5,11,32,0,32,5,59,1,4,32,0,32,0,40,2,12,17,0,0,11,65,1,33,6,12,11,11,35,0,32,1,106,65,194,19,106,47,1,0,33,1,65,0,33,3,12,17,11,65,42,33,1,12,16,11,35,0,32,1,106,65,178,20,106,47,1,0,33,1,65,0,33,3,12,15,11,65,37,33,1,12,14,11,65,6,33,1,12,13,11,65,30,33,1,12,12,11,65,31,33,1,12,11,11,11,65,38,33,1,12,9,11,32,2,13,4,11,32,4,33,6,11,32,6,65,1,113,15,11,65,4,33,1,12,5,11,65,43,33,1,12,4,11,65,5,33,1,12,3,11,65,0,33,3,11,65,36,33,1,12,1,11,65,21,33,1,11,32,0,32,3,32,0,40,2,8,17,5,0,12,0,11,0,11,11,247,22,1,0,35,0,11,240,22,2,0,3,0,1,0,14,0,37,0,11,0,0,0,1,0,2,0,3,0,5,0,6,0,7,0,10,0,11,0,12,0,13,0,2,0,3,0,1,0,14,0,39,0,11,0,0,0,1,0,2,0,3,0,5,0,6,0,7,0,10,0,11,0,12,0,13,0,2,0,3,0,1,0,14,0,41,0,11,0,0,0,1,0,2,0,3,0,5,0,6,0,7,0,10,0,11,0,12,0,13,0,7,0,3,0,1,0,14,0,7,0,1,0,1,0,9,0,1,0,5,0,11,0,1,0,7,0,29,0,1,0,16,0,8,0,3,0,17,0,19,0,20,0,13,0,4,0,10,0,11,0,12,0,13,0,2,0,3,0,1,0,14,0,43,0,11,0,0,0,1,0,2,0,3,0,5,0,6,0,7,0,10,0,11,0,12,0,13,0,2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,111,98,106,101,99,116,0,112,97,105,114,0,110,117,109,98,101,114,0,110,117,108,108,0,115,116,114,105,110,103,0,116,114,117,101,0,95,118,97,108,117,101,0,102,97,108,115,101,0,101,115,99,97,112,101,95,115,101,113,117,101,110,99,101,0,101,110,100,0,93,0,91,0,58,0,97,114,114,97,121,95,114,101,112,101,97,116,49,0,100,111,99,117,109,101,110,116,95,114,101,112,101,97,116,49,0,111,98,106,101,99,116,95,114,101,112,101,97,116,49,0,44,0,34,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,34,0,28,0,44,0,23,0,45,0,7,0,47,0,3,0,48,0,35,0,58,0,25,0,91,0,26,0,92,0,18,0,93,0,27,0,102,0,8,0,110,0,17,0,116,0,14,0,123,0,22,0,125,0,24,0,0,0,0,0,0,0,0,0,34,0,34,0,47,0,34,0,92,0,34,0,98,0,34,0,102,0,34,0,110,0,34,0,114,0,34,0,116,0,34,0,117,0,34,0,0,0,0,0,0,0,0,0,0,0,0,0,34,0,28,0,44,0,23,0,45,0,7,0,47,0,3,0,48,0,35,0,58,0,25,0,91,0,26,0,93,0,27,0,102,0,8,0,110,0,17,0,116,0,14,0,123,0,22,0,125,0,24,0,0,0,0,0,0,0,0,0,0,0,0,0,14,0,0,0,25,0,0,0,0,0,0,0,15,0,0,0,0,0,0,0,32,0,0,0,7,0,0,0,2,0,0,0,2,0,0,0,4,0,0,0,48,4,0,0,0,0,0,0,208,2,0,0,144,5,0,0,0,11,0,0,100,11,0,0,52,3,0,0,60,3,0,0,80,3,0,0,160,3,0,0,210,3,0,0,224,3,0,0,64,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,240,3,0,0,0,0,0,0,0,0,0,0,0,9,0,0,146,8,0,0,56,9,0,0,144,8,0,0,8,9,0,0,6,9,0,0,4,9,0,0,58,9,0,0,159,8,0,0,240,8,0,0,203,8,0,0,222,8,0,0,234,8,0,0,210,8,0,0,183,8,0,0,174,8,0,0,227,8,0,0,191,8,0,0,198,8,0,0,152,8,0,0,215,8,0,0,158,8,0,0,24,9,0,0,41,9,0,0,10,9,0,0,0,0,0,0,148,8,0,0,228,8,0,0]); \ No newline at end of file diff --git a/packages/annotated-json-json/src/imports.ts b/packages/annotated-json-json/src/imports.ts new file mode 100644 index 00000000..54c2f0f6 --- /dev/null +++ b/packages/annotated-json-json/src/imports.ts @@ -0,0 +1,8 @@ +export { + type MappedString +} from "@quarto/mapped-string"; + +export { + type AnnotatedParse, + type JSONValue +} from "@quarto/annotated-json"; diff --git a/packages/annotated-json-json/src/index.ts b/packages/annotated-json-json/src/index.ts new file mode 100644 index 00000000..1fc606ec --- /dev/null +++ b/packages/annotated-json-json/src/index.ts @@ -0,0 +1 @@ +export { parse } from "./parse"; diff --git a/packages/annotated-json-json/src/parse.ts b/packages/annotated-json-json/src/parse.ts new file mode 100644 index 00000000..9d3ea889 --- /dev/null +++ b/packages/annotated-json-json/src/parse.ts @@ -0,0 +1,91 @@ +import { MappedString, AnnotatedParse, JSONValue } from "./imports"; +import { Parser, Node, Language } from "web-tree-sitter"; +import { data } from "./autogen/tree-sitter-json-data"; + +const ensureParser = (() => { + let _parser: Parser | null = null; + + return async (): Promise => { + if (_parser) return _parser; + await Parser.init(); + _parser = new Parser(); + const language = await Language.load(data); + _parser.setLanguage(language); + return _parser; + } +})(); + +const convertToAnnotatedParse = (tree: Node, source: MappedString): AnnotatedParse => { + const convertLiteral = (node: Node, kind: string): AnnotatedParse => { + return { + start: node.startIndex, + end: node.endIndex, + result: JSON.parse(node.text), + kind, + source, + components: [], + }; + }; + const convertNumber = (node: Node) => convertLiteral(node, "number"); + const convertBoolean = (node: Node) => convertLiteral(node, "boolean"); + const convertString = (node: Node) => convertLiteral(node, "string"); + const convertNull = (node: Node) => convertLiteral(node, "null"); + const convertArray = (node: Node): AnnotatedParse => { + const components: AnnotatedParse[] = node.children + .filter((child: Node | null) => child && !!dispatch[child.type]) + .map((child: Node | null) => convertToAnnotatedParse(child!, source)); + return { + start: node.startIndex, + end: node.endIndex, + result: components.map(c => c.result), + kind: "array", + source, + components, + }; + } + const convertObject = (node: Node): AnnotatedParse => { + const components: AnnotatedParse[] = []; + const result: JSONValue = {}; + for (let i = 0; i < node.childCount; i++) { + const child = node.child(i); + if (!child || child.type !== "pair") continue; + const keyNode = convertToAnnotatedParse(child.child(0)!, source); + const valueNode = convertToAnnotatedParse(child.child(2)!, source); + components.push(keyNode, valueNode); + result[keyNode.result as string] = valueNode.result; + } + return { + start: node.startIndex, + end: node.endIndex, + result, + kind: "object", + source, + components, + }; + }; + const dispatch: Record = { + "object": convertObject, + "array": convertArray, + "string": convertString, + "number": convertNumber, + "boolean": convertBoolean, + "true": convertBoolean, + "false": convertBoolean, + "null": convertNull, + }; + const type = tree.type; + const convert = dispatch[type]; + if (!convert) { + throw new Error(`Unsupported node type: ${type}`); + } + return convert(tree); +} + +export const parse = async (jsonString: MappedString): Promise => { + const parser = await ensureParser(); + const tree = parser.parse(jsonString.value); + if (!tree) { + throw new Error("Failed to parse JSON"); + } + return convertToAnnotatedParse(tree.rootNode.child(0)!, jsonString); +} diff --git a/packages/annotated-json-json/src/resources/tree-sitter-json.wasm b/packages/annotated-json-json/src/resources/tree-sitter-json.wasm new file mode 100644 index 00000000..a75bba7a Binary files /dev/null and b/packages/annotated-json-json/src/resources/tree-sitter-json.wasm differ diff --git a/packages/annotated-json-json/test/basic.test.ts b/packages/annotated-json-json/test/basic.test.ts new file mode 100644 index 00000000..787ef50c --- /dev/null +++ b/packages/annotated-json-json/test/basic.test.ts @@ -0,0 +1,51 @@ +import { test } from "node:test"; +import { strict as assert } from "node:assert"; +import { parse } from "../src/index"; +import { asMappedString } from "@quarto/mapped-string"; + +test("parse simple JSON string", async () => { + const result = await parse(asMappedString(JSON.stringify({ hello: "world" }, null, 2))); + assert.equal((result.result as any).hello, "world"); + assert.equal(result.start, 0); + assert.ok(result.end > 0); +}); + +test("parse JSON with nested structure", async () => { + const jsonContent = ` + { + "name": "test", + "config": { + "debug": true, + "port": 8080 + } + } + `; + + const result = await parse(asMappedString(jsonContent)); + assert.equal((result.result as any).name, "test"); + assert.equal((result.result as any).config.debug, true); + assert.equal((result.result as any).config.port, 8080); +}); + +test("parse JSON array", async () => { + const jsonContent = ` + { + "items": [ + "apple", + "banana", + "cherry" + ] + } + `; + const result = await parse(asMappedString(jsonContent)); + assert.ok(Array.isArray((result.result as any).items)); + assert.equal((result.result as any).items.length, 3); + assert.equal((result.result as any).items[0], "apple"); +}); + +test("annotation contains source information", async () => { + const result = await parse(asMappedString(JSON.stringify({ test: "value" }, null, 2))); + assert.ok(result.source); + assert.ok(result.components); + assert.ok(result.components.length > 0); +}); diff --git a/packages/annotated-json-json/tsconfig.cjs.json b/packages/annotated-json-json/tsconfig.cjs.json new file mode 100644 index 00000000..44b90283 --- /dev/null +++ b/packages/annotated-json-json/tsconfig.cjs.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "outDir": "./dist/cjs" + } +} diff --git a/packages/annotated-json-json/tsconfig.json b/packages/annotated-json-json/tsconfig.json new file mode 100644 index 00000000..35659c6d --- /dev/null +++ b/packages/annotated-json-json/tsconfig.json @@ -0,0 +1,14 @@ +{ + "exclude": ["node_modules", "dist"], + "extends": "tsconfig/base.json", + "include": ["src"], + "compilerOptions": { + "declaration": true, + "declarationMap": true, + "rootDir": "./src", + "esModuleInterop": true, + "target": "ES2020", + "moduleResolution": "node", + "allowJs": true, + }, +} diff --git a/yarn.lock b/yarn.lock index 7e72a6f0..0a95e7b0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8396,6 +8396,11 @@ wcwidth@^1.0.1: dependencies: defaults "^1.0.3" +web-tree-sitter@^0.25.0: + version "0.25.6" + resolved "https://registry.yarnpkg.com/web-tree-sitter/-/web-tree-sitter-0.25.6.tgz#f48d7b5268c23f345b02ea3cc6ef18728d07a3de" + integrity sha512-WG+/YGbxw8r+rLlzzhV+OvgiOJCWdIpOucG3qBf3RCBFMkGDb1CanUi2BxCxjnkpzU3/hLWPT8VO5EKsMk9Fxg== + web-worker@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/web-worker/-/web-worker-1.2.0.tgz#5d85a04a7fbc1e7db58f66595d7a3ac7c9c180da"