Skip to content

Commit 227fb5f

Browse files
committed
feat: detect language based on file name
1 parent 3bc98d4 commit 227fb5f

File tree

9 files changed

+72
-83
lines changed

9 files changed

+72
-83
lines changed

cli/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ docker run --rm smelly-cli
2828
Single file
2929

3030
```sh
31-
npx smelly-cli /smells/my-test.ts typescript
31+
npx smelly-cli /smells/my-test.ts
3232
```
3333

3434
Report (must be a path with tests in it)
3535

3636
```sh
37-
npx smelly-cli /smells/ javascript --report=html $(pwd)
37+
npx smelly-cli /smells/ --report=html $(pwd)
3838
```
3939

4040
## Resources

cli/src/find-smells.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import { statSync } from 'fs';
77

88
const args = process.argv;
99
const fileName = args[2];
10-
const language = args[3] as SupportedLanguages || SupportedLanguages.javascript;
11-
const report = args[4];
12-
const reportOutput = args[5];
10+
const report = args[3];
11+
const reportOutput = args[4];
1312

1413
if (!fileName) {
1514
console.error('[SMELLY] please provide a test file or a regex to search for test files');
@@ -41,7 +40,7 @@ async function execute() {
4140

4241
for (const file of pathWithAllFilesFound) {
4342
const fileContent = await fs.readFile(file, { encoding: 'utf8' });
44-
const smellDetector = new SmellDetector(file, fileContent, language);
43+
const smellDetector = new SmellDetector(file, fileContent);
4544
const smells = smellDetector.findAll();
4645
aggregator.push(smells.smellsList);
4746
testCases.push(...smells.testCases);
@@ -58,7 +57,7 @@ async function execute() {
5857
const output: any[] = [];
5958
for (const file of pathWithAllFilesFound) {
6059
const fileContents = await fs.readFile(file, { encoding: 'utf8' });
61-
const smellDetector = new SmellDetector(file, fileContents, language);
60+
const smellDetector = new SmellDetector(file, fileContents);
6261
const result = smellDetector.findAll().smellsList.smells;
6362

6463
if (result.length) {

cli/test/cli.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@ describe('cli', () => {
2020
});
2121

2222
test('find no smells for a single file', async () => {
23-
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/no-smells.test.js javascript --report=html --report-output=.`);
23+
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/no-smells.test.js --report=html --report-output=.`);
2424

2525
expect(stdout).toContain("Report HTML generated");
2626
expect(stdout).not.toContain("Error:");
2727
});
2828

2929
test('find no smells for a given path', async () => {
30-
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/ javascript --report=html --report-output=$(pwd)`);
30+
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/ --report=html --report-output=$(pwd)`);
3131

3232
expect(stdout).toContain("[SMELLY] please use a regex or a file");
3333
});
3434

3535
test('find no smells for a path with regex', async () => {
36-
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/**/*test.js javascript --report=html --report-output=$(pwd)`);
36+
const { stdout } = await execPromise(`npm run cli -- fake-data/no-smells/**/*test.js --report=html --report-output=$(pwd)`);
3737

3838
expect(stdout).toContain("Report HTML generated");
3939
expect(stdout).not.toContain("Error:");
4040
});
4141

4242
describe('validations', () => {
4343
test('generate empty report if path does not exists', async () => {
44-
const { stdout } = await execPromise(`npm run cli -- /bla/foo/whatever/ javascript --report=html --report-output=$(pwd)`);
44+
const { stdout } = await execPromise(`npm run cli -- /bla/foo/whatever/ --report=html --report-output=$(pwd)`);
4545

4646
expect(stdout).toContain("Report HTML generated");
4747
});

detector/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The detector package can be used as a standalone package.
2020
```typescript
2121
import { SmellDetector } from 'smelly-detector';
2222

23-
const detector = new SmellDetector("my source code", "javascript");
23+
const detector = new SmellDetector("my-file.js", "my source code");
2424

2525
console.log(detector.findAll());
2626
```

detector/src/smells-detector.ts

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
1-
import { parseScript } from 'esprima';
21
import * as ts from 'typescript';
3-
import { JavascriptSmells } from './languages/JavascriptSmells';
42
import { TypescriptSmells } from './languages/TypescriptSmells';
53
import { SmellDetectorRunnerResult, SupportedLanguages, TestCase } from './types';
64

75
export 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

detector/test/smells-detector-builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export const TIMEOUT = 'timeout';
99
export const CONSOLE = 'console-statement';
1010
export const MOCKERY = 'excessive-jest-mock';
1111

12-
export const JAVASCRIPT = 'javascript';
13-
export const TYPESCRIPT = 'typescript';
12+
export const JAVASCRIPT_FILE = 'javascript.js';
13+
export const TYPESCRIPT_FILE = 'javascript.ts';
1414

1515
export function smellDetectorInstance(code: string, language: string): Smell[] {
16-
const smellDetector = new SmellDetector('my-file', code, language);
16+
const smellDetector = new SmellDetector(language, code);
1717
return smellDetector.findAll().smellsList.smells;
1818
}
1919

2020
export function totalTestCaseDetectorInstance(code: string, language: string): TestCase[] {
21-
const smellDetector = new SmellDetector('my-file', code, language);
21+
const smellDetector = new SmellDetector(language, code);
2222
return smellDetector.findAll().testCases;
2323
}

0 commit comments

Comments
 (0)