docgen: remove flatMap util

This commit is contained in:
Patrik Oldsberg
2020-07-15 12:08:32 +02:00
parent 7a5f4bcbd4
commit 2ec31c5264
4 changed files with 16 additions and 67 deletions
+15 -17
View File
@@ -24,7 +24,6 @@ import {
TypeInfo,
TypeLink,
} from './types';
import { flatMap } from './flatMap';
/**
* The ApiDocGenerator uses the typescript compiler API to build the data structure that
@@ -103,14 +102,13 @@ export default class ApiDocGenerator {
);
const docs = this.getNodeDocs(declaration);
const membersAndTypes = flatMap(
Array.from(interfaceMembers.values()),
fieldSymbol => this.getMemberInfo(name, fieldSymbol),
);
const membersAndTypes = Array.from(
interfaceMembers.values(),
).flatMap(fieldSymbol => this.getMemberInfo(name, fieldSymbol));
const members = membersAndTypes.map(t => t.member);
const dependentTypes = this.flattenTypes(
flatMap(membersAndTypes, t => t.dependentTypes),
membersAndTypes.flatMap(t => t.dependentTypes),
);
return { name, docs, file, lineInFile: line + 1, members, dependentTypes };
@@ -184,14 +182,14 @@ export default class ApiDocGenerator {
// It doesn't exclude repeated types, e.g. Array<Array<MyType>>, since we're exiting on duplicate nodes, not types.
visited.add(node);
const children = flatMap(node.getChildren(), child =>
this.findAllTypeReferences(child, visited),
);
const children = node
.getChildren()
.flatMap(child => this.findAllTypeReferences(child, visited));
if (!ts.isTypeReferenceNode(node)) {
return {
links: flatMap(children, child => child.links),
infos: flatMap(children, child => child.infos),
links: children.flatMap(child => child.links),
infos: children.flatMap(child => child.infos),
};
}
@@ -200,8 +198,8 @@ export default class ApiDocGenerator {
const info = this.validateTypeDeclaration(type);
if (!info) {
return {
links: flatMap(children, child => child.links),
infos: flatMap(children, child => child.infos),
links: children.flatMap(child => child.links),
infos: children.flatMap(child => child.infos),
};
}
const { symbol, declaration } = info;
@@ -233,8 +231,8 @@ export default class ApiDocGenerator {
};
return {
links: [link, ...flatMap(children, child => child.links)],
infos: [typeInfo, ...flatMap(children, child => child.infos)],
links: [link, ...children.flatMap(child => child.links)],
infos: [typeInfo, ...children.flatMap(child => child.infos)],
};
};
@@ -270,11 +268,11 @@ export default class ApiDocGenerator {
...parent,
children: [],
},
...flatMap(parent.children, getChildren),
...parent.children.flatMap(getChildren),
];
}
const flatDescs = flatMap(descs, getChildren);
const flatDescs = descs.flatMap(getChildren);
const seenTypes = new Set<number>();
return flatDescs.filter(desc => {
if (seenTypes.has(desc.id)) {
@@ -15,7 +15,6 @@
*/
import { Highlighter } from './types';
import { flatMap } from './flatMap';
// Simple syntax highlighter that mimics hilite
export default class TypescriptHighlighter implements Highlighter {
@@ -79,7 +78,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)), [
.reduce((parts, highlighter) => parts.flatMap(painter(...highlighter)), [
{ text: fullText },
])
.map(({ text }) => text)
@@ -1,28 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { flatMap } from './flatMap';
describe('flatMap', () => {
it('should flat map', () => {
expect(flatMap([], x => x)).toEqual([]);
expect(flatMap([[]], x => x)).toEqual([]);
// This simple implementation only flattens once
expect(flatMap([[[]]], x => x)).toEqual([[]]);
expect(flatMap([[1, 2], 3, [4, 5]], x => x)).toEqual([1, 2, 3, 4, 5]);
});
});
-20
View File
@@ -1,20 +0,0 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export function flatMap<T, R>(array: T[], func: (t: T) => R[] | R): R[] {
const out = array.map(func);
return ([] as R[]).concat.apply<R[], R[], R[]>([], out as R[]);
}