Skip to content

Commit

Permalink
fix: warning level logging (#51)
Browse files Browse the repository at this point in the history
* Fix warning level logging

* test: add unit test to logger

Co-authored-by: fi3ework <fi3ework@gmail.com>
  • Loading branch information
fgblomqvist and fi3ework authored Jul 22, 2021
1 parent 1403f6f commit 5b0ac3a
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`logger diagnosticToTerminalLog get error 1`] = `
" ERROR(ESLint) Unexpected var, use let or const instead.
FILE /Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts:3:1
1 | import { text } from './text'
2 |
> 3 | var hello = 'Hello'
| ^^^^^^^^^^^^^^^^^^^
4 |
5 | const rootDom = document.querySelector('#root')!
6 | rootDom.innerHTML = hello + text
"
`;

exports[`logger diagnosticToTerminalLog get warning 1`] = `
" WARNING(ESLint) Unexpected var, use let or const instead.
FILE /Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts:3:1
1 | import { text } from './text'
2 |
> 3 | var hello = 'Hello'
| ^^^^^^^^^^^^^^^^^^^
4 |
5 | const rootDom = document.querySelector('#root')!
6 | rootDom.innerHTML = hello + text
"
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const error1 = {
message: 'Unexpected var, use let or const instead.',
conclusion: '',
codeFrame:
" \u001b[0m \u001b[90m 1 |\u001b[39m \u001b[36mimport\u001b[39m { text } \u001b[36mfrom\u001b[39m \u001b[32m'./text'\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 2 |\u001b[39m\u001b[0m\n \u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 3 |\u001b[39m \u001b[36mvar\u001b[39m hello \u001b[33m=\u001b[39m \u001b[32m'Hello'\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m |\u001b[39m \u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 4 |\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 5 |\u001b[39m \u001b[36mconst\u001b[39m rootDom \u001b[33m=\u001b[39m document\u001b[33m.\u001b[39mquerySelector(\u001b[32m'#root'\u001b[39m)\u001b[33m!\u001b[39m\u001b[0m\n \u001b[0m \u001b[90m 6 |\u001b[39m rootDom\u001b[33m.\u001b[39minnerHTML \u001b[33m=\u001b[39m hello \u001b[33m+\u001b[39m text\u001b[0m",
stripedCodeFrame:
" 1 | import { text } from './text'\n 2 |\n > 3 | var hello = 'Hello'\n | ^^^^^^^^^^^^^^^^^^^\n 4 |\n 5 | const rootDom = document.querySelector('#root')!\n 6 | rootDom.innerHTML = hello + text",
id: '/Users/vite-plugin-checker/playground/vanilla-ts/src/main.ts',
checker: 'ESLint',
loc: { start: { line: 3, column: 1 }, end: { line: 3, column: 20 } },
level: 1,
} as const

export const warning1 = { ...error1, level: 0 }
17 changes: 17 additions & 0 deletions packages/vite-plugin-checker/__tests__/unit/logger.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { diagnosticToTerminalLog } from '../../src/logger'
import { error1 as eslintError1, warning1 as eslintWarning1 } from './fixtures/eslintDiagnostic'
import strip from 'strip-ansi'

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

it('get warning', () => {
const receive = strip(diagnosticToTerminalLog(eslintWarning1, 'ESLint'))
expect(receive).toMatchSnapshot()
})
})
})
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function diagnosticToTerminalLog(
[DiagnosticLevel.Message]: boldBlack.bgCyanBright(` MESSAGE${nameInLabel} `),
}

const levelLabel = labelMap[d.level || DiagnosticLevel.Error]
const levelLabel = labelMap[d.level ?? DiagnosticLevel.Error]
const fileLabel = boldBlack.bgCyanBright(' FILE ') + ' '
const position = d.loc
? chalk.yellow(d.loc.start.line) + ':' + chalk.yellow(d.loc.start.column)
Expand Down

0 comments on commit 5b0ac3a

Please sign in to comment.