feat: init

This commit is contained in:
2026-02-13 22:02:30 +01:00
commit 8f9ff830fb
16711 changed files with 3307340 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
declare function translateOptions({ cache, cacheFile, cacheLocation, cacheStrategy, config, env, errorOnUnmatchedPattern, eslintrc, ext, fix, fixDryRun, fixType, global, ignore, ignorePath, ignorePattern, inlineConfig, parser, parserOptions, plugin, quiet, reportUnusedDisableDirectives, resolvePluginsRelativeTo, rule, rulesdir, }: any): {
allowInlineConfig: any;
cache: any;
cacheLocation: any;
cacheStrategy: any;
errorOnUnmatchedPattern: any;
extensions: any;
fix: any;
fixTypes: any;
ignore: any;
ignorePath: any;
overrideConfig: {
env: any;
globals: any;
ignorePatterns: any;
parser: any;
parserOptions: any;
plugins: any;
rules: any;
};
overrideConfigFile: any;
reportUnusedDisableDirectives: string | undefined;
resolvePluginsRelativeTo: any;
rulePaths: any;
useEslintrc: any;
};
export { translateOptions };

View File

@@ -0,0 +1,75 @@
function quietFixPredicate(message) {
return message.severity === 2;
}
function translateOptions({
cache,
cacheFile,
cacheLocation,
cacheStrategy,
config,
env,
errorOnUnmatchedPattern,
eslintrc,
ext,
fix,
fixDryRun,
fixType,
global,
ignore,
ignorePath,
ignorePattern,
inlineConfig,
parser,
parserOptions,
plugin,
quiet,
reportUnusedDisableDirectives,
resolvePluginsRelativeTo,
rule,
rulesdir
}) {
return {
allowInlineConfig: inlineConfig,
cache,
cacheLocation: cacheLocation || cacheFile,
cacheStrategy,
errorOnUnmatchedPattern,
extensions: ext,
fix: (fix || fixDryRun) && (quiet ? quietFixPredicate : true),
fixTypes: fixType,
ignore,
ignorePath,
overrideConfig: {
env: (
// @ts-expect-error
env == null ? void 0 : env.reduce((obj, name) => {
obj[name] = true;
return obj;
}, {})
),
// @ts-expect-error
globals: global == null ? void 0 : global.reduce((obj, name) => {
if (name.endsWith(":true")) {
obj[name.slice(0, -5)] = "writable";
} else {
obj[name] = "readonly";
}
return obj;
}, {}),
ignorePatterns: ignorePattern,
parser,
parserOptions,
plugins: plugin,
rules: rule
},
overrideConfigFile: config,
reportUnusedDisableDirectives: reportUnusedDisableDirectives ? "error" : void 0,
resolvePluginsRelativeTo,
rulePaths: rulesdir,
useEslintrc: eslintrc
};
}
export {
translateOptions
};
//# sourceMappingURL=cli.js.map

View File

