Skip to content

Commit

Permalink
chore: clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Aug 26, 2022
1 parent e946e56 commit 6163d65
Show file tree
Hide file tree
Showing 35 changed files with 969 additions and 1,699 deletions.
13 changes: 7 additions & 6 deletions packages/vite-plugin-checker/src/checkers/eslint/main.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import chokidar from 'chokidar'
import { ESLint } from 'eslint'
import { options as optionator } from './options.js'
import invariant from 'tiny-invariant'
import { parentPort } from 'worker_threads'
import path from 'path'
import { fileURLToPath } from 'url'
import { parentPort } from 'worker_threads'

import { Checker } from '../../Checker.js'
import { FileDiagnosticManager } from '../../FileDiagnosticManager.js'
import {
composeCheckerSummary,
consoleLog,
diagnosticToTerminalLog,
diagnosticToRuntimeError,
diagnosticToTerminalLog,
filterLogLevel,
normalizeEslintDiagnostic,
toViteCustomPayload,
composeCheckerSummary,
} from '../../logger.js'
import { ACTION_TYPES, DiagnosticLevel } from '../../types.js'
import { translateOptions } from './cli.js'
import { options as optionator } from './options.js'

const __filename = fileURLToPath(import.meta.url)

const manager = new FileDiagnosticManager()
let createServeAndBuild
Expand Down Expand Up @@ -138,7 +140,6 @@ export class EslintChecker extends Checker<'eslint'> {

public init() {
const _createServeAndBuild = super.initMainThread()
// module.exports.createServeAndBuild = createServeAndBuild
createServeAndBuild = _createServeAndBuild
super.initWorkerThread()
}
Expand Down
4 changes: 3 additions & 1 deletion packages/vite-plugin-checker/src/checkers/typescript/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import path from 'path'
import invariant from 'tiny-invariant'
import ts from 'typescript'
import { parentPort } from 'worker_threads'
import { fileURLToPath } from 'url'

import { Checker } from '../../Checker.js'
import {
consoleLog,
diagnosticToTerminalLog,
diagnosticToRuntimeError,
diagnosticToTerminalLog,
ensureCall,
normalizeTsDiagnostic,
toViteCustomPayload,
Expand All @@ -21,6 +22,7 @@ import {
DiagnosticToRuntime,
} from '../../types.js'

const __filename = fileURLToPath(import.meta.url)
let createServeAndBuild

const createDiagnostic: CreateDiagnostic<'typescript'> = (pluginConfig) => {
Expand Down
4 changes: 3 additions & 1 deletion packages/vite-plugin-checker/src/checkers/vls/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os from 'os'
import { fileURLToPath } from 'url'
import { parentPort } from 'worker_threads'

import { Checker } from '../../Checker.js'
Expand All @@ -13,9 +14,10 @@ import { ACTION_TYPES } from '../../types.js'
import { DiagnosticOptions, diagnostics } from './diagnostics.js'

import type { ConfigEnv } from 'vite'

import type { CreateDiagnostic } from '../../types.js'

const __filename = fileURLToPath(import.meta.url)

let createServeAndBuild

export const createDiagnostic: CreateDiagnostic<'vls'> = (pluginConfig) => {
Expand Down
13 changes: 8 additions & 5 deletions packages/vite-plugin-checker/src/checkers/vueTsc/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { createRequire } from 'module'
import os from 'os'
import path from 'path'
import invariant from 'tiny-invariant'
import ts from 'typescript'
import { fileURLToPath } from 'url'
import { parentPort } from 'worker_threads'
import { createRequire } from 'module'
const _require = createRequire(import.meta.url)

import { Checker } from '../../Checker.js'
import {
consoleLog,
Expand All @@ -18,6 +19,9 @@ import {
import { ACTION_TYPES, CreateDiagnostic, DiagnosticToRuntime } from '../../types.js'
import { prepareVueTsc } from './prepareVueTsc.js'

const _require = createRequire(import.meta.url)
const __filename = fileURLToPath(import.meta.url)

let createServeAndBuild

const createDiagnostic: CreateDiagnostic<'vueTsc'> = (pluginConfig) => {
Expand All @@ -30,10 +34,10 @@ const createDiagnostic: CreateDiagnostic<'vueTsc'> = (pluginConfig) => {
overlay = enableOverlay
terminal = enableTerminal
},
configureServer({ root }) {
async configureServer({ root }) {
invariant(pluginConfig.vueTsc, 'config.vueTsc should be `false`')

const { targetTsDir } = prepareVueTsc()
const { targetTsDir } = await prepareVueTsc()

const vueTs = _require(path.resolve(targetTsDir, 'lib/tsc.js'))

Expand Down Expand Up @@ -160,7 +164,6 @@ export class VueTscChecker extends Checker<'vueTsc'> {

public init() {
const _createServeAndBuild = super.initMainThread()
// module.exports.createServeAndBuild = createServeAndBuild
createServeAndBuild = _createServeAndBuild
super.initWorkerThread()
}
Expand Down
20 changes: 12 additions & 8 deletions packages/vite-plugin-checker/src/checkers/vueTsc/prepareVueTsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ export function prepareVueTsc() {
let shouldPrepare = true
const targetDirExist = fs.existsSync(targetTsDir)
if (targetDirExist) {
const targetTsVersion = _require(path.resolve(targetTsDir, 'package.json')).version
const currTsVersion = _require('typescript/package.json').version
// check fixture versions before re-use
if (
targetTsVersion === currTsVersion &&
fs.existsSync(vueTscFlagFile) &&
fs.readFileSync(vueTscFlagFile, 'utf8') === proxyPath
) {
try {
const targetTsVersion = _require(path.resolve(targetTsDir, 'package.json')).version
const currTsVersion = _require('typescript/package.json').version
// check fixture versions before re-use
if (
targetTsVersion === currTsVersion &&
fs.existsSync(vueTscFlagFile) &&
fs.readFileSync(vueTscFlagFile, 'utf8') === proxyPath
) {
shouldPrepare = true
}
} catch {
shouldPrepare = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-checker/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function checker(userConfig: UserPluginConfig): Plugin {
return {
name: 'vite-plugin-checker',
// @ts-ignore
__checker: Checker,
__internal__checker: Checker,
config: async (config, env) => {
// for dev mode (1/2)
// Initialize checker with config
Expand Down
1 change: 1 addition & 0 deletions packages/vite-plugin-checker/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineConfig({
bundle: false,
format: ['esm'],
sourcemap: true,
// do not clean @runtime code on watch mode
clean: false,
target: 'node14',
platform: 'node',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ import stringify from 'fast-json-stable-stringify'
import { describe, expect, it } from 'vitest'

import {
expectStderrContains,
sleepForServerReady,
diagnostics,
expectStderrContains,
isBuild,
isServe,
log,
sleepForServerReady,
stripedLog,
} from '../../testUtils'

describe('config-default', () => {
describe.runIf(isServe)('serve', () => {
it('get initial error', async () => {
await sleepForServerReady()

expect(stringify(diagnostics)).toMatchSnapshot()
expect(stripedLog).toMatchSnapshot()
})
})

describe.runIf(isBuild)('build', () => {
it('default', async () => {
it('should fail', async () => {
const expectedMsg = 'Unexpected var, use let or const instead no-var'
expectStderrContains(log, expectedMsg)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { describe, expect, it } from 'vitest'

import { buildSucceed, isBuild } from '../../testUtils'
import { describe, it, expect } from 'vitest'

describe('enableBuild-false', () => {
describe('config-enableBuild-false', () => {
describe.runIf(isBuild)('build', () => {
it('build', async () => {
it('should pass', async () => {
expect(buildSucceed).toBe(true)
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, it } from 'vitest'

import { isServe, sleepForServerReady, getHmrOverlayText } from '../../testUtils'
import { getHmrOverlayText, isServe, sleepForServerReady } from '../../testUtils'

describe('initial-is-open-false', () => {
describe('config-initialIsOpen-false', () => {
describe.runIf(isServe)('serve', () => {
it('default', async () => {
it('should not find overlay', async () => {
if (isServe) {
await sleepForServerReady()
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fs from 'fs'
import klaw from 'klaw'
import path from 'path'
import { sleepForServerReady, isBuild, testDir } from '../../testUtils'
import { describe, expect, it } from 'vitest'

describe('no-runtime-code-in-build', () => {
import { isBuild, sleepForServerReady, testDir } from '../../testUtils'

describe('config-no-runtime-code-in-build', () => {
describe.runIf(isBuild)('build', () => {
it('test', async () => {
it('should not contain plugin code in build artifacts', async () => {
await sleepForServerReady()
for await (const file of klaw(path.resolve(testDir, 'dist'))) {
if (file.stats.isFile()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Vitest Snapshot v1

exports[`config-overlay-changes > serve > get initial error from overlay and overlay error content changes on modifying 1`] = `"Unexpected var, use let or const instead. (no-var)"`;

exports[`config-overlay-changes > serve > get initial error from overlay and overlay error content changes on modifying 2`] = `"<PROJECT_ROOT>/playground-temp/config-overlay-changes/src/main.ts:3:1"`;
exports[`config-overlay-changes > serve > get initial error from overlay and overlay error content changes on modifying 3`] = `
" 1 | import { text } from './text'
2 |
> 3 | var hello = 'Hello'
| ^^^^^^^^^^^^^^^^^^^
4 |
5 | const rootDom = document.querySelector('#root')! as HTMLElement
6 | rootDom.innerHTML = hello + text"
`;
exports[`config-overlay-changes > serve > get initial error from overlay and overlay error content changes on modifying 4`] = `
" 1 | import { text } from './text'
2 |
> 3 | var hello = 'Hello1'
| ^^^^^^^^^^^^^^^^^^^^
4 |
5 | const rootDom = document.querySelector('#root')! as HTMLElement
6 | rootDom.innerHTML = hello + text"
`;
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest'

import {
editFile,
getHmrOverlay,
Expand All @@ -10,9 +11,9 @@ import {
sleepForServerReady,
} from '../../testUtils'

describe('config-default', () => {
describe('config-overlay-changes', () => {
describe.runIf(isServe)('serve', () => {
it('get initial and following errors', async () => {
it('get initial error from overlay and overlay error content changes on modifying', async () => {
await sleepForServerReady()
const [message1, file1, frame1] = await getHmrOverlayText()
expect(message1).toMatchSnapshot()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { isServe, getHmrOverlayText, sleepForServerReady } from '../../testUtils'
import { describe, expect, it } from 'vitest'

describe('overlay-false', () => {
it('serve', async () => {
describe.runIf(isServe)('serve', async () => {
import { getHmrOverlayText, isServe, sleepForServerReady } from '../../testUtils'

describe('config-overlay-false', () => {
describe.runIf(isServe)('serve', async () => {
it('should not find overlay', async () => {
await sleepForServerReady()
try {
await getHmrOverlayText()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'

describe('config-overlay-position-style', () => {
describe.runIf(isServe)('serve', () => {
it('get initial error and subsequent error', async () => {
it('find badge in right top corner', async () => {
await sleepForServerReady()
const shadowRoot = await getHmrOverlay()
const badge = await shadowRoot!.$('.badge-base')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Vitest Snapshot v1

exports[`config-terminal-false > serve > should not log into terminal 1`] = `"[{\\"checkerId\\":\\"ESLint\\",\\"frame\\":\\" 1 | import { text } from './text'/n 2 |/n > 3 | var hello = 'Hello'/n | ^^^^^^^^^^^^^^^^^^^/n 4 |/n 5 | const rootDom = document.querySelector('#root')! as HTMLElement/n 6 | rootDom.innerHTML = hello + text\\",\\"id\\":\\"<PROJECT_ROOT>/playground-temp/config-terminal-false/src/main.ts\\",\\"level\\":1,\\"loc\\":{\\"column\\":1,\\"file\\":\\"<PROJECT_ROOT>/playground-temp/config-terminal-false/src/main.ts\\",\\"line\\":3},\\"message\\":\\"Unexpected var, use let or const instead. (no-var)\\",\\"stack\\":\\"\\"},{\\"checkerId\\":\\"ESLint\\",\\"frame\\":\\" 3 | var hello = 'Hello'/n 4 |/n > 5 | const rootDom = document.querySelector('#root')! as HTMLElement/n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/n 6 | rootDom.innerHTML = hello + text/n 7 |/n 8 | export {}\\",\\"id\\":\\"<PROJECT_ROOT>/playground-temp/config-terminal-false/src/main.ts\\",\\"level\\":0,\\"loc\\":{\\"column\\":17,\\"file\\":\\"<PROJECT_ROOT>/playground-temp/config-terminal-false/src/main.ts\\",\\"line\\":5},\\"message\\":\\"Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)\\",\\"stack\\":\\"\\"}]"`;

exports[`config-terminal-false > serve > should not log into terminal 2`] = `""`;
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import stringify from 'fast-json-stable-stringify'
import { describe, expect, it } from 'vitest'

import {
expectStderrContains,
sleepForServerReady,
diagnostics,
isBuild,
isServe,
log,
stripedLog,
} from '../../testUtils'
import { diagnostics, isServe, sleepForServerReady, stripedLog } from '../../testUtils'

describe('overlay-terminal-false', () => {
describe('config-terminal-false', () => {
describe.runIf(isServe)('serve', () => {
it('get initial error and subsequent error', async () => {
it('should not log into terminal', async () => {
await sleepForServerReady()
expect(stringify(diagnostics)).toMatchSnapshot()
expect(stripedLog).toMatchSnapshot()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
// Vitest Snapshot v1

exports[`eslint-config-log-level > serve > get initial error and subsequent error 1`] = `undefined`;
exports[`eslint-config-log-level > serve > should only emit warning logs 1`] = `"[{\\"checkerId\\":\\"ESLint\\",\\"frame\\":\\" 3 | var hello = 'Hello'/n 4 |/n > 5 | const rootDom = document.querySelector('#root')!/n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/n 6 | rootDom.innerHTML = hello + text/n 7 |/n 8 | export {}\\",\\"id\\":\\"<PROJECT_ROOT>/playground-temp/eslint-config-log-level/src/main.ts\\",\\"level\\":0,\\"loc\\":{\\"column\\":17,\\"file\\":\\"<PROJECT_ROOT>/playground-temp/eslint-config-log-level/src/main.ts\\",\\"line\\":5},\\"message\\":\\"Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)\\",\\"stack\\":\\"\\"}]"`;

exports[`eslint-config-log-level > serve > get initial error and subsequent error 2`] = `
" ERROR(ESLint) Unexpected var, use let or const instead. (no-var)
FILE <PROJECT_ROOT>/playground-temp/eslint-config-log-level/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
WARNING(ESLint) Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)
exports[`eslint-config-log-level > serve > should only emit warning logs 2`] = `
" WARNING(ESLint) Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)
FILE <PROJECT_ROOT>/playground-temp/eslint-config-log-level/src/main.ts:5:17
3 | var hello = 'Hello'
Expand All @@ -23,23 +13,13 @@ exports[`eslint-config-log-level > serve > get initial error and subsequent erro
6 | rootDom.innerHTML = hello + text
7 |
8 | export {}
[ESLint] Found 1 error and 1 warning"
[ESLint] Found 0 error and 1 warning"
`;
exports[`eslint-config-log-level > serve > get initial error and subsequent error 3`] = `undefined`;
exports[`eslint-config-log-level > serve > should only emit warning logs 3`] = `"[{\\"checkerId\\":\\"ESLint\\",\\"frame\\":\\" 3 | var hello = 'Hello~'/n 4 |/n > 5 | const rootDom = document.querySelector('#root')!/n | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^/n 6 | rootDom.innerHTML = hello + text/n 7 |/n 8 | export {}\\",\\"id\\":\\"<PROJECT_ROOT>/playground-temp/eslint-config-log-level/src/main.ts\\",\\"level\\":0,\\"loc\\":{\\"column\\":17,\\"file\\":\\"<PROJECT_ROOT>/playground-temp/eslint-config-log-level/src/main.ts\\",\\"line\\":5},\\"message\\":\\"Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)\\",\\"stack\\":\\"\\"}]"`;
exports[`eslint-config-log-level > serve > get initial error and subsequent error 4`] = `
" ERROR(ESLint) Unexpected var, use let or const instead. (no-var)
FILE <PROJECT_ROOT>/playground-temp/eslint-config-log-level/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
WARNING(ESLint) Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)
exports[`eslint-config-log-level > serve > should only emit warning logs 4`] = `
" WARNING(ESLint) Forbidden non-null assertion. (@typescript-eslint/no-non-null-assertion)
FILE <PROJECT_ROOT>/playground-temp/eslint-config-log-level/src/main.ts:5:17
3 | var hello = 'Hello~'
Expand All @@ -49,5 +29,5 @@ exports[`eslint-config-log-level > serve > get initial error and subsequent erro
6 | rootDom.innerHTML = hello + text
7 |
8 | export {}
[ESLint] Found 1 error and 1 warning"
[ESLint] Found 0 error and 1 warning"
`;
Loading

0 comments on commit 6163d65

Please sign in to comment.