Skip to content

Commit

Permalink
chore(typescript): enable useUnknownInCatchVariables in core (#3211)
Browse files Browse the repository at this point in the history
this commit enables the 'useUnknownInCatchVariables' flag in the core
compiler's tsconfig.json file. the remainder of the changes related
directly to errors emitted by the TypeScript compiler as a result of
this change.

the most common change in this commit is updating a `catch` block that
consists of a single call to stencil's `catchError` function. because
`catchError` may theoretically accept a value or object that does not
conform to the `Error` interface, we annotate many of the error
variables declared in the `catch` block as type `any`, in a good-faith
effort not to lose any useful information that we would otherwise turn
into a `Diagnostic`. Using a type guard or other type narrowing
mechanism would be feasible if any value that is not of type `Error`
were logged in some other way. However in an attempt to not pollute the
codebase with two different logging paths, we accept the ambiguity of
`catchError` and catching errors of type `any`  for the time being.
  • Loading branch information
rwaskiewicz committed Jan 27, 2022
1 parent 806012e commit d9f87ac
Show file tree
Hide file tree
Showing 47 changed files with 131 additions and 83 deletions.
3 changes: 2 additions & 1 deletion src/cli/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export const run = async (init: d.CliInitOptions) => {
});
} catch (e) {
if (!shouldIgnoreError(e)) {
logger.error(`uncaught cli error: ${e}${logger.getLevel() === 'debug' ? e.stack : ''}`);
const details = `${logger.getLevel() === 'debug' && e instanceof Error ? e.stack : ''}`;
logger.error(`uncaught cli error: ${e}${details}`);
return sys.exit(1);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/task-prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const runPrerenderTask = async (
});

diagnostics.push(...results.diagnostics);
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}

