Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create a script to detect missing functions #846

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Validate

on:
push:
branches:
- main
pull_request:

jobs:
missing-functions:
name: Detect missing functions
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v2
- run: bun install
working-directory: ./scripts
- run: bun run index.ts
working-directory: ./scripts
env:
EXCLUDED_FUNCS: |
.chain
NOT_IMPLEMENTED_FUNCS: |
string::distance::hamming
string::distance::levenshtein
string::similarity::jaro
vector::distance::mahalanobis
vector::similarity::spearman

Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ These functions can be used when analysing numeric data and numeric collections.
<td scope="row" data-label="Description">Represents the mathematical constant π.</td>
</tr>
<tr>
<td scope="row" data-label="Constant"><a href="#mathpow"><code>math::pow()</code></a></td>
<td scope="row" data-label="Function"><a href="#mathpow"><code>math::pow()</code></a></td>
<td scope="row" data-label="Description">Returns a number raised to a power</td>
</tr>
<tr>
Expand Down
175 changes: 175 additions & 0 deletions scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# scripts

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.27. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added scripts/bun.lockb
Binary file not shown.
133 changes: 133 additions & 0 deletions scripts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Glob } from "bun";
import * as cheerio from "cheerio";

const repositoryName = "surrealdb/surrealdb";
const branch = "main";
const fncSourceCodeFilePath = "core/src/fnc/mod.rs";

const fncExclusions = Bun.env.EXCLUDED_FUNCS
? Bun.env.EXCLUDED_FUNCS.split("\n")
: [];
const fncNotImplemented = Bun.env.NOT_IMPLEMENTED_FUNCS
? Bun.env.NOT_IMPLEMENTED_FUNCS.split("\n")
: [];

const detectMissingFunctions = async () => {
console.log("Detecting missing functions...");
console.log("--------------------------------------------------");

const response = await fetch(
`https://raw.githubusercontent.com/${repositoryName}/${branch}/${fncSourceCodeFilePath}`
);
const result = await response.text();

let lineStartForSyncFns = -1;
let lineEndForSyncFns = -1;

let lineStartForAsyncFns = -1;
let lineEndForAsyncFns = -1;

const lines = result.split("\n");

for (let i = 0; i < lines.length; i++) {
const line = lines[i];

if (line.startsWith("pub fn synchronous(")) {
lineStartForSyncFns = i;
continue;
}
if (line.startsWith("pub async fn asynchronous(")) {
lineStartForAsyncFns = i;
continue;
}

if (line === "}") {
if (lineStartForSyncFns > -1 && lineEndForSyncFns === -1) {
lineEndForSyncFns = i;
continue;
}
if (lineStartForAsyncFns > -1 && lineEndForAsyncFns === -1) {
lineEndForAsyncFns = i;
continue;
}
}
}

const allExistingFunctions: string[] = [];

for (let i = 0; i < lines.length; i++) {
const line = lines[i];

if (
(lineStartForSyncFns <= i && i < lineEndForSyncFns) ||
(lineStartForAsyncFns <= i && i < lineEndForAsyncFns)
) {
const regexResult = /"(.+)" =>/gi.exec(line);
if (regexResult) {
const functionName = regexResult[1];
if (!fncNotImplemented.includes(functionName)) {
allExistingFunctions.push(regexResult[1]);
}
}
}
}

const allDocumentedFunctions: string[] = [];

const glob = new Glob(
"../doc-surrealql_versioned_docs/version-latest/functions/database/*.mdx"
);
for await (const path of glob.scan(".")) {
if (path.endsWith("index.mdx")) {
continue;
}

const data = await Bun.file(path).text();
const $ = cheerio.load(data);

const functionNames = $('td[data-label="Function"]')
.map((_, el) =>
$(el)
.text()
.replace("()", "")
.replace(/\u200B/g, "")
)
.get()
.filter((functionName) => !fncExclusions.includes(functionName));

allDocumentedFunctions.push(...functionNames);
}

// find undocumented functions
const undocumentedFunctions = allExistingFunctions.filter(
(existingFunction) => !allDocumentedFunctions.includes(existingFunction)
);

if (undocumentedFunctions.length > 0) {
console.log("Undocumented functions:");
for (const undocumentedFunction of undocumentedFunctions) {
console.log(`* ${undocumentedFunction}`);
}

console.log("--------------------------------------------------");
}

// find removed functions (is documented but no longer exists)
const removedFunctions = allDocumentedFunctions.filter(
(documentedFunction) => !allExistingFunctions.includes(documentedFunction)
);

if (removedFunctions.length > 0) {
console.log("Removed function that is currently documented:");
for (const removedFunction of removedFunctions) {
console.log(`* ${removedFunction}`);
}
}

// exit with error code if there are undocumented functions
if (undocumentedFunctions.length > 0) {
process.exit(1);
}
};

await detectMissingFunctions();
14 changes: 14 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "scripts",
"module": "index.ts",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"type": "module",
"dependencies": {
"cheerio": "^1.0.0"
}
}
Loading
Loading