Skip to content

Commit

Permalink
chore: @typescript-eslint/explicit-module-boundary-types (#2735)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored Apr 14, 2021
1 parent 6dfba78 commit c61b3e4
Show file tree
Hide file tree
Showing 23 changed files with 119 additions and 67 deletions.
4 changes: 2 additions & 2 deletions packages/plugin-vue/src/handleHotUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export async function handleHotUpdate({
return [...affectedModules].filter(Boolean) as ModuleNode[]
}

export function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) {
export function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null): boolean {
if (!a && !b) return true
if (!a || !b) return false
// src imports will trigger their own updates
Expand All @@ -161,7 +161,7 @@ export function isEqualBlock(a: SFCBlock | null, b: SFCBlock | null) {
export function isOnlyTemplateChanged(
prev: SFCDescriptor,
next: SFCDescriptor
) {
): boolean {
return (
isEqualBlock(prev.script, next.script) &&
isEqualBlock(prev.scriptSetup, next.scriptSetup) &&
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-vue/src/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export function setResolvedScript(
descriptor: SFCDescriptor,
script: SFCScriptBlock,
ssr: boolean
) {
): void {
;(ssr ? ssrCache : clientCache).set(descriptor, script)
}

export function resolveScript(
descriptor: SFCDescriptor,
options: ResolvedOptions,
ssr: boolean
) {
): SFCScriptBlock | null {
if (!descriptor.script && !descriptor.scriptSetup) {
return null
}
Expand Down
24 changes: 18 additions & 6 deletions packages/plugin-vue/src/utils/descriptorCache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import path from 'path'
import slash from 'slash'
import hash from 'hash-sum'
import { parse, SFCDescriptor } from '@vue/compiler-sfc'
import { CompilerError, parse, SFCDescriptor } from '@vue/compiler-sfc'

// node_modules/@vue/compiler-sfc/dist/compiler-sfc.d.ts SFCParseResult should be exported so it can be re-used
export interface SFCParseResult {
descriptor: SFCDescriptor
errors: Array<CompilerError | SyntaxError>
}

const cache = new Map<string, SFCDescriptor>()
const prevCache = new Map<string, SFCDescriptor | undefined>()
Expand All @@ -11,7 +17,7 @@ export function createDescriptor(
source: string,
root: string,
isProduction: boolean | undefined
) {
): SFCParseResult {
const { descriptor, errors } = parse(source, {
filename,
sourceMap: true
Expand All @@ -26,15 +32,21 @@ export function createDescriptor(
return { descriptor, errors }
}

export function getPrevDescriptor(filename: string) {
export function getPrevDescriptor(filename: string): SFCDescriptor | undefined {
return prevCache.get(filename)
}

export function setPrevDescriptor(filename: string, entry: SFCDescriptor) {
export function setPrevDescriptor(
filename: string,
entry: SFCDescriptor
): void {
prevCache.set(filename, entry)
}

export function getDescriptor(filename: string, errorOnMissing = true) {
export function getDescriptor(
filename: string,
errorOnMissing = true
): SFCDescriptor | undefined {
if (cache.has(filename)) {
return cache.get(filename)!
}
Expand All @@ -46,6 +58,6 @@ export function getDescriptor(filename: string, errorOnMissing = true) {
}
}

export function setDescriptor(filename: string, entry: SFCDescriptor) {
export function setDescriptor(filename: string, entry: SFCDescriptor): void {
cache.set(filename, entry)
}
4 changes: 3 additions & 1 deletion packages/plugin-vue/src/utils/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export interface VueQuery {
raw?: boolean
}

export function parseVueRequest(id: string) {
export function parseVueRequest(
id: string
): { filename: string; query: VueQuery } {
const [filename, rawQuery] = id.split(`?`, 2)
const query = qs.parse(rawQuery) as VueQuery
if (query.vue != null) {
Expand Down
8 changes: 5 additions & 3 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const supportsConstructedSheet = (() => {

const sheetsMap = new Map()

export function updateStyle(id: string, content: string) {
export function updateStyle(id: string, content: string): void {
let style = sheetsMap.get(id)
if (supportsConstructedSheet && !content.includes('@import')) {
if (style && !(style instanceof CSSStyleSheet)) {
Expand Down Expand Up @@ -235,7 +235,7 @@ export function updateStyle(id: string, content: string) {
sheetsMap.set(id, style)
}

export function removeStyle(id: string) {
export function removeStyle(id: string): void {
const style = sheetsMap.get(id)
if (style) {
if (style instanceof CSSStyleSheet) {
Expand Down Expand Up @@ -334,6 +334,8 @@ const ctxToListenersMap = new Map<
Map<string, ((customData: any) => void)[]>
>()

// Just infer the return type for now
// _This would have to be activated when used `plugin:@typescript-eslint/recommended`_ eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const createHotContext = (ownerPath: string) => {
if (!dataMap.has(ownerPath)) {
dataMap.set(ownerPath, {})
Expand Down Expand Up @@ -436,7 +438,7 @@ export const createHotContext = (ownerPath: string) => {
/**
* urls here are dynamic import() urls that couldn't be statically analyzed
*/
export function injectQuery(url: string, queryToInject: string) {
export function injectQuery(url: string, queryToInject: string): string {
// skip urls that won't be handled by vite
if (!url.startsWith('.') && !url.startsWith('/')) {
return url
Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/client/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class ErrorOverlay extends HTMLElement {
})
}

text(selector: string, text: string, linkFiles = false) {
text(selector: string, text: string, linkFiles = false): void {
const el = this.root.querySelector(selector)!
if (!linkFiles) {
el.textContent = text
Expand All @@ -176,7 +176,7 @@ export class ErrorOverlay extends HTMLElement {
}
}

close() {
close(): void {
this.parentNode?.removeChild(this)
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ export function onRollupWarning(
warning: RollupWarning,
warn: WarningHandler,
config: ResolvedConfig
) {
): void {
if (warning.code === 'UNRESOLVED_IMPORT') {
const id = warning.source
const importer = warning.importer
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,11 @@ async function loadConfigFromBundledFile(
return config
}

export function loadEnv(mode: string, root: string, prefix = 'VITE_') {
export function loadEnv(
mode: string,
root: string,
prefix = 'VITE_'
): Record<string, string> {
if (mode === 'local') {
throw new Error(
`"local" cannot be used as a mode name because it conflicts with ` +
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/optimizer/registerMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { resolveSSRExternal } from '../ssr/ssrExternal'
*/
const debounceMs = 100

export function createMissingImporterRegisterFn(server: ViteDevServer) {
export function createMissingImporterRegisterFn(
server: ViteDevServer
): (id: string, resolved: string) => void {
const { logger } = server.config
let knownOptimized = server._optimizeDepsMetadata!.optimized
let currentMissing: Record<string, string> = {}
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@ async function transformGlob(
return s.toString()
}

export function shouldExternalizeDep(resolvedId: string, rawId: string) {
export function shouldExternalizeDep(
resolvedId: string,
rawId: string
): boolean {
// not a valid file path
if (!path.isAbsolute(resolvedId)) {
return true
Expand All @@ -430,4 +433,5 @@ export function shouldExternalizeDep(resolvedId: string, rawId: string) {
if (!JS_TYPES_RE.test(resolvedId) && !htmlTypesRE.test(resolvedId)) {
return true
}
return false
}
9 changes: 6 additions & 3 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function assetPlugin(config: ResolvedConfig): Plugin {
}
}

export function registerAssetToChunk(chunk: RenderedChunk, file: string) {
export function registerAssetToChunk(chunk: RenderedChunk, file: string): void {
let emitted = chunkToEmittedAssetsMap.get(chunk)
if (!emitted) {
emitted = new Set()
Expand Down Expand Up @@ -132,7 +132,7 @@ export function fileToUrl(
id: string,
config: ResolvedConfig,
ctx: PluginContext
) {
): string | Promise<string> {
if (config.command === 'serve') {
return fileToDevUrl(id, config)
} else {
Expand Down Expand Up @@ -163,7 +163,10 @@ const assetHashToFilenameMap = new WeakMap<
Map<string, string>
>()

export function getAssetFilename(hash: string, config: ResolvedConfig) {
export function getAssetFilename(
hash: string,
config: ResolvedConfig
): string | undefined {
return assetHashToFilenameMap.get(config)?.get(hash)
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ const enum PureCssLang {
}
type CssLang = keyof typeof PureCssLang | keyof typeof PreprocessLang

export const isCSSRequest = (request: string) =>
export const isCSSRequest = (request: string): boolean =>
cssLangRE.test(request) && !directRequestRE.test(request)

export const isDirectCSSRequest = (request: string) =>
export const isDirectCSSRequest = (request: string): boolean =>
cssLangRE.test(request) && directRequestRE.test(request)

const cssModulesCache = new WeakMap<
Expand Down
15 changes: 11 additions & 4 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '@vue/compiler-dom'

const htmlProxyRE = /\?html-proxy&index=(\d+)\.js$/
export const isHTMLProxy = (id: string) => htmlProxyRE.test(id)
export const isHTMLProxy = (id: string): boolean => htmlProxyRE.test(id)

const htmlCommentRE = /<!--[\s\S]*?-->/g
const scriptModuleRE = /(<script\b[^>]*type\s*=\s*(?:"module"|'module')[^>]*>)(.*?)<\/script>/gims
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function traverseHtml(
html: string,
filePath: string,
visitor: NodeTransform
) {
): Promise<void> {
// lazy load compiler
const { parse, transform } = await import('@vue/compiler-dom')
// @vue/compiler-core doesn't like lowercase doctypes
Expand All @@ -101,7 +101,12 @@ export async function traverseHtml(
}
}

export function getScriptInfo(node: ElementNode) {
export function getScriptInfo(
node: ElementNode
): {
src: AttributeNode | undefined
isModule: boolean
} {
let src: AttributeNode | undefined
let isModule = false
for (let i = 0; i < node.props.length; i++) {
Expand Down Expand Up @@ -440,7 +445,9 @@ export type IndexHtmlTransform =
transform: IndexHtmlTransformHook
}

export function resolveHtmlTransforms(plugins: readonly Plugin[]) {
export function resolveHtmlTransforms(
plugins: readonly Plugin[]
): [IndexHtmlTransformHook[], IndexHtmlTransformHook[]] {
const preHooks: IndexHtmlTransformHook[] = []
const postHooks: IndexHtmlTransformHook[] = []

Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'

export async function preview(config: ResolvedConfig, port = 5000) {
export async function preview(
config: ResolvedConfig,
port = 5000
): Promise<void> {
const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(config.server, app)

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export async function handleFileAddUnlink(
file: string,
server: ViteDevServer,
isUnlink = false
) {
): Promise<void> {
const modules = [...(server.moduleGraph.getModulesByFile(file) ?? [])]
if (isUnlink && file in server._globImporters) {
delete server._globImporters[file]
Expand Down Expand Up @@ -247,7 +247,7 @@ function invalidate(mod: ModuleNode, timestamp: number, seen: Set<ModuleNode>) {
export function handlePrunedModules(
mods: Set<ModuleNode>,
{ ws }: ViteDevServer
) {
): void {
// update the disposed modules' hmr timestamp
// since if it's re-imported, it should re-apply side effects
// and without the timestamp the browser will not re-import it!
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export async function resolveHttpServer(
}
}

export async function resolveHttpsConfig(httpsOption: HttpsServerOptions) {
export async function resolveHttpsConfig(
httpsOption: HttpsServerOptions
): Promise<HttpsServerOptions> {
const { ca, cert, key, pfx } = httpsOption
Object.assign(httpsOption, {
ca: readFileIfExists(ca),
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function buildErrorMessage(
err: RollupError,
args: string[] = [],
includeStack = true
) {
): string {
if (err.plugin) args.push(` Plugin: ${chalk.magenta(err.plugin)}`)
if (err.id) args.push(` File: ${chalk.cyan(err.id)}`)
if (err.frame) args.push(chalk.yellow(pad(err.frame)))
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { CLIENT_PUBLIC_PATH, FS_PREFIX } from '../../constants'
import { cleanUrl, fsPathFromId } from '../../utils'
import { assetAttrsConfig } from '../../plugins/html'

export function createDevHtmlTransformFn(server: ViteDevServer) {
export function createDevHtmlTransformFn(
server: ViteDevServer
): (url: string, html: string) => Promise<string> {
const [preHooks, postHooks] = resolveHtmlTransforms(server.config.plugins)

return (url: string, html: string): Promise<string> => {
Expand Down
Loading

0 comments on commit c61b3e4

Please sign in to comment.