@swayamkothari/codepulse 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +240 -2
- package/bin/codepulse.js +6 -0
- package/package.json +60 -4
- package/src/analyzers/complexity/cognitive.js +170 -0
- package/src/analyzers/complexity/cyclomatic.js +96 -0
- package/src/analyzers/complexity/index.js +232 -0
- package/src/analyzers/dependencies/cycle-detector.js +107 -0
- package/src/analyzers/dependencies/graph-builder.js +118 -0
- package/src/analyzers/dependencies/import-extractor.js +85 -0
- package/src/analyzers/dependencies/index.js +152 -0
- package/src/analyzers/duplication/index.js +366 -0
- package/src/analyzers/duplication/normalizer.js +238 -0
- package/src/analyzers/index.js +9 -0
- package/src/analyzers/smells/index.js +213 -0
- package/src/cli/argument-parser.js +45 -0
- package/src/cli/commands/analyze.js +146 -0
- package/src/cli/commands/compare.js +121 -0
- package/src/cli/commands/history.js +91 -0
- package/src/cli/commands/init.js +51 -0
- package/src/cli/commands/rules.js +110 -0
- package/src/config/loader.js +142 -0
- package/src/config/validator.js +130 -0
- package/src/core/analyzer.js +198 -0
- package/src/core/file-filter.js +95 -0
- package/src/core/project-scanner.js +119 -0
- package/src/history/comparison-engine.js +86 -0
- package/src/history/snapshot-manager.js +190 -0
- package/src/index.js +16 -0
- package/src/reporters/html.js +313 -0
- package/src/reporters/json.js +45 -0
- package/src/reporters/terminal.js +197 -0
- package/src/rules/defaults.js +145 -0
- package/src/rules/registry.js +75 -0
- package/src/scoring/score-calculator.js +299 -0
- package/src/scoring/weights.js +53 -0
- package/src/utils/ast-utils.js +173 -0
- package/src/utils/file-utils.js +64 -0
- package/src/utils/logger.js +54 -0
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { parse } from '@babel/parser';
|
|
2
|
+
import _traverse from '@babel/traverse';
|
|
3
|
+
import { readFileSafe } from './file-utils.js';
|
|
4
|
+
|
|
5
|
+
// @babel/traverse ships as CJS; handle both interop styles.
|
|
6
|
+
const traverse = typeof _traverse === 'function'
|
|
7
|
+
? _traverse
|
|
8
|
+
: (_traverse.default ?? _traverse);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Babel parser plugins enabled for all files.
|
|
12
|
+
* Covers everything in modern JS + JSX without TypeScript.
|
|
13
|
+
*/
|
|
14
|
+
const PARSE_PLUGINS = [
|
|
15
|
+
'jsx',
|
|
16
|
+
'asyncGenerators',
|
|
17
|
+
'bigInt',
|
|
18
|
+
'classProperties',
|
|
19
|
+
'classPrivateProperties',
|
|
20
|
+
'classPrivateMethods',
|
|
21
|
+
'classStaticBlock',
|
|
22
|
+
'dynamicImport',
|
|
23
|
+
'exportDefaultFrom',
|
|
24
|
+
'nullishCoalescingOperator',
|
|
25
|
+
'numericSeparator',
|
|
26
|
+
'objectRestSpread',
|
|
27
|
+
'optionalCatchBinding',
|
|
28
|
+
'optionalChaining',
|
|
29
|
+
'privateIn',
|
|
30
|
+
'topLevelAwait',
|
|
31
|
+
'logicalAssignment',
|
|
32
|
+
'decorators-legacy',
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Parse a JS/JSX/MJS/CJS file into a Babel AST.
|
|
37
|
+
*
|
|
38
|
+
* Returns:
|
|
39
|
+
* { ast, code, sourceType, parseErrors, error }
|
|
40
|
+
*
|
|
41
|
+
* - ast: Babel File node, or null if unrecoverable.
|
|
42
|
+
* - code: Raw source string, or null if file is unreadable.
|
|
43
|
+
* - sourceType: 'module' | 'script'
|
|
44
|
+
* - parseErrors: Array of soft parse errors recovered by Babel (may be empty).
|
|
45
|
+
* - error: Hard Error — file unreadable or completely unparseable.
|
|
46
|
+
*/
|
|
47
|
+
export function parseFile(filePath) {
|
|
48
|
+
const code = readFileSafe(filePath);
|
|
49
|
+
if (code === null) {
|
|
50
|
+
return { ast: null, code: null, sourceType: null, parseErrors: [], error: new Error(`Cannot read file: ${filePath}`) };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Try sourceType=module first (covers ESM and most modern files),
|
|
54
|
+
// then fall back to script (covers CJS and legacy files).
|
|
55
|
+
for (const sourceType of ['module', 'script']) {
|
|
56
|
+
try {
|
|
57
|
+
const ast = parse(code, {
|
|
58
|
+
sourceType,
|
|
59
|
+
allowImportExportEverywhere: true,
|
|
60
|
+
allowReturnOutsideFunction: true,
|
|
61
|
+
allowSuperOutsideMethod: true,
|
|
62
|
+
errorRecovery: true, // return a partial AST on syntax errors
|
|
63
|
+
plugins: PARSE_PLUGINS,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
ast,
|
|
68
|
+
code,
|
|
69
|
+
sourceType,
|
|
70
|
+
parseErrors: ast.errors ?? [],
|
|
71
|
+
error: null,
|
|
72
|
+
};
|
|
73
|
+
} catch {
|
|
74
|
+
// Try next sourceType
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
ast: null,
|
|
80
|
+
code,
|
|
81
|
+
sourceType: null,
|
|
82
|
+
parseErrors: [],
|
|
83
|
+
error: new Error(`Parse failed: ${filePath}`),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Walk an AST with a Babel visitor object.
|
|
89
|
+
* Thin wrapper so callers never import @babel/traverse directly.
|
|
90
|
+
*/
|
|
91
|
+
export function walkAST(ast, visitor) {
|
|
92
|
+
traverse(ast, visitor);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Count total lines in a source string.
|
|
97
|
+
*/
|
|
98
|
+
export function countLines(code) {
|
|
99
|
+
if (code == null) return 0; // null or undefined → no file
|
|
100
|
+
if (code === '') return 1; // empty file still has one (empty) line
|
|
101
|
+
return code.split('\n').length;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Get the 1-based start line of an AST node. Returns 0 if unknown.
|
|
106
|
+
*/
|
|
107
|
+
export function getLine(node) {
|
|
108
|
+
return node?.loc?.start?.line ?? 0;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Get the 1-based end line of an AST node. Returns 0 if unknown.
|
|
113
|
+
*/
|
|
114
|
+
export function getEndLine(node) {
|
|
115
|
+
return node?.loc?.end?.line ?? 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get the 0-based start column of an AST node.
|
|
120
|
+
*/
|
|
121
|
+
export function getColumn(node) {
|
|
122
|
+
return node?.loc?.start?.column ?? 0;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Calculate the number of lines spanned by an AST node.
|
|
127
|
+
*/
|
|
128
|
+
export function nodeLength(node) {
|
|
129
|
+
const start = getLine(node);
|
|
130
|
+
const end = getEndLine(node);
|
|
131
|
+
if (start === 0 && end === 0) return 0;
|
|
132
|
+
return end - start + 1;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Extract the name of a function node (FunctionDeclaration, FunctionExpression,
|
|
137
|
+
* ArrowFunctionExpression, or ObjectMethod/ClassMethod).
|
|
138
|
+
* Returns '<anonymous>' if no name can be determined.
|
|
139
|
+
*/
|
|
140
|
+
export function getFunctionName(node, parentNode) {
|
|
141
|
+
// Named function declaration: function foo() {}
|
|
142
|
+
if (node.id?.name) return node.id.name;
|
|
143
|
+
|
|
144
|
+
// Variable assignment: const foo = () => {}
|
|
145
|
+
if (parentNode?.type === 'VariableDeclarator' && parentNode.id?.name) {
|
|
146
|
+
return parentNode.id.name;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Object/class method: { foo() {} }
|
|
150
|
+
if (parentNode?.type === 'ObjectProperty' || parentNode?.type === 'ClassProperty') {
|
|
151
|
+
if (parentNode.key?.name) return parentNode.key.name;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Assignment expression: foo = function() {}
|
|
155
|
+
if (parentNode?.type === 'AssignmentExpression' && parentNode.left?.name) {
|
|
156
|
+
return parentNode.left.name;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return '<anonymous>';
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Returns true if the node is any kind of function node.
|
|
164
|
+
*/
|
|
165
|
+
export function isFunctionNode(node) {
|
|
166
|
+
return (
|
|
167
|
+
node.type === 'FunctionDeclaration' ||
|
|
168
|
+
node.type === 'FunctionExpression' ||
|
|
169
|
+
node.type === 'ArrowFunctionExpression' ||
|
|
170
|
+
node.type === 'ObjectMethod' ||
|
|
171
|
+
node.type === 'ClassMethod'
|
|
172
|
+
);
|
|
173
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Resolve a path safely, preventing traversal outside of a known base.
|
|
6
|
+
* Throws if the resolved path escapes the base directory.
|
|
7
|
+
*/
|
|
8
|
+
export function safePath(base, relative) {
|
|
9
|
+
const resolvedBase = path.resolve(base);
|
|
10
|
+
const resolved = path.resolve(base, relative);
|
|
11
|
+
if (!resolved.startsWith(resolvedBase + path.sep) && resolved !== resolvedBase) {
|
|
12
|
+
throw new Error(`Path traversal detected: "${relative}" escapes base "${base}"`);
|
|
13
|
+
}
|
|
14
|
+
return resolved;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Returns true if the given path is a symbolic link.
|
|
19
|
+
*/
|
|
20
|
+
export function isSymlink(filePath) {
|
|
21
|
+
try {
|
|
22
|
+
return fs.lstatSync(filePath).isSymbolicLink();
|
|
23
|
+
} catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Read a file as UTF-8, returning null on any error rather than throwing.
|
|
30
|
+
*/
|
|
31
|
+
export function readFileSafe(filePath) {
|
|
32
|
+
try {
|
|
33
|
+
return fs.readFileSync(filePath, 'utf8');
|
|
34
|
+
} catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Return the size of a file in bytes, or 0 if it cannot be stat'd.
|
|
41
|
+
*/
|
|
42
|
+
export function getFileSize(filePath) {
|
|
43
|
+
try {
|
|
44
|
+
return fs.statSync(filePath).size;
|
|
45
|
+
} catch {
|
|
46
|
+
return 0;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Ensure a directory exists, creating it recursively if needed.
|
|
52
|
+
*/
|
|
53
|
+
export function ensureDir(dirPath) {
|
|
54
|
+
fs.mkdirSync(dirPath, { recursive: true });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Write JSON to a file atomically (write to .tmp then rename).
|
|
59
|
+
*/
|
|
60
|
+
export function writeJsonSafe(filePath, data) {
|
|
61
|
+
const tmp = filePath + '.tmp';
|
|
62
|
+
fs.writeFileSync(tmp, JSON.stringify(data, null, 2), 'utf8');
|
|
63
|
+
fs.renameSync(tmp, filePath);
|
|
64
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Levelled logger. All output goes through here so callers never
|
|
3
|
+
* import chalk directly and so-color / NO_COLOR is honoured everywhere.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import chalk from 'chalk';
|
|
7
|
+
|
|
8
|
+
const LEVELS = { silent: 0, error: 1, warn: 2, info: 3, verbose: 4 };
|
|
9
|
+
|
|
10
|
+
let currentLevel = LEVELS.info;
|
|
11
|
+
|
|
12
|
+
export function setLevel(level) {
|
|
13
|
+
currentLevel = LEVELS[level] ?? LEVELS.info;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getLevel() {
|
|
17
|
+
return Object.keys(LEVELS).find(k => LEVELS[k] === currentLevel) ?? 'info';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function error(...args) {
|
|
21
|
+
if (currentLevel >= LEVELS.error) {
|
|
22
|
+
console.error(chalk.red('✖'), ...args);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function warn(...args) {
|
|
27
|
+
if (currentLevel >= LEVELS.warn) {
|
|
28
|
+
console.warn(chalk.yellow('⚠'), ...args);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function info(...args) {
|
|
33
|
+
if (currentLevel >= LEVELS.info) {
|
|
34
|
+
console.log(chalk.blue('ℹ'), ...args);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function verbose(...args) {
|
|
39
|
+
if (currentLevel >= LEVELS.verbose) {
|
|
40
|
+
console.log(chalk.gray('·'), ...args);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function success(...args) {
|
|
45
|
+
if (currentLevel >= LEVELS.info) {
|
|
46
|
+
console.log(chalk.green('✔'), ...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function plain(...args) {
|
|
51
|
+
if (currentLevel >= LEVELS.info) {
|
|
52
|
+
console.log(...args);
|
|
53
|
+
}
|
|
54
|
+
}
|