Skip to content

Commit 2ad7c41

Browse files
refactoring
1 parent da8648a commit 2ad7c41

1 file changed

Lines changed: 25 additions & 4 deletions

File tree

code2logic/analyzer.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from collections import defaultdict
99
from datetime import datetime
1010
from pathlib import Path
11-
from typing import Dict, List
11+
from typing import Dict, List, Optional
1212

1313
from .dependency import NETWORKX_AVAILABLE, DependencyAnalyzer
1414
from .intent import NLTK_AVAILABLE, SPACY_AVAILABLE
@@ -45,9 +45,13 @@ class ProjectAnalyzer:
4545
LANGUAGE_EXTENSIONS: Dict[str, str] = {
4646
'.py': 'python',
4747
'.js': 'javascript',
48+
'.mjs': 'javascript',
49+
'.cjs': 'javascript',
4850
'.jsx': 'javascript',
4951
'.ts': 'typescript',
5052
'.tsx': 'typescript',
53+
'.mts': 'typescript',
54+
'.cts': 'typescript',
5155
'.sql': 'sql',
5256
'.java': 'java',
5357
'.go': 'go',
@@ -82,6 +86,17 @@ class ProjectAnalyzer:
8286
'Cargo.lock', 'pnpm-lock.yaml',
8387
}
8488

89+
@staticmethod
90+
def _language_from_shebang(first_line: str) -> Optional[str]:
91+
s = (first_line or '').strip().lower()
92+
if not s.startswith('#!'):
93+
return None
94+
if 'python' in s:
95+
return 'python'
96+
if 'node' in s:
97+
return 'javascript'
98+
return None
99+
85100
def __init__(
86101
self,
87102
root_path: str,
@@ -177,12 +192,18 @@ def _scan_files(self):
177192
if fp.name in self.IGNORE_FILES:
178193
continue
179194

180-
# Check extension
181195
ext = fp.suffix.lower()
182-
if ext not in self.LANGUAGE_EXTENSIONS:
196+
language = self.LANGUAGE_EXTENSIONS.get(ext)
197+
if language is None and ext == '':
198+
try:
199+
with fp.open('r', encoding='utf-8', errors='ignore') as f:
200+
language = self._language_from_shebang(f.readline())
201+
except Exception:
202+
language = None
203+
204+
if language is None:
183205
continue
184206

185-
language = self.LANGUAGE_EXTENSIONS[ext]
186207
self.languages[language] += 1
187208

188209
# Read file

0 commit comments

Comments
 (0)