From a5e6cb976554f92c60955e78f0bd53ffc6afee2f Mon Sep 17 00:00:00 2001 From: Labayna Neil Brian Narido Date: Thu, 23 Apr 2020 16:48:00 +0800 Subject: [PATCH] Replace console with logger --- src/Page.js | 12 ++---- src/lib/markbind/src/parser.js | 20 ++++------ .../markbind/src/parsers/componentParser.js | 39 +++++-------------- .../preprocessors/componentPreprocessor.js | 9 +++-- 4 files changed, 26 insertions(+), 54 deletions(-) diff --git a/src/Page.js b/src/Page.js index a5405d90c3..cff78627b6 100644 --- a/src/Page.js +++ b/src/Page.js @@ -568,9 +568,7 @@ class Page { * @param {FileConfig} fileConfig */ generateExpressiveLayout(pageData, fileConfig) { - const markbinder = new MarkBind({ - errorHandler: logger.error, - }); + const markbinder = new MarkBind(); const template = {}; template[LAYOUT_PAGE_BODY_VARIABLE] = pageData; const { layout } = this.frontMatter; @@ -916,9 +914,7 @@ class Page { this.includedFiles = new Set([this.sourcePath]); this.headerIdMap = {}; // Reset for live reload - const markbinder = new MarkBind({ - errorHandler: logger.error, - }); + const markbinder = new MarkBind(); /** * @type {FileConfig} */ @@ -1236,9 +1232,7 @@ class Page { * We create a local instance of Markbind for an empty dynamicIncludeSrc * so that we only recursively rebuild the file's included content */ - const markbinder = new MarkBind({ - errorHandler: logger.error, - }); + const markbinder = new MarkBind(); return markbinder.includeFile(dependency.to, { baseUrlMap: this.baseUrlMap, userDefinedVariablesMap: this.userDefinedVariablesMap, diff --git a/src/lib/markbind/src/parser.js b/src/lib/markbind/src/parser.js index 3fb28f84a6..db1309a910 100644 --- a/src/lib/markbind/src/parser.js +++ b/src/lib/markbind/src/parser.js @@ -33,8 +33,6 @@ const { class Parser { constructor(options) { this._options = options || {}; - // eslint-disable-next-line no-console - this._onError = this._options.errorHandler || console.error; this._fileCache = {}; this.dynamicIncludeSrc = []; this.staticIncludeSrc = []; @@ -122,8 +120,7 @@ class Parser { } } else { if (!variableName) { - // eslint-disable-next-line no-console - console.warn(`Missing 'name' for variable in ${fileName}\n`); + logger.warn(`Missing 'name' for variable in ${fileName}\n`); return; } setPageVariable(variableName, md.renderInline(variableElement.html())); @@ -155,7 +152,7 @@ class Parser { // Read file fail const missingReferenceErrorMessage = `Missing reference in: ${node.attribs[ATTRIB_CWF]}`; e.message += `\n${missingReferenceErrorMessage}`; - this._onError(e); + logger.error(e); return utils.createErrorNode(node, e); } const fileContent = this._fileCache[filePath]; // cache the file contents to save some I/O @@ -345,7 +342,7 @@ class Parser { break; } - componentParser.parseComponents(node, this._onError); + componentParser.parseComponents(node); if (node.children) { node.children.forEach((child) => { @@ -353,7 +350,7 @@ class Parser { }); } - componentParser.postParseComponents(node, this._onError); + componentParser.postParseComponents(node); // If a fixed header is applied to this page, generate dummy spans as anchor points if (config.fixedHeader && isHeadingTag && node.attribs.id) { @@ -398,7 +395,7 @@ class Parser { processed = componentPreprocessor.preProcessComponent(d, context, config, this); } catch (err) { err.message += `\nError while preprocessing '${file}'`; - this._onError(err); + logger.error(err); processed = utils.createErrorNode(d, err); } return processed; @@ -479,7 +476,7 @@ class Parser { processed = componentPreprocessor.preProcessComponent(d, currentContext, config, this); } catch (err) { err.message += `\nError while preprocessing '${actualFilePath}'`; - this._onError(err); + logger.error(err); processed = utils.createErrorNode(d, err); } return processed; @@ -538,7 +535,7 @@ class Parser { parsed = this._parse(d, config); } catch (err) { err.message += `\nError while rendering '${filePath}'`; - this._onError(err); + logger.error(err); parsed = utils.createErrorNode(d, err); } return parsed; @@ -672,8 +669,7 @@ class Parser { } const variableName = child.attribs.name || child.attribs.id; if (!variableName) { - // eslint-disable-next-line no-console - console.warn(`Missing reference in ${includeElement.attribs[ATTRIB_CWF]}\n` + logger.warn(`Missing reference in ${includeElement.attribs[ATTRIB_CWF]}\n` + `Missing 'name' or 'id' in variable for ${includeElement.attribs.src} include.`); return; } diff --git a/src/lib/markbind/src/parsers/componentParser.js b/src/lib/markbind/src/parsers/componentParser.js index 40d369e445..d7d58028cb 100644 --- a/src/lib/markbind/src/parsers/componentParser.js +++ b/src/lib/markbind/src/parsers/componentParser.js @@ -4,6 +4,7 @@ const _ = {}; _.has = require('lodash/has'); const md = require('../lib/markdown-it'); +const logger = require('../../../../util/logger'); cheerio.prototype.options.xmlMode = true; // Enable xml mode for self-closing tag cheerio.prototype.options.decodeEntities = false; // Don't escape HTML entities @@ -148,9 +149,7 @@ function _warnConflictingAttributes(node, attribute, attrsConflictingWith) { } attrsConflictingWith.forEach((conflictingAttr) => { if (conflictingAttr in node.attribs) { - // TODO: Use logger here instead of console.warn. See issue #1060 - // eslint-disable-next-line no-console - console.warn(`warn: Usage of conflicting ${node.name} attributes: ` + logger.warn(`Usage of conflicting ${node.name} attributes: ` + `'${attribute}' with '${conflictingAttr}'`); } }); @@ -164,9 +163,7 @@ function _warnConflictingAttributes(node, attribute, attrsConflictingWith) { function _warnDeprecatedAttributes(node, attributeNamePairs) { Object.entries(attributeNamePairs).forEach(([deprecatedAttrib, correctAttrib]) => { if (deprecatedAttrib in node.attribs) { - // TODO: Use logger here instead of console.warn. See issue #1060 - // eslint-disable-next-line no-console - console.warn(`warn: ${node.name} attribute '${deprecatedAttrib}' ` + logger.warn(`${node.name} attribute '${deprecatedAttrib}' ` + `is deprecated and may be removed in the future. Please use '${correctAttrib}'`); } }); @@ -249,9 +246,7 @@ function _warnDeprecatedSlotNames(element, namePairs) { if (child.attribs.slot !== deprecatedName) { return; } - // TODO: Use logger here instead of console.warn. See issue #1060 - // eslint-disable-next-line no-console - console.warn(`warn: ${element.name} slot name '${deprecatedName}' ` + logger.warn(`${element.name} slot name '${deprecatedName}' ` + `is deprecated and may be removed in the future. Please use '${correctName}'`); }); }); @@ -389,14 +384,10 @@ function _parseDropdownAttributes(node) { // If header slot is present, the header attribute has no effect, and we can simply remove it. if (hasHeaderSlot) { if (_.has(node.attribs, 'header')) { - // TODO: Use logger here instead of console.warn. See issue #1060 - // eslint-disable-next-line no-console - console.warn(`warn: ${node.name} has a header slot, 'header' attribute has no effect.`); + logger.warn(`${node.name} has a header slot, 'header' attribute has no effect.`); } if (_.has(node.attribs, 'text')) { - // TODO: Use logger here instead of console.warn. See issue #1060 - // eslint-disable-next-line no-console - console.warn(`warn: ${node.name} has a header slot, 'text' attribute has no effect.`); + logger.warn(`${node.name} has a header slot, 'text' attribute has no effect.`); } delete node.attribs.header; delete node.attribs.text; @@ -438,7 +429,7 @@ function _parseThumbnailAttributes(node) { * API */ -function parseComponents(node, errorHandler) { +function parseComponents(node) { try { switch (node.name) { case 'panel': @@ -473,16 +464,11 @@ function parseComponents(node, errorHandler) { break; } } catch (error) { - if (!errorHandler) { - // eslint-disable-next-line no-console - console.error(error); - return; - } - errorHandler(error); + logger.error(error); } } -function postParseComponents(node, errorHandler) { +function postParseComponents(node) { try { switch (node.name) { case 'panel': @@ -492,12 +478,7 @@ function postParseComponents(node, errorHandler) { break; } } catch (error) { - if (!errorHandler) { - // eslint-disable-next-line no-console - console.error(error); - return; - } - errorHandler(error); + logger.error(error); } } diff --git a/src/lib/markbind/src/preprocessors/componentPreprocessor.js b/src/lib/markbind/src/preprocessors/componentPreprocessor.js index 0829c727eb..49a3dca2ba 100644 --- a/src/lib/markbind/src/preprocessors/componentPreprocessor.js +++ b/src/lib/markbind/src/preprocessors/componentPreprocessor.js @@ -1,6 +1,7 @@ const cheerio = require('cheerio'); const path = require('path'); const url = require('url'); +const logger = require('../../../../util/logger'); const CyclicReferenceError = require('../handlers/cyclicReferenceError.js'); @@ -64,7 +65,7 @@ function _getFileExistsNode(element, context, config, parser, actualFilePath, is parser.missingIncludeSrc.push({ from: context.cwf, to: actualFilePath }); const error = new Error( `No such file: ${actualFilePath}\nMissing reference in ${element.attribs[ATTRIB_CWF]}`); - parser._onError(error); + logger.error(error); return utils.createErrorNode(element, error); } @@ -212,7 +213,7 @@ function _preprocessInclude(node, context, config, parser) { if (_.isEmpty(element.attribs.src)) { const error = new Error(`Empty src attribute in include in: ${element.attribs[ATTRIB_CWF]}`); - parser._onError(error); + logger.error(error); return utils.createErrorNode(element, error); } @@ -285,7 +286,7 @@ function _preprocessInclude(node, context, config, parser) { const hashSrcWithoutHash = hash.substring(1); const error = new Error(`No such segment '${hashSrcWithoutHash}' in file: ${actualFilePath}\n` + `Missing reference in ${element.attribs[ATTRIB_CWF]}`); - parser._onError(error); + logger.error(error); return utils.createErrorNode(element, error); } @@ -309,7 +310,7 @@ function _preprocessInclude(node, context, config, parser) { if (element.children && element.children.length > 0) { if (childContext.callStack.length > CyclicReferenceError.MAX_RECURSIVE_DEPTH) { const error = new CyclicReferenceError(childContext.callStack); - parser._onError(error); + logger.error(error); return utils.createErrorNode(element, error); }