From 2ec31c52642a81754bfa3cc11fb3b6486f5313a9 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Wed, 15 Jul 2020 12:08:32 +0200 Subject: [PATCH] docgen: remove flatMap util --- packages/docgen/src/docgen/ApiDocGenerator.ts | 32 +++++++++---------- .../src/docgen/TypescriptHighlighter.ts | 3 +- packages/docgen/src/docgen/flatMap.test.ts | 28 ---------------- packages/docgen/src/docgen/flatMap.ts | 20 ------------ 4 files changed, 16 insertions(+), 67 deletions(-) delete mode 100644 packages/docgen/src/docgen/flatMap.test.ts delete mode 100644 packages/docgen/src/docgen/flatMap.ts diff --git a/packages/docgen/src/docgen/ApiDocGenerator.ts b/packages/docgen/src/docgen/ApiDocGenerator.ts index 8d0fd04240..01b1e0bf0f 100644 --- a/packages/docgen/src/docgen/ApiDocGenerator.ts +++ b/packages/docgen/src/docgen/ApiDocGenerator.ts @@ -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>, 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(); return flatDescs.filter(desc => { if (seenTypes.has(desc.id)) { diff --git a/packages/docgen/src/docgen/TypescriptHighlighter.ts b/packages/docgen/src/docgen/TypescriptHighlighter.ts index 4bbb6548dd..ffdd4f1906 100644 --- a/packages/docgen/src/docgen/TypescriptHighlighter.ts +++ b/packages/docgen/src/docgen/TypescriptHighlighter.ts @@ -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) diff --git a/packages/docgen/src/docgen/flatMap.test.ts b/packages/docgen/src/docgen/flatMap.test.ts deleted file mode 100644 index 6186863039..0000000000 --- a/packages/docgen/src/docgen/flatMap.test.ts +++ /dev/null @@ -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]); - }); -}); diff --git a/packages/docgen/src/docgen/flatMap.ts b/packages/docgen/src/docgen/flatMap.ts deleted file mode 100644 index bdea672a41..0000000000 --- a/packages/docgen/src/docgen/flatMap.ts +++ /dev/null @@ -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(array: T[], func: (t: T) => R[] | R): R[] { - const out = array.map(func); - return ([] as R[]).concat.apply([], out as R[]); -}