diff --git a/README.md b/README.md index af622964c..24c3ca5a1 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 69aba887f..c3d618e8d 100644 --- a/src/scriptlets/index.js +++ b/src/scriptlets/index.js @@ -29,16 +29,24 @@ 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(); + const scriptletFunction = getScriptletFunction(source.name); + // In case isValidScriptletName check will pass invalid scriptlet name, + // for example when there is a bad alias + 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; }