Skip to content

Commit 9437518

Browse files
authored
refactor: enable some typecheck rules (#22278)
1 parent 04f974f commit 9437518

18 files changed

Lines changed: 56 additions & 55 deletions

File tree

eslint.config.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,25 @@ export default defineConfig(
155155
'@typescript-eslint/consistent-type-definitions': 'off',
156156
'@typescript-eslint/prefer-for-of': 'off',
157157
'@typescript-eslint/prefer-function-type': 'off',
158-
// disable typecheck-specific rules (but worth revisiting again)
159-
'@typescript-eslint/await-thenable': 'off',
160-
'@typescript-eslint/dot-notation': 'off',
161-
'@typescript-eslint/no-base-to-string': 'off',
162-
'@typescript-eslint/no-duplicate-type-constituents': 'off',
163-
'@typescript-eslint/no-implied-eval': 'off',
158+
// disable typecheck-specific rules
159+
'@typescript-eslint/await-thenable': 'off', // does not handle `void | Promise<void>` well
160+
'@typescript-eslint/no-base-to-string': 'off', // does not matter for us
161+
'@typescript-eslint/no-implied-eval': 'off', // we intentionally use `Function()`
164162
'@typescript-eslint/no-floating-promises': 'off',
165163
'@typescript-eslint/no-misused-promises': 'off',
166-
'@typescript-eslint/no-redundant-type-constituents': 'off',
164+
'@typescript-eslint/no-redundant-type-constituents': 'off', // hard to handle some cases
167165
'@typescript-eslint/no-unnecessary-type-assertion': 'off',
168166
'@typescript-eslint/no-unsafe-argument': 'off',
169167
'@typescript-eslint/no-unsafe-assignment': 'off',
170168
'@typescript-eslint/no-unsafe-call': 'off',
171-
'@typescript-eslint/no-unsafe-enum-comparison': 'off',
172169
'@typescript-eslint/no-unsafe-member-access': 'off',
173170
'@typescript-eslint/no-unsafe-return': 'off',
174-
'@typescript-eslint/non-nullable-type-assertion-style': 'off',
175171
'@typescript-eslint/only-throw-error': 'off',
176-
'@typescript-eslint/prefer-includes': 'off',
177172
'@typescript-eslint/prefer-nullish-coalescing': 'off',
178173
'@typescript-eslint/prefer-optional-chain': 'off',
179-
'@typescript-eslint/prefer-promise-reject-errors': 'off',
180-
'@typescript-eslint/prefer-string-starts-ends-with': 'off',
174+
'@typescript-eslint/prefer-string-starts-ends-with': 'off', // prefer indexed access for better performance
181175
'@typescript-eslint/require-await': 'off',
182-
'@typescript-eslint/restrict-plus-operands': 'off',
183-
'@typescript-eslint/restrict-template-expressions': 'off',
176+
'@typescript-eslint/restrict-template-expressions': 'off', // does not matter for us
184177
'@typescript-eslint/unbound-method': 'off',
185178

186179
'import-x/no-duplicates': 'error',
@@ -376,6 +369,14 @@ export default defineConfig(
376369
'@typescript-eslint/ban-ts-comment': 'off',
377370
},
378371
},
372+
{
373+
name: 'disables/test-dts',
374+
files: ['**/__tests_dts__/**/*.?([cm])[jt]s?(x)'],
375+
rules: {
376+
// disable typecheck-specific rules
377+
'@typescript-eslint/no-duplicate-type-constituents': 'off',
378+
},
379+
},
379380
{
380381
name: 'disables/typechecking',
381382
files: [

packages/vite/rolldown.config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import path from 'node:path'
33
import MagicString from 'magic-string'
44
import type { Plugin } from 'rolldown'
55
import { defineConfig } from 'rolldown'
6-
import { init, parse } from 'es-module-lexer'
6+
import { ImportType, init, parse } from 'es-module-lexer'
77
import licensePlugin from './rollupLicensePlugin'
88

99
// eslint-disable-next-line n/no-unsupported-features/node-builtins
@@ -321,7 +321,10 @@ function buildTimeImportMetaUrlPlugin(): Plugin {
321321
const s = new MagicString(code)
322322
const [imports] = parse(code)
323323
for (const { t, ss, se } of imports) {
324-
if (t === 3 && code.slice(se, se + 4) === '.url') {
324+
if (
325+
t === ImportType.ImportMeta &&
326+
code.slice(se, se + 4) === '.url'
327+
) {
325328
// ignore import.meta.url with /** #__KEEP__ */ comment
326329
if (keepCommentRE.test(code.slice(0, ss))) {
327330
keepCommentRE.lastIndex = 0

packages/vite/rollupLicensePlugin.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function licensePlugin(
1616
// MIT Licensed https://github.com/rollup/rollup/blob/master/LICENSE-CORE.md
1717
const coreLicense = fs.readFileSync(
1818
new URL('../../LICENSE', import.meta.url),
19+
'utf-8',
1920
)
2021

2122
const deps = sortDependencies(dependencies)

packages/vite/src/client/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ function waitForSuccessfulPing(socketUrl: string) {
395395
document.removeEventListener('visibilitychange', onVisibilityChange)
396396
sharedWorker.port.close()
397397

398-
const data: { type: 'success' } | { type: 'error'; error: unknown } =
398+
const data: { type: 'success' } | { type: 'error'; error: Error } =
399399
event.data
400400
if (data.type === 'error') {
401401
reject(data.error)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class EvaluatedModuleNode {
1414
public evaluated = false
1515
public meta: ResolvedResult | undefined
1616
public promise: Promise<any> | undefined
17-
public exports: any | undefined
17+
public exports: any
1818
public file: string
1919
public map: DecodedMap | undefined
2020

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function CallSiteToString(this: CallSite) {
271271
} else {
272272
fileName = this.getScriptNameOrSourceURL()
273273
if (!fileName && this.isEval()) {
274-
fileLocation = this.getEvalOrigin() as string
274+
fileLocation = this.getEvalOrigin()!
275275
fileLocation += ', ' // Expecting source position to follow.
276276
}
277277

@@ -388,7 +388,7 @@ function wrapCallSite(frame: CallSite, state: State) {
388388
return position.column + 1
389389
}
390390
frame.getScriptNameOrSourceURL = function () {
391-
return position.source as string
391+
return position.source!
392392
}
393393
return frame
394394
}

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@ const questionRegex = /\?/g
2424
const hashRegex = /#/g
2525

2626
function encodePathChars(filepath: string) {
27-
if (filepath.indexOf('%') !== -1)
28-
filepath = filepath.replace(percentRegEx, '%25')
27+
if (filepath.includes('%')) filepath = filepath.replace(percentRegEx, '%25')
2928
// In posix, backslash is a valid character in paths:
30-
if (!isWindows && filepath.indexOf('\\') !== -1)
29+
if (!isWindows && filepath.includes('\\'))
3130
filepath = filepath.replace(backslashRegEx, '%5C')
32-
if (filepath.indexOf('\n') !== -1)
33-
filepath = filepath.replace(newlineRegEx, '%0A')
34-
if (filepath.indexOf('\r') !== -1)
31+
if (filepath.includes('\n')) filepath = filepath.replace(newlineRegEx, '%0A')
32+
if (filepath.includes('\r'))
3533
filepath = filepath.replace(carriageReturnRegEx, '%0D')
36-
if (filepath.indexOf('\t') !== -1)
37-
filepath = filepath.replace(tabRegEx, '%09')
34+
if (filepath.includes('\t')) filepath = filepath.replace(tabRegEx, '%09')
3835
return filepath
3936
}
4037

@@ -59,10 +56,8 @@ export function posixPathToFileHref(posixPath: string): string {
5956
// Therefore, encoding is required to eliminate parsing them in different states.
6057
// This is done as an optimization to not creating a URL instance and
6158
// later triggering pathname setter, which impacts performance
62-
if (resolved.indexOf('?') !== -1)
63-
resolved = resolved.replace(questionRegex, '%3F')
64-
if (resolved.indexOf('#') !== -1)
65-
resolved = resolved.replace(hashRegex, '%23')
59+
if (resolved.includes('?')) resolved = resolved.replace(questionRegex, '%3F')
60+
if (resolved.includes('#')) resolved = resolved.replace(hashRegex, '%23')
6661
return new URL(`file://${resolved}`).href
6762
}
6863

packages/vite/src/node/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const stopProfiler = (
7070
): void | Promise<void> => {
7171
if (!profileSession) return
7272
return new Promise((res, rej) => {
73-
profileSession!.post('Profiler.stop', (err: any, { profile }: any) => {
73+
profileSession!.post('Profiler.stop', (err, { profile }) => {
7474
// Write profile to disk, upload, etc.
7575
if (!err) {
7676
const outPath = path.resolve(

packages/vite/src/node/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export function loadEnv(
8585
// these are typically provided inline and should be prioritized
8686
for (const key in process.env) {
8787
if (prefixes.some((prefix) => key.startsWith(prefix))) {
88-
env[key] = process.env[key] as string
88+
env[key] = process.env[key]!
8989
}
9090
}
9191

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ export function registerCustomMime(): void {
6464
// https://github.com/lukeed/mrmime/issues/3
6565
// instead of `image/vnd.microsoft.icon` which is registered on IANA Media Types DB
6666
// image/x-icon should be used instead for better compatibility (https://github.com/h5bp/html5-boilerplate/issues/219)
67-
mrmime.mimes['ico'] = 'image/x-icon'
67+
mrmime.mimes.ico = 'image/x-icon'
6868
// https://mimesniff.spec.whatwg.org/#matching-an-image-type-pattern
69-
mrmime.mimes['cur'] = 'image/x-icon'
69+
mrmime.mimes.cur = 'image/x-icon'
7070
// https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Containers#flac
71-
mrmime.mimes['flac'] = 'audio/flac'
71+
mrmime.mimes.flac = 'audio/flac'
7272
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
73-
mrmime.mimes['eot'] = 'application/vnd.ms-fontobject'
73+
mrmime.mimes.eot = 'application/vnd.ms-fontobject'
7474
}
7575

7676
export function renderAssetUrlInJS(

0 commit comments

Comments
 (0)