|
| 1 | +/** |
| 2 | + * Advanced JavaScript sample for testing TOON parser coverage. |
| 3 | + * Covers: IIFEs, nested functions, var declarations, function expressions, |
| 4 | + * module.exports, CommonJS require, arrow functions with params. |
| 5 | + */ |
| 6 | + |
| 7 | +const fs = require('fs'); |
| 8 | +const path = require('path'); |
| 9 | +const { EventEmitter } = require('events'); |
| 10 | + |
| 11 | +// Arrow function assigned to const |
| 12 | +const getArg = (name, def) => { |
| 13 | + const idx = process.argv.indexOf(`--${name}`); |
| 14 | + if (idx !== -1) return process.argv[idx + 1]; |
| 15 | + return def; |
| 16 | +}; |
| 17 | + |
| 18 | +// Arrow function assigned to let |
| 19 | +let processItem = (item) => { |
| 20 | + return item.trim().toLowerCase(); |
| 21 | +}; |
| 22 | + |
| 23 | +// Function expression assigned to const |
| 24 | +const validate = function(input) { |
| 25 | + if (!input || typeof input !== 'string') return false; |
| 26 | + return input.length > 0; |
| 27 | +}; |
| 28 | + |
| 29 | +// Async function expression assigned to const |
| 30 | +const fetchResource = async function(url, options) { |
| 31 | + const response = await fetch(url, options); |
| 32 | + return response.json(); |
| 33 | +}; |
| 34 | + |
| 35 | +// var arrow function |
| 36 | +var shouldIgnore = (filePath) => { |
| 37 | + return filePath.includes('node_modules'); |
| 38 | +}; |
| 39 | + |
| 40 | +// var function expression |
| 41 | +var formatOutput = function(data, indent) { |
| 42 | + return JSON.stringify(data, null, indent || 2); |
| 43 | +}; |
| 44 | + |
| 45 | +// Regular top-level function |
| 46 | +function walk(dir, onFile) { |
| 47 | + const entries = fs.readdirSync(dir, { withFileTypes: true }); |
| 48 | + for (const e of entries) { |
| 49 | + const full = path.join(dir, e.name); |
| 50 | + if (e.isDirectory()) walk(full, onFile); |
| 51 | + else onFile(full); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +// Function with nested function inside |
| 56 | +function findFiles(dir, extensions) { |
| 57 | + const files = []; |
| 58 | + |
| 59 | + function traverse(currentDir) { |
| 60 | + const items = fs.readdirSync(currentDir); |
| 61 | + for (const item of items) { |
| 62 | + const fullPath = path.join(currentDir, item); |
| 63 | + if (fs.statSync(fullPath).isDirectory()) { |
| 64 | + traverse(fullPath); |
| 65 | + } else if (extensions.includes(path.extname(item))) { |
| 66 | + files.push(fullPath); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + traverse(dir); |
| 72 | + return files; |
| 73 | +} |
| 74 | + |
| 75 | +// Async top-level function |
| 76 | +async function processFiles(pattern) { |
| 77 | + const files = findFiles('.', ['.js', '.ts']); |
| 78 | + for (const file of files) { |
| 79 | + const content = fs.readFileSync(file, 'utf8'); |
| 80 | + console.log(`Processing: ${file}`); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// Class with methods |
| 85 | +class FileProcessor extends EventEmitter { |
| 86 | + constructor(rootDir) { |
| 87 | + super(); |
| 88 | + this.rootDir = rootDir; |
| 89 | + this.results = []; |
| 90 | + } |
| 91 | + |
| 92 | + async analyze(filePath) { |
| 93 | + const content = fs.readFileSync(filePath, 'utf8'); |
| 94 | + const result = { path: filePath, lines: content.split('\n').length }; |
| 95 | + this.results.push(result); |
| 96 | + this.emit('analyzed', result); |
| 97 | + return result; |
| 98 | + } |
| 99 | + |
| 100 | + static fromConfig(configPath) { |
| 101 | + const config = JSON.parse(fs.readFileSync(configPath, 'utf8')); |
| 102 | + return new FileProcessor(config.rootDir); |
| 103 | + } |
| 104 | + |
| 105 | + getResults() { |
| 106 | + return [...this.results]; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Deeply nested function |
| 111 | +function outerFunction(data) { |
| 112 | + function middleHelper(items) { |
| 113 | + function innerSort(a, b) { |
| 114 | + return a.localeCompare(b); |
| 115 | + } |
| 116 | + return items.sort(innerSort); |
| 117 | + } |
| 118 | + return middleHelper(data); |
| 119 | +} |
| 120 | + |
| 121 | +// IIFE with named function |
| 122 | +(function main() { |
| 123 | + const root = getArg('root', '.'); |
| 124 | + console.log(`Scanning ${root}...`); |
| 125 | + walk(root, (file) => { |
| 126 | + console.log(file); |
| 127 | + }); |
| 128 | +})(); |
| 129 | + |
| 130 | +// module.exports with shorthand properties |
| 131 | +module.exports = { |
| 132 | + getArg, |
| 133 | + processItem, |
| 134 | + validate, |
| 135 | + fetchResource, |
| 136 | + shouldIgnore, |
| 137 | + formatOutput, |
| 138 | + walk, |
| 139 | + findFiles, |
| 140 | + processFiles, |
| 141 | + FileProcessor, |
| 142 | + outerFunction |
| 143 | +}; |
0 commit comments