Permalink
Browse files

New: Add category to report messages and new options to formatters

  • Loading branch information...
sarvaje authored and alrra committed Aug 10, 2018
1 parent 02d2c6a commit 2f3d1a017694bd904daa8e418165343e8e33f922
@@ -20,6 +20,8 @@ import cutString from '../utils/misc/cut-string';
import * as resourceLoader from '../utils/resource-loader';
import { installPackages } from '../utils/npm';
import * as insights from '../utils/appinsights';
import { FormatterOptions } from '../types/formatters';
import loadHintPackage from '../utils/packages/load-hint-package';
const each = promisify(async.each);
const debug: debug.IDebugger = d(__filename);
@@ -354,27 +356,34 @@ export default async (actions: CLIOptions): Promise<boolean> => {
});
};
const print = async (reports: Array<Problem>, target: string): Promise<void> => {
const print = async (reports: Array<Problem>, target: string, scanTime: number): Promise<void> => {
const formatterOptions: FormatterOptions = {
config: userConfig,
scanTime,
version: loadHintPackage().version
};
await each(engine.formatters, async (formatter) => {
await formatter.format(reports, target);
await formatter.format(reports, target, formatterOptions);
});
};
engine.on('print', print);
for (const target of targets) {
try {
const scanStart = Date.now();
const results: Array<Problem> = await engine.executeOn(target);
const scanEnd = Date.now();
if (hasError(results)) {
exitCode = 1;
}
endSpinner(exitCode ? 'fail' : 'succeed');
await print(results, target.href);
await askForTelemetryConfirmation(config);
await print(results, target.href, scanEnd - scanStart);
} catch (e) {
exitCode = 1;
endSpinner('fail');
@@ -22,6 +22,7 @@ import * as logger from './utils/logging';
import { HintContext } from './hint-context';
import { HintScope } from './enums/hintscope';
import { Configuration } from './config';
import { Category } from './enums/category';
const debug: debug.IDebugger = d(__filename);
@@ -249,8 +250,9 @@ export class Engine extends EventEmitter {
}
/** Reports a message from one of the hints. */
public report(hintId: string, severity: Severity, sourceCode: string, location: ProblemLocation, message: string, resource: string) {
public report(hintId: string, category: Category, severity: Severity, sourceCode: string, location: ProblemLocation, message: string, resource: string) {
const problem: Problem = {
category,
hintId,
location: location || { column: -1, line: -1 },
message,
@@ -9,6 +9,7 @@ import { URL } from 'url';
import { Engine } from './engine';
import { IAsyncHTMLElement, NetworkData, ProblemLocation, HintMetadata, Severity } from './types';
import { findInElement, findProblemLocation } from './utils/location-helpers';
import { Category } from './enums/category';
/** Acts as an abstraction layer between hints and the main hint object. */
@@ -106,6 +107,7 @@ export class HintContext {
this.engine.report(
this.id,
(this.meta && this.meta.docs) ? this.meta.docs.category : Category.other,
severity || this.severity,
codeSnippet || sourceCode,
position,
@@ -1,10 +1,20 @@
import { Problem } from './problems';
import { UserConfig } from '../types';
export type FormatterOptions = {
isScanner?: boolean;
noGenerateFiles?: boolean;
scanTime?: number;
status?: string;
version?: string;
config?: UserConfig;
};
export interface IFormatterConstructor {
new(): IFormatter;
}
/** A format function that will output the results obtained by hint */
export interface IFormatter {
format(problems: Array<Problem>, target: string): void;
format(problems: Array<Problem>, target: string, options: FormatterOptions): void;
}
@@ -1,3 +1,5 @@
import { Category } from '../enums/category';
/** The severity configuration of a hint */
export enum Severity {
off = 0,
@@ -29,6 +31,8 @@ export type Problem = {
resource: string;
/** The name of the triggered hint */
hintId: string;
/** The category of the triggered hint */
category: Category;
/** The severity of the hint based on the actual configuration */
severity: Severity;
};
@@ -20,6 +20,7 @@ proxyquire('../../src/lib/engine', { eventemitter2: eventEmitter });
import { Engine } from '../../src/lib/engine';
import { HintResources, IFormatter, IConnector, IHint, HintMetadata, Problem } from '../../src/lib/types';
import { Category } from '../../src/lib/enums/category';
class FakeConnector implements IConnector {
private config;
@@ -866,8 +867,8 @@ test.serial('executeOn should return all messages', async (t) => {
const localUrl = new url.URL('http://localhost/');
engineObject.report('1', 1, 'node', { column: 1, line: 1 }, 'message', 'resource');
engineObject.report('2', 1, 'node', { column: 1, line: 2 }, 'message2', 'resource2');
engineObject.report('1', Category.other, 1, 'node', { column: 1, line: 1 }, 'message', 'resource');
engineObject.report('2', Category.other, 1, 'node', { column: 1, line: 2 }, 'message2', 'resource2');
const result = await engineObject.executeOn(localUrl);

0 comments on commit 2f3d1a0

Please sign in to comment.