From 44fb8ebcc40cfe1f334e6e149a2a33b61968e034 Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Tue, 24 Aug 2021 22:50:24 +0300 Subject: [PATCH] Fix linter errors and warnings (#1218) * fix lint issues, enable lint in ci * add changeset --- .changeset/thirty-ads-destroy.md | 5 +++++ .github/workflows/ci.yml | 4 +--- packages/astro/src/cli.ts | 2 +- packages/astro/src/compiler/codegen/index.ts | 2 +- .../astro/src/compiler/transform/util/end-of-head.ts | 10 ++++++---- packages/astro/src/config_manager.ts | 4 ++-- packages/astro/src/frontend/hydrate/media.ts | 12 +++++++----- packages/astro/src/internal/__astro_component.ts | 4 ++-- packages/astro/src/internal/element-registry.ts | 2 +- packages/astro/src/logger.ts | 6 +++--- packages/astro/src/manifest/create.ts | 4 ++-- packages/astro/src/runtime.ts | 4 ++-- 12 files changed, 33 insertions(+), 26 deletions(-) create mode 100644 .changeset/thirty-ads-destroy.md diff --git a/.changeset/thirty-ads-destroy.md b/.changeset/thirty-ads-destroy.md new file mode 100644 index 0000000000..8152fd13a6 --- /dev/null +++ b/.changeset/thirty-ads-destroy.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Remove non-null assertions, fix lint issues and enable lint in CI. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d1cec0e464..44776cdc2f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -95,8 +95,6 @@ jobs: - name: Lint run: yarn lint - # linting is currently not passing, so just do this for inline for now - continue-on-error: true smoke: runs-on: ubuntu-latest @@ -139,4 +137,4 @@ jobs: - name: "Smoke Test: Build 'www'" run: yarn build - working-directory: ./www \ No newline at end of file + working-directory: ./www diff --git a/packages/astro/src/cli.ts b/packages/astro/src/cli.ts index accdb7978a..6f81cfcb02 100644 --- a/packages/astro/src/cli.ts +++ b/packages/astro/src/cli.ts @@ -101,7 +101,7 @@ function mergeCLIFlags(astroConfig: AstroConfig, flags: CLIState['options']) { } /** Handle `astro run` command */ -async function runCommand(rawRoot: string, cmd: (a: AstroConfig, options: any) => Promise, options: CLIState['options']) { +async function runCommand(rawRoot: string, cmd: (a: AstroConfig, opts: any) => Promise, options: CLIState['options']) { try { const projectRoot = options.projectRoot || rawRoot; const astroConfig = await loadConfig(projectRoot, options.config); diff --git a/packages/astro/src/compiler/codegen/index.ts b/packages/astro/src/compiler/codegen/index.ts index 8fcf507685..446fa760b9 100644 --- a/packages/astro/src/compiler/codegen/index.ts +++ b/packages/astro/src/compiler/codegen/index.ts @@ -223,7 +223,7 @@ function getComponentWrapper(_name: string, hydration: HydrationAttributes, { ur } }; - let metadata: string = ''; + let metadata = ''; if (method) { const componentUrl = getComponentUrl(astroConfig, url, pathToFileURL(filename)); const componentExport = getComponentExport(); diff --git a/packages/astro/src/compiler/transform/util/end-of-head.ts b/packages/astro/src/compiler/transform/util/end-of-head.ts index ddc4b5136f..1b798be2c0 100644 --- a/packages/astro/src/compiler/transform/util/end-of-head.ts +++ b/packages/astro/src/compiler/transform/util/end-of-head.ts @@ -82,13 +82,15 @@ export class EndOfHead { } private appendToHead(...nodes: TemplateNode[]) { - const head = this.head!; - head.children = head.children ?? []; - head.children.push(...nodes); + if (this.head) { + const head = this.head; + head.children = head.children ?? []; + head.children.push(...nodes); + } } private prependToFirstNonHead(...nodes: TemplateNode[]) { - let idx: number = this.parent?.children!.indexOf(this.firstNonHead!) || 0; + let idx: number = (this.firstNonHead && this.parent?.children?.indexOf(this.firstNonHead)) || 0; this.parent?.children?.splice(idx, 0, ...nodes); } } diff --git a/packages/astro/src/config_manager.ts b/packages/astro/src/config_manager.ts index 3705a1b3fe..75957cf65a 100644 --- a/packages/astro/src/config_manager.ts +++ b/packages/astro/src/config_manager.ts @@ -138,7 +138,7 @@ export class ConfigManager { async buildSource(contents: string): Promise { const renderers = await this.buildRendererInstances(); const rendererServerPackages = renderers.map(({ server }) => server); - const rendererClientPackages = await Promise.all(renderers.filter(({ client }) => client).map(({ client }) => this.resolvePackageUrl(client!))); + const rendererClientPackages = await Promise.all(renderers.filter((instance): instance is RendererInstance & { client: string } => !!instance.client).map(({ client }) => this.resolvePackageUrl(client))); const rendererPolyfills = await Promise.all(renderers.map(({ polyfills }) => Promise.all(polyfills.map((src) => this.resolvePackageUrl(src))))); const rendererHydrationPolyfills = await Promise.all(renderers.map(({ hydrationPolyfills }) => Promise.all(hydrationPolyfills.map((src) => this.resolvePackageUrl(src))))); @@ -173,6 +173,6 @@ ${contents} } private async importModule(snowpackRuntime: SnowpackServerRuntime): Promise { - await snowpackRuntime!.importModule(CONFIG_MODULE_URL); + await snowpackRuntime.importModule(CONFIG_MODULE_URL); } } diff --git a/packages/astro/src/frontend/hydrate/media.ts b/packages/astro/src/frontend/hydrate/media.ts index 99e968f0b1..a999ba3eea 100644 --- a/packages/astro/src/frontend/hydrate/media.ts +++ b/packages/astro/src/frontend/hydrate/media.ts @@ -14,10 +14,12 @@ export default async function onMedia(astroId: string, options: HydrateOptions, } }; - const mql = matchMedia(options.value!); - if (mql.matches) { - cb(); - } else { - mql.addEventListener('change', cb, { once: true }); + if (options.value) { + const mql = matchMedia(options.value); + if (mql.matches) { + cb(); + } else { + mql.addEventListener('change', cb, { once: true }); + } } } diff --git a/packages/astro/src/internal/__astro_component.ts b/packages/astro/src/internal/__astro_component.ts index bba6d4e687..3f19c73c72 100644 --- a/packages/astro/src/internal/__astro_component.ts +++ b/packages/astro/src/internal/__astro_component.ts @@ -45,7 +45,7 @@ export function setRenderers(_rendererInstances: RendererInstance[]) { rendererInstances = ([] as RendererInstance[]).concat(_rendererInstances); } -function isCustomElementTag(name: string | Function) { +function isCustomElementTag(name: unknown) { return typeof name === 'string' && /-/.test(name); } @@ -90,7 +90,7 @@ async function resolveRenderer(Component: any, props: any = {}, children?: strin } if (rendererCache.has(Component)) { - return rendererCache.get(Component)!; + return rendererCache.get(Component); } const errors: Error[] = []; diff --git a/packages/astro/src/internal/element-registry.ts b/packages/astro/src/internal/element-registry.ts index a0124523f3..272f1c506c 100644 --- a/packages/astro/src/internal/element-registry.ts +++ b/packages/astro/src/internal/element-registry.ts @@ -26,7 +26,7 @@ class AstroElementRegistry { findCached(tagName: string) { if (this.cache.has(tagName)) { - return this.cache.get(tagName)!; + return this.cache.get(tagName); } let specifier = this.find(tagName); if (specifier) { diff --git a/packages/astro/src/logger.ts b/packages/astro/src/logger.ts index 59be70f02f..baee62b9e4 100644 --- a/packages/astro/src/logger.ts +++ b/packages/astro/src/logger.ts @@ -47,7 +47,7 @@ export interface LogOptions { level?: LoggerLevel; } -export const defaultLogOptions: LogOptions = { +export const defaultLogOptions: Required = { dest: defaultLogDestination, level: 'info', }; @@ -69,8 +69,8 @@ const levels: Record = { /** Full logging API */ export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | null, ...args: Array) { - const logLevel = opts.level ?? defaultLogOptions.level!; - const dest = opts.dest ?? defaultLogOptions.dest!; + const logLevel = opts.level ?? defaultLogOptions.level; + const dest = opts.dest ?? defaultLogOptions.dest; const event: LogMessage = { type, level, diff --git a/packages/astro/src/manifest/create.ts b/packages/astro/src/manifest/create.ts index 4faf473389..c945586e20 100644 --- a/packages/astro/src/manifest/create.ts +++ b/packages/astro/src/manifest/create.ts @@ -150,9 +150,9 @@ function countOccurrences(needle: string, haystack: string) { return count; } -function isSpread(path: string) { +function isSpread(str: string) { const spreadPattern = /\[\.{3}/g; - return spreadPattern.test(path); + return spreadPattern.test(str); } function comparator(a: Item, b: Item) { diff --git a/packages/astro/src/runtime.ts b/packages/astro/src/runtime.ts index cf69b4aa16..165df0a02c 100644 --- a/packages/astro/src/runtime.ts +++ b/packages/astro/src/runtime.ts @@ -116,9 +116,9 @@ async function load(config: AstroRuntimeConfig, rawPathname: string | undefined) return { statusCode: 404, error: new Error('No matching route found.') }; } - const paramsMatch = routeMatch.pattern.exec(reqPath)!; + const paramsMatch = routeMatch.pattern.exec(reqPath); const routeLocation = convertMatchToLocation(routeMatch, config.astroConfig); - const params = getParams(routeMatch.params)(paramsMatch); + const params = paramsMatch ? getParams(routeMatch.params)(paramsMatch) : {}; let pageProps = {} as Record; try {