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
This commit is contained in:
Anton Gilgur 2022-08-19 16:58:02 -04:00 committed by GitHub
parent fd6f195834
commit c1f3a35dea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 119 additions and 130 deletions

View File

@ -74,5 +74,5 @@ A useful resource as you dive deeper are the [unit tests](__tests__/). They're g
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.
- The [integration tests](__tests__/integration/) could be useful to review at this point as well.
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).
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)
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)
- While these are necessary to the implementation, they are fairly ancillary to understanding and working with the codebase.

View File

@ -1,8 +1,13 @@
import { jest, test, expect } from "@jest/globals";
import { ConsoleContext } from "../src/context";
import { makeStubbedContext } from "./fixtures/context";
import { ConsoleContext, RollupContext } from "../src/context";
(global as any).console = {log: jest.fn()};
(global as any).console = {
warn: jest.fn(),
log: jest.fn(),
info: jest.fn(),
};
test("ConsoleContext", () => {
const proxy = new ConsoleContext(6, "=>");
@ -47,3 +52,61 @@ test("ConsoleContext 0 verbosity", () => {
proxy.error("no-test4");
expect(console.log).not.toHaveBeenLastCalledWith("no-test4");
});
test("RollupContext", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(5, false, stubbedContext);
context.warn("test");
expect((data as any).warn).toEqual("test");
context.warn(() => "test2");
expect((data as any).warn).toEqual("test2");
context.error("test!");
expect((data as any).warn).toEqual("test!");
context.error(() => "test2!");
expect((data as any).warn).toEqual("test2!");
context.info("test3");
expect(console.log).toHaveBeenLastCalledWith("test3");
context.info(() => "test4");
expect(console.log).toHaveBeenLastCalledWith("test4");
context.debug("test5");
expect(console.log).toHaveBeenLastCalledWith("test5");
context.debug(() => "test6");
expect(console.log).toHaveBeenLastCalledWith("test6");
});
test("RollupContext with 0 verbosity", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(0, false, stubbedContext);
expect(context.debug("verbosity is too low here")).toBeFalsy();
expect(context.info("verbosity is too low here")).toBeFalsy();
expect(context.warn("verbosity is too low here")).toBeFalsy();
});
test("RollupContext.error + debug negative verbosity", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(-100, true, stubbedContext);
expect(context.error("whatever")).toBeFalsy();
expect(context.debug("whatever")).toBeFalsy();
});
test("RollupContext.error with bail", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(5, true, stubbedContext);
expect(context.error("whatever")).toBeFalsy();
expect((data as any).error).toEqual("whatever");
});

View File

@ -1,68 +0,0 @@
import { jest, test, expect } from "@jest/globals";
import { makeStubbedContext } from "./fixtures/context";
import { RollupContext } from "../src/rollupcontext";
(global as any).console = {
warn: jest.fn(),
log: jest.fn(),
info: jest.fn(),
};
test("RollupContext", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(5, false, stubbedContext);
context.warn("test");
expect((data as any).warn).toEqual("test");
context.warn(() => "test2");
expect((data as any).warn).toEqual("test2");
context.error("test!");
expect((data as any).warn).toEqual("test!");
context.error(() => "test2!");
expect((data as any).warn).toEqual("test2!");
context.info("test3");
expect(console.log).toHaveBeenLastCalledWith("test3");
context.info(() => "test4");
expect(console.log).toHaveBeenLastCalledWith("test4");
context.debug("test5");
expect(console.log).toHaveBeenLastCalledWith("test5");
context.debug(() => "test6");
expect(console.log).toHaveBeenLastCalledWith("test6");
});
test("RollupContext with 0 verbosity", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(0, false, stubbedContext);
expect(context.debug("verbosity is too low here")).toBeFalsy();
expect(context.info("verbosity is too low here")).toBeFalsy();
expect(context.warn("verbosity is too low here")).toBeFalsy();
});
test("RollupContext.error + debug negative verbosity", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(-100, true, stubbedContext);
expect(context.error("whatever")).toBeFalsy();
expect(context.debug("whatever")).toBeFalsy();
});
test("RollupContext.error with bail", () => {
const data = {};
const stubbedContext = makeStubbedContext(data);
const context = new RollupContext(5, true, stubbedContext);
expect(context.error("whatever")).toBeFalsy();
expect((data as any).error).toEqual("whatever");
});

