From dc38b52a3ead6e3f65e937f584d21ed1689b6c5d Mon Sep 17 00:00:00 2001 From: Stanislav A Date: Thu, 15 Dec 2022 21:41:26 +0300 Subject: [PATCH] refactor and update readme --- README.md | 5 +++-- src/scriptlets/index.js | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index af622964..24c3ca5a 100644 --- a/README.md +++ b/README.md @@ -161,9 +161,10 @@ And also there is a module at `dist/scriptlets.js` which has been exported to a ```javascript /** - * Returns scriptlet code + * Returns scriptlet code by param * @param {Source} source - * @returns {string} + * @returns {string|null} scriptlet code + * @throws on unknown scriptlet name */ scriptlets.invoke(source); ``` diff --git a/src/scriptlets/index.js b/src/scriptlets/index.js index 52508664..c2ca22f7 100644 --- a/src/scriptlets/index.js +++ b/src/scriptlets/index.js @@ -29,20 +29,22 @@ import { getScriptletFunction } from '../../tmp/scriptlets-func'; * Returns scriptlet code by param * @param {Source} source * @returns {string|null} scriptlet code + * @throws on unknown scriptlet name */ function getScriptletCode(source) { if (!validator.isValidScriptletName(source.name)) { return null; } - const scriptletFunction = getScriptletFunction(source.name)?.toString(); - if (!scriptletFunction) { + const scriptletFunction = getScriptletFunction(source.name); + if (typeof scriptletFunction !== 'function') { throw new Error(`Error: cannot invoke scriptlet with name: '${source.name}'`); } + const scriptletFunctionString = scriptletFunction.toString(); const result = source.engine === 'corelibs' || source.engine === 'test' - ? wrapInNonameFunc(scriptletFunction) - : passSourceAndProps(source, scriptletFunction); + ? wrapInNonameFunc(scriptletFunctionString) + : passSourceAndProps(source, scriptletFunctionString); return result; }