react-code-smell-detector 1.0.0 → 1.1.1
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/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +43 -1
- package/dist/cli.js +0 -0
- package/dist/detectors/deadCode.d.ts +14 -0
- package/dist/detectors/deadCode.d.ts.map +1 -0
- package/dist/detectors/deadCode.js +141 -0
- package/dist/detectors/dependencyArray.d.ts +7 -0
- package/dist/detectors/dependencyArray.d.ts.map +1 -0
- package/dist/detectors/dependencyArray.js +164 -0
- package/dist/detectors/hooksRules.d.ts +7 -0
- package/dist/detectors/hooksRules.d.ts.map +1 -0
- package/dist/detectors/hooksRules.js +81 -0
- package/dist/detectors/index.d.ts +11 -0
- package/dist/detectors/index.d.ts.map +1 -1
- package/dist/detectors/index.js +12 -0
- package/dist/detectors/javascript.d.ts +11 -0
- package/dist/detectors/javascript.d.ts.map +1 -0
- package/dist/detectors/javascript.js +148 -0
- package/dist/detectors/magicValues.d.ts +7 -0
- package/dist/detectors/magicValues.d.ts.map +1 -0
- package/dist/detectors/magicValues.js +99 -0
- package/dist/detectors/missingKey.d.ts +7 -0
- package/dist/detectors/missingKey.d.ts.map +1 -0
- package/dist/detectors/missingKey.js +93 -0
- package/dist/detectors/nestedTernary.d.ts +7 -0
- package/dist/detectors/nestedTernary.d.ts.map +1 -0
- package/dist/detectors/nestedTernary.js +58 -0
- package/dist/detectors/nextjs.d.ts +11 -0
- package/dist/detectors/nextjs.d.ts.map +1 -0
- package/dist/detectors/nextjs.js +103 -0
- package/dist/detectors/nodejs.d.ts +11 -0
- package/dist/detectors/nodejs.d.ts.map +1 -0
- package/dist/detectors/nodejs.js +169 -0
- package/dist/detectors/reactNative.d.ts +10 -0
- package/dist/detectors/reactNative.d.ts.map +1 -0
- package/dist/detectors/reactNative.js +135 -0
- package/dist/detectors/typescript.d.ts +11 -0
- package/dist/detectors/typescript.d.ts.map +1 -0
- package/dist/detectors/typescript.js +135 -0
- package/dist/reporter.js +30 -0
- package/dist/types/index.d.ts +14 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +14 -0
- package/package.json +1 -1
- package/src/analyzer.ts +53 -0
- package/src/detectors/deadCode.ts +163 -0
- package/src/detectors/dependencyArray.ts +176 -0
- package/src/detectors/hooksRules.ts +101 -0
- package/src/detectors/index.ts +12 -0
- package/src/detectors/javascript.ts +169 -0
- package/src/detectors/magicValues.ts +114 -0
- package/src/detectors/missingKey.ts +105 -0
- package/src/detectors/nestedTernary.ts +75 -0
- package/src/detectors/nextjs.ts +124 -0
- package/src/detectors/nodejs.ts +199 -0
- package/src/detectors/reactNative.ts +154 -0
- package/src/detectors/typescript.ts +151 -0
- package/src/reporter.ts +30 -0
- package/src/types/index.ts +59 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { ParsedComponent, getCodeSnippet } from '../parser/index.js';
|
|
3
|
+
import { CodeSmell, DetectorConfig, DEFAULT_CONFIG } from '../types/index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Detects magic numbers and strings that should be constants
|
|
7
|
+
*/
|
|
8
|
+
export function detectMagicValues(
|
|
9
|
+
component: ParsedComponent,
|
|
10
|
+
filePath: string,
|
|
11
|
+
sourceCode: string,
|
|
12
|
+
config: DetectorConfig = DEFAULT_CONFIG
|
|
13
|
+
): CodeSmell[] {
|
|
14
|
+
if (!config.checkMagicValues) return [];
|
|
15
|
+
|
|
16
|
+
const smells: CodeSmell[] = [];
|
|
17
|
+
const threshold = config.magicNumberThreshold;
|
|
18
|
+
|
|
19
|
+
// Track locations to avoid duplicate reports
|
|
20
|
+
const reportedLines = new Set<number>();
|
|
21
|
+
|
|
22
|
+
component.path.traverse({
|
|
23
|
+
NumericLiteral(path) {
|
|
24
|
+
const value = path.node.value;
|
|
25
|
+
const loc = path.node.loc;
|
|
26
|
+
const line = loc?.start.line || 0;
|
|
27
|
+
|
|
28
|
+
// Skip common acceptable values
|
|
29
|
+
if (value === 0 || value === 1 || value === -1 || value === 2 || value === 100) return;
|
|
30
|
+
|
|
31
|
+
// Skip array indices and small numbers
|
|
32
|
+
if (value < threshold && value > -threshold) return;
|
|
33
|
+
|
|
34
|
+
// Skip if inside an object key position (e.g., style objects)
|
|
35
|
+
if (t.isObjectProperty(path.parent) && path.parent.key === path.node) return;
|
|
36
|
+
|
|
37
|
+
// Skip if it's a port number or similar config
|
|
38
|
+
if (value === 3000 || value === 8080 || value === 80 || value === 443) return;
|
|
39
|
+
|
|
40
|
+
// Skip if already reported this line
|
|
41
|
+
if (reportedLines.has(line)) return;
|
|
42
|
+
reportedLines.add(line);
|
|
43
|
+
|
|
44
|
+
// Check context - skip if it's in a variable declaration that names the constant
|
|
45
|
+
if (t.isVariableDeclarator(path.parent)) {
|
|
46
|
+
const varName = t.isIdentifier(path.parent.id) ? path.parent.id.name : '';
|
|
47
|
+
// If variable name is UPPER_CASE, it's already a constant
|
|
48
|
+
if (varName === varName.toUpperCase() && varName.includes('_')) return;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
smells.push({
|
|
52
|
+
type: 'magic-value',
|
|
53
|
+
severity: 'info',
|
|
54
|
+
message: `Magic number ${value} in "${component.name}"`,
|
|
55
|
+
file: filePath,
|
|
56
|
+
line,
|
|
57
|
+
column: loc?.start.column || 0,
|
|
58
|
+
suggestion: `Extract to a named constant: const DESCRIPTIVE_NAME = ${value}`,
|
|
59
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
60
|
+
});
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
StringLiteral(path) {
|
|
64
|
+
const value = path.node.value;
|
|
65
|
+
const loc = path.node.loc;
|
|
66
|
+
const line = loc?.start.line || 0;
|
|
67
|
+
|
|
68
|
+
// Skip short strings, empty strings, common values
|
|
69
|
+
if (value.length < 10) return;
|
|
70
|
+
|
|
71
|
+
// Skip if it looks like a URL, path, or import
|
|
72
|
+
if (value.startsWith('http') || value.startsWith('/') || value.startsWith('.')) return;
|
|
73
|
+
|
|
74
|
+
// Skip if it's an import source
|
|
75
|
+
if (t.isImportDeclaration(path.parent)) return;
|
|
76
|
+
|
|
77
|
+
// Skip JSX text content (usually intentional)
|
|
78
|
+
if (t.isJSXText(path.parent) || t.isJSXExpressionContainer(path.parent)) return;
|
|
79
|
+
|
|
80
|
+
// Skip object property keys
|
|
81
|
+
if (t.isObjectProperty(path.parent) && path.parent.key === path.node) return;
|
|
82
|
+
|
|
83
|
+
// Skip if already reported this line
|
|
84
|
+
if (reportedLines.has(line)) return;
|
|
85
|
+
|
|
86
|
+
// Skip common patterns
|
|
87
|
+
if (
|
|
88
|
+
value.includes('className') ||
|
|
89
|
+
value.includes('px') ||
|
|
90
|
+
value.includes('rem') ||
|
|
91
|
+
value.includes('%')
|
|
92
|
+
) return;
|
|
93
|
+
|
|
94
|
+
// Check if it looks like a user-facing message or error
|
|
95
|
+
const looksLikeMessage = /^[A-Z][a-z]+.*[.!?]?$/.test(value) || value.includes(' ');
|
|
96
|
+
|
|
97
|
+
if (looksLikeMessage && value.length > 30) {
|
|
98
|
+
reportedLines.add(line);
|
|
99
|
+
smells.push({
|
|
100
|
+
type: 'magic-value',
|
|
101
|
+
severity: 'info',
|
|
102
|
+
message: `Hardcoded string in "${component.name}" should be in constants or i18n`,
|
|
103
|
+
file: filePath,
|
|
104
|
+
line,
|
|
105
|
+
column: loc?.start.column || 0,
|
|
106
|
+
suggestion: `Extract to a constants file or use i18n: "${value.substring(0, 30)}..."`,
|
|
107
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
return smells;
|
|
114
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { ParsedComponent, getCodeSnippet } from '../parser/index.js';
|
|
3
|
+
import { CodeSmell, DetectorConfig, DEFAULT_CONFIG } from '../types/index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Detects .map() calls that render JSX without a key prop
|
|
7
|
+
*/
|
|
8
|
+
export function detectMissingKeys(
|
|
9
|
+
component: ParsedComponent,
|
|
10
|
+
filePath: string,
|
|
11
|
+
sourceCode: string,
|
|
12
|
+
config: DetectorConfig = DEFAULT_CONFIG
|
|
13
|
+
): CodeSmell[] {
|
|
14
|
+
if (!config.checkMissingKeys) return [];
|
|
15
|
+
|
|
16
|
+
const smells: CodeSmell[] = [];
|
|
17
|
+
|
|
18
|
+
component.path.traverse({
|
|
19
|
+
CallExpression(path) {
|
|
20
|
+
// Check if it's a .map() call
|
|
21
|
+
if (!t.isMemberExpression(path.node.callee)) return;
|
|
22
|
+
|
|
23
|
+
const prop = path.node.callee.property;
|
|
24
|
+
if (!t.isIdentifier(prop) || prop.name !== 'map') return;
|
|
25
|
+
|
|
26
|
+
// Check if the callback returns JSX
|
|
27
|
+
const callback = path.node.arguments[0];
|
|
28
|
+
if (!callback) return;
|
|
29
|
+
|
|
30
|
+
let callbackBody: t.Node | null = null;
|
|
31
|
+
|
|
32
|
+
if (t.isArrowFunctionExpression(callback)) {
|
|
33
|
+
callbackBody = callback.body;
|
|
34
|
+
} else if (t.isFunctionExpression(callback)) {
|
|
35
|
+
callbackBody = callback.body;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!callbackBody) return;
|
|
39
|
+
|
|
40
|
+
// Find JSX elements in the callback
|
|
41
|
+
let hasJSX = false;
|
|
42
|
+
let hasKey = false;
|
|
43
|
+
let jsxLine = path.node.loc?.start.line || 0;
|
|
44
|
+
|
|
45
|
+
const checkForKey = (node: t.Node) => {
|
|
46
|
+
if (t.isJSXElement(node)) {
|
|
47
|
+
hasJSX = true;
|
|
48
|
+
jsxLine = node.loc?.start.line || jsxLine;
|
|
49
|
+
|
|
50
|
+
// Check if the opening element has a key prop
|
|
51
|
+
const openingElement = node.openingElement;
|
|
52
|
+
const keyAttr = openingElement.attributes.find(attr => {
|
|
53
|
+
if (t.isJSXAttribute(attr)) {
|
|
54
|
+
return t.isJSXIdentifier(attr.name) && attr.name.name === 'key';
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
if (keyAttr) {
|
|
60
|
+
hasKey = true;
|
|
61
|
+
}
|
|
62
|
+
} else if (t.isJSXFragment(node)) {
|
|
63
|
+
hasJSX = true;
|
|
64
|
+
// Fragments can't have keys directly, so check children
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Traverse the callback body to find JSX
|
|
69
|
+
if (t.isJSXElement(callbackBody) || t.isJSXFragment(callbackBody)) {
|
|
70
|
+
checkForKey(callbackBody);
|
|
71
|
+
} else if (t.isBlockStatement(callbackBody)) {
|
|
72
|
+
path.traverse({
|
|
73
|
+
ReturnStatement(returnPath) {
|
|
74
|
+
if (returnPath.node.argument) {
|
|
75
|
+
checkForKey(returnPath.node.argument);
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
JSXElement(jsxPath) {
|
|
79
|
+
// Only check top-level JSX in the map callback
|
|
80
|
+
if (jsxPath.parent === callbackBody ||
|
|
81
|
+
(t.isReturnStatement(jsxPath.parent) && jsxPath.parent.argument === jsxPath.node)) {
|
|
82
|
+
checkForKey(jsxPath.node);
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (hasJSX && !hasKey) {
|
|
89
|
+
const loc = path.node.loc;
|
|
90
|
+
smells.push({
|
|
91
|
+
type: 'missing-key',
|
|
92
|
+
severity: 'error',
|
|
93
|
+
message: `Missing "key" prop in .map() that renders JSX in "${component.name}"`,
|
|
94
|
+
file: filePath,
|
|
95
|
+
line: jsxLine || loc?.start.line || 0,
|
|
96
|
+
column: loc?.start.column || 0,
|
|
97
|
+
suggestion: 'Add a unique "key" prop to the root element: <Element key={item.id}>',
|
|
98
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
return smells;
|
|
105
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { NodePath } from '@babel/traverse';
|
|
3
|
+
import { ParsedComponent, getCodeSnippet } from '../parser/index.js';
|
|
4
|
+
import { CodeSmell, DetectorConfig, DEFAULT_CONFIG } from '../types/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Detects deeply nested ternary expressions (complex conditional rendering)
|
|
8
|
+
*/
|
|
9
|
+
export function detectNestedTernaries(
|
|
10
|
+
component: ParsedComponent,
|
|
11
|
+
filePath: string,
|
|
12
|
+
sourceCode: string,
|
|
13
|
+
config: DetectorConfig = DEFAULT_CONFIG
|
|
14
|
+
): CodeSmell[] {
|
|
15
|
+
const smells: CodeSmell[] = [];
|
|
16
|
+
const maxDepth = config.maxTernaryDepth;
|
|
17
|
+
|
|
18
|
+
component.path.traverse({
|
|
19
|
+
ConditionalExpression(path) {
|
|
20
|
+
// Only check the outermost ternary
|
|
21
|
+
if (isNestedInTernary(path)) return;
|
|
22
|
+
|
|
23
|
+
const depth = getTernaryDepth(path.node);
|
|
24
|
+
|
|
25
|
+
if (depth > maxDepth) {
|
|
26
|
+
const loc = path.node.loc;
|
|
27
|
+
smells.push({
|
|
28
|
+
type: 'nested-ternary',
|
|
29
|
+
severity: depth > maxDepth + 1 ? 'warning' : 'info',
|
|
30
|
+
message: `Nested ternary expression (depth: ${depth}) in "${component.name}"`,
|
|
31
|
+
file: filePath,
|
|
32
|
+
line: loc?.start.line || 0,
|
|
33
|
+
column: loc?.start.column || 0,
|
|
34
|
+
suggestion: `Refactor to use if/else, switch, or extract into separate components. Max recommended depth: ${maxDepth}`,
|
|
35
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return smells;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Check if a ternary is nested inside another ternary
|
|
46
|
+
*/
|
|
47
|
+
function isNestedInTernary(path: NodePath): boolean {
|
|
48
|
+
let current: NodePath | null = path.parentPath;
|
|
49
|
+
|
|
50
|
+
while (current) {
|
|
51
|
+
if (t.isConditionalExpression(current.node)) {
|
|
52
|
+
return true;
|
|
53
|
+
}
|
|
54
|
+
current = current.parentPath;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Calculate the depth of nested ternary expressions
|
|
62
|
+
*/
|
|
63
|
+
function getTernaryDepth(node: t.ConditionalExpression): number {
|
|
64
|
+
let maxChildDepth = 0;
|
|
65
|
+
|
|
66
|
+
if (t.isConditionalExpression(node.consequent)) {
|
|
67
|
+
maxChildDepth = Math.max(maxChildDepth, getTernaryDepth(node.consequent));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (t.isConditionalExpression(node.alternate)) {
|
|
71
|
+
maxChildDepth = Math.max(maxChildDepth, getTernaryDepth(node.alternate));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return 1 + maxChildDepth;
|
|
75
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { ParsedComponent, getCodeSnippet } from '../parser/index.js';
|
|
3
|
+
import { CodeSmell, DetectorConfig, DEFAULT_CONFIG } from '../types/index.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Detects Next.js-specific code smells:
|
|
7
|
+
* - Missing 'use client' / 'use server' directives
|
|
8
|
+
* - Unoptimized images (using <img> instead of next/image)
|
|
9
|
+
* - Router misuse patterns
|
|
10
|
+
* - Missing metadata exports
|
|
11
|
+
*/
|
|
12
|
+
export function detectNextjsIssues(
|
|
13
|
+
component: ParsedComponent,
|
|
14
|
+
filePath: string,
|
|
15
|
+
sourceCode: string,
|
|
16
|
+
config: DetectorConfig = DEFAULT_CONFIG,
|
|
17
|
+
imports: string[] = []
|
|
18
|
+
): CodeSmell[] {
|
|
19
|
+
if (!config.checkNextjs) return [];
|
|
20
|
+
|
|
21
|
+
// Only run on Next.js projects (check for next imports)
|
|
22
|
+
const isNextProject = imports.some(imp =>
|
|
23
|
+
imp.includes('next/') || imp.includes('next')
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// Also check file path patterns
|
|
27
|
+
const isAppRouter = filePath.includes('/app/') &&
|
|
28
|
+
(filePath.endsWith('page.tsx') || filePath.endsWith('page.jsx') ||
|
|
29
|
+
filePath.endsWith('layout.tsx') || filePath.endsWith('layout.jsx'));
|
|
30
|
+
|
|
31
|
+
const smells: CodeSmell[] = [];
|
|
32
|
+
|
|
33
|
+
// Check for unoptimized images (using <img> instead of next/image)
|
|
34
|
+
component.path.traverse({
|
|
35
|
+
JSXOpeningElement(path) {
|
|
36
|
+
if (t.isJSXIdentifier(path.node.name) && path.node.name.name === 'img') {
|
|
37
|
+
const loc = path.node.loc;
|
|
38
|
+
smells.push({
|
|
39
|
+
type: 'nextjs-image-unoptimized',
|
|
40
|
+
severity: 'warning',
|
|
41
|
+
message: `Using native <img> instead of next/image in "${component.name}"`,
|
|
42
|
+
file: filePath,
|
|
43
|
+
line: loc?.start.line || 0,
|
|
44
|
+
column: loc?.start.column || 0,
|
|
45
|
+
suggestion: 'Use next/image for automatic image optimization: import Image from "next/image"',
|
|
46
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Check for client-side hooks in server components (App Router)
|
|
53
|
+
if (isAppRouter && !sourceCode.includes("'use client'") && !sourceCode.includes('"use client"')) {
|
|
54
|
+
const clientHooks = ['useState', 'useEffect', 'useContext', 'useReducer', 'useRef'];
|
|
55
|
+
const usedClientHooks: string[] = [];
|
|
56
|
+
|
|
57
|
+
component.path.traverse({
|
|
58
|
+
CallExpression(path) {
|
|
59
|
+
if (t.isIdentifier(path.node.callee) && clientHooks.includes(path.node.callee.name)) {
|
|
60
|
+
usedClientHooks.push(path.node.callee.name);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
if (usedClientHooks.length > 0) {
|
|
66
|
+
const loc = component.node.loc;
|
|
67
|
+
smells.push({
|
|
68
|
+
type: 'nextjs-client-server-boundary',
|
|
69
|
+
severity: 'error',
|
|
70
|
+
message: `Client hooks (${usedClientHooks.join(', ')}) used without 'use client' directive in "${component.name}"`,
|
|
71
|
+
file: filePath,
|
|
72
|
+
line: loc?.start.line || 1,
|
|
73
|
+
column: 0,
|
|
74
|
+
suggestion: "Add 'use client' at the top of the file, or move client logic to a separate component",
|
|
75
|
+
codeSnippet: getCodeSnippet(sourceCode, 1),
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Check for missing metadata in page/layout files
|
|
81
|
+
if (isAppRouter && filePath.includes('page.')) {
|
|
82
|
+
// This would require checking exports, which needs file-level analysis
|
|
83
|
+
const hasMetadata = sourceCode.includes('export const metadata') ||
|
|
84
|
+
sourceCode.includes('export function generateMetadata');
|
|
85
|
+
|
|
86
|
+
if (!hasMetadata && component.name === 'default') {
|
|
87
|
+
smells.push({
|
|
88
|
+
type: 'nextjs-missing-metadata',
|
|
89
|
+
severity: 'info',
|
|
90
|
+
message: 'Page component missing metadata export',
|
|
91
|
+
file: filePath,
|
|
92
|
+
line: 1,
|
|
93
|
+
column: 0,
|
|
94
|
+
suggestion: 'Add metadata for SEO: export const metadata = { title: "...", description: "..." }',
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Check for router misuse (using window.location instead of next/router)
|
|
100
|
+
component.path.traverse({
|
|
101
|
+
MemberExpression(path) {
|
|
102
|
+
if (
|
|
103
|
+
t.isIdentifier(path.node.object) &&
|
|
104
|
+
path.node.object.name === 'window' &&
|
|
105
|
+
t.isIdentifier(path.node.property) &&
|
|
106
|
+
path.node.property.name === 'location'
|
|
107
|
+
) {
|
|
108
|
+
const loc = path.node.loc;
|
|
109
|
+
smells.push({
|
|
110
|
+
type: 'nextjs-router-misuse',
|
|
111
|
+
severity: 'warning',
|
|
112
|
+
message: `Using window.location instead of Next.js router in "${component.name}"`,
|
|
113
|
+
file: filePath,
|
|
114
|
+
line: loc?.start.line || 0,
|
|
115
|
+
column: loc?.start.column || 0,
|
|
116
|
+
suggestion: 'Use next/navigation: import { useRouter } from "next/navigation"',
|
|
117
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return smells;
|
|
124
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import * as t from '@babel/types';
|
|
2
|
+
import { NodePath } from '@babel/traverse';
|
|
3
|
+
import { ParsedComponent, getCodeSnippet } from '../parser/index.js';
|
|
4
|
+
import { CodeSmell, DetectorConfig, DEFAULT_CONFIG } from '../types/index.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Detects Node.js-specific code smells:
|
|
8
|
+
* - Callback hell (deeply nested callbacks)
|
|
9
|
+
* - Unhandled promise rejections
|
|
10
|
+
* - Synchronous I/O operations
|
|
11
|
+
* - Missing error handling
|
|
12
|
+
*/
|
|
13
|
+
export function detectNodejsIssues(
|
|
14
|
+
component: ParsedComponent,
|
|
15
|
+
filePath: string,
|
|
16
|
+
sourceCode: string,
|
|
17
|
+
config: DetectorConfig = DEFAULT_CONFIG,
|
|
18
|
+
imports: string[] = []
|
|
19
|
+
): CodeSmell[] {
|
|
20
|
+
if (!config.checkNodejs) return [];
|
|
21
|
+
|
|
22
|
+
// Check if this looks like a Node.js file
|
|
23
|
+
const isNodeFile = imports.some(imp =>
|
|
24
|
+
imp.includes('fs') || imp.includes('path') || imp.includes('http') ||
|
|
25
|
+
imp.includes('express') || imp.includes('child_process') ||
|
|
26
|
+
imp.includes('crypto') || imp.includes('os') || imp.includes('stream')
|
|
27
|
+
) || filePath.includes('.server.') || filePath.includes('/api/');
|
|
28
|
+
|
|
29
|
+
if (!isNodeFile) return [];
|
|
30
|
+
|
|
31
|
+
const smells: CodeSmell[] = [];
|
|
32
|
+
|
|
33
|
+
// Detect callback hell (nested callbacks > maxCallbackDepth)
|
|
34
|
+
component.path.traverse({
|
|
35
|
+
CallExpression(path) {
|
|
36
|
+
const depth = getCallbackDepth(path);
|
|
37
|
+
|
|
38
|
+
if (depth > config.maxCallbackDepth) {
|
|
39
|
+
const loc = path.node.loc;
|
|
40
|
+
smells.push({
|
|
41
|
+
type: 'nodejs-callback-hell',
|
|
42
|
+
severity: 'warning',
|
|
43
|
+
message: `Callback hell detected (depth: ${depth}) in "${component.name}"`,
|
|
44
|
+
file: filePath,
|
|
45
|
+
line: loc?.start.line || 0,
|
|
46
|
+
column: loc?.start.column || 0,
|
|
47
|
+
suggestion: 'Refactor to async/await or use Promise.all() for parallel operations',
|
|
48
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Detect unhandled promise rejections (Promise without .catch or try/catch)
|
|
55
|
+
component.path.traverse({
|
|
56
|
+
CallExpression(path) {
|
|
57
|
+
// Check for .then() without .catch()
|
|
58
|
+
if (t.isMemberExpression(path.node.callee) &&
|
|
59
|
+
t.isIdentifier(path.node.callee.property) &&
|
|
60
|
+
path.node.callee.property.name === 'then') {
|
|
61
|
+
|
|
62
|
+
// Check if followed by .catch() in chain
|
|
63
|
+
const parent = path.parent;
|
|
64
|
+
let hasCatch = false;
|
|
65
|
+
|
|
66
|
+
if (t.isMemberExpression(parent)) {
|
|
67
|
+
const prop = (parent as t.MemberExpression).property;
|
|
68
|
+
if (t.isIdentifier(prop) && prop.name === 'catch') {
|
|
69
|
+
hasCatch = true;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Check if inside try block
|
|
74
|
+
let current: NodePath | null = path;
|
|
75
|
+
while (current) {
|
|
76
|
+
if (t.isTryStatement(current.node)) {
|
|
77
|
+
hasCatch = true;
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
current = current.parentPath;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (!hasCatch) {
|
|
84
|
+
const loc = path.node.loc;
|
|
85
|
+
smells.push({
|
|
86
|
+
type: 'nodejs-unhandled-promise',
|
|
87
|
+
severity: 'warning',
|
|
88
|
+
message: `.then() without .catch() in "${component.name}"`,
|
|
89
|
+
file: filePath,
|
|
90
|
+
line: loc?.start.line || 0,
|
|
91
|
+
column: loc?.start.column || 0,
|
|
92
|
+
suggestion: 'Add .catch() to handle rejections, or use try/catch with async/await',
|
|
93
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Detect synchronous file operations
|
|
101
|
+
const syncMethods = ['readFileSync', 'writeFileSync', 'appendFileSync', 'readdirSync',
|
|
102
|
+
'statSync', 'mkdirSync', 'rmdirSync', 'unlinkSync', 'existsSync'];
|
|
103
|
+
|
|
104
|
+
component.path.traverse({
|
|
105
|
+
CallExpression(path) {
|
|
106
|
+
const callee = path.node.callee;
|
|
107
|
+
|
|
108
|
+
if (t.isMemberExpression(callee) && t.isIdentifier(callee.property)) {
|
|
109
|
+
if (syncMethods.includes(callee.property.name)) {
|
|
110
|
+
const loc = path.node.loc;
|
|
111
|
+
smells.push({
|
|
112
|
+
type: 'nodejs-sync-io',
|
|
113
|
+
severity: 'warning',
|
|
114
|
+
message: `Synchronous file operation "${callee.property.name}" blocks event loop`,
|
|
115
|
+
file: filePath,
|
|
116
|
+
line: loc?.start.line || 0,
|
|
117
|
+
column: loc?.start.column || 0,
|
|
118
|
+
suggestion: `Use async version: ${callee.property.name.replace('Sync', '')} with await or promises`,
|
|
119
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Direct function call
|
|
125
|
+
if (t.isIdentifier(callee) && syncMethods.includes(callee.name)) {
|
|
126
|
+
const loc = path.node.loc;
|
|
127
|
+
smells.push({
|
|
128
|
+
type: 'nodejs-sync-io',
|
|
129
|
+
severity: 'warning',
|
|
130
|
+
message: `Synchronous file operation "${callee.name}" blocks event loop`,
|
|
131
|
+
file: filePath,
|
|
132
|
+
line: loc?.start.line || 0,
|
|
133
|
+
column: loc?.start.column || 0,
|
|
134
|
+
suggestion: `Use async version: ${callee.name.replace('Sync', '')} with await or promises`,
|
|
135
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// Detect missing error handling in async functions
|
|
142
|
+
component.path.traverse({
|
|
143
|
+
AwaitExpression(path) {
|
|
144
|
+
// Check if inside try block
|
|
145
|
+
let insideTry = false;
|
|
146
|
+
let current: NodePath | null = path;
|
|
147
|
+
|
|
148
|
+
while (current) {
|
|
149
|
+
if (t.isTryStatement(current.node)) {
|
|
150
|
+
insideTry = true;
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
// Stop at function boundary
|
|
154
|
+
if (t.isFunction(current.node)) break;
|
|
155
|
+
current = current.parentPath;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (!insideTry) {
|
|
159
|
+
// Check if the parent function has error handling at call site
|
|
160
|
+
// This is a simplified check - in practice you'd want more context
|
|
161
|
+
const loc = path.node.loc;
|
|
162
|
+
smells.push({
|
|
163
|
+
type: 'nodejs-missing-error-handling',
|
|
164
|
+
severity: 'info',
|
|
165
|
+
message: `await without try/catch may cause unhandled rejections`,
|
|
166
|
+
file: filePath,
|
|
167
|
+
line: loc?.start.line || 0,
|
|
168
|
+
column: loc?.start.column || 0,
|
|
169
|
+
suggestion: 'Wrap await in try/catch or handle errors at the call site',
|
|
170
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
return smells;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Calculate the depth of nested callbacks
|
|
181
|
+
*/
|
|
182
|
+
function getCallbackDepth(path: NodePath): number {
|
|
183
|
+
let depth = 0;
|
|
184
|
+
let current: NodePath | null = path;
|
|
185
|
+
|
|
186
|
+
while (current) {
|
|
187
|
+
const node = current.node;
|
|
188
|
+
|
|
189
|
+
// Count function expressions that are arguments to calls
|
|
190
|
+
if ((t.isFunctionExpression(node) || t.isArrowFunctionExpression(node)) &&
|
|
191
|
+
t.isCallExpression(current.parent)) {
|
|
192
|
+
depth++;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
current = current.parentPath;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return depth;
|
|
199
|
+
}
|