@@ -0,0 +1 @@
{"version":3,"sources":["../../../src/checkers/eslint/cli.ts"],"sourcesContent":["/* eslint-disable */\n\n/**\n * This file is directly copied from https://github.com/eslint/eslint/blob/6f940c3ce715327f282c197d0f71b91848e5d83d/lib/cli.js\n *\n * Usually, developer rarely use JS programming API to run ESLint. So we let\n * developers to write their own ESLint commands just like in CI or lint-staged.\n * And the config will be translated and pass to `new ESLint(translatedOptions)`.\n * So in build mode, it's the same as the command you pass in.\n * In dev mode, some flag will be ignored (such as `max-warnings`) because it\n * will be only respected in ESLint CLI.\n */\n\n// @ts-expect-error\nfunction quietFixPredicate(message) {\n return message.severity === 2\n}\n\nexport function translateOptions({\n cache,\n cacheFile,\n cacheLocation,\n cacheStrategy,\n config,\n env,\n errorOnUnmatchedPattern,\n eslintrc,\n ext,\n fix,\n fixDryRun,\n fixType,\n global,\n ignore,\n ignorePath,\n ignorePattern,\n inlineConfig,\n parser,\n parserOptions,\n plugin,\n quiet,\n reportUnusedDisableDirectives,\n resolvePluginsRelativeTo,\n rule,\n rulesdir,\n}: any) {\n return {\n allowInlineConfig: inlineConfig,\n cache,\n cacheLocation: cacheLocation || cacheFile,\n cacheStrategy,\n errorOnUnmatchedPattern,\n extensions: ext,\n fix: (fix || fixDryRun) && (quiet ? quietFixPredicate : true),\n fixTypes: fixType,\n ignore,\n ignorePath,\n overrideConfig: {\n env:\n // @ts-expect-error\n env?.reduce((obj, name) => {\n obj[name] = true\n return obj\n }, {}),\n // @ts-expect-error\n globals: global?.reduce((obj, name) => {\n if (name.endsWith(':true')) {\n obj[name.slice(0, -5)] = 'writable'\n } else {\n obj[name] = 'readonly'\n }\n return obj\n }, {}),\n ignorePatterns: ignorePattern,\n parser,\n parserOptions,\n plugins: plugin,\n rules: rule,\n },\n overrideConfigFile: config,\n reportUnusedDisableDirectives: reportUnusedDisableDirectives\n ? 'error'\n : void 0,\n resolvePluginsRelativeTo,\n rulePaths: rulesdir,\n useEslintrc: eslintrc,\n }\n}\n"],"mappings":"AAcA,SAAS,kBAAkB,SAAS;AAClC,SAAO,QAAQ,aAAa;AAC9B;AAEO,SAAS,iBAAiB;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAQ;AACN,SAAO;AAAA,IACL,mBAAmB;AAAA,IACnB;AAAA,IACA,eAAe,iBAAiB;AAAA,IAChC;AAAA,IACA;AAAA,IACA,YAAY;AAAA,IACZ,MAAM,OAAO,eAAe,QAAQ,oBAAoB;AAAA,IACxD,UAAU;AAAA,IACV;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,MACd;AAAA;AAAA,QAEE,2BAAK,OAAO,CAAC,KAAK,SAAS;AACzB,cAAI,IAAI,IAAI;AACZ,iBAAO;AAAA,QACT,GAAG,CAAC;AAAA;AAAA;AAAA,MAEN,SAAS,iCAAQ,OAAO,CAAC,KAAK,SAAS;AACrC,YAAI,KAAK,SAAS,OAAO,GAAG;AAC1B,cAAI,KAAK,MAAM,GAAG,EAAE,CAAC,IAAI;AAAA,QAC3B,OAAO;AACL,cAAI,IAAI,IAAI;AAAA,QACd;AACA,eAAO;AAAA,MACT,GAAG,CAAC;AAAA,MACJ,gBAAgB;AAAA,MAChB;AAAA,MACA;AAAA,MACA,SAAS;AAAA,MACT,OAAO;AAAA,IACT;AAAA,IACA,oBAAoB;AAAA,IACpB,+BAA+B,gCAC3B,UACA;AAAA,IACJ;AAAA,IACA,WAAW;AAAA,IACX,aAAa;AAAA,EACf;AACF;","names":[]}

View File

@@ -0,0 +1,18 @@
import { Checker } from '../../Checker.js';
import 'vite';
import '../../types.js';
import 'node:worker_threads';
import 'eslint';
import 'stylelint';
import '../vls/initParams.js';
import 'vscode-languageserver/node';
import 'vscode-uri';
import '../../worker.js';
declare let createServeAndBuild: any;
declare class EslintChecker extends Checker<'eslint'> {
constructor();
init(): void;
}
export { EslintChecker, createServeAndBuild };

View File

