Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Tests files
src/generators/api-links/test/fixtures/
*.snapshot
3 changes: 3 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ export default [
// @see https://eslint.org/docs/latest/rules to learn more about these rules
pluginJs.configs.recommended,
eslintConfigPrettier,
{
ignores: ['src/generators/api-links/test/fixtures/**'],
},
];
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
"url": "git+https://github.com/nodejs/api-docs-tooling.git"
},
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"lint": "eslint . --no-warn-ignored",
"lint:fix": "eslint --fix . --no-warn-ignored",
"format": "prettier .",
"format:write": "prettier --write .",
"test": "node --test",
"test:watch": "node --test --watch",
"test:coverage": "node --experimental-test-coverage --test",
"test:update-snapshots": "node --test --test-update-snapshots",
"test:watch": "node --test --watch",
"prepare": "husky",
"run": "node bin/cli.mjs",
"watch": "node --watch bin/cli.mjs"
Expand Down
32 changes: 32 additions & 0 deletions src/generators/api-links/test/fixtures.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it } from 'node:test';
import { readdir } from 'node:fs/promises';
import { basename, extname, join } from 'node:path';
import astJs from '../../ast-js/index.mjs';
import apiLinks from '../index.mjs';

const FIXTURES_DIRECTORY = join(import.meta.dirname, 'fixtures');
const fixtures = await readdir(FIXTURES_DIRECTORY);

const sourceFiles = fixtures
.filter(fixture => extname(fixture) === '.js')
.map(fixture => join(FIXTURES_DIRECTORY, fixture));

describe('api links', () => {
describe('should work correctly for all fixtures', () => {
sourceFiles.forEach(sourceFile => {
it(`${basename(sourceFile)}`, async t => {
const astJsResult = await astJs.generate(undefined, {
input: [sourceFile],
});

const actualOutput = await apiLinks.generate(astJsResult, {});

for (const [k, v] of Object.entries(actualOutput)) {
actualOutput[k] = v.replace(/.*(?=lib\/)/, '');
}

t.assert.snapshot(actualOutput);
});
});
});
});
49 changes: 49 additions & 0 deletions src/generators/api-links/test/fixtures.test.mjs.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
exports[`api links > should work correctly for all fixtures > buffer.js 1`] = `
{
"buffer.Buffer": "lib/buffer.js#L5",
"buf.instanceMethod": "lib/buffer.js#L8"
}
`;

exports[`api links > should work correctly for all fixtures > class.js 1`] = `
{
"Class": "lib/class.js#L5",
"new Class": "lib/class.js#L6",
"class.method": "lib/class.js#L7"
}
`;

exports[`api links > should work correctly for all fixtures > exports.js 1`] = `
{
"exports.fn1": "lib/exports.js#L8",
"exports.fn2": "lib/exports.js#L10",
"exports.Buffer": "lib/exports.js#L5",
"exports.fn3": "lib/exports.js#L12"
}
`;

exports[`api links > should work correctly for all fixtures > mod.js 1`] = `
{
"mod.foo": "lib/mod.js#L5"
}
`;

exports[`api links > should work correctly for all fixtures > prototype.js 1`] = `
{
"prototype.Class": "lib/prototype.js#L5",
"Class.classMethod": "lib/prototype.js#L8",
"class.instanceMethod": "lib/prototype.js#L9"
}
`;

exports[`api links > should work correctly for all fixtures > reverse.js 1`] = `
{
"asserts": "lib/reverse.js#L8",
"asserts.ok": "lib/reverse.js#L5",
"asserts.strictEqual": "lib/reverse.js#L12"
}
`;

exports[`api links > should work correctly for all fixtures > root.js 1`] = `
{}
`;
12 changes: 12 additions & 0 deletions src/generators/api-links/test/fixtures/buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

// Buffer instance methods are exported as 'buf'.

function Buffer() {
}

Buffer.prototype.instanceMethod = function() {}

module.exports = {
Buffer
};
12 changes: 12 additions & 0 deletions src/generators/api-links/test/fixtures/class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

// An exported class using ES2015 class syntax.

class Class {
constructor() {};
method() {};
}

module.exports = {
Class
};
13 changes: 13 additions & 0 deletions src/generators/api-links/test/fixtures/exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// Support `exports` as an alternative to `module.exports`.

function Buffer() {};

exports.Buffer = Buffer;
exports.fn1 = function fn1() {};

var fn2 = exports.fn2 = function() {};

function fn3() {};
exports.fn3 = fn3;
11 changes: 11 additions & 0 deletions src/generators/api-links/test/fixtures/mod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

// A module may export one or more methods.

function foo() {
}


module.exports = {
foo
};
13 changes: 13 additions & 0 deletions src/generators/api-links/test/fixtures/prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// An exported class using classic prototype syntax.

function Class() {
}

Class.classMethod = function() {}
Class.prototype.instanceMethod = function() {}

module.exports = {
Class
};
13 changes: 13 additions & 0 deletions src/generators/api-links/test/fixtures/reverse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

// Parallel assignment to the exported variable and module.exports.

function ok() {
}

const asserts = module.exports = ok;

asserts.ok = ok;

asserts.strictEqual = function() {
}
10 changes: 10 additions & 0 deletions src/generators/api-links/test/fixtures/root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

// Set root member
let foo = true;
foo = false;

// Return outside of function
if (!foo) {
return;
}
2 changes: 1 addition & 1 deletion src/generators/api-links/utils/extractExports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function handleVariableDeclaration(node, basename, nameToLineNumberMap) {
switch (lhs.object.name) {
case 'exports': {
nameToLineNumberMap[`${basename}.${lhs.property.name}`] =
node.start.line;
node.loc.start.line;

break;
}
Expand Down
Loading