doc-sync-check 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +46 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +51 -0
- package/dist/src/extractor.d.ts +7 -0
- package/dist/src/extractor.js +38 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +2 -0
- package/dist/src/validator.d.ts +2 -0
- package/dist/src/validator.js +50 -0
- package/dist/tests/extractor.test.d.ts +1 -0
- package/dist/tests/extractor.test.js +24 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chetan Basuray
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# doc-sync-check
|
|
2
|
+
|
|
3
|
+
> **Stop documentation drift in its tracks.**
|
|
4
|
+
|
|
5
|
+
`doc-sync-check` is a fast, specialized CLI tool that statically analyzes your TypeScript codebase using an Abstract Syntax Tree (AST). It scans your Markdown files for function names and ensures that the documented function signatures match the actual source code perfectly.
|
|
6
|
+
|
|
7
|
+
If you update a parameter or return type in your code but forget to update the documentation, `doc-sync-check` will catch it and fail your CI build, reminding your team to keep the docs in sync!
|
|
8
|
+
|
|
9
|
+
## š Installation
|
|
10
|
+
|
|
11
|
+
You can install `doc-sync-check` globally, but it is recommended to add it as a `devDependency` to your project and run it via an npm script or CI pipeline.
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install -D doc-sync-check
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## š ļø Usage
|
|
18
|
+
|
|
19
|
+
Run `doc-sync-check` by pointing it to your source code directory and specifying your documentation folder.
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
npx doc-sync-check <source-dir> --docs <docs-dir>
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Options
|
|
26
|
+
- `<source-dir>`: The root directory containing your TypeScript files.
|
|
27
|
+
- `--docs, -d`: The path to the folder containing your Markdown documentation files. Defaults to `./docs`.
|
|
28
|
+
|
|
29
|
+
### Example
|
|
30
|
+
```bash
|
|
31
|
+
npx doc-sync-check src --docs docs
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## š§ How it Works
|
|
35
|
+
|
|
36
|
+
1. **Extraction**: Parsers comb through your target TypeScript directory looking for exported functions.
|
|
37
|
+
2. **Analysis**: The script breaks apart definitions into precise semantic substrings (e.g. tracking `userAuth(uuid: string, options?: any): boolean | void`).
|
|
38
|
+
3. **Drift Detection**: Any Markdown file mentioning a detected function name by exact match must also include its corresponding up-to-date signature. If it doesn't, the CLI flags a drift warning and immediately fails the process (Exit Code 1).
|
|
39
|
+
|
|
40
|
+
## š¤ Contributing
|
|
41
|
+
|
|
42
|
+
We welcome community contributions! Please check out our [Contributing Guide](CONTRIBUTING.md) to get started on setting up the repository, running tests, and understanding the architecture.
|
|
43
|
+
|
|
44
|
+
## š License
|
|
45
|
+
|
|
46
|
+
This project is licensed under the [MIT License](LICENSE).
|
package/dist/src/cli.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import meow from 'meow';
|
|
3
|
+
import { globby } from 'globby';
|
|
4
|
+
import fs from 'fs-extra';
|
|
5
|
+
import { extractSignatures } from './extractor.js';
|
|
6
|
+
import { checkDrift } from './validator.js';
|
|
7
|
+
const cli = meow(`
|
|
8
|
+
Usage
|
|
9
|
+
$ doc-sync-check <source-dir>
|
|
10
|
+
|
|
11
|
+
Options
|
|
12
|
+
--docs, -d Path to documentation folder (default: ./docs)
|
|
13
|
+
|
|
14
|
+
Examples
|
|
15
|
+
$ doc-sync-check src --docs ./documentation
|
|
16
|
+
`, {
|
|
17
|
+
importMeta: import.meta,
|
|
18
|
+
flags: {
|
|
19
|
+
docs: {
|
|
20
|
+
type: 'string',
|
|
21
|
+
shortFlag: 'd',
|
|
22
|
+
default: './docs'
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
async function run() {
|
|
27
|
+
const sourceDir = cli.input[0];
|
|
28
|
+
if (!sourceDir) {
|
|
29
|
+
console.error("Please specify a source directory.");
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
console.log(`š Scanning ${sourceDir} for documentation drift...`);
|
|
33
|
+
// 1. Find all TypeScript files
|
|
34
|
+
const files = await globby(`${sourceDir}/**/*.ts`);
|
|
35
|
+
const allSigs = [];
|
|
36
|
+
for (const file of files) {
|
|
37
|
+
const code = fs.readFileSync(file, 'utf-8');
|
|
38
|
+
const sigs = extractSignatures(code);
|
|
39
|
+
if (sigs.length > 0) {
|
|
40
|
+
allSigs.push(...sigs);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
console.log(`\nš Checking against documentation in ${cli.flags.docs}...`);
|
|
44
|
+
const hasDrift = await checkDrift(allSigs, cli.flags.docs);
|
|
45
|
+
if (hasDrift) {
|
|
46
|
+
console.error("\nā Drift check failed. Please update your documentation.");
|
|
47
|
+
process.exit(1);
|
|
48
|
+
}
|
|
49
|
+
console.log("\nā
Drift check complete. No issues found.");
|
|
50
|
+
}
|
|
51
|
+
run();
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import * as babelTraverse from '@babel/traverse';
|
|
3
|
+
const traverse = (babelTraverse.default?.default || babelTraverse.default || babelTraverse);
|
|
4
|
+
export function extractSignatures(code) {
|
|
5
|
+
const signatures = [];
|
|
6
|
+
try {
|
|
7
|
+
const ast = parse(code, {
|
|
8
|
+
sourceType: "module",
|
|
9
|
+
plugins: ["typescript"]
|
|
10
|
+
});
|
|
11
|
+
traverse(ast, {
|
|
12
|
+
// TypeScript now knows this is a type-only reference
|
|
13
|
+
ExportNamedDeclaration(path) {
|
|
14
|
+
const { declaration } = path.node;
|
|
15
|
+
if (declaration && declaration.type === 'FunctionDeclaration') {
|
|
16
|
+
const name = declaration.id?.name || 'anonymous';
|
|
17
|
+
const parameters = declaration.params.map((param) => {
|
|
18
|
+
return code.substring(param.start, param.end);
|
|
19
|
+
});
|
|
20
|
+
const returnType = declaration.returnType
|
|
21
|
+
? code.substring(declaration.returnType.start, declaration.returnType.end)
|
|
22
|
+
: '';
|
|
23
|
+
const fullSignature = `${name}(${parameters.join(', ')})${returnType}`;
|
|
24
|
+
signatures.push({
|
|
25
|
+
name,
|
|
26
|
+
parameters,
|
|
27
|
+
returnType,
|
|
28
|
+
fullSignature,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
console.error("Parsing error:", e);
|
|
36
|
+
}
|
|
37
|
+
return signatures;
|
|
38
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { globby } from 'globby';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
function normalizeSpace(str) {
|
|
4
|
+
return str.replace(/\s+/g, ' ').trim();
|
|
5
|
+
}
|
|
6
|
+
export async function checkDrift(signatures, docsDir) {
|
|
7
|
+
const mdFiles = await globby(`${docsDir}/**/*.md`);
|
|
8
|
+
if (mdFiles.length === 0) {
|
|
9
|
+
console.warn(`No markdown files found in ${docsDir}`);
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
const docs = await Promise.all(mdFiles.map(async (file) => {
|
|
13
|
+
const content = await fs.readFile(file, 'utf-8');
|
|
14
|
+
return {
|
|
15
|
+
path: file,
|
|
16
|
+
content,
|
|
17
|
+
normalizedContent: normalizeSpace(content)
|
|
18
|
+
};
|
|
19
|
+
}));
|
|
20
|
+
let hasDrift = false;
|
|
21
|
+
for (const sig of signatures) {
|
|
22
|
+
const normalizedSig = normalizeSpace(sig.fullSignature);
|
|
23
|
+
let nameFound = false;
|
|
24
|
+
let signatureFound = false;
|
|
25
|
+
for (const doc of docs) {
|
|
26
|
+
// Check if the exact function name is present (bounded by non-word chars if possible, but includes is fine for now)
|
|
27
|
+
// A simple includes might match 'a' inside 'banana', but 'sig.name' usually is a full word.
|
|
28
|
+
const nameRegex = new RegExp(`\\b${sig.name}\\b`);
|
|
29
|
+
if (nameRegex.test(doc.content)) {
|
|
30
|
+
nameFound = true;
|
|
31
|
+
if (doc.normalizedContent.includes(normalizedSig)) {
|
|
32
|
+
signatureFound = true;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (nameFound && !signatureFound) {
|
|
38
|
+
console.error(`ā DRIFT DETECTED: Function '${sig.name}' is mentioned in documentation, but the updated signature was not found.`);
|
|
39
|
+
console.error(` Expected to find: ${sig.fullSignature}`);
|
|
40
|
+
hasDrift = true;
|
|
41
|
+
}
|
|
42
|
+
else if (nameFound && signatureFound) {
|
|
43
|
+
console.log(`ā
IN SYNC: '${sig.name}' is correctly documented.`);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
console.log(`ā ļø UNDOCUMENTED: '${sig.name}' was not found in any documentation.`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return hasDrift;
|
|
50
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { extractSignatures } from '../src/extractor.js';
|
|
2
|
+
describe('AST Extractor', () => {
|
|
3
|
+
it('should correctly extract function name and parameters', () => {
|
|
4
|
+
const code = `
|
|
5
|
+
export function testAuth(token: string, options?: any): boolean {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
`;
|
|
9
|
+
const signatures = extractSignatures(code);
|
|
10
|
+
expect(signatures).toHaveLength(1);
|
|
11
|
+
expect(signatures[0].name).toBe('testAuth');
|
|
12
|
+
expect(signatures[0].parameters).toEqual(['token: string', 'options?: any']);
|
|
13
|
+
expect(signatures[0].returnType).toBe(': boolean');
|
|
14
|
+
});
|
|
15
|
+
it('should correctly assemble the full signature string', () => {
|
|
16
|
+
const code = `
|
|
17
|
+
export function getUser(id: number): any {
|
|
18
|
+
return { name: "Test" };
|
|
19
|
+
}
|
|
20
|
+
`;
|
|
21
|
+
const signatures = extractSignatures(code);
|
|
22
|
+
expect(signatures[0].fullSignature).toBe('getUser(id: number): any');
|
|
23
|
+
});
|
|
24
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "doc-sync-check",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Documentation Drift Detector",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=16"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"bin": {
|
|
14
|
+
"doc-sync-check": "./dist/cli.js"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc",
|
|
18
|
+
"start": "node dist/cli.js",
|
|
19
|
+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
|
|
20
|
+
"lint": "eslint 'src/**/*.ts' 'tests/**/*.ts'",
|
|
21
|
+
"typecheck": "tsc --noEmit",
|
|
22
|
+
"prepublishOnly": "npm run build",
|
|
23
|
+
"release": "standard-version",
|
|
24
|
+
"prepare": "husky"
|
|
25
|
+
},
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/chetanbasuray/doc-sync-check.git"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [],
|
|
31
|
+
"author": "Chetan Basuray",
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"type": "module",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/chetanbasuray/doc-sync-check/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/chetanbasuray/doc-sync-check#readme",
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@babel/parser": "^7.29.2",
|
|
40
|
+
"@babel/traverse": "^7.29.0",
|
|
41
|
+
"fs-extra": "^11.3.4",
|
|
42
|
+
"globby": "^16.2.0",
|
|
43
|
+
"meow": "^14.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@commitlint/cli": "^20.5.0",
|
|
47
|
+
"@commitlint/config-conventional": "^20.5.0",
|
|
48
|
+
"@eslint/js": "^10.0.1",
|
|
49
|
+
"@types/babel__traverse": "^7.28.0",
|
|
50
|
+
"@types/fs-extra": "^11.0.4",
|
|
51
|
+
"@types/jest": "^30.0.0",
|
|
52
|
+
"@types/node": "^25.5.2",
|
|
53
|
+
"conventional-changelog-conventionalcommits": "^9.3.1",
|
|
54
|
+
"eslint": "^10.2.0",
|
|
55
|
+
"husky": "^9.1.7",
|
|
56
|
+
"jest": "^30.3.0",
|
|
57
|
+
"semantic-release": "^25.0.3",
|
|
58
|
+
"standard-version": "^9.5.0",
|
|
59
|
+
"ts-jest": "^29.4.9",
|
|
60
|
+
"ts-node": "^10.9.2",
|
|
61
|
+
"typescript": "^6.0.2",
|
|
62
|
+
"typescript-eslint": "^8.58.0"
|
|
63
|
+
},
|
|
64
|
+
"overrides": {
|
|
65
|
+
"picomatch": "^4.0.4",
|
|
66
|
+
"brace-expansion": "^5.0.5"
|
|
67
|
+
}
|
|
68
|
+
}
|