Skip to content

Commit

Permalink
chore: misc/ts-markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Oct 28, 2020
1 parent 0427ee0 commit faf4b63
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 45 deletions.
4 changes: 2 additions & 2 deletions misc/ts-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
"license": "MIT",
"dependencies": {
"chalk": "^4.0.0",
"react-docgen-typescript": "^1.16.1",
"react-docgen-typescript": "^1.20.5",
"remark": "^11.0.2",
"remark-toc": "^7.0.0",
"typedoc": "^0.17.7",
"typescript": "^3.8.3",
"typescript": "^4.0.5",
"unified": "^9.0.0",
"yargs": "^15.3.1"
},
Expand Down
6 changes: 3 additions & 3 deletions misc/ts-markdown/src/blocks/props-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ export const createPropsTable = (
type: 'paragraph',
children: [table],
});
table.children.push.apply(
table.children,
children.map((child: PropItem) => createPropsRow(child)),
// eslint-disable-next-line prefer-spread
table.children.push(
...children.map((child: PropItem) => createPropsRow(child)),
);
}
return { propsTable, table };
Expand Down
2 changes: 1 addition & 1 deletion misc/ts-markdown/src/common/package-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const traverseFolder = (
levels: number = 10,
fileName: string = 'package.json',
): string | null => {
var files = fs.readdirSync(filePath);
const files = fs.readdirSync(filePath);
if (levels === 0) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions misc/ts-markdown/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const extractCustomTag = (
export const inlineNewContent = (
{ section, tagName, node }: AttrsArg,
newContent: Node[],
) => {
): void => {
const startTag = `<!-- START-${tagName.toUpperCase()} -->`;
const endTag = `<!-- END-${tagName.toUpperCase()} -->`;
const index = node.children ? node.children.indexOf(section) : -1;
Expand Down Expand Up @@ -93,7 +93,7 @@ export const inlineNewContent = (
export const traverseDirs = (
attributes: string[][] | undefined,
callback: TraverseCallback,
) => {
): void => {
const getDirectories = (
folder: string,
excludeFiles: string[],
Expand Down
2 changes: 1 addition & 1 deletion misc/ts-markdown/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { insertOverview } from './overview-sections/insert-overview';
import { insertTSDoc } from './tsdoc/insert-tsdoc';
import { insertReactDocgenTypescript } from './react-docgen-typescript/insert-react-docgen-typescript';

export default () => {
export default (): void => {
const options = yargs
.usage('ts-md -f <file>')
.option('f', {
Expand Down
2 changes: 1 addition & 1 deletion misc/ts-markdown/src/overview-sections/insert-overview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Node } from '../common/types';
import { extractCustomTag, inlineNewContent } from '../common/utils';

export const insertOverview = () => {
return (node: Node) => {
return (node: Node): void => {
const sections = extractCustomTag(node, 'package-section');
if (sections) {
sections.forEach(({ attrs, attributes }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const propsToMDNodes = (propTable: ComponentDoc, repoFileName: string) => {
remark()
.use(() => (node: Node) => {
if (node.children) {
nodes.push.apply(nodes, [...node.children]);
nodes.push(...node.children);
}
})
.process(propTable.description);
Expand Down Expand Up @@ -68,14 +68,15 @@ const propsToMDNodes = (propTable: ComponentDoc, repoFileName: string) => {
};
});
const { propsTable } = createPropsTable('properties', props);
nodes.push.apply(nodes, propsTable);
// eslint-disable-next-line prefer-spread
nodes.push(...propsTable);
}
return nodes;
};
export const insertReactDocgenTypescript = (
settings: Settings = { path: './src' },
) => {
return (node: Node) => {
return (node: Node): void => {
const {
propFilter = (prop: RDPropItem) => {
// Currently not working, prop.parent is always null.
Expand Down Expand Up @@ -110,7 +111,8 @@ export const insertReactDocgenTypescript = (
propTable,
`${repoFolder}/${name}`,
);
newNodes.push.apply(newNodes, propNodes);
// eslint-disable-next-line prefer-spread
newNodes.push(...propNodes);
});
}
});
Expand Down
41 changes: 15 additions & 26 deletions misc/ts-markdown/src/tsdoc/extract-tsdoc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-spread */
import path from 'path';
import fs from 'fs';
import { Application } from 'typedoc';
Expand Down Expand Up @@ -134,10 +135,7 @@ export const extractTSDoc = (
type: 'text',
value: ': ',
});
declaration.children.push.apply(
declaration.children,
extractPropType(p.type),
);
declaration.children.push(...extractPropType(p.type));
}
}
declaration.children.push({
Expand All @@ -149,10 +147,7 @@ export const extractTSDoc = (
type: 'text',
value: ': ',
});
declaration.children.push.apply(
declaration.children,
extractPropType(signature.type),
);
declaration.children.push(...extractPropType(signature.type));
}
declaration.children.push({
type: 'text',
Expand All @@ -173,7 +168,7 @@ export const extractTSDoc = (
}),
);
}
result.push.apply(result, propsTable);
result.push(...propsTable);
}
}
return result;
Expand Down Expand Up @@ -228,28 +223,22 @@ export const extractTSDoc = (
type: 'text',
value: ': ',
});
declaration.children.push.apply(
declaration.children,
extractPropType(p.type),
);
declaration.children.push(...extractPropType(p.type));
}
if (signature.type) {
declaration.children.push({
type: 'text',
value: ': ',
});
declaration.children.push.apply(
declaration.children,
extractPropType(signature.type),
);
declaration.children.push(...extractPropType(signature.type));
}
});
}
const { propsTable } = extractPropTable(
node.children || node.type,
'properties',
);
result.push.apply(result, propsTable);
result.push(...propsTable);
return result;
};

