Skip to content

refactor: combine two context files into one #396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
67 changes: 65 additions & 2 deletions __tests__/context.spec.ts
Original file line number Diff line number Diff line change
@@ -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, "=>");
Expand Down Expand Up @@ -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");
});
68 changes: 0 additions & 68 deletions __tests__/rollupcontext.spec.ts

This file was deleted.

58 changes: 52 additions & 6 deletions src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

import * as _ from "lodash";
import { PluginContext } from "rollup";

export interface IContext
{
Expand All @@ -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 = "")
Expand All @@ -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)}`);
}
}
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
51 changes: 0 additions & 51 deletions src/rollupcontext.ts

This file was deleted.