1- import type { CopyDeck , ExplainOptions , ExplainResult , Trace } from "./types" ;
1+ import type { CopyDeck , ExplainOptions , ExplainResult , Section , Trace } from "./types" ;
22import { escapeHtml , safeRegexTest , tmpl } from "./utils" ;
33
44type InternalState = {
@@ -37,7 +37,7 @@ const coerceTrace = (input: string | Error | Trace, code?: string, runtime?: str
3737 return parsed ;
3838} ;
3939
40- const pickVariant = ( trace : Trace , code : string | undefined ) => {
40+ const pickVariant = ( trace : Trace , code : string | undefined , sections ?: Section [ ] ) => {
4141 const deck = state . copy ;
4242 const kind = trace . type && deck ?. errors [ trace . type ] ? trace . type : "Other" ;
4343 const entry = deck ?. errors [ kind ] ;
@@ -111,15 +111,16 @@ const pickVariant = (trace: Trace, code: string | undefined) => {
111111 patch = codeLine . replace ( / \+ \s * ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) / , "+ str($1)" ) ;
112112 }
113113
114+ const has = ( s : Section ) => ! sections || sections . includes ( s ) ;
114115 const html = [
115- `<div class="pfem__title">${ titleHtml } </div>` ,
116- `<div class="pfem__summary">${ summaryHtml } </div>` ,
117- whyHtml ? `<div class="pfem__why">${ whyHtml } </div>` : "" ,
118- stepsHtml ?. length ? `<ul class="pfem__steps">${ stepsHtml . map ( ( s ) => `<li>${ s } </li>` ) . join ( "" ) } </ul>` : "" ,
119- patch ? `<pre class="pfem__patch">${ escapeHtml ( patch ) } </pre>` : "" ,
120- `<details class="pfem__details"><summary>${ escapeHtml ( getUiString ( "originalError" , "Original error" ) ) } </summary><pre>${ escapeHtml (
116+ has ( "title" ) ? `<div class="pfem__title">${ titleHtml } </div>` : "" ,
117+ has ( "summary" ) ? `<div class="pfem__summary">${ summaryHtml } </div>` : "" ,
118+ has ( "why" ) && whyHtml ? `<div class="pfem__why">${ whyHtml } </div>` : "" ,
119+ has ( "steps" ) && stepsHtml ?. length ? `<ul class="pfem__steps">${ stepsHtml . map ( ( s ) => `<li>${ s } </li>` ) . join ( "" ) } </ul>` : "" ,
120+ has ( "patch" ) && patch ? `<pre class="pfem__patch">${ escapeHtml ( patch ) } </pre>` : "" ,
121+ has ( "details" ) ? `<details class="pfem__details"><summary>${ escapeHtml ( getUiString ( "originalError" , "Original error" ) ) } </summary><pre>${ escapeHtml (
121122 ( trace . type || getUiString ( "error" , "Error" ) ) + ": " + trace . message
122- ) } </pre></details>`
123+ ) } </pre></details>` : "" ,
123124 ] . filter ( Boolean ) . join ( "\n" ) ;
124125
125126 return {
@@ -146,7 +147,7 @@ export const friendlyExplain = (opts: ExplainOptions): ExplainResult => {
146147 trace . codeLine = lines [ trace . line - 1 ] ?. trim ( ) ;
147148 }
148149
149- const chosen = pickVariant ( trace , code ) ;
150+ const chosen = pickVariant ( trace , code , opts . sections ) ;
150151 if ( ! chosen ) {
151152 return {
152153 trace,
0 commit comments