Expand Down Expand Up @@ -294,7 +283,7 @@ export const extractTSDoc = (

if (p.nodeArguments) {
p.nodeArguments.forEach((arg: any) =>
nodeArguments.push.apply(nodeArguments, extractPropType(arg)),
nodeArguments.push(...extractPropType(arg)),
);
}
return [
Expand Down Expand Up @@ -513,7 +502,7 @@ export const extractTSDoc = (
const { line }: { fileName?: string; line?: number; character?: number } =
source || {};
if (line) {
let sourceLocation = fileName.includes('node_modules')
const sourceLocation = fileName.includes('node_modules')
? repo
: `${repo}/${relativePath}#L${line}`;
result.push({
Expand Down Expand Up @@ -544,21 +533,21 @@ export const extractTSDoc = (
}
switch (node.kindString) {
case 'Type alias':
result.push.apply(result, extractPropType(node.type, true));
result.push(...extractPropType(node.type, true));
break;
case 'Type literal':
node.children.forEach((child: any) => {
result.push.apply(result, extractPropType(child, true));
result.push(...extractPropType(child, true));
});
break;
case 'Class':
case 'Interface':
default: {
result.push.apply(result, extractInterface(node));
result.push(...extractInterface(node));
break;
}
case 'Function': {
result.push.apply(result, extractFunction(node));
result.push(...extractFunction(node));
break;
}
}
Expand All @@ -581,7 +570,7 @@ export const extractTSDoc = (
main.children.forEach((child: any) => {
if (child.sources) {
const nodes = extractTSType(child, main.originalName);
result.push.apply(result, nodes);
result.push(...nodes);
}
});
}
Expand All @@ -603,7 +592,7 @@ export const extractTSDoc = (
if (propNode) {
if (propNode.sources) {
const nodes = extractTSType(propNode, fileName);
result.push.apply(result, nodes);
result.push(...nodes);
}
} else {
console.log('could not find external reference: ', propName);
Expand Down
9 changes: 5 additions & 4 deletions misc/ts-markdown/src/tsdoc/insert-tsdoc.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
/* eslint-disable prefer-spread */
import path from 'path';
import { Node } from '../common/types';
import { extractCustomTag, inlineNewContent } from '../common/utils';
import { extractTSDoc } from './extract-tsdoc';

export const insertTSDoc = () => {
export const insertTSDoc = (): ((node: Node) => void) => {
const resolve = (file: string): string => {
if (file.startsWith('.')) {
return path.join(path.resolve('./'), file);
}
return require.resolve(file);
};
return (node: Node) => {
return (node: Node): void => {
const sections = extractCustomTag(node, 'tsdoc-typescript');
if (sections) {
sections.forEach(({ attrs, attributes }) => {
Expand All @@ -32,14 +33,14 @@ export const insertTSDoc = () => {
if (entry) {
const fileNames = files ? files[1].split(',').filter(s => s) : [];
const entryNames = entry ? entry[1].split(',').filter(s => s) : [];
fileNames.push.apply(fileNames, entryNames);
fileNames.push(...entryNames);
const tsNodes = extractTSDoc(
fileNames.map(file => resolve(file)),
entryNames.map(file => resolve(file)),
linkMaps,
);
if (tsNodes) {
newNodes.push.apply(newNodes, tsNodes);
newNodes.push(...tsNodes);
}
// console.log(tsdocs);
}
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20583,7 +20583,7 @@ react-docgen-typescript-plugin@^0.6.0:
react-docgen-typescript-loader "^3.7.2"
tslib "^2.0.0"

react-docgen-typescript@^1.12.5, react-docgen-typescript@^1.15.0, react-docgen-typescript@^1.16.1, react-docgen-typescript@^1.20.1:
react-docgen-typescript@^1.12.5, react-docgen-typescript@^1.15.0, react-docgen-typescript@^1.16.1, react-docgen-typescript@^1.20.1, react-docgen-typescript@^1.20.5:
version "1.20.5"
resolved "https://registry.yarnpkg.com/react-docgen-typescript/-/react-docgen-typescript-1.20.5.tgz#fb8d78a707243498436c2952bd3f6f488a68d4f3"
integrity sha512-AbLGMtn76bn7SYBJSSaKJrZ0lgNRRR3qL60PucM5M4v/AXyC8221cKBXW5Pyt9TfDRfe+LDnPNlg7TibxX0ovA==
Expand Down

0 comments on commit faf4b63

Please sign in to comment.