Skip to content

Commit e9a2746

Browse files
bluwysapphi-red
andauthored
chore: enable some eslint rules (#18084)
Co-authored-by: 翠 / green <[email protected]>
1 parent 35cf59c commit e9a2746

39 files changed

+93
-92
lines changed

eslint.config.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default tseslint.config(
9696
],
9797

9898
'@typescript-eslint/ban-ts-comment': 'error',
99-
'@typescript-eslint/no-unsafe-function-type': 'off', // TODO: we should turn this on in a new PR
99+
'@typescript-eslint/no-unsafe-function-type': 'off',
100100
'@typescript-eslint/explicit-module-boundary-types': [
101101
'error',
102102
{ allowArgumentsExplicitlyTypedAsAny: true },
@@ -107,26 +107,33 @@ export default tseslint.config(
107107
],
108108
'@typescript-eslint/no-empty-object-type': [
109109
'error',
110-
{ allowInterfaces: 'with-single-extends' }, // maybe we should turn this on in a new PR
110+
{ allowInterfaces: 'with-single-extends' },
111111
],
112112
'@typescript-eslint/no-empty-interface': 'off',
113-
'@typescript-eslint/no-explicit-any': 'off', // maybe we should turn this on in a new PR
113+
'@typescript-eslint/no-explicit-any': 'off',
114114
'no-extra-semi': 'off',
115115
'@typescript-eslint/no-extra-semi': 'off', // conflicts with prettier
116116
'@typescript-eslint/no-inferrable-types': 'off',
117-
'@typescript-eslint/no-unused-expressions': 'off', // maybe we should turn this on in a new PR
118-
'@typescript-eslint/no-unused-vars': 'off', // maybe we should turn this on in a new PR
117+
'@typescript-eslint/no-unused-vars': [
118+
'error',
119+
{
120+
args: 'all',
121+
argsIgnorePattern: '^_',
122+
caughtErrors: 'all',
123+
caughtErrorsIgnorePattern: '^_',
124+
destructuredArrayIgnorePattern: '^_',
125+
varsIgnorePattern: '^_',
126+
ignoreRestSiblings: true,
127+
},
128+
],
119129
'@typescript-eslint/no-require-imports': 'off',
120130
'@typescript-eslint/consistent-type-imports': [
121131
'error',
122132
{ prefer: 'type-imports', disallowTypeAnnotations: false },
123133
],
124134
// disable rules set in @typescript-eslint/stylistic which conflict with current code
125-
// maybe we should turn them on in a new PR
135+
// we should discuss if we want to enable these as they encourage consistent code
126136
'@typescript-eslint/array-type': 'off',
127-
'@typescript-eslint/ban-tslint-comment': 'off',
128-
'@typescript-eslint/consistent-generic-constructors': 'off',
129-
'@typescript-eslint/consistent-indexed-object-style': 'off',
130137
'@typescript-eslint/consistent-type-definitions': 'off',
131138
'@typescript-eslint/prefer-for-of': 'off',
132139
'@typescript-eslint/prefer-function-type': 'off',
@@ -270,6 +277,8 @@ export default tseslint.config(
270277
'n/no-unsupported-features/es-builtins': 'off',
271278
'n/no-unsupported-features/node-builtins': 'off',
272279
'@typescript-eslint/explicit-module-boundary-types': 'off',
280+
'@typescript-eslint/no-unused-expressions': 'off',
281+
'@typescript-eslint/no-unused-vars': 'off',
273282
'no-undef': 'off',
274283
'no-empty': 'off',
275284
'no-constant-condition': 'off',
@@ -298,6 +307,7 @@ export default tseslint.config(
298307
name: 'disables/dts',
299308
files: ['**/*.d.ts'],
300309
rules: {
310+
'@typescript-eslint/consistent-indexed-object-style': 'off',
301311
'@typescript-eslint/triple-slash-reference': 'off',
302312
},
303313
},

packages/create-vite/build.config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default defineBuildConfig({
2020
prompts: 'prompts/lib/index.js',
2121
},
2222
hooks: {
23-
'rollup:options'(ctx, options) {
23+
'rollup:options'(_ctx, options) {
2424
options.plugins = [
2525
options.plugins,
2626
// @ts-expect-error TODO: unbuild uses rollup v3 and Vite uses rollup v4

packages/plugin-legacy/src/index.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function toAssetPathFromHtml(
115115
config: ResolvedConfig,
116116
): string {
117117
const relativeUrlPath = normalizePath(path.relative(config.root, htmlPath))
118-
const toRelative = (filename: string, hostId: string) =>
118+
const toRelative = (filename: string, _hostId: string) =>
119119
getBaseInHTML(relativeUrlPath, config) + filename
120120
return toOutputFilePathInHtml(
121121
filename,
@@ -296,11 +296,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
296296
if (!modernPolyfills.size) {
297297
return
298298
}
299-
isDebug &&
299+
if (isDebug) {
300300
console.log(
301301
`[@vitejs/plugin-legacy] modern polyfills:`,
302302
modernPolyfills,
303303
)
304+
}
304305
const polyfillChunk = await buildPolyfillChunk(
305306
config.mode,
306307
modernPolyfills,
@@ -338,11 +339,12 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
338339
}
339340

340341
if (legacyPolyfills.size || !options.externalSystemJS) {
341-
isDebug &&
342+
if (isDebug) {
342343
console.log(
343344
`[@vitejs/plugin-legacy] legacy polyfills:`,
344345
legacyPolyfills,
345346
)
347+
}
346348

347349
await buildPolyfillChunk(
348350
config.mode,
@@ -377,8 +379,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
377379
config = _config
378380

379381
modernTargets = options.modernTargets || modernTargetsBabel
380-
isDebug &&
382+
if (isDebug) {
381383
console.log(`[@vitejs/plugin-legacy] modernTargets:`, modernTargets)
384+
}
382385

383386
if (!genLegacy || config.build.ssr) {
384387
return
@@ -388,7 +391,9 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] {
388391
options.targets ||
389392
browserslistLoadConfig({ path: config.root }) ||
390393
'last 2 versions and not dead, > 0.3%, Firefox ESR'
391-
isDebug && console.log(`[@vitejs/plugin-legacy] targets:`, targets)
394+
if (isDebug) {
395+
console.log(`[@vitejs/plugin-legacy] targets:`, targets)
396+
}
392397

393398
const getLegacyOutputFileName = (
394399
fileNames:

packages/plugin-legacy/src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export interface Options {
22
/**
33
* default: 'defaults'
44
*/
5-
targets?: string | string[] | { [key: string]: string }
5+
targets?: string | string[] | Record<string, string>
66
/**
77
* default: 'edge>=79, firefox>=67, chrome>=64, safari>=12, chromeAndroid>=64, iOS>=12'
88
*/

packages/vite/bin/vite.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if (!import.meta.url.includes('node_modules')) {
55
try {
66
// only available as dev dependency
77
await import('source-map-support').then((r) => r.default.install())
8-
} catch (e) {}
8+
} catch {}
99
}
1010

1111
global.__vite_start_time = performance.now()

packages/vite/src/client/env.ts

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
declare const __MODE__: string
21
declare const __DEFINES__: Record<string, any>
32

43
const context = (() => {

packages/vite/src/module-runner/moduleCache.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
114114
const subIds = Array.from(super.entries())
115115
.filter(([, mod]) => mod.importers?.has(id))
116116
.map(([key]) => key)
117-
subIds.length && this.invalidateSubDepTree(subIds, invalidated)
117+
if (subIds.length) {
118+
this.invalidateSubDepTree(subIds, invalidated)
119+
}
118120
super.delete(id)
119121
}
120122
return invalidated

packages/vite/src/module-runner/runner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ export class ModuleRunner {
364364
dirname: isWindows ? toWindowsPath(dirname) : dirname,
365365
url: href,
366366
env: this.envProxy,
367-
resolve(id, parent) {
367+
resolve(_id, _parent) {
368368
throw new Error(
369369
'[module runner] "import.meta.resolve" is not supported.',
370370
)
@@ -437,7 +437,7 @@ function exportAll(exports: any, sourceModule: any) {
437437
configurable: true,
438438
get: () => sourceModule[key],
439439
})
440-
} catch (_err) {}
440+
} catch {}
441441
}
442442
}
443443
}

packages/vite/src/module-runner/sourcemap/interceptor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface InterceptorOptions {
2121
const sourceMapCache: Record<string, CachedMapEntry> = {}
2222
const fileContentsCache: Record<string, string> = {}
2323

24-
const moduleGraphs: Set<ModuleCacheMap> = new Set()
24+
const moduleGraphs = new Set<ModuleCacheMap>()
2525
const retrieveFileHandlers = new Set<RetrieveFileHandler>()
2626
const retrieveSourceMapHandlers = new Set<RetrieveSourceMapHandler>()
2727

packages/vite/src/node/__tests__/plugins/css.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ async function createCssPluginTransform(
227227
const mockFs = vi
228228
.spyOn(fs, 'readFile')
229229
// @ts-expect-error vi.spyOn not recognize override `fs.readFile` definition.
230-
.mockImplementationOnce((p, encoding, callback) => {
230+
.mockImplementationOnce((p, _encoding, callback) => {
231231
callback(null, Buffer.from(files?.[p] ?? ''))
232232
})
233233

packages/vite/src/node/external.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export function createIsConfiguredAsExternal(
9595
// configured as external
9696
!!configuredAsExternal,
9797
)?.external
98-
} catch (e) {
98+
} catch {
9999
debug?.(
100100
`Failed to node resolve "${id}". Skipping externalizing it by default.`,
101101
)

packages/vite/src/node/optimizer/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ export async function loadCachedDepOptimizationMetadata(
367367
await fsp.readFile(cachedMetadataPath, 'utf-8'),
368368
depsCacheDir,
369369
)
370-
} catch (e) {}
370+
} catch {}
371371
// hash is consistent, no need to re-bundle
372372
if (cachedMetadata) {
373373
if (cachedMetadata.lockfileHash !== getLockfileHash(environment)) {
@@ -508,7 +508,7 @@ export function runOptimizeDeps(
508508
try {
509509
// When exiting the process, `fsp.rm` may not take effect, so we use `fs.rmSync`
510510
fs.rmSync(processingCacheDir, { recursive: true, force: true })
511-
} catch (error) {
511+
} catch {
512512
// Ignore errors
513513
}
514514
}
@@ -1286,7 +1286,7 @@ export async function cleanupDepsCacheStaleDirs(
12861286
for (const dirent of dirents) {
12871287
if (dirent.isDirectory() && dirent.name.includes('_temp_')) {
12881288
const tempDirPath = path.resolve(config.cacheDir, dirent.name)
1289-
const stats = await fsp.stat(tempDirPath).catch((_) => null)
1289+
const stats = await fsp.stat(tempDirPath).catch(() => null)
12901290
if (
12911291
stats?.mtime &&
12921292
Date.now() - stats.mtime.getTime() > MAX_TEMP_DIR_AGE_MS
@@ -1331,7 +1331,7 @@ const safeRename = promisify(function gracefulRename(
13311331
Date.now() - start < GRACEFUL_RENAME_TIMEOUT
13321332
) {
13331333
setTimeout(function () {
1334-
fs.stat(to, function (stater, st) {
1334+
fs.stat(to, function (stater, _st) {
13351335
if (stater && stater.code === 'ENOENT') fs.rename(from, to, CB)
13361336
else CB(er)
13371337
})

packages/vite/src/node/plugin.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
ResolvedConfig,
1515
UserConfig,
1616
} from './config'
17-
import type { ServerHook, ViteDevServer } from './server'
17+
import type { ServerHook } from './server'
1818
import type { IndexHtmlTransform } from './plugins/html'
1919
import type { EnvironmentModuleNode } from './server/moduleGraph'
2020
import type { ModuleNode } from './server/mixedModuleGraph'

packages/vite/src/node/plugins/completeSystemWrap.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function completeSystemWrapPlugin(): Plugin {
99
return {
1010
name: 'vite:force-systemjs-wrap-complete',
1111

12-
renderChunk(code, chunk, opts) {
12+
renderChunk(code, _chunk, opts) {
1313
if (opts.format === 'system') {
1414
return {
1515
code: code.replace(SystemJSWrapRE, (s, s1) =>

packages/vite/src/node/plugins/css.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ const enum PreprocessLang {
210210
styl = 'styl',
211211
stylus = 'stylus',
212212
}
213+
// eslint-disable-next-line @typescript-eslint/no-unused-vars -- bug in typescript-eslint
213214
const enum PureCssLang {
214215
css = 'css',
215216
}
@@ -964,7 +965,7 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin {
964965
return {
965966
name: 'vite:css-analysis',
966967

967-
async transform(_, id, options) {
968+
async transform(_, id) {
968969
if (
969970
!isCSSRequest(id) ||
970971
commonjsProxyRE.test(id) ||
@@ -2056,7 +2057,7 @@ function loadSassPackage(root: string): {
20562057
try {
20572058
const path = loadPreprocessorPath(PreprocessLang.sass, root)
20582059
return { name: 'sass', path }
2059-
} catch (e2) {
2060+
} catch {
20602061
throw e1
20612062
}
20622063
}
@@ -2173,9 +2174,11 @@ const makeScssWorker = (
21732174
}
21742175
const importer = [_internalImporter]
21752176
if (options.importer) {
2176-
Array.isArray(options.importer)
2177-
? importer.unshift(...options.importer)
2178-
: importer.unshift(options.importer)
2177+
if (Array.isArray(options.importer)) {
2178+
importer.unshift(...options.importer)
2179+
} else {
2180+
importer.unshift(options.importer)
2181+
}
21792182
}
21802183

21812184
const finalOptions: Sass.LegacyOptions<'async'> = {
@@ -2847,7 +2850,7 @@ const stylProcessor = (
28472850
worker.stop()
28482851
}
28492852
},
2850-
async process(environment, source, root, options, resolvers) {
2853+
async process(_environment, source, root, options, _resolvers) {
28512854
const stylusPath = loadPreprocessorPath(PreprocessLang.stylus, root)
28522855

28532856
if (!workerMap.has(options.alias)) {

packages/vite/src/node/plugins/dynamicImportVars.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
207207
let imports: readonly ImportSpecifier[] = []
208208
try {
209209
imports = parseImports(source)[0]
210-
} catch (e: any) {
210+
} catch {
211211
// ignore as it might not be a JS file, the subsequent plugins will catch the error
212212
return null
213213
}

packages/vite/src/node/plugins/esbuild.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ export async function transformWithEsbuild(
152152
// esbuild uses tsconfig fields when both the normal options and tsconfig was set
153153
// but we want to prioritize the normal options
154154
if (options) {
155-
options.jsx && (compilerOptions.jsx = undefined)
156-
options.jsxFactory && (compilerOptions.jsxFactory = undefined)
157-
options.jsxFragment && (compilerOptions.jsxFragmentFactory = undefined)
158-
options.jsxImportSource && (compilerOptions.jsxImportSource = undefined)
155+
if (options.jsx) compilerOptions.jsx = undefined
156+
if (options.jsxFactory) compilerOptions.jsxFactory = undefined
157+
if (options.jsxFragment) compilerOptions.jsxFragmentFactory = undefined
158+
if (options.jsxImportSource) compilerOptions.jsxImportSource = undefined
159159
}
160160

161161
tsconfigRaw = {

packages/vite/src/node/plugins/html.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
338338
const publicPath = `/${relativeUrlPath}`
339339
const publicBase = getBaseInHTML(relativeUrlPath, config)
340340

341-
const publicToRelative = (filename: string, importer: string) =>
342-
publicBase + filename
341+
const publicToRelative = (filename: string) => publicBase + filename
343342
const toOutputPublicFilePath = (url: string) =>
344343
toOutputFilePathInHtml(
345344
url.slice(1),
@@ -694,7 +693,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
694693
},
695694

696695
async generateBundle(options, bundle) {
697-
const analyzedChunk: Map<OutputChunk, number> = new Map()
696+
const analyzedChunk = new Map<OutputChunk, number>()
698697
const inlineEntryChunk = new Set<string>()
699698
const getImportedChunks = (
700699
chunk: OutputChunk,
@@ -797,7 +796,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
797796
relativeUrlPath,
798797
'html',
799798
config,
800-
(filename: string, importer: string) => assetsBase + filename,
799+
(filename) => assetsBase + filename,
801800
)
802801
}
803802
}

packages/vite/src/node/plugins/importAnalysisBuild.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ export function buildImportAnalysisPlugin(config: ResolvedConfig): Plugin {
513513
if (rawUrl[0] === `"` && rawUrl[rawUrl.length - 1] === `"`)
514514
url = rawUrl.slice(1, -1)
515515
}
516-
const deps: Set<string> = new Set()
516+
const deps = new Set<string>()
517517
let hasRemovedPureCssChunk = false
518518

519519
let normalizedFile: string | undefined = undefined

packages/vite/src/node/plugins/importMetaGlob.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export async function parseImportGlob(
220220
let cleanCode: string
221221
try {
222222
cleanCode = stripLiteral(code)
223-
} catch (e) {
223+
} catch {
224224
// skip invalid js code
225225
return []
226226
}

0 commit comments

Comments
 (0)