diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a57131a3..37b61305 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/__tests__/context.spec.ts b/__tests__/context.spec.ts index e9fa20ee..e862af8f 100644 --- a/__tests__/context.spec.ts +++ b/__tests__/context.spec.ts @@ -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"); +}); diff --git a/__tests__/rollupcontext.spec.ts b/__tests__/rollupcontext.spec.ts deleted file mode 100644 index 25925aa2..00000000 --- a/__tests__/rollupcontext.spec.ts +++ /dev/null @@ -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"); -}); diff --git a/src/context.ts b/src/context.ts index d3c07417..50b994a0 100644 --- a/src/context.ts +++ b/src/context.ts @@ -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}${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)}`); + } +} + +/** 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; - console.log(`${this.prefix}${_.isFunction(message) ? message() : message}`); + + 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}${_.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)}`); } } diff --git a/src/index.ts b/src/index.ts index 6ec64ddf..15514292 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 } from "./tscache"; import { tsModule, setTypescriptModule } from "./tsproxy"; diff --git a/src/rollupcontext.ts b/src/rollupcontext.ts deleted file mode 100644 index 68c55dba..00000000 --- a/src/rollupcontext.ts +++ /dev/null @@ -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}`); - } -}