Skip to content

Commit

Permalink
chore: biome fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ModyQyW committed Sep 11, 2024
1 parent bda924f commit 0b7be66
Showing 6 changed files with 156 additions and 122 deletions.
14 changes: 7 additions & 7 deletions packages/core/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { unlinkSync } from 'node:fs';
import { resolve } from 'node:path';
import { defineBuildConfig } from 'unbuild';
import { unlinkSync } from "node:fs";
import { resolve } from "node:path";
import { defineBuildConfig } from "unbuild";

export default defineBuildConfig({
entries: ['src/index', 'src/worker'],
entries: ["src/index", "src/worker"],
clean: true,
declaration: true,
rollup: {
emitCJS: true,
inlineDependencies: true,
esbuild: {
target: 'node14.18',
target: "node18",
},
},
hooks: {
'build:done': (ctx) => {
unlinkSync(resolve(ctx.options.outDir, 'worker.d.ts'));
"build:done": (ctx) => {
unlinkSync(resolve(ctx.options.outDir, "worker.d.ts"));
},
},
});
17 changes: 10 additions & 7 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Colors } from 'picocolors/types';
import type { TextType } from './types';
import type { Colors } from "picocolors/types";
import type { TextType } from "./types";

export const ESLINT_SEVERITY = {
ERROR: 2,
@@ -8,10 +8,13 @@ export const ESLINT_SEVERITY = {

export const CWD = process.cwd();

export const PLUGIN_NAME = 'vite:eslint2';
export const PLUGIN_NAME = "vite:eslint2";

export const COLOR_MAPPING: Record<TextType, keyof Omit<Colors, 'isColorSupported'>> = {
error: 'red',
warning: 'yellow',
plugin: 'magenta',
export const COLOR_MAPPING: Record<
TextType,
keyof Omit<Colors, "isColorSupported">
> = {
error: "red",
warning: "yellow",
plugin: "magenta",
};
84 changes: 48 additions & 36 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import { Worker } from 'node:worker_threads';
import { dirname, extname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type * as Vite from 'vite';
import type { FSWatcher } from 'chokidar';
import chokidar from 'chokidar';
import debugWrap from 'debug';
import { dirname, extname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { Worker } from "node:worker_threads";
import type { FSWatcher } from "chokidar";
import chokidar from "chokidar";
import debugWrap from "debug";
import type * as Vite from "vite";
import { CWD, PLUGIN_NAME } from "./constants";
import type {
ESLintFormatter,
ESLintInstance,
ESLintOutputFixes,
ESLintPluginUserOptions,
} from "./types";
import {
getOptions,
getFilePath,
getFilter,
shouldIgnoreModule,
getOptions,
initializeESLint,
lintFiles,
getFilePath,
} from './utils';
import type {
ESLintPluginUserOptions,
ESLintInstance,
ESLintFormatter,
ESLintOutputFixes,
} from './types';
import { PLUGIN_NAME, CWD } from './constants';
shouldIgnoreModule,
} from "./utils";

const debug = debugWrap(PLUGIN_NAME);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const ext = extname(__filename);

export default function ESLintPlugin(userOptions: ESLintPluginUserOptions = {}): Vite.Plugin {
export default function ESLintPlugin(
userOptions: ESLintPluginUserOptions = {},
): Vite.Plugin {
const options = getOptions(userOptions);

let worker: Worker;
@@ -40,31 +42,34 @@ export default function ESLintPlugin(userOptions: ESLintPluginUserOptions = {}):
return {
name: PLUGIN_NAME,
apply(config, { command }) {
debug(`==== apply hook ====`);
if (config.mode === 'test' || process.env.VITEST) return options.test;
debug("==== apply hook ====");
if (config.mode === "test" || process.env.VITEST) return options.test;
const shouldApply =
(command === 'serve' && options.dev) || (command === 'build' && options.build);
(command === "serve" && options.dev) ||
(command === "build" && options.build);
debug(`should apply this plugin: ${shouldApply}`);
return shouldApply;
},
async buildStart() {
debug(`==== buildStart hook ====`);
debug("==== buildStart hook ====");
// initialize worker
if (options.lintInWorker) {
if (worker) return;
debug(`Initialize worker`);
worker = new Worker(resolve(__dirname, `worker${ext}`), { workerData: { options } });
debug("Initialize worker");
worker = new Worker(resolve(__dirname, `worker${ext}`), {
workerData: { options },
});
return;
}
// initialize ESLint
debug(`Initial ESLint`);
debug("Initial ESLint");
const result = await initializeESLint(options);
eslintInstance = result.eslintInstance;
formatter = result.formatter;
outputFixes = result.outputFixes;
// lint on start if needed
if (options.lintOnStart) {
debug(`Lint on start`);
debug("Lint on start");
await lintFiles(
{
files: options.include,
@@ -78,20 +83,24 @@ export default function ESLintPlugin(userOptions: ESLintPluginUserOptions = {}):
}
},
async transform(_, id) {
debug('==== transform hook ====');
debug("==== transform hook ====");
// initialize watcher
if (options.chokidar) {
if (watcher) return;
debug(`Initialize watcher`);
debug("Initialize watcher");
watcher = chokidar
.watch(options.include, { ignored: options.exclude })
.on('change', async (path) => {
debug(`==== change event ====`);
.on("change", async (path) => {
debug("==== change event ====");
const fullPath = resolve(CWD, path);
// worker + watcher
if (worker) return worker.postMessage(fullPath);
// watcher only
const shouldIgnore = await shouldIgnoreModule(fullPath, filter, eslintInstance);
const shouldIgnore = await shouldIgnoreModule(
fullPath,
filter,
eslintInstance,
);
debug(`should ignore: ${shouldIgnore}`);
if (shouldIgnore) return;
return await lintFiles(
@@ -108,9 +117,9 @@ export default function ESLintPlugin(userOptions: ESLintPluginUserOptions = {}):
return;
}
// no watcher
debug('id: ', id);
debug(`id: ${id}`);
const filePath = getFilePath(id);
debug(`filePath`, filePath);
debug(`filePath: ${filePath}`);
// worker
if (worker) return worker.postMessage(filePath);
// no worker
@@ -129,10 +138,13 @@ export default function ESLintPlugin(userOptions: ESLintPluginUserOptions = {}):
);
},
async buildEnd() {
debug('==== buildEnd ====');
debug("==== buildEnd ====");
if (watcher?.close) await watcher.close();
},
};
}

export { type ESLintPluginOptions, type ESLintPluginUserOptions } from './types';
export {
type ESLintPluginOptions,
type ESLintPluginUserOptions,
} from "./types";
8 changes: 4 additions & 4 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type * as ESLint from 'eslint';
import type * as Rollup from 'rollup';
import type { CreateFilter } from '@rollup/pluginutils';
import type { CreateFilter } from "@rollup/pluginutils";
import type * as ESLint from "eslint";
import type * as Rollup from "rollup";

export type FilterPattern = string | string[];
export type Filter = ReturnType<CreateFilter>;
@@ -256,4 +256,4 @@ export type LintFiles = (
context?: Rollup.PluginContext,
) => Promise<void>;

export type TextType = 'error' | 'warning' | 'plugin';
export type TextType = "error" | "warning" | "plugin";
Loading

0 comments on commit 0b7be66

Please sign in to comment.