Expand Down
9 changes: 7 additions & 2 deletions src/compiler/build/build-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ export function generateBuildStats(

jsonData = stats;
}
} catch (e) {
} catch (e: unknown) {
const diagnostic: d.Diagnostic = {
messageText: `Generate Build Stats Error: ` + e,
level: `error`,
type: `build`,
};
jsonData = {
diagnostics: [e.message],
diagnostics: [diagnostic],
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const build = async (
// write outputs
await buildCtx.stylesPromise;
await writeBuild(config, compilerCtx, buildCtx);
} catch (e) {
} catch (e: any) {
// ¯\_(ツ)_/¯
catchError(buildCtx.diagnostics, e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/build/write-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const writeBuild = async (config: d.Config, compilerCtx: d.CompilerCtx, b
// buildCtx.debug(`cache: ${compilerCtx.cache.getMemoryStats()}`);

await outputServiceWorkers(config, buildCtx), await validateBuildFiles(config, compilerCtx, buildCtx);
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/bundle/bundle-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const bundleOutput = async (

compilerCtx.rollupCache.set(bundleOpts.id, rollupBuild.cache);
return rollupBuild;
} catch (e) {
} catch (e: any) {
if (!buildCtx.hasError) {
// TODO(STENCIL-353): Implement a type guard that balances using our own copy of Rollup types (which are
// breakable) and type safety (so that the error variable may be something other than `any`)
loadRollupDiagnostics(config, compilerCtx, buildCtx, e);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/bundle/dev-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ export const compilerRequest = async (config: d.Config, compilerCtx: d.CompilerC
results.status = 400;
return results;
}
} catch (e) {
} catch (e: unknown) {
if (e) {
if (e.stack) {
if (e instanceof Error && e.stack) {
results.content = `/*\n${e.stack}\n*/`;
} else {
results.content = `/*\n${e}\n*/`;
Expand Down Expand Up @@ -165,7 +165,8 @@ const bundleDevModule = async (
}
} catch (e) {
results.status = 500;
results.content = `console.error(${JSON.stringify((e.stack || e) + '')})`;
const errorMsg = e instanceof Error ? e.stack : e + '';
results.content = `console.error(${JSON.stringify(errorMsg)})`;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/config/load-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const loadConfig = async (init: LoadConfigInit = {}) => {
results.tsconfig.exclude = tsConfigResults.exclude;
results.tsconfig.extends = tsConfigResults.extends;
}
} catch (e) {
} catch (e: any) {
catchError(results.diagnostics, e);
}

Expand Down Expand Up @@ -131,7 +131,7 @@ const evaluateConfigFile = async (sys: CompilerSystem, diagnostics: Diagnostic[]
const evalConfig = new Function(`const exports = {}; ${sourceText}; return exports;`);
configFileData = evalConfig();
}
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/config/outputs/validate-custom-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const validateCustomOutput = (config: d.Config, diagnostics: d.Diagnostic
const localDiagnostics: d.Diagnostic[] = [];
try {
o.validate(config, diagnostics);
} catch (e) {
} catch (e: any) {
catchError(localDiagnostics, e);
}
if (o.copy && o.copy.length > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/html/remove-unused-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const removeUnusedStyles = (doc: Document, diagnostics: d.Diagnostic[]) =
removeUnusedStyleText(usedSelectors, diagnostics, styleElms[i]);
}
}
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}
};
Expand All @@ -43,15 +43,15 @@ const removeUnusedStyleText = (
styleElm.innerHTML = serializeCss(parseResults.stylesheet, {
usedSelectors,
});
} catch (e) {
} catch (e: any) {
diagnostics.push({
level: 'warn',
type: 'css',
header: 'CSS Stringify',
messageText: e,
});
}
} catch (e) {
} catch (e: any) {
diagnostics.push({
level: 'warn',
type: 'css',
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/optimize/autoprefixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const autoprefixCss = async (cssText: string, opts: any) => {
});

output.output = result.css;
} catch (e) {
} catch (e: any) {
const diagnostic: d.Diagnostic = {
header: `Autoprefix CSS`,
messageText: `CSS Error` + e,
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/optimize/minify-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ export const minifyJs = async (input: string, opts?: MinifyOptions): Promise<d.O
results.output = results.output.substring(0, results.output.length - 1);
}
} catch (e) {
console.log(e.stack);
if (e instanceof Error) {
console.log(e.stack);
}
loadMinifyJsDiagnostics(input, results.diagnostics, e);
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/optimize/optimize-js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const optimizeJs = async (inputOpts: OptimizeJsInput) => {
result.output = minifyResults.output;
result.sourceMap = minifyResults.sourceMap;
}
} catch (e) {
} catch (e: any) {
catchError(result.diagnostics, e);
}

Expand Down
4 changes: 3 additions & 1 deletion src/compiler/output-targets/copy/output-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const outputCopy = async (config: d.Config, compilerCtx: d.CompilerCtx, b
}
} catch (e) {
const err = buildError(buildCtx.diagnostics);
err.messageText = e.message;
if (e instanceof Error) {
err.messageText = e.message;
}
}
timespan.finish(`copy finished (${copiedFiles} file${copiedFiles === 1 ? '' : 's'})`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/output-targets/dist-collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const outputCollection = async (
);

await writeCollectionManifests(config, compilerCtx, buildCtx, outputTargets);
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const bundleCustomElements = async (
});
await Promise.all(files);
}
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/output-targets/dist-custom-elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ const bundleCustomElements = async (
});
await Promise.all(files);
}
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ export const bundleHydrateFactory = async (

const rollupBuild = await bundleOutput(config, compilerCtx, buildCtx, bundleOpts);
return rollupBuild;
} catch (e) {
} catch (e: any) {
if (!buildCtx.hasError) {
// TODO(STENCIL-353): Implement a type guard that balances using our own copy of Rollup types (which are
// breakable) and type safety (so that the error variable may be something other than `any`)
loadRollupDiagnostics(config, compilerCtx, buildCtx, e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ export const generateHydrateApp = async (
});

await writeHydrateOutputs(config, compilerCtx, buildCtx, outputTargets, rollupOutput);
} catch (e) {
} catch (e: any) {
if (!buildCtx.hasError) {
// TODO(STENCIL-353): Implement a type guard that balances using our own copy of Rollup types (which are
// breakable) and type safety (so that the error variable may be something other than `any`)
loadRollupDiagnostics(config, compilerCtx, buildCtx, e);
}
}
Expand Down Expand Up @@ -93,7 +95,7 @@ const generateHydrateFactory = async (config: d.Config, compilerCtx: d.CompilerC
return rollupOutput.output[0].code;
}
}
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/output-targets/dist-lazy/lazy-output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const outputLazy = async (config: d.Config, compilerCtx: d.CompilerCtx, b
buildCtx.componentGraph = generateModuleGraph(buildCtx.components, buildCtx.esmBrowserComponentBundle);
}
}
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}

Expand Down Expand Up @@ -120,7 +120,7 @@ function generateEntryModules(config: d.Config, buildCtx: d.BuildCtx): void {
try {
const bundles = generateComponentBundles(config, buildCtx);
buildCtx.entryModules = bundles.map(createEntryModule);
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/output-targets/output-custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const outputCustom = async (
const timespan = buildCtx.createTimeSpan(`generating ${o.name} started`);
try {
await o.generator(config, compilerCtx, buildCtx, docs);
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
timespan.finish(`generate ${o.name} finished`);
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/output-targets/output-www.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const generateIndexHtml = async (
}

buildCtx.debug(`generateIndexHtml, write: ${relative(config.rootDir, outputTarget.indexHtml)}`);
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
};
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const runPluginResolveId = async (pluginCtx: PluginCtx, importee: string)
return results;
}
}
} catch (e) {
} catch (e: any) {
catchError(pluginCtx.diagnostics, e);
}
}
Expand All @@ -47,7 +47,7 @@ export const runPluginLoad = async (pluginCtx: PluginCtx, id: string) => {
return results;
}
}
} catch (e) {
} catch (e: any) {
catchError(pluginCtx.diagnostics, e);
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ export const runPluginTransforms = async (
}
}
}
} catch (e) {
} catch (e: any) {
catchError(buildCtx.diagnostics, e);
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ export const runPluginTransformsEsmImports = async (
}
}
}
} catch (e) {
} catch (e: any) {
catchError(transformResults.diagnostics, e);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/compiler/prerender/crawl-urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const crawlAnchorsForNextUrls = (
if (userFilterAnchor === false) {
return false;
}
} catch (e) {
} catch (e: any) {
// user filterAnchor() error
catchError(diagnostics, e);
return false;
Expand All @@ -49,7 +49,7 @@ export const crawlAnchorsForNextUrls = (

// standard normalizeUrl(), after user normalized
return standardNormalizeUrl(diagnostics, userNormalizedUrl.href, currentUrl);
} catch (e) {
} catch (e: any) {
// user normalizeUrl() error
catchError(diagnostics, e);
}
Expand All @@ -68,7 +68,7 @@ export const crawlAnchorsForNextUrls = (
if (userFilterUrl === false) {
return false;
}
} catch (e) {
} catch (e: any) {
// user filterUrl() error
catchError(diagnostics, e);
return false;
Expand Down Expand Up @@ -123,7 +123,7 @@ const standardFilterAnchor = (diagnostics: d.Diagnostic[], attrs: { [attrName: s
return true;
}
}
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}

Expand All @@ -146,7 +146,7 @@ const standardNormalizeUrl = (diagnostics: d.Diagnostic[], href: string, current
}

return outputUrl;
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ const standardFilterUrl = (diagnostics: d.Diagnostic[], url: URL, currentUrl: UR
}

return true;
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}
return false;
Expand Down Expand Up @@ -214,7 +214,7 @@ export const standardNormalizeHref = (prerenderConfig: d.PrerenderConfig, diagno

return href;
}
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/prerender/prerender-hydrate-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getHydrateOptions = (prerenderConfig: d.PrerenderConfig, url: URL,
} else if (typeof prerenderConfig.canonicalUrl === 'function') {
try {
opts.canonicalUrl = prerenderConfig.canonicalUrl(url);
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}
} else {
Expand All @@ -39,7 +39,7 @@ export const getHydrateOptions = (prerenderConfig: d.PrerenderConfig, url: URL,
}
Object.assign(opts, userOpts);
}
} catch (e) {
} catch (e: any) {
catchError(diagnostics, e);
}
}
Expand Down

0 comments on commit d9f87ac

Please sign in to comment.