Spaces:
Running
Running
File size: 6,980 Bytes
6f3e28d dd9f776 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JavaScript Code Analyzer</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script>
<script src="https://unpkg.com/jshint@2.13.5/dist/jshint.js"></script>
</head>
<body class="bg-gray-100 p-8">
<div class="container mx-auto">
<h1 class="text-3xl font-bold mb-6 text-center">
JavaScript Code Analysis Tool
</h1>
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<!-- Code Editor -->
<div>
<h2 class="text-xl font-semibold mb-4">Input JavaScript Code</h2>
<div
id="editor"
class="h-96 border rounded-lg"
>// Paste your JavaScript code here
function exampleFunction() {
// Your code goes here
}
</div>
</div>
<!-- Results Panel -->
<div>
<h2 class="text-xl font-semibold mb-4">Analysis Results</h2>
<div
id="analysisResults"
class="bg-white border rounded-lg p-4 h-96 overflow-y-auto"
>
<div id="lintingErrors" class="mb-4">
<h3 class="font-bold text-red-600">Linting Errors</h3>
</div>
<div id="consoleOutput">
<h3 class="font-bold text-green-600">Console Output</h3>
</div>
</div>
</div>
</div>
<!-- Action Buttons -->
<div class="flex justify-center space-x-4 mt-6">
<button
id="lintButton"
class="bg-yellow-500 text-white px-6 py-2 rounded hover:bg-yellow-600"
>
Lint Code
</button>
<button
id="runButton"
class="bg-blue-500 text-white px-6 py-2 rounded hover:bg-blue-600"
>
Run Code
</button>
<button
id="autoFixButton"
class="bg-green-500 text-white px-6 py-2 rounded hover:bg-green-600"
>
Auto Fix
</button>
</div>
</div>
<script>
const editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
document.getElementById('lintButton').addEventListener('click', () => {
const code = editor.getValue();
const lintingErrors = document.getElementById('lintingErrors');
lintingErrors.innerHTML = '<h3 class="font-bold text-red-600">Linting Errors</h3>';
JSHINT(code, {
esversion: 6,
asi: true,
unused: true,
globals: {
console: true,
window: true,
document: true
}
});
if (JSHINT.errors.length > 0) {
JSHINT.errors.forEach((error, index) => {
if (error) {
const errorEl = document.createElement('div');
errorEl.innerHTML = `
<span class="text-red-500">Error ${index + 1}:</span>
Line ${error.line}: ${error.reason}
`;
errorEl.className = 'mt-2 p-2 bg-red-50 rounded';
lintingErrors.appendChild(errorEl);
}
});
} else {
const successEl = document.createElement('div');
successEl.textContent = '✅ No linting errors found!';
successEl.className = 'text-green-600 mt-2';
lintingErrors.appendChild(successEl);
}
});
document.getElementById('runButton').addEventListener('click', () => {
const consoleOutput = document.getElementById('consoleOutput');
consoleOutput.innerHTML = '<h3 class="font-bold text-green-600">Console Output</h3>';
try {
const code = editor.getValue();
const originalLog = console.log;
const logs = [];
console.log = (...args) => {
logs.push(args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : arg
).join(' '));
};
const runCode = new Function('console', code);
runCode(console);
console.log = originalLog;
logs.forEach(log => {
const logEl = document.createElement('div');
logEl.textContent = log;
consoleOutput.appendChild(logEl);
});
} catch (error) {
const errorEl = document.createElement('div');
errorEl.innerHTML = `
<span class="text-red-500">🚨 Execution Error:</span>
${error.message}
`;
errorEl.className = 'mt-2 p-2 bg-red-50 rounded';
consoleOutput.appendChild(errorEl);
}
});
document.getElementById('autoFixButton').addEventListener('click', () => {
const code = editor.getValue();
const lintingErrors = document.getElementById('lintingErrors');
lintingErrors.innerHTML = '<h3 class="font-bold text-green-600">Auto-Fix Suggestions</h3>';
let fixedCode = code;
// Basic auto-fix strategies
fixedCode = fixedCode.replace(/([^;])\s*$/gm, '$1;');
const unclosedStringRegex = /([`'"])[^'"]*$/gm;
if (unclosedStringRegex.test(fixedCode)) {
fixedCode = fixedCode.replace(unclosedStringRegex, '$1');
}
const undefinedVarRegex = /\b(\w+)\s*=[^;]+;(?!\s*const|\s*let|\s*var)/g;
fixedCode = fixedCode.replace(undefinedVarRegex, 'let $1 = $&');
editor.setValue(fixedCode, -1);
const suggestionsEl = document.createElement('div');
suggestionsEl.innerHTML = `
✅ Auto-Fixed:
<ul>
<li>• Added missing semicolons</li>
<li>• Closed unclosed strings</li>
<li>• Declared potential undefined variables</li>
</ul>
`;
suggestionsEl.className = 'mt-2 p-2 bg-green-50 rounded';
lintingErrors.appendChild(suggestionsEl);
});
</script>
</body>
</html>
|