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.
Files changed (53) hide show
  1. package/README.md +69 -11
  2. package/dist/analyzer.d.ts.map +1 -1
  3. package/dist/analyzer.js +78 -2
  4. package/dist/cli.js +43 -9
  5. package/dist/detectors/accessibility.d.ts +12 -0
  6. package/dist/detectors/accessibility.d.ts.map +1 -0
  7. package/dist/detectors/accessibility.js +191 -0
  8. package/dist/detectors/debug.d.ts +10 -0
  9. package/dist/detectors/debug.d.ts.map +1 -0
  10. package/dist/detectors/debug.js +87 -0
  11. package/dist/detectors/index.d.ts +8 -0
  12. package/dist/detectors/index.d.ts.map +1 -1
  13. package/dist/detectors/index.js +10 -0
  14. package/dist/detectors/javascript.d.ts +11 -0
  15. package/dist/detectors/javascript.d.ts.map +1 -0
  16. package/dist/detectors/javascript.js +148 -0
  17. package/dist/detectors/nextjs.d.ts +11 -0
  18. package/dist/detectors/nextjs.d.ts.map +1 -0
  19. package/dist/detectors/nextjs.js +103 -0
  20. package/dist/detectors/nodejs.d.ts +11 -0
  21. package/dist/detectors/nodejs.d.ts.map +1 -0
  22. package/dist/detectors/nodejs.js +169 -0
  23. package/dist/detectors/reactNative.d.ts +10 -0
  24. package/dist/detectors/reactNative.d.ts.map +1 -0
  25. package/dist/detectors/reactNative.js +135 -0
  26. package/dist/detectors/security.d.ts +12 -0
  27. package/dist/detectors/security.d.ts.map +1 -0
  28. package/dist/detectors/security.js +161 -0
  29. package/dist/detectors/typescript.d.ts +11 -0
  30. package/dist/detectors/typescript.d.ts.map +1 -0
  31. package/dist/detectors/typescript.js +135 -0
  32. package/dist/htmlReporter.d.ts +6 -0
  33. package/dist/htmlReporter.d.ts.map +1 -0
  34. package/dist/htmlReporter.js +453 -0
  35. package/dist/reporter.js +37 -0
  36. package/dist/types/index.d.ts +10 -1
  37. package/dist/types/index.d.ts.map +1 -1
  38. package/dist/types/index.js +11 -0
  39. package/package.json +2 -2
  40. package/src/analyzer.ts +91 -1
  41. package/src/cli.ts +43 -9
  42. package/src/detectors/accessibility.ts +212 -0
  43. package/src/detectors/debug.ts +103 -0
  44. package/src/detectors/index.ts +10 -0
  45. package/src/detectors/javascript.ts +169 -0
  46. package/src/detectors/nextjs.ts +124 -0
  47. package/src/detectors/nodejs.ts +199 -0
  48. package/src/detectors/reactNative.ts +154 -0
  49. package/src/detectors/security.ts +179 -0
  50. package/src/detectors/typescript.ts +151 -0
  51. package/src/htmlReporter.ts +464 -0
  52. package/src/reporter.ts +37 -0
  53. package/src/types/index.ts +61 -2