@@ -0,0 +1,186 @@
import Module from "node:module";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { parentPort } from "node:worker_threads";
import chokidar from "chokidar";
import { ESLint } from "eslint";
import invariant from "tiny-invariant";
import { Checker } from "../../Checker.js";
import { FileDiagnosticManager } from "../../FileDiagnosticManager.js";
import { createIgnore } from "../../glob.js";
import {
composeCheckerSummary,
consoleLog,
diagnosticToConsoleLevel,
diagnosticToRuntimeError,
diagnosticToTerminalLog,
filterLogLevel,
normalizeEslintDiagnostic,
toClientPayload
} 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 require2 = Module.createRequire(import.meta.url);
const manager = new FileDiagnosticManager();
let createServeAndBuild;
const createDiagnostic = (pluginConfig) => {
let overlay = true;
let terminal = true;
return {
config: async ({ enableOverlay, enableTerminal }) => {
overlay = enableOverlay;
terminal = enableTerminal;
},
async configureServer({ root }) {
var _a;
if (!pluginConfig.eslint) return;
const options = optionator.parse(pluginConfig.eslint.lintCommand);
invariant(
!options.fix,
"Using `--fix` in `config.eslint.lintCommand` is not allowed in vite-plugin-checker, you could using `--fix` with editor."
);
const translatedOptions = translateOptions(options);
const logLevel = (() => {
var _a2;
if (typeof pluginConfig.eslint !== "object") return void 0;
const userLogLevel = (_a2 = pluginConfig.eslint.dev) == null ? void 0 : _a2.logLevel;
if (!userLogLevel) return void 0;
const map = {
error: DiagnosticLevel.Error,
warning: DiagnosticLevel.Warning
};
return userLogLevel.map((l) => map[l]);
})();
const eslintOptions = {
cwd: root,
...translatedOptions,
...(_a = pluginConfig.eslint.dev) == null ? void 0 : _a.overrideConfig
};
let eslint;
if (pluginConfig.eslint.useFlatConfig) {
const {
FlatESLint,
shouldUseFlatConfig
} = require2("eslint/use-at-your-own-risk");
if (shouldUseFlatConfig == null ? void 0 : shouldUseFlatConfig()) {
eslint = new FlatESLint({
cwd: root
});
} else {
throw Error(
"Please upgrade your eslint to latest version to use `useFlatConfig` option."
);
}
} else {
eslint = new ESLint(eslintOptions);
}
const dispatchDiagnostics = () => {
var _a2;
const diagnostics2 = filterLogLevel(manager.getDiagnostics(), logLevel);
if (terminal) {
for (const d of diagnostics2) {
consoleLog(
diagnosticToTerminalLog(d, "ESLint"),
diagnosticToConsoleLevel(d)
);
}
const errorCount = diagnostics2.filter(
(d) => d.level === DiagnosticLevel.Error
).length;
const warningCount = diagnostics2.filter(
(d) => d.level === DiagnosticLevel.Warning
).length;
consoleLog(
composeCheckerSummary("ESLint", errorCount, warningCount),
errorCount ? "error" : warningCount ? "warn" : "info"
);
}
if (overlay) {
(_a2 = parentPort) == null ? void 0 : _a2.postMessage({
type: ACTION_TYPES.overlayError,
payload: toClientPayload(
"eslint",
diagnostics2.map((d) => diagnosticToRuntimeError(d))
)
});
}
};
const handleFileChange = async (filePath, type) => {
const extension = path.extname(filePath);
const { extensions } = eslintOptions;
const hasExtensionsConfig = Array.isArray(extensions);
if (hasExtensionsConfig && !extensions.includes(extension)) return;
const isChangedFileIgnored = await eslint.isPathIgnored(filePath);
if (isChangedFileIgnored) return;
const absPath = path.resolve(root, filePath);
if (type === "unlink") {
manager.updateByFileId(absPath, []);
} else if (type === "change") {
const diagnosticsOfChangedFile = await eslint.lintFiles(filePath);
const newDiagnostics = diagnosticsOfChangedFile.flatMap(
(d) => normalizeEslintDiagnostic(d)
);
manager.updateByFileId(absPath, newDiagnostics);
}
dispatchDiagnostics();
};
const files = options._.slice(1);
const diagnostics = await eslint.lintFiles(files);
manager.initWith(diagnostics.flatMap((p) => normalizeEslintDiagnostic(p)));
dispatchDiagnostics();
let watchTarget = root;
if (pluginConfig.eslint.watchPath) {
if (Array.isArray(pluginConfig.eslint.watchPath)) {
watchTarget = pluginConfig.eslint.watchPath.map(
(p) => path.resolve(root, p)
);
} else {
watchTarget = path.resolve(root, pluginConfig.eslint.watchPath);
}
}
const watcher = chokidar.watch(watchTarget, {
cwd: root,
ignored: createIgnore(root, files)
});
watcher.on("change", async (filePath) => {
handleFileChange(filePath, "change");
});
watcher.on("unlink", async (filePath) => {
handleFileChange(filePath, "unlink");
});
}
};
};
class EslintChecker extends Checker {
constructor() {
super({
name: "eslint",
absFilePath: __filename,
build: {
buildBin: (pluginConfig) => {
if (pluginConfig.eslint) {
const { lintCommand } = pluginConfig.eslint;
return ["eslint", lintCommand.split(" ").slice(1)];
}
return ["eslint", [""]];
}
},
createDiagnostic
});
}
init() {
const _createServeAndBuild = super.initMainThread();
createServeAndBuild = _createServeAndBuild;
super.initWorkerThread();
}
}
const eslintChecker = new EslintChecker();
eslintChecker.prepare();
eslintChecker.init();
export {
EslintChecker,
createServeAndBuild
};
//# sourceMappingURL=main.js.map

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,45 @@
/**
* The options object parsed by Optionator.
* @typedef {Object} ParsedCLIOptions
* @property {boolean} cache Only check changed files
* @property {string} cacheFile Path to the cache file. Deprecated: use --cache-location
* @property {string} [cacheLocation] Path to the cache file or directory
* @property {"metadata" | "content"} cacheStrategy Strategy to use for detecting changed files in the cache
* @property {boolean} [color] Force enabling/disabling of color
* @property {string} [config] Use this configuration, overriding .eslintrc.* config options if present
* @property {boolean} debug Output debugging information
* @property {string[]} [env] Specify environments
* @property {boolean} envInfo Output execution environment information
* @property {boolean} errorOnUnmatchedPattern Prevent errors when pattern is unmatched
* @property {boolean} eslintrc Disable use of configuration from .eslintrc.*
* @property {string[]} [ext] Specify JavaScript file extensions
* @property {boolean} fix Automatically fix problems
* @property {boolean} fixDryRun Automatically fix problems without saving the changes to the file system
* @property {("directive" | "problem" | "suggestion" | "layout")[]} [fixType] Specify the types of fixes to apply (directive, problem, suggestion, layout)
* @property {string} format Use a specific output format
* @property {string[]} [global] Define global variables
* @property {boolean} [help] Show help
* @property {boolean} ignore Disable use of ignore files and patterns
* @property {string} [ignorePath] Specify path of ignore file
* @property {string[]} [ignorePattern] Pattern of files to ignore (in addition to those in .eslintignore)
* @property {boolean} init Run config initialization wizard
* @property {boolean} inlineConfig Prevent comments from changing config or rules
* @property {number} maxWarnings Number of warnings to trigger nonzero exit code
* @property {string} [outputFile] Specify file to write report to
* @property {string} [parser] Specify the parser to be used
* @property {Object} [parserOptions] Specify parser options
* @property {string[]} [plugin] Specify plugins
* @property {string} [printConfig] Print the configuration for the given file
* @property {boolean | undefined} reportUnusedDisableDirectives Adds reported errors for unused eslint-disable directives
* @property {string} [resolvePluginsRelativeTo] A folder where plugins should be resolved from, CWD by default
* @property {Object} [rule] Specify rules
* @property {string[]} [rulesdir] Load additional rules from this directory. Deprecated: Use rules from plugins
* @property {boolean} stdin Lint code provided on <STDIN>
* @property {string} [stdinFilename] Specify filename to process STDIN as
* @property {boolean} quiet Report errors only
* @property {boolean} [version] Output the version number
* @property {string[]} _ Positional filenames or patterns
*/
declare const options: any;
export { options };

