Skip to content

Commit

Permalink
fix: eslint normalization (#52)
Browse files Browse the repository at this point in the history
* Fix eslint normalization

* test: add unit test

Co-authored-by: Fredrik Blomqvist <fredrik.blomqvist.95@gmail.com>
  • Loading branch information
fi3ework and fgblomqvist authored Jul 22, 2021
1 parent 5b0ac3a commit 95aa772
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,70 @@ exports[`logger diagnosticToTerminalLog get warning 1`] = `
6 | rootDom.innerHTML = hello + text
"
`;

exports[`logger normalizeEslintDiagnostic get multiple errors 1`] = `
Array [
Object {
"checker": "ESLint",
"codeFrame": "   1 | import { text } from './text'
  2 |
> 3 | var hello = 'Hello'
  | ^^^^^^^^^^^^^^^^^^^
  4 | var hello1 = 'Hello1'
  5 |
  6 | const rootDom = document.querySelector('#root')!",
"conclusion": "",
"id": "/Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts",
"level": 1,
"loc": Object {
"end": Object {
"column": 20,
"line": 3,
},
"start": Object {
"column": 1,
"line": 3,
},
},
"message": "Unexpected var, use let or const instead.",
"stripedCodeFrame": " 1 | import { text } from './text'
2 |
> 3 | var hello = 'Hello'
| ^^^^^^^^^^^^^^^^^^^
4 | var hello1 = 'Hello1'
5 |
6 | const rootDom = document.querySelector('#root')!",
},
Object {
"checker": "ESLint",
"codeFrame": "   2 |
  3 | var hello = 'Hello'
> 4 | var hello1 = 'Hello1'
  | ^^^^^^^^^^^^^^^^^^^^^
  5 |
  6 | const rootDom = document.querySelector('#root')!
  7 | rootDom.innerHTML = hello + text",
"conclusion": "",
"id": "/Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts",
"level": 1,
"loc": Object {
"end": Object {
"column": 22,
"line": 4,
},
"start": Object {
"column": 1,
"line": 4,
},
},
"message": "Unexpected var, use let or const instead.",
"stripedCodeFrame": " 2 |
3 | var hello = 'Hello'
> 4 | var hello1 = 'Hello1'
| ^^^^^^^^^^^^^^^^^^^^^
5 |
6 | const rootDom = document.querySelector('#root')!
7 | rootDom.innerHTML = hello + text",
},
]
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export const error1 = {
import type { NormalizedDiagnostic } from './../../../src/logger'
import type { ESLint } from 'eslint'

export const error1: NormalizedDiagnostic = {
message: 'Unexpected var, use let or const instead.',
conclusion: '',
codeFrame:
Expand All @@ -9,6 +12,43 @@ export const error1 = {
checker: 'ESLint',
loc: { start: { line: 3, column: 1 }, end: { line: 3, column: 20 } },
level: 1,
} as const
}

export const warning1: NormalizedDiagnostic = { ...error1, level: 0 }

export const warning1 = { ...error1, level: 0 }
export const eslintResult1: ESLint.LintResult = {
filePath: '/Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts',
messages: [
{
ruleId: 'no-var',
severity: 2,
message: 'Unexpected var, use let or const instead.',
line: 3,
column: 1,
nodeType: 'VariableDeclaration',
messageId: 'unexpectedVar',
endLine: 3,
endColumn: 20,
fix: { range: [31, 34], text: 'let' },
},
{
ruleId: 'no-var',
severity: 2,
message: 'Unexpected var, use let or const instead.',
line: 4,
column: 1,
nodeType: 'VariableDeclaration',
messageId: 'unexpectedVar',
endLine: 4,
endColumn: 22,
fix: { range: [51, 54], text: 'let' },
},
],
errorCount: 2,
warningCount: 0,
fixableErrorCount: 2,
fixableWarningCount: 0,
source:
"import { text } from './text'\n\nvar hello = 'Hello'\nvar hello1 = 'Hello1'\n\nconst rootDom = document.querySelector('#root')!\nrootDom.innerHTML = hello + text\n\nexport {}\n",
usedDeprecatedRules: [],
}
28 changes: 22 additions & 6 deletions packages/vite-plugin-checker/__tests__/unit/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
import { diagnosticToTerminalLog } from '../../src/logger'
import { error1 as eslintError1, warning1 as eslintWarning1 } from './fixtures/eslintDiagnostic'
import strip from 'strip-ansi'

import {
diagnosticToTerminalLog,
NormalizedDiagnostic,
normalizeEslintDiagnostic,
} from '../../src/logger'
import {
error1 as eslintError1,
warning1 as eslintWarning1,
eslintResult1,
} from './fixtures/eslintDiagnostic'

describe('logger', () => {
describe('diagnosticToTerminalLog', () => {
it('get error', () => {
const receive = strip(diagnosticToTerminalLog(eslintError1, 'ESLint'))
expect(receive).toMatchSnapshot()
const received = strip(diagnosticToTerminalLog(eslintError1, 'ESLint'))
expect(received).toMatchSnapshot()
})

it('get warning', () => {
const receive = strip(diagnosticToTerminalLog(eslintWarning1, 'ESLint'))
expect(receive).toMatchSnapshot()
const received = strip(diagnosticToTerminalLog(eslintWarning1, 'ESLint'))
expect(received).toMatchSnapshot()
})
})

describe('normalizeEslintDiagnostic', () => {
it('get multiple errors', () => {
const received = normalizeEslintDiagnostic(eslintResult1)
expect(received).toMatchSnapshot()
})
})
})
15 changes: 6 additions & 9 deletions packages/vite-plugin-checker/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,10 @@ const isNormalizedDiagnostic = (
}

export function normalizeEslintDiagnostic(diagnostic: ESLint.LintResult): NormalizedDiagnostic[] {
const firstMessage = diagnostic.messages[0]
if (!firstMessage) return []

return diagnostic.messages
.map((d) => {
let level = DiagnosticLevel.Error
switch (firstMessage.severity) {
switch (d.severity) {
case 0: // off, ignore
level = DiagnosticLevel.Error
return null
Expand All @@ -283,12 +280,12 @@ export function normalizeEslintDiagnostic(diagnostic: ESLint.LintResult): Normal

const loc: SourceLocation = {
start: {
line: firstMessage.line,
column: firstMessage.column,
line: d.line,
column: d.column,
},
end: {
line: firstMessage.endLine || 0,
column: firstMessage.endColumn,
line: d.endLine || 0,
column: d.endColumn,
},
}

Expand All @@ -298,7 +295,7 @@ export function normalizeEslintDiagnostic(diagnostic: ESLint.LintResult): Normal
})

return {
message: firstMessage.message,
message: d.message,
conclusion: '',
codeFrame,
stripedCodeFrame: codeFrame && strip(codeFrame),
Expand Down

0 comments on commit 95aa772

Please sign in to comment.