Skip to content

Commit 49ead11

Browse files
authoredSep 21, 2022
fix(logger): fix possibly-null property access in logger-typescript.ts (#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.
1 parent 5e863cd commit 49ead11

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed
 

‎src/utils/logger/logger-typescript.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,14 @@ export const loadTypeScriptDiagnostics = (tsDiagnostics: readonly Diagnostic[])
9393
return diagnostics;
9494
};
9595

96-
export const loadTypeScriptDiagnostic = (tsDiagnostic: Diagnostic) => {
96+
/**
97+
* Convert a TypeScript diagnostic object into our internal, Stencil-specific
98+
* diagnostic format
99+
*
100+
* @param tsDiagnostic a TypeScript diagnostic message record
101+
* @returns a Stencil diagnostic, suitable for showing an error to the user
102+
*/
103+
export const loadTypeScriptDiagnostic = (tsDiagnostic: Diagnostic): d.Diagnostic => {
97104
const d: d.Diagnostic = {
98105
level: 'warn',
99106
type: 'typescript',
@@ -164,15 +171,27 @@ export const loadTypeScriptDiagnostic = (tsDiagnostic: Diagnostic) => {
164171
return d;
165172
};
166173

167-
const flattenDiagnosticMessageText = (tsDiagnostic: Diagnostic, diag: string | DiagnosticMessageChain | undefined) => {
174+
/**
175+
* Flatten a TypeScript diagnostic object into a string which can be easily
176+
* included in a Stencil diagnostic record.
177+
*
178+
* @param tsDiagnostic a TypeScript diagnostic record
179+
* @param diag a {@link DiagnosticMessageChain} or a string with further info
180+
* @returns a string with the relevant error message
181+
*/
182+
const flattenDiagnosticMessageText = (
183+
tsDiagnostic: Diagnostic,
184+
diag: string | DiagnosticMessageChain | undefined
185+
): string => {
168186
if (typeof diag === 'string') {
169187
return diag;
170188
} else if (diag === undefined) {
171189
return '';
172190
}
173191

174192
const ignoreCodes: number[] = [];
175-
const isStencilConfig = tsDiagnostic.file.fileName.includes('stencil.config');
193+
// `tsDiagnostic.file` can be `undefined`, so we need to be a little careful here
194+
const isStencilConfig = (tsDiagnostic.file?.fileName ?? '').includes('stencil.config');
176195
if (isStencilConfig) {
177196
ignoreCodes.push(2322);
178197
}

0 commit comments

Comments
 (0)
Please sign in to comment.