Skip to content

Commit 59798ff

Browse files
authored
Trim references to shadow positions from diagnostics (#306)
1 parent 0b3339d commit 59798ff

File tree

4 files changed

+53
-83
lines changed

4 files changed

+53
-83
lines changed

.changeset/shadow-diagnostics.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
vscode-mdx: patch
3+
---
4+
5+
Trim positions that can’t be mapped to the original MDX source code from diagnostics.

packages/language-server/tests/diagnostics.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,23 @@ test('type errors', async () => {
9090
]
9191
})
9292
})
93+
94+
test('does not resolve shadow content', async () => {
95+
await connection.sendRequest(InitializeRequest.type, {
96+
processId: null,
97+
rootUri: null,
98+
capabilities: {}
99+
})
100+
101+
const diagnosticsPromise = waitForDiagnostics(connection)
102+
const textDocument = await openTextDocument(
103+
connection,
104+
'node16/link-reference.mdx'
105+
)
106+
const diagnostics = await diagnosticsPromise
107+
108+
assert.deepEqual(diagnostics, {
109+
uri: textDocument.uri,
110+
diagnostics: []
111+
})
112+
})

packages/language-service/lib/index.js

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
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'),

packages/monaco/tests/diagnostics.test.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,3 @@ test('syntax errors', async ({editor}) => {
2323
}
2424
])
2525
})
26-
27-
test('ESM errors', async ({editor}) => {
28-
const result = await editor.waitForMarkers('file:///a.mdx', async () => {
29-
await editor.createModel(
30-
'export const foo = /** @type {boolean} */ (42)',
31-
'file:///a.mdx'
32-
)
33-
})
34-
expect(result).toStrictEqual([
35-
{
36-
code: '6133',
37-
endColumn: 6,
38-
endLineNumber: 1,
39-
message: "'props' is declared but its value is never read.",
40-
owner: 'mdx',
41-
relatedInformation: [],
42-
resource: 'file:///a.mdx',
43-
severity: 1,
44-
source: undefined,
45-
startColumn: 1,
46-
startLineNumber: 1,
47-
tags: [1]
48-
}
49-
])
50-
})
51-
52-
test('JSX errors', async ({editor}) => {
53-
const result = await editor.waitForMarkers('file:///a.mdx', async () => {
54-
await editor.createModel('{/** @type {boolean} */ (42)}', 'file:///a.mdx')
55-
})
56-
expect(result).toStrictEqual([
57-
{
58-
code: '6133',
59-
endColumn: 6,
60-
endLineNumber: 1,
61-
message: "'props' is declared but its value is never read.",
62-
owner: 'mdx',
63-
relatedInformation: [],
64-
resource: 'file:///a.mdx',
65-
severity: 1,
66-
source: undefined,
67-
startColumn: 1,
68-
startLineNumber: 1,
69-
tags: [1]
70-
}
71-
])
72-
})

0 commit comments

Comments
 (0)