docgen: fix lint issues

This commit is contained in:
Patrik Oldsberg
2020-07-14 23:00:30 +02:00
parent 1fc5f9f468
commit 833d3f4bb4
6 changed files with 27 additions and 29 deletions
+6 -6
View File
@@ -17,28 +17,28 @@
import * as ts from 'typescript';
import { resolve, join, dirname } from 'path';
import { promisify } from 'util';
import fs from 'fs';
import fs from 'fs-extra';
import ApiDocGenerator from './docgen/ApiDocGenerator';
import sortSelector from './docgen/sortSelector';
import TypeLocator from './docgen/TypeLocator';
import ApiDocPrinter from './docgen/ApiDocPrinter';
import TypescriptHighlighter from './docgen/TypescriptHighlighter';
import MarkdownPrinter from './docgen/MarkdownPrinter';
import { sync as mkdirpSync } from 'mkdirp';
const writeFile = promisify(fs.writeFile);
function loadOptions(path: string): ts.CompilerOptions {
let { extends: parent, compilerOptions }: any = require(path);
const config: any = require(path);
let parent = config.extends as string | undefined;
if (!parent) {
return compilerOptions;
return config.compilerOptions;
}
if (parent.startsWith('.')) {
parent = join(dirname(path), parent);
}
return { ...loadOptions(parent), ...compilerOptions };
return { ...loadOptions(parent), ...config.compilerOptions };
}
async function main() {
@@ -84,7 +84,7 @@ async function main() {
() => new MarkdownPrinter(new TypescriptHighlighter()),
);
mkdirpSync(resolve(apiRefsDir, 'docs'));
fs.ensureDirSync(resolve(apiRefsDir, 'docs'));
await Promise.all(
apiDocs.map(apiDoc => {
@@ -116,18 +116,18 @@ export default class ApiDocGenerator {
objectLiteral: ts.ObjectLiteralExpression,
propertyName: string,
): string {
const prop = objectLiteral.properties.filter(
const matchingProp = objectLiteral.properties.filter(
prop =>
ts.isPropertyAssignment(prop) &&
ts.isIdentifier(prop.name) &&
prop.name.text === propertyName,
)[0] as ts.PropertyAssignment;
if (!prop) {
if (!matchingProp) {
throw new Error(`no identifier found for property ${propertyName}`);
}
const { initializer } = prop;
const { initializer } = matchingProp;
if (!ts.isStringLiteral(initializer)) {
throw new Error(`no string literal for ${propertyName}`);
}
@@ -238,22 +238,22 @@ export default class ApiDocGenerator {
type: ts.Type | undefined,
): undefined | { symbol: ts.Symbol; declaration: ts.Declaration } {
if (!type) {
return;
return undefined;
}
const symbol = type.aliasSymbol || type.symbol;
if (!symbol) {
return;
return undefined;
}
const [declaration] = symbol.declarations;
// Don't generate standalone infos for type paramters
if (ts.isTypeParameterDeclaration(declaration)) {
return;
return undefined;
}
if (!declaration.getSourceFile().fileName.startsWith(this.sourcePath)) {
return;
return undefined;
}
return { symbol, declaration };
@@ -108,7 +108,7 @@ export default class MarkdownPrinter {
const parts: Array<{ text: string; path?: string }> = [];
const end = sortedLinks.reduce((prev, link) => {
const endLocation = sortedLinks.reduce((prev, link) => {
const [start, end] = link.location;
parts.push(
{ text: text.slice(prev, start) },
@@ -117,15 +117,14 @@ export default class MarkdownPrinter {
return end;
}, 0);
parts.push({ text: text.slice(end) });
parts.push({ text: text.slice(endLocation) });
return parts
.map(part => {
if (part.path) {
return `<a href="#${part.path}">${this.escapeText(part.text)}</a>`;
} else {
return this.highlighter.highlight(this.escapeText(part.text));
}
return this.highlighter.highlight(this.escapeText(part.text));
})
.join('');
}
+6 -6
View File
@@ -95,30 +95,30 @@ export default class TypeLocator {
| { constructorType: ts.Type; initializer: ts.NewExpression; name: string }
| undefined {
if (!ts.isVariableStatement(node)) {
return;
return undefined;
}
if (
!node.modifiers ||
!node.modifiers.some(mod => mod.kind === ts.SyntaxKind.ExportKeyword)
) {
return;
return undefined;
}
const { declarations } = node.declarationList;
if (declarations.length !== 1) {
return;
return undefined;
}
const [declaration] = declarations;
const { initializer, name } = declaration;
if (!initializer || !name) {
return;
return undefined;
}
if (!ts.isNewExpression(initializer)) {
return;
return undefined;
}
if (!ts.isIdentifier(name)) {
return;
return undefined;
}
const constructorType = this.checker.getTypeAtLocation(
@@ -56,7 +56,7 @@ export default class TypescriptHighlighter implements Highlighter {
], // keywords
] as readonly [RegExp, string][];
highlight(text: string): string {
highlight(fullText: string): string {
// Each part is either plain text that can be highlighted or text that is already highlighted
type HighlightPart = { text: string; highlighted?: boolean };
@@ -80,7 +80,7 @@ export default class TypescriptHighlighter implements Highlighter {
// Order here is important, e.g. comments must be first to avoid string literals inside comments being highlighted
return TypescriptHighlighter.highlighters
.reduce((parts, highlighter) => flatMap(parts, painter(...highlighter)), [
{ text },
{ text: fullText },
])
.map(({ text }) => text)
.join('');
+3 -4
View File
@@ -21,14 +21,13 @@ export default function sortSelector<T>(
selector: (x: T) => any,
): (a: T, b: T) => -1 | 1 | 0 {
return (a: T, b: T) => {
let aV = selector(a);
let bV = selector(b);
const aV = selector(a);
const bV = selector(b);
if (aV < bV) {
return -1;
} else if (aV > bV) {
return 1;
} else {
return 0;
}
return 0;
};
}