Skip to content

Commit c1f3a35

Browse files
authored
refactor: combine two context files into one (#396)
* refactor: combine two context files into one - they're virtually identical, so combine them instead of keeping them separate - changes to one should probably be made to both - still a < 100 LoC file - refactor out `_.isFunction` with a simple `getText` function instead - checks the opposite - one more lodash removal! - add docstrings about when to use the two contexts * disable tslint rule for this file
1 parent fd6f195 commit c1f3a35

File tree

6 files changed

+119
-130
lines changed

6 files changed

+119
-130
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,5 @@ A useful resource as you dive deeper are the [unit tests](__tests__/). They're g
7474
1. At this point, you may be ready to read the more complicated bits of [`index`](src/index.ts) in detail and see how it interacts with the other modules.
7575
- The [integration tests](__tests__/integration/) could be useful to review at this point as well.
7676
1. Once you're pretty familiar with `index`, you can dive into some of the cache code in [`tscache`](src/tscache.ts) and [`rollingcache`](src/rollingcache.ts).
77-
1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and [`rollupcontext`](src/rollupcontext.ts), and then the TS logging nuances in [`print-diagnostics`](src/print-diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts)
77+
1. And finally, you can see some of the Rollup logging nuances in [`context`](src/context.ts) and then the TS logging nuances in [`print-diagnostics`](src/print-diagnostics.ts), and [`diagnostics-format-host`](src/diagnostics-format-host.ts)
7878
- While these are necessary to the implementation, they are fairly ancillary to understanding and working with the codebase.

__tests__/context.spec.ts

+65-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { jest, test, expect } from "@jest/globals";
22

3-
import { ConsoleContext } from "../src/context";
3+
import { makeStubbedContext } from "./fixtures/context";
4+
import { ConsoleContext, RollupContext } from "../src/context";
45

5-
(global as any).console = {log: jest.fn()};
6+
(global as any).console = {
7+
warn: jest.fn(),
8+
log: jest.fn(),
9+
info: jest.fn(),
10+
};
611

712
test("ConsoleContext", () => {
813
const proxy = new ConsoleContext(6, "=>");
@@ -47,3 +52,61 @@ test("ConsoleContext 0 verbosity", () => {
4752
proxy.error("no-test4");
4853
expect(console.log).not.toHaveBeenLastCalledWith("no-test4");
4954
});
55+
56+
test("RollupContext", () => {
57+
const data = {};
58+
const stubbedContext = makeStubbedContext(data);
59+
const context = new RollupContext(5, false, stubbedContext);
60+
61+
context.warn("test");
62+
expect((data as any).warn).toEqual("test");
63+
64+
context.warn(() => "test2");
65+
expect((data as any).warn).toEqual("test2");
66+
67+
context.error("test!");
68+
expect((data as any).warn).toEqual("test!");
69+
70+
context.error(() => "test2!");
71+
expect((data as any).warn).toEqual("test2!");
72+
73+
context.info("test3");
74+
expect(console.log).toHaveBeenLastCalledWith("test3");
75+
76+
context.info(() => "test4");
77+
expect(console.log).toHaveBeenLastCalledWith("test4");
78+
79+
context.debug("test5");
80+
expect(console.log).toHaveBeenLastCalledWith("test5");
81+
82+
context.debug(() => "test6");
83+
expect(console.log).toHaveBeenLastCalledWith("test6");
84+
});
85+
86+
test("RollupContext with 0 verbosity", () => {
87+
const data = {};
88+
const stubbedContext = makeStubbedContext(data);
89+
const context = new RollupContext(0, false, stubbedContext);
90+
91+
expect(context.debug("verbosity is too low here")).toBeFalsy();
92+
expect(context.info("verbosity is too low here")).toBeFalsy();
93+
expect(context.warn("verbosity is too low here")).toBeFalsy();
94+
});
95+
96+
test("RollupContext.error + debug negative verbosity", () => {
97+
const data = {};
98+
const stubbedContext = makeStubbedContext(data);
99+
const context = new RollupContext(-100, true, stubbedContext);
100+
101+
expect(context.error("whatever")).toBeFalsy();
102+
expect(context.debug("whatever")).toBeFalsy();
103+
});
104+
105+
test("RollupContext.error with bail", () => {
106+
const data = {};
107+
const stubbedContext = makeStubbedContext(data);
108+
const context = new RollupContext(5, true, stubbedContext);
109+
110+
expect(context.error("whatever")).toBeFalsy();
111+
expect((data as any).error).toEqual("whatever");
112+
});

__tests__/rollupcontext.spec.ts

-68
This file was deleted.

src/context.ts

+52-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
import * as _ from "lodash";
1+
import { PluginContext } from "rollup";
32

43
export interface IContext
54
{
@@ -17,6 +16,13 @@ export enum VerbosityLevel
1716
Debug,
1817
}
1918

19+
function getText (message: string | (() => string)): string {
20+
return typeof message === "string" ? message : message();
21+
}
22+
23+
/* tslint:disable:max-classes-per-file -- generally a good rule to follow, but these two classes could basically be one */
24+
25+
/** mainly to be used in options hook, but can be used in other hooks too */
2026
export class ConsoleContext implements IContext
2127
{
2228
constructor(private verbosity: VerbosityLevel, private prefix: string = "")
@@ -27,27 +33,67 @@ export class ConsoleContext implements IContext
2733
{
2834
if (this.verbosity < VerbosityLevel.Warning)
2935
return;
30-
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
36+
console.log(`${this.prefix}${getText(message)}`);
37+
}
38+
39+
public error(message: string | (() => string)): void
40+
{
41+
if (this.verbosity < VerbosityLevel.Error)
42+
return;
43+
console.log(`${this.prefix}${getText(message)}`);
44+
}
45+
46+
public info(message: string | (() => string)): void
47+
{
48+
if (this.verbosity < VerbosityLevel.Info)
49+
return;
50+
console.log(`${this.prefix}${getText(message)}`);
51+
}
52+
53+
public debug(message: string | (() => string)): void
54+
{
55+
if (this.verbosity < VerbosityLevel.Debug)
56+
return;
57+
console.log(`${this.prefix}${getText(message)}`);
58+
}
59+
}
60+
61+
/** cannot be used in options hook (which does not have this.warn and this.error), but can be in other hooks */
62+
export class RollupContext implements IContext
63+
{
64+
constructor(private verbosity: VerbosityLevel, private bail: boolean, private context: PluginContext, private prefix: string = "")
65+
{
66+
}
67+
68+
public warn(message: string | (() => string)): void
69+
{
70+
if (this.verbosity < VerbosityLevel.Warning)
71+
return;
72+
this.context.warn(`${getText(message)}`);
3173
}
3274

3375
public error(message: string | (() => string)): void
3476
{
3577
if (this.verbosity < VerbosityLevel.Error)
3678
return;
37-
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
79+
80+
if (this.bail)
81+
this.context.error(`${getText(message)}`);
82+
else
83+
this.context.warn(`${getText(message)}`);
3884
}
3985

4086
public info(message: string | (() => string)): void
4187
{
4288
if (this.verbosity < VerbosityLevel.Info)
4389
return;
44-
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
90+
console.log(`${this.prefix}${getText(message)}`);
4591
}
4692

4793
public debug(message: string | (() => string)): void
4894
{
4995
if (this.verbosity < VerbosityLevel.Debug)
5096
return;
51-
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
97+
console.log(`${this.prefix}${getText(message)}`);
5298
}
5399
}

src/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { normalizePath as normalize } from "@rollup/pluginutils";
55
import { blue, red, yellow, green } from "colors/safe";
66
import findCacheDir from "find-cache-dir";
77

8-
import { RollupContext } from "./rollupcontext";
9-
import { ConsoleContext, IContext, VerbosityLevel } from "./context";
8+
import { ConsoleContext, RollupContext, IContext, VerbosityLevel } from "./context";
109
import { LanguageServiceHost } from "./host";
1110
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences, ICode } from "./tscache";
1211
import { tsModule, setTypescriptModule } from "./tsproxy";

src/rollupcontext.ts

-51
This file was deleted.

0 commit comments

Comments
 (0)