1- import  {  parseScript  }  from  'esprima' ; 
21import  *  as  ts  from  'typescript' ; 
3- import  {  JavascriptSmells  }  from  './languages/JavascriptSmells' ; 
42import  {  TypescriptSmells  }  from  './languages/TypescriptSmells' ; 
53import  {  SmellDetectorRunnerResult ,  SupportedLanguages ,  TestCase  }  from  './types' ; 
64
75export  class  SmellDetector  { 
86
97  constructor ( 
108    private  readonly  fileName : string , 
11-     private  readonly  code : string , 
12-     private  readonly  language : string 
9+     private  readonly  code : string 
1310  )  {  } 
1411
1512  findAll ( ) : SmellDetectorRunnerResult  { 
16-     if  ( this . language  ===  SupportedLanguages . javascript )  { 
17-       const  ast  =  parseScript ( this . code ,  {  loc : true  } ) ; 
18- 
19-       const  finder  =  new  JavascriptSmells ( ast ) ; 
20-       const  smellsList  =  { 
21-         fileName : this . fileName , 
22-         fileContent : this . code , 
23-         smells : finder . searchSmells ( ) , 
24-         language : this . language 
25-       } ; 
26-       return  {  smellsList,  testCases : [ ]  } ; 
27-     } 
28- 
2913    // wondering why createSource? https://stackoverflow.com/a/60462133/2258921 
3014    const  ast  =  ts . createSourceFile ( 'temp.ts' ,  this . code ,  ts . ScriptTarget . ES2020 ,  true ) ; 
3115
@@ -36,18 +20,24 @@ export class SmellDetector {
3620      endsAt : endsAt , 
3721    } ) ) ; 
3822
23+     const  language  =  this . isJavascriptFile ( )  ? SupportedLanguages . javascript  : SupportedLanguages . typescript ; 
24+ 
3925    const  foundItEachCalls  =  this . findItEachCalls ( ast ) ; 
4026    testCases . push ( ...foundItEachCalls ) ; 
4127    testCases . push ( ...this . findItSkipCalls ( ast ) ) ; 
4228
4329    const  smellsList  =  { 
4430      fileName : this . fileName , 
4531      fileContent : this . code , 
46-       smells : new  TypescriptSmells ( ast ) . searchSmells ( ) ,  language :  SupportedLanguages . typescript 
32+       smells : new  TypescriptSmells ( ast ) . searchSmells ( ) ,  language  
4733    } ; 
4834    return  {  smellsList,  testCases } ; 
4935  } 
5036
37+   private  isJavascriptFile ( )  { 
38+     return  this . fileName . endsWith ( '.js' )  ||  this . fileName . endsWith ( '.jsx' ) ; 
39+   } 
40+ 
5141  private  findItCalls ( sourceFile : ts . SourceFile ) : {  lineStart : number ,  startAt : number ,  lineEnd : number ,  endsAt : number  } [ ]  { 
5242    const  itCalls : {  lineStart : number ,  startAt : number ,  lineEnd : number ,  endsAt : number  } [ ]  =  [ ] ; 
5343
0 commit comments