11/**
2- * @typedef {import('mdast').Root } Root
32 * @typedef {import('typescript').Diagnostic } Diagnostic
4- * @typedef {import('typescript').DiagnosticWithLocation } DiagnosticWithLocation
53 * @typedef {import('typescript').DocumentSpan } DocumentSpan
6- * @typedef {import('typescript').IScriptSnapshot } IScriptSnapshot
74 * @typedef {import('typescript').LanguageService } LanguageService
85 * @typedef {import('typescript').LanguageServiceHost } LanguageServiceHost
96 * @typedef {import('typescript').NavigationBarItem } NavigationBarItem
@@ -371,32 +368,37 @@ export function createMdxLanguageService(ts, host, plugins) {
371368 }
372369
373370 /**
374- * @param {Diagnostic } diagnostic
371+ * @template {Diagnostic} T
372+ * @param {readonly T[] } diagnostics
373+ * @returns {T[] }
375374 */
376- function patchDiagnostic ( diagnostic ) {
377- const fileName = diagnostic . file ?. fileName
378- if ( ! fileName || ! isMdx ( fileName ) ) {
379- return
380- }
375+ function patchDiagnostics ( diagnostics ) {
376+ /** @type {T[] } */
377+ const result = [ ]
381378
382- const snapshot = getMdxSnapshot ( fileName )
379+ for ( const diagnostic of diagnostics ) {
380+ const fileName = diagnostic . file ?. fileName
381+ if ( ! fileName || ! isMdx ( fileName ) ) {
382+ result . push ( diagnostic )
383+ continue
384+ }
383385
384- if ( ! snapshot ) {
385- return
386- }
386+ const snapshot = getMdxSnapshot ( fileName )
387387
388- if ( diagnostic . start !== undefined ) {
389- diagnostic . start = snapshot . getRealPosition ( diagnostic . start )
390- }
391- }
388+ if ( ! snapshot ) {
389+ continue
390+ }
392391
393- /**
394- * @param {DiagnosticWithLocation[] } diagnostics
395- */
396- function patchDiagnosticsWithLocation ( diagnostics ) {
397- for ( const diagnostic of diagnostics ) {
398- patchDiagnostic ( diagnostic )
392+ if ( diagnostic . start === undefined ) {
393+ result . push ( diagnostic )
394+ }
395+
396+ if ( patchTextSpan ( snapshot , /** @type {TextSpan } */ ( diagnostic ) ) ) {
397+ result . push ( diagnostic )
398+ }
399399 }
400+
401+ return result
400402 }
401403
402404 return {
@@ -794,11 +796,7 @@ export function createMdxLanguageService(ts, host, plugins) {
794796 syncSnapshot ( fileName )
795797 const diagnostics = ls . getSemanticDiagnostics ( fileName )
796798
797- for ( const diagnostic of diagnostics ) {
798- patchDiagnostic ( diagnostic )
799- }
800-
801- return diagnostics
799+ return patchDiagnostics ( diagnostics )
802800 } ,
803801
804802 getSignatureHelpItems : notImplemented ( 'getSignatureHelpItems' ) ,
@@ -809,11 +807,7 @@ export function createMdxLanguageService(ts, host, plugins) {
809807 syncSnapshot ( fileName )
810808 const diagnostics = ls . getSuggestionDiagnostics ( fileName )
811809
812- for ( const diagnostic of diagnostics ) {
813- patchDiagnostic ( diagnostic )
814- }
815-
816- return diagnostics
810+ return patchDiagnostics ( diagnostics )
817811 } ,
818812
819813 getSyntacticClassifications : notImplemented ( 'getSyntacticClassifications' ) ,
@@ -826,9 +820,7 @@ export function createMdxLanguageService(ts, host, plugins) {
826820
827821 const diagnostics = ls . getSyntacticDiagnostics ( fileName )
828822
829- patchDiagnosticsWithLocation ( diagnostics )
830-
831- return diagnostics
823+ return patchDiagnostics ( diagnostics )
832824 } ,
833825
834826 getTodoComments : notImplemented ( 'getTodoComments' ) ,
0 commit comments