Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
8 changes: 7 additions & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
"all": true,
"exclude": [
"eslint.config.mjs",
"playwright.config.js",
"**/fixtures",
"**/__tests__",
"src/generators/legacy-html/assets",
"src/generators/web/ui",
"**/*.d.ts"
"**/*.d.ts",
"www",
"beta",
"scripts",
"e2e"
]
}
6 changes: 4 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ updates:
- 'rehype-*'
ast:
patterns:
- 'estree-*'
- 'hast-*'
- 'mdast-*'
- 'hastscript'
- 'acorn'
oxc:
patterns:
- 'oxc-*'
- 'esrap'
recma:
patterns:
- 'recma-*'
Expand Down
28 changes: 21 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@
"@orama/ui": "^1.5.4",
"@rollup/plugin-virtual": "^3.0.2",
"@swc/html-wasm": "^1.15.43",
"acorn": "^8.17.0",
"commander": "^15.0.0",
"dedent": "^1.7.2",
"estree-util-to-js": "^2.0.0",
"estree-util-visit": "^2.0.0",
"esrap": "^2.3.0",
"github-slugger": "^2.0.0",
"glob-parent": "^6.0.2",
"hast-util-to-string": "^3.0.1",
Expand Down
3 changes: 1 addition & 2 deletions src/generators/api-links/__tests__/fixtures.test.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { globSync } from 'node:fs';
import { basename, join, relative, sep } from 'node:path';
import { after, before, describe, it } from 'node:test';

import { globSync } from 'tinyglobby';

import createWorkerPool from '../../../threading/index.mjs';
import createParallelWorker from '../../../threading/parallel.mjs';
import { setConfig } from '../../../utils/configuration/index.mjs';
Expand Down
209 changes: 209 additions & 0 deletions src/generators/api-links/__tests__/utils.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import assert from 'node:assert/strict';
import { test } from 'node:test';

import { parse } from 'oxc-parser';

import { checkIndirectReferences } from '../utils/checkIndirectReferences.mjs';
import { extractExports } from '../utils/extractExports.mjs';
import { findDefinitions } from '../utils/findDefinitions.mjs';

async function parseProgram(source) {
const result = await parse('test.js', source, {
lang: 'js',
sourceType: 'commonjs',
range: true,
});

// oxc-parser's ParseResult doesn't attach the original source text to
// `program`, but checkIndirectReferences/findDefinitions rely on
// `program.sourceText` to compute real line numbers via getLineNumber.
// Without this, getLineNumber falls back to a default and line numbers
// silently come back wrong for anything beyond a single line.
result.program.sourceText = source;

return result.program;
}

test('checkIndirectReferences resolves indirect function references', async () => {
const source = `exports.Buffer = Buffer;

function Buffer() {}`;

const program = await parseProgram(source);

const exports = {
ctors: [],
identifiers: [],
indirects: {
Buffer: 'buffer.Buffer',
},
};

const map = {};

checkIndirectReferences(program, exports, map);

assert.equal(map['buffer.Buffer'], 3);
});

test('checkIndirectReferences ignores unknown functions', async () => {
const source = `function Other() {}`;

const program = await parseProgram(source);

const exports = {
ctors: [],
identifiers: [],
indirects: {
Buffer: 'buffer.Buffer',
},
};

const map = {};

checkIndirectReferences(program, exports, map);

assert.deepEqual(map, {});
});

test('extractExports handles exports.function assignment', async () => {
const source = `exports.foo = function () {};`;

const program = await parseProgram(source);

const map = {};

const result = extractExports(program, 'api', map);

assert.deepEqual(result.ctors, []);
assert.deepEqual(result.identifiers, []);
assert.deepEqual(result.indirects, {});

assert.equal(map['api.foo'], 1);
});

test('extractExports handles indirect exports', async () => {
const source = `exports.Buffer = Buffer;`;

const program = await parseProgram(source);

const map = {};

const result = extractExports(program, 'api', map);

assert.deepEqual(result.indirects, {
Buffer: 'api.Buffer',
});
});

test('extractExports handles module.exports constructor', async () => {
const source = `module.exports = new Buffer();`;

const program = await parseProgram(source);

const map = {};

const result = extractExports(program, 'api', map);

assert.deepEqual(result.ctors, ['Buffer']);
});

test('extractExports handles module.exports object exports', async () => {
const source = `module.exports = {
Buffer,
foo
};`;

const program = await parseProgram(source);

const map = {};

const result = extractExports(program, 'api', map);

assert.deepEqual(result.identifiers, ['Buffer', 'foo']);

assert.deepEqual(result.ctors, ['Buffer']);
});

test('findDefinitions finds exported function declaration', async () => {
const source = `exports.foo = foo;

function foo() {}`;

const program = await parseProgram(source);

const map = {};

const exports = {
ctors: [],
identifiers: ['foo'],
indirects: {},
};

findDefinitions(program, 'api', map, exports);

assert.equal(map['api.foo'], 3);
});

test('findDefinitions finds prototype assignments', async () => {
const source = `Buffer.prototype.write = function () {};`;

const program = await parseProgram(source);

const map = {};

const exports = {
ctors: ['Buffer'],
identifiers: [],
indirects: {},
};

findDefinitions(program, 'api', map, exports);

assert.equal(map['buf.write'], 1);
});

test('findDefinitions tracks prototype indirect references', async () => {
const source = `Buffer.prototype.write = write;`;

const program = await parseProgram(source);

const map = {};

const exports = {
ctors: ['Buffer'],
identifiers: [],
indirects: {},
};

findDefinitions(program, 'api', map, exports);

assert.equal(map['buf.write'], 1);

assert.deepEqual(exports.indirects, {
write: 'buf.write',
});
});

test('findDefinitions finds class constructors and methods', async () => {
const source = `class Buffer {
constructor() {}
write() {}
}`;

const program = await parseProgram(source);

const map = {};

const exports = {
ctors: ['Buffer'],
identifiers: [],
indirects: {},
};

findDefinitions(program, 'api', map, exports);

assert.equal(map.Buffer, 1);
assert.equal(map['new Buffer'], 2);
assert.equal(map['buffer.write'], 3);
});
30 changes: 19 additions & 11 deletions src/generators/api-links/utils/checkIndirectReferences.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { visit } from 'estree-util-visit';
import { Visitor } from 'oxc-parser';

import { getLineNumber } from './getLineNumber.mjs';

/**
* @param {import('acorn').Program} program
* @param {import('@oxc-project/types').Program} program
* @param {import('../types.d.ts').ProgramExports} exports
* @param {Record<string, number>} nameToLineNumberMap
*/
Expand All @@ -10,15 +12,21 @@ export function checkIndirectReferences(program, exports, nameToLineNumberMap) {
return;
}

visit(program, node => {
if (!node.loc || node.type !== 'FunctionDeclaration') {
return;
}

const name = node.id.name;
const visitor = new Visitor({
/**
*
*/
FunctionDeclaration(node) {
const name = node.id?.name;

if (name in exports.indirects) {
nameToLineNumberMap[exports.indirects[name]] = node.loc.start.line;
}
if (name && name in exports.indirects) {
nameToLineNumberMap[exports.indirects[name]] = getLineNumber(
program.sourceText,
node.range[0]
);
}
},
});

visitor.visit(program);
}
Loading
Loading