Skip to content

Commit

Permalink
fix(logger): fix possibly-null property access in logger-typescript.ts (
Browse files Browse the repository at this point in the history
#3627)

This fixes a small issue where we were accessing a possibly-undefined
property on a TypeScript Diagnostic record.

This relates to the issue reported in #3443.
  • Loading branch information
alicewriteswrongs committed Sep 21, 2022
1 parent 5e863cd commit 49ead11
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/utils/logger/logger-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,14 @@ export const loadTypeScriptDiagnostics = (tsDiagnostics: readonly Diagnostic[])
return diagnostics;
};

export const loadTypeScriptDiagnostic = (tsDiagnostic: Diagnostic) => {
/**
* Convert a TypeScript diagnostic object into our internal, Stencil-specific
* diagnostic format
*
* @param tsDiagnostic a TypeScript diagnostic message record
* @returns a Stencil diagnostic, suitable for showing an error to the user
*/
export const loadTypeScriptDiagnostic = (tsDiagnostic: Diagnostic): d.Diagnostic => {
const d: d.Diagnostic = {
level: 'warn',
type: 'typescript',
Expand Down Expand Up @@ -164,15 +171,27 @@ export const loadTypeScriptDiagnostic = (tsDiagnostic: Diagnostic) => {
return d;
};

const flattenDiagnosticMessageText = (tsDiagnostic: Diagnostic, diag: string | DiagnosticMessageChain | undefined) => {
/**
* Flatten a TypeScript diagnostic object into a string which can be easily
* included in a Stencil diagnostic record.
*
* @param tsDiagnostic a TypeScript diagnostic record
* @param diag a {@link DiagnosticMessageChain} or a string with further info
* @returns a string with the relevant error message
*/
const flattenDiagnosticMessageText = (
tsDiagnostic: Diagnostic,
diag: string | DiagnosticMessageChain | undefined
): string => {
if (typeof diag === 'string') {
return diag;
} else if (diag === undefined) {
return '';
}

const ignoreCodes: number[] = [];
const isStencilConfig = tsDiagnostic.file.fileName.includes('stencil.config');
// `tsDiagnostic.file` can be `undefined`, so we need to be a little careful here
const isStencilConfig = (tsDiagnostic.file?.fileName ?? '').includes('stencil.config');
if (isStencilConfig) {
ignoreCodes.push(2322);
}
Expand Down

0 comments on commit 49ead11

Please sign in to comment.