'use strict' ; import * as FS from 'fs' ; import * as VSCode from 'vscode' ; import { ShowRuleDescriptionParams } from './protocol' ; import { ResourceResolver } from './webview' ; export function computeRuleDescPanelContent ( context , webview , rule ) { const resolver = new ResourceResolver ( context , webview ) ; const styleSrc = resolver . resolve ( 'styles' , 'rule.css' ) ; const severityImgSrc = resolver . resolve ( 'images' , 'severity' , `${ rule . severity . toLowerCase ( ) }.png` ) ; const typeImgSrc = resolver . resolve ( 'images' , 'type' , `${ rule . type . toLowerCase ( ) }.png` ) ; const ruleParamsHtml = renderRuleParams ( rule ) ; return `<!doctype html><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <meta http-equiv="Encoding" content="utf-8" />
    <meta http-equiv="Content-Security-Policy"
      content="default-src 'none'; img-src ${ webview . cspSource }; style-src ${ webview . cspSource }"/>
    <link rel="stylesheet" type="text/css" href="${ styleSrc }" />
    </head>
    <body><h1><big>${ escapeHtml ( rule . name ) }</big> (${ rule . key })</h1>
    <div>
    <img class="type" alt="${ rule . type }" src="${ typeImgSrc }" />&nbsp;
    ${ clean ( rule . type ) }&nbsp;
    <img class="severity" alt="${ rule . severity }" src="${ severityImgSrc }" />&nbsp;
    ${ clean ( rule . severity ) }
    </div>
    <div class="rule-desc">${ rule . htmlDescription }</div>
    ${ ruleParamsHtml }
    </body></html>` ; } const entityMap = { '&' : '&amp;' , '<' : '&lt;' , '>' : '&gt;' , '"' : '&quot;' , "'" : '&#39;' , '/' : '&#x2F;' , '`' : '&#x60;' , '=' : '&#x3D;' } ; function escapeHtml ( str ) { return String ( str ) . replace ( /[&<>""`=\/]/g , function ( s ) { return entityMap [ s ] ; } ) ; } function clean ( str ) { return capitalizeName ( str . toLowerCase ( ) . split ( '_' ) . join ( ' ' ) ) ; } function capitalizeName ( name ) { return name . replace ( /\b(\w)/g , s => s . toUpperCase ( ) ) ; } function base64encode ( file ) { return FS . readFileSync ( file ) . toString ( 'base64' ) ; } function renderRuleParams ( rule ) { if ( rule . parameters && rule . parameters . length > 0 ) { const ruleParamsConfig = VSCode . workspace . getConfiguration ( `sonarlint.rules.${ rule . key }.parameters` ) ; return `<table class="rule-params">
  <caption>Parameters</caption>
  <thead>
    <tr>
      <td colspan="2">
        Following parameter values can be set in the <em>SonarLint:Rules</em> user settings.
        In connected mode, server side configuration overrides local settings.
      </td>
    </tr>
  </thead>
  <tbody>
    ${ rule . parameters . map ( p => renderRuleParam ( p , ruleParamsConfig ) ) . join ( '\n' ) }
  </tbody>
</table>` ; } else { return '' ; } } function renderRuleParam ( param , config ) { const { name , description , defaultValue } = param ; const descriptionP = description ? `<p>${ description }</p>` : '' ; const currentValue = config . has ( name ) ? `<small>Current value: <code>${ config . get ( name ) }</code></small>` : '' ; const defaultRendered = defaultValue ? `<small>(Default value: <code>${ defaultValue }</code>)</small>` : '' ; return `<tr>
  <th>${ name }</th>
  <td>
    ${ descriptionP }
    ${ currentValue }
    ${ defaultRendered }
  </td>
</tr>` ; }