Skip to content

Commit

Permalink
add selectors, not quite correct
Browse files Browse the repository at this point in the history
  • Loading branch information
twelch committed Aug 9, 2024
1 parent 843937d commit a69257d
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions scripts/generate-readmes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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";
Expand Down

0 comments on commit a69257d

Please sign in to comment.