UNPKG

5.46 kBTypeScriptView Raw
1// Type definitions for istanbul-lib-report 3.0
2// Project: https://istanbul.js.org, https://github.com/istanbuljs/istanbuljs
3// Definitions by: Jason Cheatham <https://github.com/jason0x43>
4// Zacharias Björngren <https://github.com/zache>
5// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6// TypeScript Version: 2.4
7
8import { CoverageMap, FileCoverage, CoverageSummary } from 'istanbul-lib-coverage';
9
10/**
11 * returns a reporting context for the supplied options
12 */
13export function createContext(options?: Partial<ContextOptions>): Context;
14/**
15 * returns the default watermarks that would be used when not
16 * overridden
17 */
18export function getDefaultWatermarks(): Watermarks;
19export class ReportBase {
20 constructor(options?: Partial<ReportBaseOptions>);
21 execute(context: Context): void;
22}
23
24export interface ReportBaseOptions {
25 summarizer: Summarizers;
26}
27
28export type Summarizers = 'flat' | 'nested' | 'pkg' | 'defaultSummarizer';
29
30export interface ContextOptions {
31 coverageMap: CoverageMap;
32 defaultSummarizer: Summarizers;
33 dir: string;
34 watermarks: Partial<Watermarks>;
35 sourceFinder(filepath: string): string;
36}
37
38export interface Context {
39 data: any;
40 dir: string;
41 sourceFinder(filepath: string): string;
42 watermarks: Watermarks;
43 writer: FileWriter;
44 /**
45 * returns the coverage class given a coverage
46 * types and a percentage value.
47 */
48 classForPercent(type: keyof Watermarks, value: number): string;
49 /**
50 * returns the source code for the specified file path or throws if
51 * the source could not be found.
52 */
53 getSource(filepath: string): string;
54 getTree(summarizer?: Summarizers): Tree;
55 /**
56 * returns a full visitor given a partial one.
57 */
58 getVisitor<N extends Node = Node>(visitor: Partial<Visitor<N>>): Visitor<N>;
59 /**
60 * returns a FileWriter implementation for reporting use. Also available
61 * as the `writer` property on the context.
62 */
63 getWriter(): FileWriter;
64 /**
65 * returns an XML writer for the supplied content writer
66 */
67 getXmlWriter(contentWriter: ContentWriter): XmlWriter;
68}
69
70/**
71 * Base class for writing content
72 */
73export class ContentWriter {
74 /**
75 * returns the colorized version of a string. Typically,
76 * content writers that write to files will return the
77 * same string and ones writing to a tty will wrap it in
78 * appropriate escape sequences.
79 */
80 colorize(str: string, clazz?: string): string;
81 /**
82 * writes a string appended with a newline to the destination
83 */
84 println(str: string): void;
85 /**
86 * closes this content writer. Should be called after all writes are complete.
87 */
88 close(): void;
89}
90
91/**
92 * a content writer that writes to a file
93 */
94export class FileContentWriter extends ContentWriter {
95 constructor(fileDescriptor: number);
96 write(str: string): void;
97}
98
99/**
100 * a content writer that writes to the console
101 */
102export class ConsoleWriter extends ContentWriter {
103 write(str: string): void;
104}
105
106/**
107 * utility for writing files under a specific directory
108 */
109export class FileWriter {
110 constructor(baseDir: string);
111 static startCapture(): void;
112 static stopCapture(): void;
113 static getOutput(): string;
114 static resetOutput(): void;
115 /**
116 * returns a FileWriter that is rooted at the supplied subdirectory
117 */
118 writeForDir(subdir: string): FileWriter;
119 /**
120 * copies a file from a source directory to a destination name
121 */
122 copyFile(source: string, dest: string, header?: string): void;
123 /**
124 * returns a content writer for writing content to the supplied file.
125 */
126 writeFile(file: string | null): ContentWriter;
127}
128
129export interface XmlWriter {
130 indent(str: string): string;
131 /**
132 * writes the opening XML tag with the supplied attributes
133 */
134 openTag(name: string, attrs?: any): void;
135 /**
136 * closes an open XML tag.
137 */
138 closeTag(name: string): void;
139 /**
140 * writes a tag and its value opening and closing it at the same time
141 */
142 inlineTag(name: string, attrs?: any, content?: string): void;
143 /**
144 * closes all open tags and ends the document
145 */
146 closeAll(): void;
147}
148
149export type Watermark = [number, number];
150
151export interface Watermarks {
152 statements: Watermark;
153 functions: Watermark;
154 branches: Watermark;
155 lines: Watermark;
156}
157
158export interface Node {
159 isRoot(): boolean;
160 visit(visitor: Visitor, state: any): void;
161}
162
163export interface ReportNode extends Node {
164 path: string;
165 parent: ReportNode | null;
166 fileCoverage: FileCoverage;
167 children: ReportNode[];
168 addChild(child: ReportNode): void;
169 asRelative(p: string): string;
170 getQualifiedName(): string;
171 getRelativeName(): string;
172 getParent(): Node;
173 getChildren(): Node[];
174 isSummary(): boolean;
175 getFileCoverage(): FileCoverage;
176 getCoverageSummary(filesOnly: boolean): CoverageSummary;
177 visit(visitor: Visitor<ReportNode>, state: any): void;
178}
179
180export interface Visitor<N extends Node = Node> {
181 onStart(root: N, state: any): void;
182 onSummary(root: N, state: any): void;
183 onDetail(root: N, state: any): void;
184 onSummaryEnd(root: N, state: any): void;
185 onEnd(root: N, state: any): void;
186}
187
188export interface Tree<N extends Node = Node> {
189 getRoot(): N;
190 visit(visitor: Partial<Visitor<N>>, state: any): void;
191}