Skip to content
This repository has been archived by the owner on Nov 5, 2021. It is now read-only.

Keep fileName property of diagnostic objects and related information #74

Merged
merged 1 commit into from
Feb 23, 2021

Conversation

spahnke
Copy link
Contributor

@spahnke spahnke commented Feb 22, 2021

Using the code below in the Monaco playground I was wondering why the related information shows the wrong file. The diagnostic references the current editor model instead of the file where the class has already been declared.
grafik

This is caused by this helper function that clears the entire file object in order to avoid cyclic references during JSON serialization:

private static clearFiles(diagnostics: ts.Diagnostic[]): Diagnostic[] {
// Clear the `file` field, which cannot be JSON'yfied because it
// contains cyclic data structures.
diagnostics.forEach((diag) => {
diag.file = undefined;
const related = <ts.Diagnostic[]>diag.relatedInformation;
if (related) {
related.forEach((diag2) => (diag2.file = undefined));
}
});
return <Diagnostic[]>diagnostics;
}

If we instead clear everything of the file object but the fileName property, we get accurate diagnostics:
grafik

monaco.languages.typescript.javascriptDefaults.setDiagnosticsOptions({
	noSemanticValidation: false,
	noSyntaxValidation: false
});

monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
	target: monaco.languages.typescript.ScriptTarget.ES6,
	allowNonTsExtensions: true
});

// extra libraries
var libSource = [
	'declare class Facts {',
	'    /**',
	'     * Returns the next fact',
	'     */',
	'    static next():string',
	'}',
].join('\n');
var libUri = 'ts:filename/facts.d.ts';
monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);
// When resolving definitions and references, the editor will try to use created models.
// Creating a model for the library allows "peek definition/references" commands to work with the library.
monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(libUri));

monaco.editor.create(document.getElementById('container'), {
	value: 'class Facts {}',
	language: 'javascript'
});

@alexdima alexdima added this to the February 2021 milestone Feb 23, 2021
@alexdima
Copy link
Member

👍 Thank you!

@oldrich-svec
Copy link

I believe this change might have caused this bug:

microsoft/monaco-editor#2392

What I could understand is that the code tries to access "text" property on a diagnostic object but since there is only "fileName" and no "text" property, it crashes.

@spahnke
Copy link
Contributor Author

spahnke commented Mar 17, 2021

Thank you for the issue report! I opened #76 which should fix this.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants