From 7a3aac3ac8bdd3b55fccfb659bfac08f49aa0f47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zacharias=20Bj=C3=B6rngren?= Date: Tue, 21 Jan 2020 01:36:32 +0100 Subject: [PATCH] [istanbul-lib-report] Updates watermark types (#41464) * [istanbul-lib-report] Updates watermark types * [istanbul-lib-report] Updates types to 3.0.0 --- types/istanbul-lib-report/index.d.ts | 205 ++++++++++++++---- .../istanbul-lib-report-tests.ts | 56 +++-- 2 files changed, 198 insertions(+), 63 deletions(-) diff --git a/types/istanbul-lib-report/index.d.ts b/types/istanbul-lib-report/index.d.ts index 9dcb5bff44..817ee10a65 100644 --- a/types/istanbul-lib-report/index.d.ts +++ b/types/istanbul-lib-report/index.d.ts @@ -1,82 +1,191 @@ -// Type definitions for istanbul-lib-report 1.1 +// Type definitions for istanbul-lib-report 3.0 // Project: https://istanbul.js.org, https://github.com/istanbuljs/istanbuljs // Definitions by: Jason Cheatham +// Zacharias Björngren // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 import { CoverageMap, FileCoverage, CoverageSummary } from 'istanbul-lib-coverage'; +/** + * returns a reporting context for the supplied options + */ export function createContext(options?: Partial): Context; +/** + * returns the default watermarks that would be used when not + * overridden + */ export function getDefaultWatermarks(): Watermarks; +export class ReportBase { + constructor(options?: Partial); + execute(context: Context): void; +} -export const summarizers: { - flat(coverageMap: CoverageMap): Tree; - nested(coverageMap: CoverageMap): Tree; - pkg(coverageMap: CoverageMap): Tree; -}; +export interface ReportBaseOptions { + summarizer: Summarizers; +} + +export type Summarizers = 'flat' | 'nested' | 'pkg' | 'defaultSummarizer'; export interface ContextOptions { - dir: string; - watermarks: Watermarks; - sourceFinder(filepath: string): string; + coverageMap: CoverageMap; + defaultSummarizer: Summarizers; + dir: string; + watermarks: Partial; + sourceFinder(filepath: string): string; } -export interface Context extends ContextOptions { - data: any; - writer: FileWriter; +export interface Context { + data: any; + dir: string; + sourceFinder(filepath: string): string; + watermarks: Watermarks; + writer: FileWriter; + /** + * returns the coverage class given a coverage + * types and a percentage value. + */ + classForPercent(type: keyof Watermarks, value: number): string; + /** + * returns the source code for the specified file path or throws if + * the source could not be found. + */ + getSource(filepath: string): string; + getTree(summarizer?: Summarizers): Tree; + /** + * returns a full visitor given a partial one. + */ + getVisitor(visitor: Partial>): Visitor; + /** + * returns a FileWriter implementation for reporting use. Also available + * as the `writer` property on the context. + */ + getWriter(): FileWriter; + /** + * returns an XML writer for the supplied content writer + */ + getXmlWriter(contentWriter: ContentWriter): XmlWriter; } -export interface ContentWriter { - write(str: string): void; - colorize(str: string, cls?: string): string; - println(str: string): void; - close(): void; +/** + * Base class for writing content + */ +export class ContentWriter { + /** + * returns the colorized version of a string. Typically, + * content writers that write to files will return the + * same string and ones writing to a tty will wrap it in + * appropriate escape sequences. + */ + colorize(str: string, clazz?: string): string; + /** + * writes a string appended with a newline to the destination + */ + println(str: string): void; + /** + * closes this content writer. Should be called after all writes are complete. + */ + close(): void; } -export interface FileWriter { - writeForDir(subdir: string): FileWriter; - copyFile(source: string, dest: string): void; - writeFile(file: string | null): ContentWriter; +/** + * a content writer that writes to a file + */ +export class FileContentWriter extends ContentWriter { + constructor(fileDescriptor: number); + write(str: string): void; } +/** + * a content writer that writes to the console + */ +export class ConsoleWriter extends ContentWriter { + write(str: string): void; +} + +/** + * utility for writing files under a specific directory + */ +export class FileWriter { + constructor(baseDir: string); + static startCapture(): void; + static stopCapture(): void; + static getOutput(): string; + static resetOutput(): void; + /** + * returns a FileWriter that is rooted at the supplied subdirectory + */ + writeForDir(subdir: string): FileWriter; + /** + * copies a file from a source directory to a destination name + */ + copyFile(source: string, dest: string, header?: string): void; + /** + * returns a content writer for writing content to the supplied file. + */ + writeFile(file: string | null): ContentWriter; +} + +export interface XmlWriter { + indent(str: string): string; + /** + * writes the opening XML tag with the supplied attributes + */ + openTag(name: string, attrs?: any): void; + /** + * closes an open XML tag. + */ + closeTag(name: string): void; + /** + * writes a tag and its value opening and closing it at the same time + */ + inlineTag(name: string, attrs?: any, content?: string): void; + /** + * closes all open tags and ends the document + */ + closeAll(): void; +} + +export type Watermark = [number, number]; + export interface Watermarks { - statements: number[]; - functions: number[]; - branches: number[]; - lines: number[]; + statements: Watermark; + functions: Watermark; + branches: Watermark; + lines: Watermark; } export interface Node { - getQualifiedName(): string; - getRelativeName(): string; - isRoot(): boolean; - getParent(): Node; - getChildren(): Node[]; - isSummary(): boolean; - getCoverageSummary(filesOnly: boolean): CoverageSummary; - getFileCoverage(): FileCoverage; - visit(visitor: Visitor, state: any): void; + isRoot(): boolean; + visit(visitor: Visitor, state: any): void; } export interface ReportNode extends Node { - path: string; - parent: ReportNode | null; - fileCoverage: FileCoverage; - children: ReportNode[]; - addChild(child: ReportNode): void; - asRelative(p: string): string; - visit(visitor: Visitor, state: any): void; + path: string; + parent: ReportNode | null; + fileCoverage: FileCoverage; + children: ReportNode[]; + addChild(child: ReportNode): void; + asRelative(p: string): string; + getQualifiedName(): string; + getRelativeName(): string; + getParent(): Node; + getChildren(): Node[]; + isSummary(): boolean; + getFileCoverage(): FileCoverage; + getCoverageSummary(filesOnly: boolean): CoverageSummary; + visit(visitor: Visitor, state: any): void; } export interface Visitor { - onStart(root: N, state: any): void; - onSummary(root: N, state: any): void; - onDetail(root: N, state: any): void; - onSummaryEnd(root: N, state: any): void; - onEnd(root: N, state: any): void; + onStart(root: N, state: any): void; + onSummary(root: N, state: any): void; + onDetail(root: N, state: any): void; + onSummaryEnd(root: N, state: any): void; + onEnd(root: N, state: any): void; } export interface Tree { - getRoot(): N; - visit(visitor: Partial>, state: any): void; + getRoot(): N; + visit(visitor: Partial>, state: any): void; } diff --git a/types/istanbul-lib-report/istanbul-lib-report-tests.ts b/types/istanbul-lib-report/istanbul-lib-report-tests.ts index 7107c14db8..3abea5690b 100644 --- a/types/istanbul-lib-report/istanbul-lib-report-tests.ts +++ b/types/istanbul-lib-report/istanbul-lib-report-tests.ts @@ -1,33 +1,59 @@ import { - createContext, - getDefaultWatermarks, - summarizers, - Watermarks + createContext, + getDefaultWatermarks, + Watermarks, + ReportBase, + ConsoleWriter, + XmlWriter, + FileWriter, } from 'istanbul-lib-report'; import { CoverageMap } from 'istanbul-lib-coverage'; const watermarks: Watermarks = { - statements: [], - branches: [], - functions: [], - lines: [] + statements: [50, 100], + branches: [10, 50], + functions: [50, 99], + lines: [25, 75], }; createContext(); createContext({}); + const context = createContext({ - dir: 'foo', - watermarks, - sourceFinder: (filepath: string) => '' + dir: 'foo', + watermarks, + sourceFinder: (filepath: string) => '', }); context.watermarks; context.sourceFinder('foo').trim(); +const coverageMap = new CoverageMap({}); +const secondContext = createContext({ + defaultSummarizer: 'nested', + watermarks: { + statements: [75, 100], + }, + coverageMap, +}); + +secondContext.watermarks.functions[1]; +secondContext.classForPercent('lines', 50); + +const fileWriter: FileWriter = secondContext.writer; + +const contentWriter = new ConsoleWriter(); +const xmlWriter: XmlWriter = secondContext.getXmlWriter(contentWriter); +xmlWriter.openTag('Coverage', { someAttribute: 'foo' }); +xmlWriter.inlineTag('Result', null, `All's well that ends well`); +xmlWriter.closeAll(); + const defaultMarks: Watermarks = getDefaultWatermarks(); -const map = new CoverageMap({}); -summarizers.flat(map).getRoot(); -summarizers.nested(map).getRoot(); -summarizers.pkg(map).getRoot(); +const report = new ReportBase({ + summarizer: 'nested', +}); +report.execute(secondContext); + +const secondReport = new ReportBase();