UNPKG

2.32 kBJavaScriptView Raw
1'use strict';
2/**
3 * @typedef {{color: boolean, cwd: string, report: function(string), issue: function(string, string,string,number,number,number=,number=), warn: function(string), verbose: function(string), getMessages: function():Array.<MESSAGE>, options:Options, messages: Array.<MESSAGE>}} CONTEXT
4 */
5/**
6 * @typedef {{msg: string, level: MESSAGE_LEVEL, file: string,line:number,column:number,startOffset:number,endOffset:number}} MESSAGE
7 */
8
9/**
10 * Enum for tri-state values.
11 * @enum {string}
12 */
13const MESSAGE_LEVEL = {
14 ERROR: 'ERROR',
15 WARN: 'WARN',
16 INFO: 'INFO'
17};
18
19const _ = require('lodash');
20const err = require('./RTCodeError');
21const norm = err.RTCodeError.norm;
22
23
24/**
25 * @type {CONTEXT}
26 */
27const context = {
28 /** @type {Array.<MESSAGE>} */
29 messages: [],
30 /** @type {boolean} */
31 color: true,
32 /** @type {string} */
33 cwd: process.cwd(),
34 report(msg) {
35 console.log(msg);
36 },
37 verbose(msg) {
38 if (context.options.verbose) {
39 console.log(msg);
40 }
41 },
42 info(msg, file, line, column) {
43 context.issue(MESSAGE_LEVEL.INFO, msg, file, line, column);
44 },
45 warn(msg, file, line, column, startOffset, endOffset) {
46 context.issue(MESSAGE_LEVEL.WARN, msg, file, line, column, startOffset, endOffset);
47 },
48 error(msg, file, line, column, startOffset, endOffset) {
49 context.issue(MESSAGE_LEVEL.ERROR, msg, file, line, column, startOffset, endOffset);
50 },
51 /**
52 * @param {MESSAGE_LEVEL} level
53 * @param {string} msg
54 * @param {string} file
55 * @param {number} line
56 * @param {number} column
57 * @param {number=} startOffset
58 * @param {number=} endOffset
59 */
60 issue(level, msg, file, line, column, startOffset, endOffset) {
61 context.messages.push({level, msg, file: file || null, line: norm(line), column: norm(column), index: norm(startOffset), startOffset: norm(startOffset), endOffset: norm(endOffset)});
62 },
63 getMessages() {
64 return context.messages;
65 },
66 clear() {
67 context.messages = [];
68 },
69 hasErrors() {
70 return _.some(context.messages, {level: MESSAGE_LEVEL.ERROR});
71 },
72 options: {
73 verbose: false,
74 outFile: null,
75 format: 'stylish'
76 },
77 MESSAGE_LEVEL
78};
79
80module.exports = context;