Skip to content

Commit 0f90c0d

Browse files
committed
feat: report to Sonar
1 parent 945cd71 commit 0f90c0d

File tree

20 files changed

+320
-68
lines changed

20 files changed

+320
-68
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.vscode
22
node_modules
33
bin
4-
coverage
4+
coverage
5+
.rete-cli
6+
.sonar

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ node_modules
44
.github
55
CODE_OF_CONDUCT.md
66
CONTRIBUTING.md
7+
.rete-cli
8+
.sonar

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default tseslint.config(
66
{
77
rules: {
88
'no-console': 'off',
9+
'no-undefined': 'off',
910
'@typescript-eslint/no-require-imports': 'off',
1011
}
1112
}

src/build/dev.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function buildDev(name: string, config: RollupOptions | RollupOptio
2828
// eslint-disable-next-line max-statements
2929
watcher.on('event', e => {
3030
if (e.code === 'START') {
31-
void safeExec(() => lint(false, true), messages.lintingFail)
31+
void safeExec(() => lint({ fix: false, quiet: true, output: ['stdout'] }), messages.lintingFail)
3232
void safeExec(() => generateTypes(outputDirectories), messages.typesFail)
3333
} else if (e.code === 'BUNDLE_START') {
3434
const index = getIndex(config, e.output)

src/build/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async function build(config: ReteConfig, pkg: Pkg, outputDirectories: string[])
3434

3535
await safeExec(() => generateTypes(outputDirectories), messages.typesFail, 1)
3636
console.log(messages.typesSuccess)
37-
await safeExec(lint, messages.lintingFail, 1)
37+
await safeExec(() => lint({ output: ['stdout'] }), messages.lintingFail, 1)
3838
console.log(messages.lintingSuccess)
3939

4040
const targets = getRollupConfig(config, outputs, pkg, outputDirectories)

src/consts.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
export const SOURCE_FOLDER = 'src'
33
export const TEST_FOLDER = 'test'
4+
export const RESULTS_FOLDER = '.rete-cli'

src/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import { createCommand } from 'commander'
3+
import { createCommand, Option } from 'commander'
44

55
import build from './build'
66
import { setVerbose } from './build/utils'
@@ -28,8 +28,16 @@ program
2828
.command('lint')
2929
.description('Lint using ESLint and TS parser')
3030
.option('--fix')
31-
.action(async (options: { fix?: boolean }) => {
32-
await lint(options.fix)
31+
.addOption(new Option('--output <output...>', 'Output target')
32+
.choices(['sonar', 'stdout'])
33+
.default('stdout'))
34+
.option('--quiet')
35+
.action(async (options: { fix?: boolean, quiet?: boolean, output: ('sonar' | 'stdout')[] }) => {
36+
await lint({
37+
fix: options.fix,
38+
quiet: options.quiet,
39+
output: options.output
40+
})
3341
})
3442

3543
program

src/lint/base.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { LintResult } from './results'
1+
import { LintResult, RuleMeta } from './results'
2+
3+
export interface LinterResponse {
4+
rules: RuleMeta[]
5+
results: LintResult[]
6+
}
27

38
export interface BaseLinter {
4-
run(): LintResult[] | Promise<LintResult[]>
9+
run(): LinterResponse | Promise<LinterResponse>
510
}

src/lint/eslint/index.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { loadESLint } from 'eslint'
22

33
import { BaseLinter } from '../base'
4-
import { LintMessage, LintResult } from '../results'
4+
import { LintMessage, RuleMeta } from '../results'
55

66
export class ESLint implements BaseLinter {
77
constructor(private options: { targets: string[], fix?: boolean }) {}
88

9-
async run(): Promise<LintResult[]> {
9+
async run() {
1010
const originESLint = await loadESLint({
1111
useFlatConfig: true
1212
})
@@ -16,29 +16,43 @@ export class ESLint implements BaseLinter {
1616
errorOnUnmatchedPattern: false
1717
})
1818

19-
const results = await instance.lintFiles(this.options.targets)
19+
const eslintResults = await instance.lintFiles(this.options.targets)
2020

2121
if (this.options.fix) {
22-
await originESLint.outputFixes(results)
22+
await originESLint.outputFixes(eslintResults)
2323
}
2424

25-
return results.map(({ filePath, messages }) => {
25+
const results = eslintResults.map(({ filePath, messages }) => {
2626
return {
2727
filePath,
2828
messages: messages.map(message => {
29-
const result: LintMessage = {
29+
return {
3030
column: message.column,
3131
line: message.line,
32-
endColumn: message.endColumn ?? message.column,
33-
endLine: message.endLine ?? message.line,
32+
endColumn: message.endColumn,
33+
endLine: message.endLine,
3434
ruleId: message.ruleId ?? null,
3535
message: message.message,
3636
severity: message.severity
37-
}
38-
39-
return result
37+
} satisfies LintMessage
4038
})
4139
}
4240
})
41+
const allMessages = eslintResults.flatMap(({ messages }) => messages)
42+
const rules = Object.entries(instance.getRulesMetaForResults(eslintResults))
43+
.map(([id, { docs }]) => {
44+
const firstMessage = allMessages.find(({ ruleId }) => ruleId === id)
45+
46+
return {
47+
id,
48+
description: docs?.description,
49+
severity: firstMessage?.severity ?? 1
50+
} satisfies RuleMeta
51+
})
52+
53+
return {
54+
results,
55+
rules
56+
}
4357
}
4458
}

src/lint/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { lint } from './linter'
1+
import { lint, LintOptions } from './linter'
22

3-
export default async function (fix?: boolean, quiet?: boolean) {
3+
export default async function (options: LintOptions) {
44
try {
5-
await lint(fix, quiet)
5+
await lint(options)
66
} catch (e) {
77
console.error(e)
88
process.exit(1)

0 commit comments

Comments
 (0)