View File

@ -1,5 +1,4 @@
import * as _ from "lodash";
import { PluginContext } from "rollup";
export interface IContext
{
@ -17,6 +16,13 @@ export enum VerbosityLevel
Debug,
}
function getText (message: string | (() => string)): string {
return typeof message === "string" ? message : message();
}
/* tslint:disable:max-classes-per-file -- generally a good rule to follow, but these two classes could basically be one */
/** mainly to be used in options hook, but can be used in other hooks too */
export class ConsoleContext implements IContext
{
constructor(private verbosity: VerbosityLevel, private prefix: string = "")
@ -27,27 +33,67 @@ export class ConsoleContext implements IContext
{
if (this.verbosity < VerbosityLevel.Warning)
return;
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
console.log(`${this.prefix}${getText(message)}`);
}
public error(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Error)
return;
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
console.log(`${this.prefix}${getText(message)}`);
}
public info(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Info)
return;
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
console.log(`${this.prefix}${getText(message)}`);
}
public debug(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Debug)
return;
console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`);
console.log(`${this.prefix}${getText(message)}`);
}
}
/** cannot be used in options hook (which does not have this.warn and this.error), but can be in other hooks */
export class RollupContext implements IContext
{
constructor(private verbosity: VerbosityLevel, private bail: boolean, private context: PluginContext, private prefix: string = "")
{
}
public warn(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Warning)
return;
this.context.warn(`${getText(message)}`);
}
public error(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Error)
return;
if (this.bail)
this.context.error(`${getText(message)}`);
else
this.context.warn(`${getText(message)}`);
}
public info(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Info)
return;
console.log(`${this.prefix}${getText(message)}`);
}
public debug(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Debug)
return;
console.log(`${this.prefix}${getText(message)}`);
}
}

View File

@ -5,8 +5,7 @@ import { normalizePath as normalize } from "@rollup/pluginutils";
import { blue, red, yellow, green } from "colors/safe";
import findCacheDir from "find-cache-dir";
import { RollupContext } from "./rollupcontext";
import { ConsoleContext, IContext, VerbosityLevel } from "./context";
import { ConsoleContext, RollupContext, IContext, VerbosityLevel } from "./context";
import { LanguageServiceHost } from "./host";
import { TsCache, convertDiagnostic, convertEmitOutput, getAllReferences, ICode } from "./tscache";
import { tsModule, setTypescriptModule } from "./tsproxy";

View File

@ -1,51 +0,0 @@
import * as _ from "lodash";
import { PluginContext } from "rollup";
import { IContext, VerbosityLevel } from "./context";
export class RollupContext implements IContext
{
constructor(private verbosity: VerbosityLevel, private bail: boolean, private context: PluginContext, private prefix: string = "")
{
}
public warn(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Warning)
return;
const text = _.isFunction(message) ? message() : message;
this.context.warn(`${text}`);
}
public error(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Error)
return;
const text = _.isFunction(message) ? message() : message;
if (this.bail)
this.context.error(`${text}`);
else
this.context.warn(`${text}`);
}
public info(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Info)
return;
const text = _.isFunction(message) ? message() : message;
console.log(`${this.prefix}${text}`);
}
public debug(message: string | (() => string)): void
{
if (this.verbosity < VerbosityLevel.Debug)
return;
const text = _.isFunction(message) ? message() : message;
console.log(`${this.prefix}${text}`);
}
}