From eb94efb637a1b9166a575740d4523b08b3725da6 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Thu, 8 Aug 2024 10:19:21 -0700 Subject: [PATCH 1/3] WIP Units string literal extraction --- package.json | 1 + pnpm-lock.yaml | 19 +++++++++++++++++++ scripts/extract-constants.ts | 17 +++++++++++++++++ 3 files changed, 37 insertions(+) create mode 100644 scripts/extract-constants.ts 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 33739f4aa8..d91aab1a6e 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 @@ -8910,6 +8913,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'} @@ -9300,6 +9313,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/extract-constants.ts b/scripts/extract-constants.ts new file mode 100644 index 0000000000..9d254d11dd --- /dev/null +++ b/scripts/extract-constants.ts @@ -0,0 +1,17 @@ +#!/usr/bin/env node + +import fs from "fs-extra"; +import { ast, query } from "@phenomnomnominal/tsquery"; + +(async () => { + try { + const helpersTs = fs + .readFileSync("packages/turf-helpers/index.ts", "utf8") + .toString(); + const theAst = ast(helpersTs); + const tNodes = query(theAst, "TypeAliasDeclaration"); + console.log(tNodes.length); // 7 + } catch (e) { + console.warn(e); + } +})(); From 843937d6a6893dd90f19e8bc3e99aa453f0fe496 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Thu, 8 Aug 2024 20:24:10 -0700 Subject: [PATCH 2/3] switch to using generate-readmes directly --- scripts/extract-constants.ts | 17 ----------------- scripts/generate-readmes.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 17 deletions(-) delete mode 100644 scripts/extract-constants.ts diff --git a/scripts/extract-constants.ts b/scripts/extract-constants.ts deleted file mode 100644 index 9d254d11dd..0000000000 --- a/scripts/extract-constants.ts +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env node - -import fs from "fs-extra"; -import { ast, query } from "@phenomnomnominal/tsquery"; - -(async () => { - try { - const helpersTs = fs - .readFileSync("packages/turf-helpers/index.ts", "utf8") - .toString(); - const theAst = ast(helpersTs); - const tNodes = query(theAst, "TypeAliasDeclaration"); - console.log(tNodes.length); // 7 - } catch (e) { - console.warn(e); - } -})(); diff --git a/scripts/generate-readmes.ts b/scripts/generate-readmes.ts index 907cac214f..0dfa3a9875 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,34 @@ const yaml = require("yamljs"); /{module}/, name )}`; + + if (packagePath.includes("turf-helpers")) { + const helpersTs = fs.readFileSync(indexPath, "utf8").toString(); + const theAst = ast(helpersTs); + + // Extract unit names + + // ToDo: This is a 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); + + // 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); From a69257d4971a3279ed770f9150796ab8e5d30fb8 Mon Sep 17 00:00:00 2001 From: Tim Welch Date: Thu, 8 Aug 2024 21:19:06 -0700 Subject: [PATCH 3/3] add selectors, not quite correct --- scripts/generate-readmes.ts | 42 +++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/scripts/generate-readmes.ts b/scripts/generate-readmes.ts index 0dfa3a9875..4069a4f441 100755 --- a/scripts/generate-readmes.ts +++ b/scripts/generate-readmes.ts @@ -65,9 +65,10 @@ const { ast, query } = require("@phenomnomnominal/tsquery"); const helpersTs = fs.readFileSync(indexPath, "utf8").toString(); const theAst = ast(helpersTs); - // Extract unit names + // To learn how to write tsquery selectors, see: https://tsquery-playground.firebaseapp.com/ and paste the code from turf-helpers/index.ts - // ToDo: This is a malformed selector. In addition to Units, it includes StringLiterals from AreaUnits as well (hectares, acres, degrees, radians) + // 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' @@ -79,6 +80,43 @@ const { ast, query } = require("@phenomnomnominal/tsquery"); 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";