feat: tests

This commit is contained in:
2023-04-21 12:08:35 +02:00
parent 8cb9285462
commit d8c79256c9
140 changed files with 2100 additions and 2693 deletions

View File

@@ -12,7 +12,7 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import * as ts from 'typescript';
import ts from 'typescript';
import {cleanupEmpty} from './util.js';
import {LightweightComment} from './types/lightweight-comment.js';
@@ -33,7 +33,7 @@ export function extractComment(node: ts.Node): LightweightComment | undefined {
? undefined
: cleanupEmpty({
shortSummary: comment?.[0],
description: comment?.[comment.length - 1],
description: comment?.slice(1).join('\n\n'),
tags: jsDocument?.tags?.map(tag =>
cleanupEmpty({
name: tag.tagName?.escapedText ?? 'UNRESOLVED_NAME',

View File

@@ -14,7 +14,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import ts from 'typescript';
import {cleanupEmpty, mapNotNil, rejectNil, expandPathToFilesSync} from './util.js';
import {cleanupEmpty, expandPathToFilesSync, mapNotNil, rejectNil} from './util.js';
import {
extractComment,
filterChildrenTo,
@@ -77,7 +77,7 @@ class LightweightDefinitionBuilder {
constructor(sourcePath: string | string[], readonly includeComments: boolean) {
const rootNames = Array.isArray(sourcePath)
? sourcePath
: expandPathToFilesSync(path.resolve(sourcePath), file => file.endsWith('ts'));
: expandPathToFilesSync(path.resolve(sourcePath), it => it.endsWith('.ts'));
this.program = ts.createProgram({
rootNames: rootNames,
@@ -121,7 +121,7 @@ class LightweightDefinitionBuilder {
classLike: ts.ClassDeclaration | ts.InterfaceDeclaration,
): LightweightClassDefinition {
const heritages = mapValues(
groupBy([...classLike.heritageClauses!], it => it.token.toString()),
groupBy([...(classLike.heritageClauses || [])], it => it.token.toString()),
heritages => heritages.flatMap(it => it.types),
);
@@ -162,23 +162,25 @@ class LightweightDefinitionBuilder {
collectProperties(
members: ts.NodeArray<ts.ClassElement | ts.TypeElement>,
): Record<string, LightweightProperty> {
return keyBy(
filterNodeTo(members as ts.NodeArray<ts.ClassElement | ts.TypeElement>, isProperty).map(property =>
cleanupEmpty({
comment: this.includeComments ? extractComment(property) : undefined,
name: resolvePropertyName(property.name) ?? property.getText(),
type: this.lightweightTypeAtNode(property),
properties: this.collectProperties((property.type as ts.TypeLiteralNode)?.members),
optional: ts.isPropertyDeclaration(property)
? property.questionToken === undefined
? undefined
: true
: undefined,
}),
),
it => it.name,
);
): Record<string, LightweightProperty> | undefined {
return members
? keyBy(
filterNodeTo(members as ts.NodeArray<ts.ClassElement | ts.TypeElement>, isProperty).map(property =>
cleanupEmpty({
comment: this.includeComments ? extractComment(property) : undefined,
name: resolvePropertyName(property.name) ?? property.getText(),
type: this.lightweightTypeAtNode(property),
properties: this.collectProperties((property.type as ts.TypeLiteralNode)?.members),
optional: ts.isPropertyDeclaration(property)
? property.questionToken === undefined
? undefined
: true
: undefined,
}),
),
it => it.name,
)
: undefined;
}
private lightweightTypeAtNode(node: ts.Node): LightweightType {

View File

@@ -12,14 +12,14 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
export * from './easy-ast.js'
export * from './ast-util.js'
export * from './easy-ast.js';
export * from './ast-util.js';
export * from './types/lightweight-alias-definition.js'
export * from './types/lightweight-class-definition.js'
export * from './types/lightweight-comment.js'
export * from './types/lightweight-definition.js'
export * from './types/lightweight-definition-kind.js'
export * from './types/lightweight-project.js'
export * from './types/lightweight-property.js'
export * from './types/lightweight-type.js'
export * from './types/lightweight-alias-definition.js';
export * from './types/lightweight-class-definition.js';
export * from './types/lightweight-comment.js';
export * from './types/lightweight-definition.js';
export * from './types/lightweight-definition-kind.js';
export * from './types/lightweight-project.js';
export * from './types/lightweight-property.js';
export * from './types/lightweight-type.js';

View File

@@ -12,15 +12,36 @@
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import path from "path";
import {readdirSync, statSync} from "fs";
import {readdirSync, statSync} from 'fs';
import path from 'path';
/**
* Expand a path to a list of all files deeply contained in it
*/
export function expandPathToFilesSync(sourcePath: string, accept: (fileName: string) => boolean): string[] {
const fullPath = path.resolve(sourcePath);
const directory = statSync(fullPath);
return directory.isDirectory()
? readdirSync(fullPath).flatMap(fragment =>
expandPathToFilesSync(path.resolve(sourcePath, fragment), accept),
)
: [fullPath].filter(accept);
}
/**
* Take a Windows path and make a Unix path out of it
*/
export function toUnixPath(pathString: string): string {
return pathString.replaceAll(path.sep, path.posix.sep);
}
/**
* Filters only defined elements
*/
export function rejectNil<T>(array: Array<T | undefined | null>): T[] {
// eslint-disable-next-line unicorn/no-null
return array.filter(it => it == null) as T[];
return array.filter(it => it != null) as T[];
}
/**
@@ -45,17 +66,3 @@ export function cleanupEmpty<T extends object>(object: T): T {
}
return out;
}
/**
* Expand a path to a list of all files deeply contained in it
*/
export function expandPathToFilesSync(sourcePath: string, accept: (fileName: string) => boolean): string[] {
const fullPath = path.resolve(sourcePath);
const directory = statSync(fullPath);
return directory.isDirectory()
? readdirSync(fullPath).flatMap(fragment =>
expandPathToFilesSync(path.resolve(sourcePath, fragment), accept),
)
: [fullPath].filter(accept);
}