View File

@@ -0,0 +1,273 @@
import { createRequire } from "node:module";
const _require = createRequire(import.meta.url);
import { dirname } from "node:path";
const eslintDir = dirname(_require.resolve("eslint/package.json"));
const optionatorPath = _require.resolve("optionator", {
paths: [eslintDir]
});
const optionator = _require(optionatorPath);
const options = optionator({
prepend: "eslint [options] file.js [file.js] [dir]",
defaults: {
concatRepeatedArrays: true,
mergeRepeatedObjects: true
},
options: [
{
heading: "Basic configuration"
},
{
option: "eslintrc",
type: "Boolean",
default: "true",
description: "Disable use of configuration from .eslintrc.*"
},
{
option: "config",
alias: "c",
type: "path::String",
description: "Use this configuration, overriding .eslintrc.* config options if present"
},
{
option: "env",
type: "[String]",
description: "Specify environments"
},
{
option: "ext",
type: "[String]",
description: "Specify JavaScript file extensions"
},
{
option: "global",
type: "[String]",
description: "Define global variables"
},
{
option: "parser",
type: "String",
description: "Specify the parser to be used"
},
{
option: "parser-options",
type: "Object",
description: "Specify parser options"
},
{
option: "resolve-plugins-relative-to",
type: "path::String",
description: "A folder where plugins should be resolved from, CWD by default"
},
{
heading: "Specifying rules and plugins"
},
{
option: "plugin",
type: "[String]",
description: "Specify plugins"
},
{
option: "rule",
type: "Object",
description: "Specify rules"
},
{
option: "rulesdir",
type: "[path::String]",
description: "Load additional rules from this directory. Deprecated: Use rules from plugins"
},
{
heading: "Fixing problems"
},
{
option: "fix",
type: "Boolean",
default: false,
description: "Automatically fix problems"
},
{
option: "fix-dry-run",
type: "Boolean",
default: false,
description: "Automatically fix problems without saving the changes to the file system"
},
{
option: "fix-type",
type: "Array",
description: "Specify the types of fixes to apply (directive, problem, suggestion, layout)"
},
{
heading: "Ignoring files"
},
{
option: "ignore-path",
type: "path::String",
description: "Specify path of ignore file"
},
{
option: "ignore",
type: "Boolean",
default: "true",
description: "Disable use of ignore files and patterns"
},
{
option: "ignore-pattern",
type: "[String]",
description: "Pattern of files to ignore (in addition to those in .eslintignore)",
concatRepeatedArrays: [
true,
{
oneValuePerFlag: true
}
]
},
{
heading: "Using stdin"
},
{
option: "stdin",
type: "Boolean",
default: "false",
description: "Lint code provided on <STDIN>"
},
{
option: "stdin-filename",
type: "String",
description: "Specify filename to process STDIN as"
},
{
heading: "Handling warnings"
},
{
option: "quiet",
type: "Boolean",
default: "false",
description: "Report errors only"
},
{
option: "max-warnings",
type: "Int",
default: "-1",
description: "Number of warnings to trigger nonzero exit code"
},
{
heading: "Output"
},
{
option: "output-file",
alias: "o",
type: "path::String",
description: "Specify file to write report to"
},
{
option: "format",
alias: "f",
type: "String",
default: "stylish",
description: "Use a specific output format"
},
{
option: "color",
type: "Boolean",
alias: "no-color",
description: "Force enabling/disabling of color"
},
{
heading: "Inline configuration comments"
},
{
option: "inline-config",
type: "Boolean",
default: "true",
description: "Prevent comments from changing config or rules"
},
{
option: "report-unused-disable-directives",
type: "Boolean",
default: void 0,
description: "Adds reported errors for unused eslint-disable directives"
},
{
heading: "Caching"
},
{
option: "cache",
type: "Boolean",
default: "false",
description: "Only check changed files"
},
{
option: "cache-file",
type: "path::String",
default: ".eslintcache",
description: "Path to the cache file. Deprecated: use --cache-location"
},
{
option: "cache-location",
type: "path::String",
description: "Path to the cache file or directory"
},
{
option: "cache-strategy",
dependsOn: ["cache"],
type: "String",
default: "metadata",
enum: ["metadata", "content"],
description: "Strategy to use for detecting changed files in the cache"
},
{
heading: "Miscellaneous"
},
{
option: "init",
type: "Boolean",
default: "false",
description: "Run config initialization wizard"
},
{
option: "env-info",
type: "Boolean",
default: "false",
description: "Output execution environment information"
},
{
option: "error-on-unmatched-pattern",
type: "Boolean",
default: "true",
description: "Prevent errors when pattern is unmatched"
},
{
option: "exit-on-fatal-error",
type: "Boolean",
default: "false",
description: "Exit with exit code 2 in case of fatal error"
},
{
option: "debug",
type: "Boolean",
default: false,
description: "Output debugging information"
},
{
option: "help",
alias: "h",
type: "Boolean",
description: "Show help"
},
{
option: "version",
alias: "v",
type: "Boolean",
description: "Output the version number"
},
{
option: "print-config",
type: "path::String",
description: "Print the configuration for the given file"
}
]
});
export {
options
};
//# sourceMappingURL=options.js.map

File diff suppressed because one or more lines are too long