agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
84 lines (70 loc) • 2.28 kB
JavaScript
/**
* @file String content stripper utility
* @description Removes string literals from code to avoid false positives in analysis
*/
/**
* Strip string content from code before analysis
* @param {string} content - Code content
* @returns {string} Content with strings stripped
*/
function stripStringContent(content) {
// First, handle escaped quotes inside strings
let stripped = content;
// Remove multi-line template literals (including nested ones)
stripped = stripped.replace(/`[\s\S]*?`/g, '``');
// Remove double-quoted strings (handling escaped quotes)
stripped = stripped.replace(/"(?:[^"\\]|\\.)*"/g, '""');
// Remove single-quoted strings (handling escaped quotes)
stripped = stripped.replace(/'(?:[^'\\]|\\.)*'/g, "''");
// Remove regex literals
stripped = stripped.replace(/\/(?:[^\/\\]|\\.)+\/[gimuy]*/g, '//');
return stripped;
}
/**
* Strip comments from code
* @param {string} content - Code content
* @returns {string} Content with comments stripped
*/
function stripComments(content) {
// Remove single-line comments
let stripped = content.replace(/\/\/.*$/gm, '');
// Remove multi-line comments
stripped = stripped.replace(/\/\*[\s\S]*?\*\//g, '');
return stripped;
}
/**
* Check if a file is a test file
* @param {string} filePath - File path
* @returns {boolean} True if test file
*/
function isTestFile(filePath) {
return filePath.includes('.test.') ||
filePath.includes('.spec.') ||
filePath.includes('__tests__') ||
filePath.includes('__test__') ||
filePath.endsWith('test.js') ||
filePath.endsWith('spec.js');
}
/**
* Get file type for threshold configuration
* @param {string} filePath - File path
* @returns {string} File type (test, config, or default)
*/
function getFileType(filePath) {
if (isTestFile(filePath)) return 'test';
const filename = filePath.split('/').pop().toLowerCase();
if (filename.includes('config') ||
filename.includes('settings') ||
filename.includes('.config.') ||
filename === 'package.json' ||
filename === 'tsconfig.json') {
return 'config';
}
return 'default';
}
module.exports = {
stripStringContent,
stripComments,
isTestFile,
getFileType
};