Skip to content

Commit

Permalink
refactor and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 15, 2022
1 parent 35440f5 commit dc38b52
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```
Expand Down
10 changes: 6 additions & 4 deletions src/scriptlets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit dc38b52

Please sign in to comment.