diff --git a/package.json b/package.json index 3521f6e877..21a528b8e0 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "@monorepolint/config": "0.5.0-alpha.132", "@monorepolint/core": "0.5.0-alpha.132", "@monorepolint/rules": "0.5.0-alpha.132", + "@phenomnomnominal/tsquery": "6.1.3", "@types/node": "18.11.9", "@typescript-eslint/eslint-plugin": "^6.10.0", "@typescript-eslint/parser": "^6.10.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 06baabcf4a..07bc97d3e2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -20,6 +20,9 @@ importers: '@monorepolint/rules': specifier: 0.5.0-alpha.132 version: 0.5.0-alpha.132 + '@phenomnomnominal/tsquery': + specifier: 6.1.3 + version: 6.1.3(typescript@5.3.3) '@types/node': specifier: 18.11.9 version: 18.11.9 @@ -8913,6 +8916,16 @@ packages: '@octokit/openapi-types': 18.1.1 dev: true + /@phenomnomnominal/tsquery@6.1.3(typescript@5.3.3): + resolution: {integrity: sha512-CEqpJ872StsxRmwv9ePCZ4BCisrJSlREUC5XxIRYxhvODt4aQoJFFmjTgaP6meyKiiXxxN/VWPZ58j4yHXRkmw==} + peerDependencies: + typescript: ^3 || ^4 || ^5 + dependencies: + '@types/esquery': 1.5.4 + esquery: 1.5.0 + typescript: 5.3.3 + dev: true + /@pkgjs/parseargs@0.11.0: resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} @@ -9303,6 +9316,12 @@ packages: '@types/ms': 0.7.34 dev: true + /@types/esquery@1.5.4: + resolution: {integrity: sha512-yYO4Q8H+KJHKW1rEeSzHxcZi90durqYgWVfnh5K6ZADVBjBv2e1NEveYX5yT2bffgN7RqzH3k9930m+i2yBoMA==} + dependencies: + '@types/estree': 1.0.5 + dev: true + /@types/estree@1.0.5: resolution: {integrity: sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==} dev: true diff --git a/scripts/generate-readmes.ts b/scripts/generate-readmes.ts index 907cac214f..4069a4f441 100755 --- a/scripts/generate-readmes.ts +++ b/scripts/generate-readmes.ts @@ -5,6 +5,7 @@ const { glob } = require("glob"); const path = require("path"); const { loadJsonFileSync } = require("load-json-file"); const yaml = require("yamljs"); +const { ast, query } = require("@phenomnomnominal/tsquery"); (async () => { // documentation v14 has moved to ESM so need to import as if async, and wrap @@ -59,6 +60,72 @@ const yaml = require("yamljs"); /{module}/, name )}`; + + if (packagePath.includes("turf-helpers")) { + const helpersTs = fs.readFileSync(indexPath, "utf8").toString(); + const theAst = ast(helpersTs); + + // To learn how to write tsquery selectors, see: https://tsquery-playground.firebaseapp.com/ and paste the code from turf-helpers/index.ts + + // query Units string literal names + // ToDo: malformed selector. In addition to Units, it includes StringLiterals from AreaUnits as well (hectares, acres, degrees, radians) + const unitLiterals = query( + theAst, + 'TypeAliasDeclaration:has(Identifier[name="Units"]) UnionType LiteralType StringLiteral' + ); + const unitArray = unitLiterals.map((sLiteral) => + sLiteral.getText().replaceAll('"', "") + ); + const unitNames = + unitLiterals.length > 0 ? "Units: " + unitArray.join(", ") : ""; + console.log(unitNames); + + // query all AreaUnits string literals (including exclusions) + const areaUnitAllLiterals = query( + theAst, + 'TypeAliasDeclaration:has(Identifier[name="AreaUnits"]) UnionType > LiteralType StringLiteral' + ); + const areaUnitAllArray = areaUnitAllLiterals.map((sLiteral) => + sLiteral.getText().replaceAll('"', "") + ); + console.log("areaUnitAllArray", areaUnitAllArray); + + // query AreaUnits string literals to Exclude + const areaUnitExcludeLiterals = query( + theAst, + 'TypeAliasDeclaration:has(Identifier[name="AreaUnits"]) UnionType TypeReference LiteralType StringLiteral' + ); + const areaUnitExcludeArray = areaUnitExcludeLiterals.map( + (sLiteral) => sLiteral.getText().replaceAll('"', "") + ); + console.log("areaUnitExcludeArray", areaUnitExcludeArray); + + // AreaUnits to add = all AreaUnits - Exclude AreaUnits + const newAreaUnitArray = areaUnitAllArray.filter( + (name) => !areaUnitExcludeArray.includes(name) + ); + // console.log("newAreaUnitArray", newAreaUnitArray) + + // Subtract Exclude AreaUnits from all AreaUnits and add new ones + const areaUnitArray = unitArray + .filter((name) => !areaUnitExcludeArray.includes(name)) + .concat(newAreaUnitArray); + // console.log("areaUnitArray", areaUnitArray) + const areaUnitNames = + areaUnitArray.length > 0 + ? "Area Units: " + areaUnitArray.join(", ") + : ""; + console.log("areaUnitNames", areaUnitNames); + + // extract area unit names. unit names - exclusions + inclusions + + const insertText = "## helpers\n"; + markdown = markdown.replace( + insertText, + `${insertText}\n### units\n\n${unitNames}\n` + ); + } + if (diagrams.length) markdown += "\n\n### Diagrams\n\n" + diagramToMarkdown(diagrams); fs.writeFileSync(path.join(directory, "README.md"), markdown);