react-code-smell-detector 1.0.1 → 1.2.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 +69 -11
- package/dist/analyzer.d.ts.map +1 -1
- package/dist/analyzer.js +78 -2
- package/dist/cli.js +43 -9
- package/dist/detectors/accessibility.d.ts +12 -0
- package/dist/detectors/accessibility.d.ts.map +1 -0
- package/dist/detectors/accessibility.js +191 -0
- package/dist/detectors/debug.d.ts +10 -0
- package/dist/detectors/debug.d.ts.map +1 -0
- package/dist/detectors/debug.js +87 -0
- package/dist/detectors/index.d.ts +8 -0
- package/dist/detectors/index.d.ts.map +1 -1
- package/dist/detectors/index.js +10 -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/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/security.d.ts +12 -0
- package/dist/detectors/security.d.ts.map +1 -0
- package/dist/detectors/security.js +161 -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/htmlReporter.d.ts +6 -0
- package/dist/htmlReporter.d.ts.map +1 -0
- package/dist/htmlReporter.js +453 -0
- package/dist/reporter.js +37 -0
- package/dist/types/index.d.ts +10 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +11 -0
- package/package.json +2 -2
- package/src/analyzer.ts +91 -1
- package/src/cli.ts +43 -9
- package/src/detectors/accessibility.ts +212 -0
- package/src/detectors/debug.ts +103 -0
- package/src/detectors/index.ts +10 -0
- package/src/detectors/javascript.ts +169 -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/security.ts +179 -0
- package/src/detectors/typescript.ts +151 -0
- package/src/htmlReporter.ts +464 -0
- package/src/reporter.ts +37 -0
- package/src/types/index.ts +61 -2
package/src/cli.ts
CHANGED
|
@@ -6,6 +6,7 @@ import ora from 'ora';
|
|
|
6
6
|
import path from 'path';
|
|
7
7
|
import { analyzeProject, DEFAULT_CONFIG } from './analyzer.js';
|
|
8
8
|
import { reportResults } from './reporter.js';
|
|
9
|
+
import { generateHTMLReport } from './htmlReporter.js';
|
|
9
10
|
import fs from 'fs/promises';
|
|
10
11
|
|
|
11
12
|
const program = new Command();
|
|
@@ -13,11 +14,13 @@ const program = new Command();
|
|
|
13
14
|
program
|
|
14
15
|
.name('react-smell')
|
|
15
16
|
.description('Detect code smells in React projects')
|
|
16
|
-
.version('1.
|
|
17
|
+
.version('1.2.0')
|
|
17
18
|
.argument('[directory]', 'Directory to analyze', '.')
|
|
18
|
-
.option('-f, --format <format>', 'Output format: console, json, markdown', 'console')
|
|
19
|
+
.option('-f, --format <format>', 'Output format: console, json, markdown, html', 'console')
|
|
19
20
|
.option('-s, --snippets', 'Show code snippets in output', false)
|
|
20
21
|
.option('-c, --config <file>', 'Path to config file')
|
|
22
|
+
.option('--ci', 'CI mode: exit with code 1 if any issues found')
|
|
23
|
+
.option('--fail-on <severity>', 'Exit with code 1 if issues of this severity or higher (error, warning, info)', 'error')
|
|
21
24
|
.option('--max-effects <number>', 'Max useEffects per component', parseInt)
|
|
22
25
|
.option('--max-props <number>', 'Max props before warning', parseInt)
|
|
23
26
|
.option('--max-lines <number>', 'Max lines per component', parseInt)
|
|
@@ -72,11 +75,20 @@ program
|
|
|
72
75
|
|
|
73
76
|
spinner.stop();
|
|
74
77
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
let output: string;
|
|
79
|
+
if (options.format === 'html') {
|
|
80
|
+
output = generateHTMLReport(result, rootDir);
|
|
81
|
+
// Auto-set output file for HTML if not specified
|
|
82
|
+
if (!options.output) {
|
|
83
|
+
options.output = 'code-smell-report.html';
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
output = reportResults(result, {
|
|
87
|
+
format: options.format,
|
|
88
|
+
showCodeSnippets: options.snippets,
|
|
89
|
+
rootDir,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
80
92
|
|
|
81
93
|
if (options.output) {
|
|
82
94
|
const outputPath = path.resolve(process.cwd(), options.output);
|
|
@@ -86,8 +98,30 @@ program
|
|
|
86
98
|
console.log(output);
|
|
87
99
|
}
|
|
88
100
|
|
|
89
|
-
//
|
|
90
|
-
|
|
101
|
+
// CI/CD exit code handling
|
|
102
|
+
const { smellsBySeverity } = result.summary;
|
|
103
|
+
let shouldFail = false;
|
|
104
|
+
|
|
105
|
+
if (options.ci) {
|
|
106
|
+
// CI mode: fail on any issues
|
|
107
|
+
shouldFail = result.summary.totalSmells > 0;
|
|
108
|
+
} else {
|
|
109
|
+
// Check fail-on threshold
|
|
110
|
+
switch (options.failOn) {
|
|
111
|
+
case 'info':
|
|
112
|
+
shouldFail = smellsBySeverity.info > 0 || smellsBySeverity.warning > 0 || smellsBySeverity.error > 0;
|
|
113
|
+
break;
|
|
114
|
+
case 'warning':
|
|
115
|
+
shouldFail = smellsBySeverity.warning > 0 || smellsBySeverity.error > 0;
|
|
116
|
+
break;
|
|
117
|
+
case 'error':
|
|
118
|
+
default:
|
|
119
|
+
shouldFail = smellsBySeverity.error > 0;
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (shouldFail) {
|
|
91
125
|
process.exit(1);
|
|
92
126
|
}
|
|
93
127
|
} catch (error) {
|
|
@@ -0,0 +1,212 @@
|
|
|
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 accessibility (a11y) issues:
|
|
7
|
+
* - Images without alt text
|
|
8
|
+
* - Form inputs without labels
|
|
9
|
+
* - Missing ARIA attributes on interactive elements
|
|
10
|
+
* - Click handlers without keyboard support
|
|
11
|
+
* - Improper heading hierarchy
|
|
12
|
+
*/
|
|
13
|
+
export function detectAccessibilityIssues(
|
|
14
|
+
component: ParsedComponent,
|
|
15
|
+
filePath: string,
|
|
16
|
+
sourceCode: string,
|
|
17
|
+
config: DetectorConfig = DEFAULT_CONFIG
|
|
18
|
+
): CodeSmell[] {
|
|
19
|
+
if (!config.checkAccessibility) return [];
|
|
20
|
+
|
|
21
|
+
const smells: CodeSmell[] = [];
|
|
22
|
+
|
|
23
|
+
component.path.traverse({
|
|
24
|
+
JSXOpeningElement(path) {
|
|
25
|
+
if (!t.isJSXIdentifier(path.node.name)) return;
|
|
26
|
+
|
|
27
|
+
const elementName = path.node.name.name;
|
|
28
|
+
const attributes = path.node.attributes;
|
|
29
|
+
const loc = path.node.loc;
|
|
30
|
+
const line = loc?.start.line || 0;
|
|
31
|
+
|
|
32
|
+
// Helper to check if attribute exists
|
|
33
|
+
const hasAttr = (name: string): boolean => {
|
|
34
|
+
return attributes.some(attr => {
|
|
35
|
+
if (t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name)) {
|
|
36
|
+
return attr.name.name === name;
|
|
37
|
+
}
|
|
38
|
+
// Handle spread attributes - assume they might contain the attribute
|
|
39
|
+
return t.isJSXSpreadAttribute(attr);
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Helper to get attribute value
|
|
44
|
+
const getAttrValue = (name: string): string | null => {
|
|
45
|
+
for (const attr of attributes) {
|
|
46
|
+
if (t.isJSXAttribute(attr) && t.isJSXIdentifier(attr.name) && attr.name.name === name) {
|
|
47
|
+
if (t.isStringLiteral(attr.value)) {
|
|
48
|
+
return attr.value.value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Check for images without alt text
|
|
56
|
+
if (elementName === 'img') {
|
|
57
|
+
if (!hasAttr('alt')) {
|
|
58
|
+
smells.push({
|
|
59
|
+
type: 'a11y-missing-alt',
|
|
60
|
+
severity: 'error',
|
|
61
|
+
message: `<img> missing alt attribute in "${component.name}"`,
|
|
62
|
+
file: filePath,
|
|
63
|
+
line,
|
|
64
|
+
column: loc?.start.column || 0,
|
|
65
|
+
suggestion: 'Add alt="description" for content images, or alt="" for decorative images',
|
|
66
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
67
|
+
});
|
|
68
|
+
} else {
|
|
69
|
+
const altValue = getAttrValue('alt');
|
|
70
|
+
if (altValue !== null && altValue.toLowerCase().includes('image')) {
|
|
71
|
+
smells.push({
|
|
72
|
+
type: 'a11y-missing-alt',
|
|
73
|
+
severity: 'info',
|
|
74
|
+
message: `<img> alt text shouldn't contain "image" - it's redundant`,
|
|
75
|
+
file: filePath,
|
|
76
|
+
line,
|
|
77
|
+
column: loc?.start.column || 0,
|
|
78
|
+
suggestion: 'Describe what the image shows, not that it is an image',
|
|
79
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Check for form inputs without associated labels
|
|
86
|
+
if (elementName === 'input' || elementName === 'textarea' || elementName === 'select') {
|
|
87
|
+
const inputType = getAttrValue('type') || 'text';
|
|
88
|
+
|
|
89
|
+
// Skip hidden inputs
|
|
90
|
+
if (inputType === 'hidden') return;
|
|
91
|
+
|
|
92
|
+
const hasLabel = hasAttr('aria-label') ||
|
|
93
|
+
hasAttr('aria-labelledby') ||
|
|
94
|
+
hasAttr('id'); // Assume id might be linked to a label
|
|
95
|
+
|
|
96
|
+
if (!hasLabel) {
|
|
97
|
+
smells.push({
|
|
98
|
+
type: 'a11y-missing-label',
|
|
99
|
+
severity: 'warning',
|
|
100
|
+
message: `<${elementName}> without accessible label in "${component.name}"`,
|
|
101
|
+
file: filePath,
|
|
102
|
+
line,
|
|
103
|
+
column: loc?.start.column || 0,
|
|
104
|
+
suggestion: 'Add aria-label, aria-labelledby, or associate with a <label> element',
|
|
105
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Check for interactive divs/spans without proper role and keyboard support
|
|
111
|
+
if (elementName === 'div' || elementName === 'span') {
|
|
112
|
+
const hasOnClick = hasAttr('onClick');
|
|
113
|
+
const hasRole = hasAttr('role');
|
|
114
|
+
const hasTabIndex = hasAttr('tabIndex');
|
|
115
|
+
const hasKeyboardHandler = hasAttr('onKeyDown') || hasAttr('onKeyUp') || hasAttr('onKeyPress');
|
|
116
|
+
|
|
117
|
+
if (hasOnClick) {
|
|
118
|
+
if (!hasRole) {
|
|
119
|
+
smells.push({
|
|
120
|
+
type: 'a11y-interactive-role',
|
|
121
|
+
severity: 'warning',
|
|
122
|
+
message: `Clickable <${elementName}> without role attribute in "${component.name}"`,
|
|
123
|
+
file: filePath,
|
|
124
|
+
line,
|
|
125
|
+
column: loc?.start.column || 0,
|
|
126
|
+
suggestion: 'Add role="button" or use a <button> element instead',
|
|
127
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!hasTabIndex) {
|
|
132
|
+
smells.push({
|
|
133
|
+
type: 'a11y-interactive-role',
|
|
134
|
+
severity: 'warning',
|
|
135
|
+
message: `Clickable <${elementName}> not focusable in "${component.name}"`,
|
|
136
|
+
file: filePath,
|
|
137
|
+
line,
|
|
138
|
+
column: loc?.start.column || 0,
|
|
139
|
+
suggestion: 'Add tabIndex={0} to make the element focusable',
|
|
140
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!hasKeyboardHandler) {
|
|
145
|
+
smells.push({
|
|
146
|
+
type: 'a11y-keyboard',
|
|
147
|
+
severity: 'info',
|
|
148
|
+
message: `Clickable <${elementName}> without keyboard handler in "${component.name}"`,
|
|
149
|
+
file: filePath,
|
|
150
|
+
line,
|
|
151
|
+
column: loc?.start.column || 0,
|
|
152
|
+
suggestion: 'Add onKeyDown to handle Enter/Space for keyboard users',
|
|
153
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// Check for anchor tags without href (should be buttons)
|
|
160
|
+
if (elementName === 'a') {
|
|
161
|
+
if (!hasAttr('href')) {
|
|
162
|
+
smells.push({
|
|
163
|
+
type: 'a11y-semantic',
|
|
164
|
+
severity: 'warning',
|
|
165
|
+
message: `<a> without href should be a <button> in "${component.name}"`,
|
|
166
|
+
file: filePath,
|
|
167
|
+
line,
|
|
168
|
+
column: loc?.start.column || 0,
|
|
169
|
+
suggestion: 'Use <button> for actions and <a href="..."> for navigation',
|
|
170
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Check for proper button usage
|
|
176
|
+
if (elementName === 'button') {
|
|
177
|
+
if (!hasAttr('type')) {
|
|
178
|
+
smells.push({
|
|
179
|
+
type: 'a11y-semantic',
|
|
180
|
+
severity: 'info',
|
|
181
|
+
message: `<button> should have explicit type attribute`,
|
|
182
|
+
file: filePath,
|
|
183
|
+
line,
|
|
184
|
+
column: loc?.start.column || 0,
|
|
185
|
+
suggestion: 'Add type="button" or type="submit" to clarify button behavior',
|
|
186
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Check for icons that might need labels
|
|
192
|
+
if (elementName === 'svg' || elementName === 'Icon' || elementName.endsWith('Icon')) {
|
|
193
|
+
const hasAriaLabel = hasAttr('aria-label') || hasAttr('aria-hidden') || hasAttr('title');
|
|
194
|
+
|
|
195
|
+
if (!hasAriaLabel) {
|
|
196
|
+
smells.push({
|
|
197
|
+
type: 'a11y-missing-label',
|
|
198
|
+
severity: 'info',
|
|
199
|
+
message: `Icon/SVG may need aria-label or aria-hidden in "${component.name}"`,
|
|
200
|
+
file: filePath,
|
|
201
|
+
line,
|
|
202
|
+
column: loc?.start.column || 0,
|
|
203
|
+
suggestion: 'Add aria-label for meaningful icons, or aria-hidden="true" for decorative ones',
|
|
204
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return smells;
|
|
212
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
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 debug statements that should be removed:
|
|
7
|
+
* - console.log/warn/error/debug
|
|
8
|
+
* - debugger statements
|
|
9
|
+
* - TODO/FIXME/HACK comments (detected via source code)
|
|
10
|
+
*/
|
|
11
|
+
export function detectDebugStatements(
|
|
12
|
+
component: ParsedComponent,
|
|
13
|
+
filePath: string,
|
|
14
|
+
sourceCode: string,
|
|
15
|
+
config: DetectorConfig = DEFAULT_CONFIG
|
|
16
|
+
): CodeSmell[] {
|
|
17
|
+
if (!config.checkDebugStatements) return [];
|
|
18
|
+
|
|
19
|
+
const smells: CodeSmell[] = [];
|
|
20
|
+
const reportedLines = new Set<number>();
|
|
21
|
+
|
|
22
|
+
// Detect console.* calls
|
|
23
|
+
component.path.traverse({
|
|
24
|
+
CallExpression(path) {
|
|
25
|
+
const { callee } = path.node;
|
|
26
|
+
|
|
27
|
+
if (t.isMemberExpression(callee) &&
|
|
28
|
+
t.isIdentifier(callee.object) &&
|
|
29
|
+
callee.object.name === 'console') {
|
|
30
|
+
|
|
31
|
+
const method = t.isIdentifier(callee.property) ? callee.property.name : '';
|
|
32
|
+
const debugMethods = ['log', 'warn', 'error', 'debug', 'info', 'trace', 'dir', 'table'];
|
|
33
|
+
|
|
34
|
+
if (debugMethods.includes(method)) {
|
|
35
|
+
const loc = path.node.loc;
|
|
36
|
+
const line = loc?.start.line || 0;
|
|
37
|
+
|
|
38
|
+
if (!reportedLines.has(line)) {
|
|
39
|
+
reportedLines.add(line);
|
|
40
|
+
smells.push({
|
|
41
|
+
type: 'debug-statement',
|
|
42
|
+
severity: 'warning',
|
|
43
|
+
message: `console.${method}() should be removed before production`,
|
|
44
|
+
file: filePath,
|
|
45
|
+
line,
|
|
46
|
+
column: loc?.start.column || 0,
|
|
47
|
+
suggestion: 'Remove console statement or use a logging library with environment-based filtering',
|
|
48
|
+
codeSnippet: getCodeSnippet(sourceCode, line),
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
// Detect debugger statements
|
|
56
|
+
DebuggerStatement(path) {
|
|
57
|
+
const loc = path.node.loc;
|
|
58
|
+
smells.push({
|
|
59
|
+
type: 'debug-statement',
|
|
60
|
+
severity: 'error',
|
|
61
|
+
message: 'debugger statement must be removed before production',
|
|
62
|
+
file: filePath,
|
|
63
|
+
line: loc?.start.line || 0,
|
|
64
|
+
column: loc?.start.column || 0,
|
|
65
|
+
suggestion: 'Remove the debugger statement',
|
|
66
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Detect TODO/FIXME/HACK comments in source
|
|
72
|
+
const todoPatterns = [
|
|
73
|
+
{ pattern: /\/\/\s*(TODO|FIXME|HACK|XXX|BUG)[:.\s]/gi, type: 'todo-comment' as const },
|
|
74
|
+
{ pattern: /\/\*\s*(TODO|FIXME|HACK|XXX|BUG)[:.\s]/gi, type: 'todo-comment' as const },
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
const lines = sourceCode.split('\n');
|
|
78
|
+
lines.forEach((line, index) => {
|
|
79
|
+
const lineNum = index + 1;
|
|
80
|
+
|
|
81
|
+
// Only check within component bounds
|
|
82
|
+
if (lineNum < component.startLine || lineNum > component.endLine) return;
|
|
83
|
+
|
|
84
|
+
todoPatterns.forEach(({ pattern }) => {
|
|
85
|
+
const match = line.match(pattern);
|
|
86
|
+
if (match) {
|
|
87
|
+
const tag = match[0].replace(/[/\*\s:]/g, '').toUpperCase();
|
|
88
|
+
smells.push({
|
|
89
|
+
type: 'todo-comment',
|
|
90
|
+
severity: 'info',
|
|
91
|
+
message: `${tag} comment found in "${component.name}"`,
|
|
92
|
+
file: filePath,
|
|
93
|
+
line: lineNum,
|
|
94
|
+
column: 0,
|
|
95
|
+
suggestion: `Address the ${tag} or create a ticket to track it`,
|
|
96
|
+
codeSnippet: getCodeSnippet(sourceCode, lineNum),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return smells;
|
|
103
|
+
}
|
package/src/detectors/index.ts
CHANGED
|
@@ -8,3 +8,13 @@ export { detectDependencyArrayIssues } from './dependencyArray.js';
|
|
|
8
8
|
export { detectNestedTernaries } from './nestedTernary.js';
|
|
9
9
|
export { detectDeadCode, detectUnusedImports } from './deadCode.js';
|
|
10
10
|
export { detectMagicValues } from './magicValues.js';
|
|
11
|
+
// Framework-specific detectors
|
|
12
|
+
export { detectNextjsIssues } from './nextjs.js';
|
|
13
|
+
export { detectReactNativeIssues } from './reactNative.js';
|
|
14
|
+
export { detectNodejsIssues } from './nodejs.js';
|
|
15
|
+
export { detectJavascriptIssues } from './javascript.js';
|
|
16
|
+
export { detectTypescriptIssues } from './typescript.js';
|
|
17
|
+
// Debug, Security, Accessibility
|
|
18
|
+
export { detectDebugStatements } from './debug.js';
|
|
19
|
+
export { detectSecurityIssues } from './security.js';
|
|
20
|
+
export { detectAccessibilityIssues } from './accessibility.js';
|
|
@@ -0,0 +1,169 @@
|
|
|
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 vanilla JavaScript code smells:
|
|
7
|
+
* - var usage (should use let/const)
|
|
8
|
+
* - Loose equality (== instead of ===)
|
|
9
|
+
* - Implicit type coercion
|
|
10
|
+
* - Global variable pollution
|
|
11
|
+
*/
|
|
12
|
+
export function detectJavascriptIssues(
|
|
13
|
+
component: ParsedComponent,
|
|
14
|
+
filePath: string,
|
|
15
|
+
sourceCode: string,
|
|
16
|
+
config: DetectorConfig = DEFAULT_CONFIG
|
|
17
|
+
): CodeSmell[] {
|
|
18
|
+
if (!config.checkJavascript) return [];
|
|
19
|
+
|
|
20
|
+
const smells: CodeSmell[] = [];
|
|
21
|
+
|
|
22
|
+
// Detect var usage (should use let/const)
|
|
23
|
+
component.path.traverse({
|
|
24
|
+
VariableDeclaration(path) {
|
|
25
|
+
if (path.node.kind === 'var') {
|
|
26
|
+
const loc = path.node.loc;
|
|
27
|
+
smells.push({
|
|
28
|
+
type: 'js-var-usage',
|
|
29
|
+
severity: 'warning',
|
|
30
|
+
message: `Using "var" instead of "let" or "const" in "${component.name}"`,
|
|
31
|
+
file: filePath,
|
|
32
|
+
line: loc?.start.line || 0,
|
|
33
|
+
column: loc?.start.column || 0,
|
|
34
|
+
suggestion: 'Use "const" for values that never change, "let" for reassignable variables',
|
|
35
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Detect loose equality (== and != instead of === and !==)
|
|
42
|
+
component.path.traverse({
|
|
43
|
+
BinaryExpression(path) {
|
|
44
|
+
const { operator, left, right } = path.node;
|
|
45
|
+
|
|
46
|
+
// Skip if comparing with null/undefined (sometimes intentional)
|
|
47
|
+
const isNullCheck =
|
|
48
|
+
(t.isNullLiteral(left) || t.isNullLiteral(right)) ||
|
|
49
|
+
(t.isIdentifier(left) && left.name === 'undefined') ||
|
|
50
|
+
(t.isIdentifier(right) && right.name === 'undefined');
|
|
51
|
+
|
|
52
|
+
if (operator === '==' && !isNullCheck) {
|
|
53
|
+
const loc = path.node.loc;
|
|
54
|
+
smells.push({
|
|
55
|
+
type: 'js-loose-equality',
|
|
56
|
+
severity: 'warning',
|
|
57
|
+
message: `Using loose equality "==" in "${component.name}"`,
|
|
58
|
+
file: filePath,
|
|
59
|
+
line: loc?.start.line || 0,
|
|
60
|
+
column: loc?.start.column || 0,
|
|
61
|
+
suggestion: 'Use strict equality "===" to avoid type coercion bugs',
|
|
62
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (operator === '!=' && !isNullCheck) {
|
|
67
|
+
const loc = path.node.loc;
|
|
68
|
+
smells.push({
|
|
69
|
+
type: 'js-loose-equality',
|
|
70
|
+
severity: 'warning',
|
|
71
|
+
message: `Using loose inequality "!=" in "${component.name}"`,
|
|
72
|
+
file: filePath,
|
|
73
|
+
line: loc?.start.line || 0,
|
|
74
|
+
column: loc?.start.column || 0,
|
|
75
|
+
suggestion: 'Use strict inequality "!==" to avoid type coercion bugs',
|
|
76
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Detect implicit type coercion patterns
|
|
83
|
+
component.path.traverse({
|
|
84
|
+
// +string to convert to number
|
|
85
|
+
UnaryExpression(path) {
|
|
86
|
+
if (path.node.operator === '+' && t.isIdentifier(path.node.argument)) {
|
|
87
|
+
const loc = path.node.loc;
|
|
88
|
+
smells.push({
|
|
89
|
+
type: 'js-implicit-coercion',
|
|
90
|
+
severity: 'info',
|
|
91
|
+
message: `Implicit number coercion with unary + in "${component.name}"`,
|
|
92
|
+
file: filePath,
|
|
93
|
+
line: loc?.start.line || 0,
|
|
94
|
+
column: loc?.start.column || 0,
|
|
95
|
+
suggestion: 'Use explicit conversion: Number(value) or parseInt(value, 10)',
|
|
96
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
// !!value for boolean coercion - detected via UnaryExpression
|
|
102
|
+
// LogicalExpression handles &&, ||, ?? - skip for !! detection
|
|
103
|
+
|
|
104
|
+
// String concatenation with + that mixes types
|
|
105
|
+
BinaryExpression(path) {
|
|
106
|
+
if (path.node.operator === '+') {
|
|
107
|
+
const { left, right } = path.node;
|
|
108
|
+
const leftIsString = t.isStringLiteral(left) || t.isTemplateLiteral(left);
|
|
109
|
+
const rightIsString = t.isStringLiteral(right) || t.isTemplateLiteral(right);
|
|
110
|
+
const leftIsNumber = t.isNumericLiteral(left);
|
|
111
|
+
const rightIsNumber = t.isNumericLiteral(right);
|
|
112
|
+
|
|
113
|
+
// Mixed string + number concatenation
|
|
114
|
+
if ((leftIsString && rightIsNumber) || (leftIsNumber && rightIsString)) {
|
|
115
|
+
const loc = path.node.loc;
|
|
116
|
+
smells.push({
|
|
117
|
+
type: 'js-implicit-coercion',
|
|
118
|
+
severity: 'info',
|
|
119
|
+
message: `Implicit string coercion in "${component.name}"`,
|
|
120
|
+
file: filePath,
|
|
121
|
+
line: loc?.start.line || 0,
|
|
122
|
+
column: loc?.start.column || 0,
|
|
123
|
+
suggestion: 'Use template literal for clarity: `${value}` or String(value)',
|
|
124
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Detect potential global pollution (assignments without declaration)
|
|
132
|
+
component.path.traverse({
|
|
133
|
+
AssignmentExpression(path) {
|
|
134
|
+
const { left } = path.node;
|
|
135
|
+
|
|
136
|
+
if (t.isIdentifier(left)) {
|
|
137
|
+
// Check if this identifier is declared in scope
|
|
138
|
+
const binding = path.scope.getBinding(left.name);
|
|
139
|
+
|
|
140
|
+
if (!binding) {
|
|
141
|
+
// Check if it's a well-known global
|
|
142
|
+
const knownGlobals = [
|
|
143
|
+
'window', 'document', 'console', 'localStorage', 'sessionStorage',
|
|
144
|
+
'setTimeout', 'setInterval', 'clearTimeout', 'clearInterval',
|
|
145
|
+
'fetch', 'XMLHttpRequest', 'WebSocket', 'module', 'exports',
|
|
146
|
+
'require', 'process', 'global', '__dirname', '__filename',
|
|
147
|
+
'Buffer', 'Promise', 'Map', 'Set', 'Symbol',
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
if (!knownGlobals.includes(left.name)) {
|
|
151
|
+
const loc = path.node.loc;
|
|
152
|
+
smells.push({
|
|
153
|
+
type: 'js-global-pollution',
|
|
154
|
+
severity: 'error',
|
|
155
|
+
message: `Implicit global variable "${left.name}" in "${component.name}"`,
|
|
156
|
+
file: filePath,
|
|
157
|
+
line: loc?.start.line || 0,
|
|
158
|
+
column: loc?.start.column || 0,
|
|
159
|
+
suggestion: `Declare the variable: const ${left.name} = ... or let ${left.name} = ...`,
|
|
160
|
+
codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
return smells;
|
|
169
|
+
}
|