Skip to content

Commit d981142

Browse files
committed
chore(detector): move up ast parser
1 parent 60dc61d commit d981142

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

detector/src/languages/JavascriptSmells.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import { Syntax, parseScript } from "esprima";
2-
import { Smell, SmellsFinder } from "../types";
3-
import { SmellsBuilder } from "../smells-builder";
1+
import { Program, Syntax } from 'esprima';
2+
import { Smell, SmellsFinder } from '../types';
3+
import { SmellsBuilder } from '../smells-builder';
44

55
export class JavascriptSmells implements SmellsFinder {
66

7-
constructor(private code: string) {}
7+
constructor(private readonly ast: Program) {}
88

99
searchSmells(): Smell[] {
1010
const smells: Smell[] = [];
1111

12-
const ast = parseScript(this.code, { loc: true });
12+
const ast = this.ast;
1313

1414
const ifs: any[] = [];
1515
const forOfs: any[] = [];

detector/src/languages/TypescriptSmells.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import { SmellsBuilder } from '../smells-builder';
55

66
export class TypescriptSmells implements SmellsFinder {
77

8-
constructor(private code: string) { }
8+
constructor(private readonly ast: ts.SourceFile) { }
99

1010
searchSmells(): Smell[] {
11-
// wondering why createSource? https://stackoverflow.com/a/60462133/2258921
12-
const ast = ts.createSourceFile('temp.ts', this.code, ts.ScriptTarget.ES2020, true);
11+
const ast = this.ast;
1312
const nodes = this.findIfStatements(ast);
1413

1514
const forOfs = this.findForOfStatements(ast).map(forStmt => {

detector/src/smells-detector.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
import { parseScript } from 'esprima';
2+
import { createSourceFile, ScriptTarget } from 'typescript';
13
import { JavascriptSmells } from './languages/JavascriptSmells';
24
import { TypescriptSmells } from './languages/TypescriptSmells';
35
import { SmellDetectorRunnerResult, SupportedLanguages } from './types';
46

57
export class SmellDetector {
68

7-
constructor(private code: string, private language: string) { }
9+
constructor(private readonly code: string, private readonly language: string) { }
810

911
findAll(): SmellDetectorRunnerResult {
1012
if (this.language === SupportedLanguages.javascript) {
11-
const finder = new JavascriptSmells(this.code);
13+
const ast = parseScript(this.code, { loc: true });
14+
15+
const finder = new JavascriptSmells(ast);
1216
return { smells: finder.searchSmells(), testCases: [] };
1317
}
1418

15-
return { smells: new TypescriptSmells(this.code).searchSmells(), testCases: [] };
19+
// wondering why createSource? https://stackoverflow.com/a/60462133/2258921
20+
const ast = createSourceFile('temp.ts', this.code, ScriptTarget.ES2020, true);
21+
return { smells: new TypescriptSmells(ast).searchSmells(), testCases: [] };
1622
}
1723
}

0 commit comments

Comments
 (0)