diff options
author | LinuxWizard42 <computerwizard@linuxmail.org> | 2022-10-12 22:54:37 +0300 |
---|---|---|
committer | LinuxWizard42 <computerwizard@linuxmail.org> | 2022-10-12 22:54:37 +0300 |
commit | 703e03aba33f234712206769f57717ba7d92d23d (patch) | |
tree | 0041f04ccb75bd5379c764e9fe42249fffe75fc3 /node_modules/ajv/scripts/compile-dots.js | |
parent | ab6e257e6e9d9a483d7e86f220d8b209a2cd7753 (diff) | |
download | FlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.gz FlashRunner-703e03aba33f234712206769f57717ba7d92d23d.tar.zst |
Added export_allowed file to make repository visible in cgit
Diffstat (limited to 'node_modules/ajv/scripts/compile-dots.js')
-rw-r--r-- | node_modules/ajv/scripts/compile-dots.js | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/node_modules/ajv/scripts/compile-dots.js b/node_modules/ajv/scripts/compile-dots.js new file mode 100644 index 0000000..5a21a7d --- /dev/null +++ b/node_modules/ajv/scripts/compile-dots.js @@ -0,0 +1,73 @@ +//compile doT templates to js functions +'use strict'; + +var glob = require('glob') + , fs = require('fs') + , path = require('path') + , doT = require('dot') + , beautify = require('js-beautify').js_beautify; + +var defsRootPath = process.argv[2] || path.join(__dirname, '../lib'); + +var defs = {}; +var defFiles = glob.sync('./dot/**/*.def', { cwd: defsRootPath }); +defFiles.forEach(function (f) { + var name = path.basename(f, '.def'); + defs[name] = fs.readFileSync(path.join(defsRootPath, f)); +}); + +var filesRootPath = process.argv[3] || path.join(__dirname, '../lib'); +var files = glob.sync('./dot/**/*.jst', { cwd: filesRootPath }); + +var dotjsPath = path.join(filesRootPath, './dotjs'); +try { fs.mkdirSync(dotjsPath); } catch(e) {} + +console.log('\n\nCompiling:'); + +var FUNCTION_NAME = /function\s+anonymous\s*\(it[^)]*\)\s*{/; +var OUT_EMPTY_STRING = /out\s*\+=\s*'\s*';/g; +var ISTANBUL = /'(istanbul[^']+)';/g; +var ERROR_KEYWORD = /\$errorKeyword/g; +var ERROR_KEYWORD_OR = /\$errorKeyword\s+\|\|/g; +var VARS = [ + '$errs', '$valid', '$lvl', '$data', '$dataLvl', + '$errorKeyword', '$closingBraces', '$schemaPath', + '$validate' +]; + +files.forEach(function (f) { + var keyword = path.basename(f, '.jst'); + var targetPath = path.join(dotjsPath, keyword + '.js'); + var template = fs.readFileSync(path.join(filesRootPath, f)); + var code = doT.compile(template, defs); + code = code.toString() + .replace(OUT_EMPTY_STRING, '') + .replace(FUNCTION_NAME, 'function generate_' + keyword + '(it, $keyword, $ruleType) {') + .replace(ISTANBUL, '/* $1 */'); + removeAlwaysFalsyInOr(); + VARS.forEach(removeUnusedVar); + code = "'use strict';\nmodule.exports = " + code; + code = beautify(code, { indent_size: 2 }) + '\n'; + fs.writeFileSync(targetPath, code); + console.log('compiled', keyword); + + function removeUnusedVar(v) { + v = v.replace(/\$/g, '\\$$'); + var regexp = new RegExp(v + '[^A-Za-z0-9_$]', 'g'); + var count = occurrences(regexp); + if (count == 1) { + regexp = new RegExp('var\\s+' + v + '\\s*=[^;]+;|var\\s+' + v + ';'); + code = code.replace(regexp, ''); + } + } + + function removeAlwaysFalsyInOr() { + var countUsed = occurrences(ERROR_KEYWORD); + var countOr = occurrences(ERROR_KEYWORD_OR); + if (countUsed == countOr + 1) code = code.replace(ERROR_KEYWORD_OR, ''); + } + + function occurrences(regexp) { + return (code.match(regexp) || []).length; + } +}); |