@@ -0,0 +1,161 @@
1
+ import * as t from '@babel/types';
2
+ import { getCodeSnippet } from '../parser/index.js';
3
+ import { DEFAULT_CONFIG } from '../types/index.js';
4
+ /**
5
+ * Detects security vulnerabilities:
6
+ * - dangerouslySetInnerHTML usage
7
+ * - eval() and Function() constructor
8
+ * - innerHTML assignments
9
+ * - Unsafe URLs (javascript:, data:)
10
+ * - Exposed secrets/API keys
11
+ */
12
+ export function detectSecurityIssues(component, filePath, sourceCode, config = DEFAULT_CONFIG) {
13
+ if (!config.checkSecurity)
14
+ return [];
15
+ const smells = [];
16
+ // Detect dangerouslySetInnerHTML
17
+ component.path.traverse({
18
+ JSXAttribute(path) {
19
+ if (t.isJSXIdentifier(path.node.name) &&
20
+ path.node.name.name === 'dangerouslySetInnerHTML') {
21
+ const loc = path.node.loc;
22
+ smells.push({
23
+ type: 'security-xss',
24
+ severity: 'error',
25
+ message: `dangerouslySetInnerHTML is a security risk in "${component.name}"`,
26
+ file: filePath,
27
+ line: loc?.start.line || 0,
28
+ column: loc?.start.column || 0,
29
+ suggestion: 'Sanitize HTML with DOMPurify or use a safe alternative like converting to React elements',
30
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
31
+ });
32
+ }
33
+ },
34
+ });
35
+ // Detect eval() and Function() constructor
36
+ component.path.traverse({
37
+ CallExpression(path) {
38
+ const { callee } = path.node;
39
+ // eval()
40
+ if (t.isIdentifier(callee) && callee.name === 'eval') {
41
+ const loc = path.node.loc;
42
+ smells.push({
43
+ type: 'security-eval',
44
+ severity: 'error',
45
+ message: `eval() is a critical security risk in "${component.name}"`,
46
+ file: filePath,
47
+ line: loc?.start.line || 0,
48
+ column: loc?.start.column || 0,
49
+ suggestion: 'Never use eval(). Parse JSON with JSON.parse() or restructure logic to avoid dynamic code execution.',
50
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
51
+ });
52
+ }
53
+ },
54
+ // new Function()
55
+ NewExpression(path) {
56
+ const { callee } = path.node;
57
+ if (t.isIdentifier(callee) && callee.name === 'Function') {
58
+ const loc = path.node.loc;
59
+ smells.push({
60
+ type: 'security-eval',
61
+ severity: 'error',
62
+ message: `new Function() is equivalent to eval() and is a security risk`,
63
+ file: filePath,
64
+ line: loc?.start.line || 0,
65
+ column: loc?.start.column || 0,
66
+ suggestion: 'Avoid creating functions from strings. Restructure to use static function definitions.',
67
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
68
+ });
69
+ }
70
+ },
71
+ });
72
+ // Detect innerHTML assignments
73
+ component.path.traverse({
74
+ AssignmentExpression(path) {
75
+ const { left } = path.node;
76
+ if (t.isMemberExpression(left) && t.isIdentifier(left.property)) {
77
+ if (left.property.name === 'innerHTML' || left.property.name === 'outerHTML') {
78
+ const loc = path.node.loc;
79
+ smells.push({
80
+ type: 'security-xss',
81
+ severity: 'warning',
82
+ message: `Direct ${left.property.name} assignment can lead to XSS`,
83
+ file: filePath,
84
+ line: loc?.start.line || 0,
85
+ column: loc?.start.column || 0,
86
+ suggestion: 'Use textContent for plain text, or sanitize HTML with DOMPurify',
87
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
88
+ });
89
+ }
90
+ }
91
+ },
92
+ });
93
+ // Detect unsafe URLs (javascript:, data:)
94
+ component.path.traverse({
95
+ JSXAttribute(path) {
96
+ if (!t.isJSXIdentifier(path.node.name))
97
+ return;
98
+ const propName = path.node.name.name;
99
+ if (!['href', 'src', 'action'].includes(propName))
100
+ return;
101
+ const value = path.node.value;
102
+ if (t.isStringLiteral(value)) {
103
+ const url = value.value.toLowerCase().trim();
104
+ if (url.startsWith('javascript:')) {
105
+ const loc = path.node.loc;
106
+ smells.push({
107
+ type: 'security-xss',
108
+ severity: 'error',
109
+ message: `javascript: URLs are a security risk`,
110
+ file: filePath,
111
+ line: loc?.start.line || 0,
112
+ column: loc?.start.column || 0,
113
+ suggestion: 'Use onClick handlers instead of javascript: URLs',
114
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
115
+ });
116
+ }
117
+ if (url.startsWith('data:') && propName === 'href') {
118
+ const loc = path.node.loc;
119
+ smells.push({
120
+ type: 'security-xss',
121
+ severity: 'warning',
122
+ message: `data: URLs in href can be a security risk`,
123
+ file: filePath,
124
+ line: loc?.start.line || 0,
125
+ column: loc?.start.column || 0,
126
+ suggestion: 'Validate and sanitize data URLs, or use blob URLs instead',
127
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
128
+ });
129
+ }
130
+ }
131
+ },
132
+ });
133
+ // Detect potential exposed secrets
134
+ const secretPatterns = [
135
+ { pattern: /['"](?:sk[-_]live|pk[-_]live|api[-_]?key|secret[-_]?key|access[-_]?token|auth[-_]?token)['"]\s*[:=]\s*['"][a-zA-Z0-9-_]{20,}/i, name: 'API key' },
136
+ { pattern: /['"](?:ghp|gho|ghu|ghs|ghr)_[a-zA-Z0-9]{36,}['"]/i, name: 'GitHub token' },
137
+ { pattern: /['"]AKIA[A-Z0-9]{16}['"]/i, name: 'AWS access key' },
138
+ { pattern: /password\s*[:=]\s*['"][^'"]{8,}['"]/i, name: 'Hardcoded password' },
139
+ ];
140
+ const lines = sourceCode.split('\n');
141
+ lines.forEach((line, index) => {
142
+ const lineNum = index + 1;
143
+ if (lineNum < component.startLine || lineNum > component.endLine)
144
+ return;
145
+ secretPatterns.forEach(({ pattern, name }) => {
146
+ if (pattern.test(line)) {
147
+ smells.push({
148
+ type: 'security-secrets',
149
+ severity: 'error',
150
+ message: `Potential ${name} exposed in code`,
151
+ file: filePath,
152
+ line: lineNum,
153
+ column: 0,
154
+ suggestion: 'Move secrets to environment variables (.env) and never commit them to version control',
155
+ codeSnippet: getCodeSnippet(sourceCode, lineNum),
156
+ });
157
+ }
158
+ });
159
+ });
160
+ return smells;
161
+ }
@@ -0,0 +1,11 @@
1
+ import { ParsedComponent } from '../parser/index.js';
2
+ import { CodeSmell, DetectorConfig } from '../types/index.js';
3
+ /**
4
+ * Detects TypeScript-specific code smells:
5
+ * - Overuse of 'any' type
6
+ * - Missing return type on functions
7
+ * - Non-null assertion operator (!)
8
+ * - Type assertions (as) that could be avoided
9
+ */
10
+ export declare function detectTypescriptIssues(component: ParsedComponent, filePath: string, sourceCode: string, config?: DetectorConfig): CodeSmell[];
11
+ //# sourceMappingURL=typescript.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typescript.d.ts","sourceRoot":"","sources":["../../src/detectors/typescript.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAkB,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAAE,SAAS,EAAE,cAAc,EAAkB,MAAM,mBAAmB,CAAC;AAE9E;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,eAAe,EAC1B,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,MAAM,EAClB,MAAM,GAAE,cAA+B,GACtC,SAAS,EAAE,CAsIb"}
@@ -0,0 +1,135 @@
1
+ import * as t from '@babel/types';
2
+ import { getCodeSnippet } from '../parser/index.js';
3
+ import { DEFAULT_CONFIG } from '../types/index.js';
4
+ /**
5
+ * Detects TypeScript-specific code smells:
6
+ * - Overuse of 'any' type
7
+ * - Missing return type on functions
8
+ * - Non-null assertion operator (!)
9
+ * - Type assertions (as) that could be avoided
10
+ */
11
+ export function detectTypescriptIssues(component, filePath, sourceCode, config = DEFAULT_CONFIG) {
12
+ if (!config.checkTypescript)
13
+ return [];
14
+ // Only run on TypeScript files
15
+ if (!filePath.endsWith('.ts') && !filePath.endsWith('.tsx'))
16
+ return [];
17
+ const smells = [];
18
+ // Detect 'any' type usage
19
+ component.path.traverse({
20
+ TSAnyKeyword(path) {
21
+ const loc = path.node.loc;
22
+ smells.push({
23
+ type: 'ts-any-usage',
24
+ severity: 'warning',
25
+ message: `Using "any" type in "${component.name}"`,
26
+ file: filePath,
27
+ line: loc?.start.line || 0,
28
+ column: loc?.start.column || 0,
29
+ suggestion: 'Use a specific type, "unknown", or create an interface',
30
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
31
+ });
32
+ },
33
+ });
34
+ // Detect functions without return type (only in complex functions)
35
+ component.path.traverse({
36
+ FunctionDeclaration(path) {
37
+ // Skip if function has explicit return type
38
+ if (path.node.returnType)
39
+ return;
40
+ // Check if function body has return statements
41
+ let hasReturn = false;
42
+ path.traverse({
43
+ ReturnStatement(returnPath) {
44
+ if (returnPath.node.argument) {
45
+ hasReturn = true;
46
+ }
47
+ },
48
+ });
49
+ if (hasReturn) {
50
+ const loc = path.node.loc;
51
+ smells.push({
52
+ type: 'ts-missing-return-type',
53
+ severity: 'info',
54
+ message: `Function "${path.node.id?.name || 'anonymous'}" missing return type`,
55
+ file: filePath,
56
+ line: loc?.start.line || 0,
57
+ column: loc?.start.column || 0,
58
+ suggestion: 'Add explicit return type: function name(): ReturnType { ... }',
59
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
60
+ });
61
+ }
62
+ },
63
+ ArrowFunctionExpression(path) {
64
+ // Only check arrow functions assigned to variables with 5+ lines
65
+ if (!path.node.returnType) {
66
+ const body = path.node.body;
67
+ // Skip simple arrow functions (single expression)
68
+ if (!t.isBlockStatement(body))
69
+ return;
70
+ // Check complexity - only flag if function is substantial
71
+ const startLine = path.node.loc?.start.line || 0;
72
+ const endLine = path.node.loc?.end.line || 0;
73
+ if (endLine - startLine >= 5) {
74
+ // Check if parent is variable declarator (assigned to variable)
75
+ const parent = path.parent;
76
+ if (t.isVariableDeclarator(parent) && t.isIdentifier(parent.id)) {
77
+ const loc = path.node.loc;
78
+ smells.push({
79
+ type: 'ts-missing-return-type',
80
+ severity: 'info',
81
+ message: `Arrow function "${parent.id.name}" missing return type`,
82
+ file: filePath,
83
+ line: loc?.start.line || 0,
84
+ column: loc?.start.column || 0,
85
+ suggestion: 'Add return type: const name = (): ReturnType => { ... }',
86
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
87
+ });
88
+ }
89
+ }
90
+ }
91
+ },
92
+ });
93
+ // Detect non-null assertion operator (!)
94
+ component.path.traverse({
95
+ TSNonNullExpression(path) {
96
+ const loc = path.node.loc;
97
+ smells.push({
98
+ type: 'ts-non-null-assertion',
99
+ severity: 'warning',
100
+ message: `Non-null assertion (!) bypasses type safety in "${component.name}"`,
101
+ file: filePath,
102
+ line: loc?.start.line || 0,
103
+ column: loc?.start.column || 0,
104
+ suggestion: 'Use optional chaining (?.) or proper null checks instead',
105
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
106
+ });
107
+ },
108
+ });
109
+ // Detect type assertions (as keyword) - these can hide type errors
110
+ component.path.traverse({
111
+ TSAsExpression(path) {
112
+ // Skip assertions to 'const' (used for const assertions)
113
+ if (t.isTSTypeReference(path.node.typeAnnotation)) {
114
+ const typeName = path.node.typeAnnotation.typeName;
115
+ if (t.isIdentifier(typeName) && typeName.name === 'const')
116
+ return;
117
+ }
118
+ // Skip double assertions (as unknown as Type) - already flagged by TypeScript
119
+ if (t.isTSAsExpression(path.node.expression))
120
+ return;
121
+ const loc = path.node.loc;
122
+ smells.push({
123
+ type: 'ts-type-assertion',
124
+ severity: 'info',
125
+ message: `Type assertion (as) bypasses type checking in "${component.name}"`,
126
+ file: filePath,
127
+ line: loc?.start.line || 0,
128
+ column: loc?.start.column || 0,
129
+ suggestion: 'Consider using type guards or proper typing instead of assertions',
130
+ codeSnippet: getCodeSnippet(sourceCode, loc?.start.line || 0),
131
+ });
132
+ },
133
+ });
134
+ return smells;
135
+ }
@@ -0,0 +1,6 @@
1
+ import { AnalysisResult } from './types/index.js';
2
+ /**
3
+ * Generate a beautiful HTML report with charts and styling
4
+ */
5
+ export declare function generateHTMLReport(result: AnalysisResult, rootDir: string): string;
6
+ //# sourceMappingURL=htmlReporter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"htmlReporter.d.ts","sourceRoot":"","sources":["../src/htmlReporter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAiB,MAAM,kBAAkB,CAAC;AAGjE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CA8YlF"}