Skip to content

Add type for diagnostics where location is defined #23686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
5 commits merged into from
May 22, 2018
Merged

Conversation

ghost
Copy link

@ghost ghost commented Apr 25, 2018

Related to #22088 -- this would allow us to access diag.file without an undefined check if it came from the diagnostics of a source file instead of global diagnostics.

@ghost ghost force-pushed the diagnosticWithLocation branch from 17ac500 to 0301fed Compare April 25, 2018 19:14
@ghost ghost requested a review from rbuckton April 25, 2018 19:16
@@ -1300,14 +1300,15 @@ namespace ts {
const includeBindAndCheckDiagnostics = sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX ||
sourceFile.scriptKind === ScriptKind.External || isCheckJs;
const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) : emptyArray;
// TODO: GH#18217
const checkDiagnostics = includeBindAndCheckDiagnostics ? typeChecker.getDiagnostics(sourceFile, cancellationToken) as ReadonlyArray<DiagnosticWithLocation> : emptyArray;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are asking for diagnostics for a particular file. But getDiagnostics in checker.ts seems to add global diagnostics in some cases. @rbuckton Do you know how this works?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you are missing some of the built in types like Array for instance, that is a global diagnostic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @mhegazy said. We add those only when they are first requested.

if (file && isSourceFileJavaScript(file)) {
return []; // No declaration diagnostics for js for now
}
const compilerOptions = host.getCompilerOptions();
const result = transformNodes(resolver, host, compilerOptions, file ? [file] : filter(host.getSourceFiles(), isSourceFileNotJavaScript), [transformDeclarations], /*allowDtsFiles*/ false);
return result.diagnostics;
return result.diagnostics as DiagnosticWithLocation[]; // TODO: GH#18217
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these all should have a location.. can we push that into TransformationResult

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weswigham can you take a look as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should all have a location.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, every addDiagnostic call uses createDiagnsoticForNode, which will always have a location - the type of addDiagnsotic on TransformationContext and diagnostics on TransformationResult can just be changed.

@mhegazy mhegazy requested a review from weswigham April 25, 2018 20:45
Copy link
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit, plus my comment from before about changing a few more types rather than using a case, but otherwise looks good.

for (const diag of program.getSemanticDiagnostics(sourceFile).concat(computeSuggestionDiagnostics(sourceFile, program))) {
if (contains(errorCodes, diag.code)) {
cb(diag);
cb(diag as Diagnostic & { file: SourceFile });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this cast be DiagnosticWithLocation?

@ghost ghost force-pushed the diagnosticWithLocation branch from 6d23224 to 7ade816 Compare May 18, 2018 18:35
@@ -1403,15 +1403,15 @@ namespace ts {
// By default, only type-check .ts, .tsx, 'Deferred' and 'External' files (external files are added by plugins)
const includeBindAndCheckDiagnostics = sourceFile.scriptKind === ScriptKind.TS || sourceFile.scriptKind === ScriptKind.TSX ||
sourceFile.scriptKind === ScriptKind.External || isCheckJs || sourceFile.scriptKind === ScriptKind.Deferred;
const bindDiagnostics = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
const bindDiagnostics: ReadonlyArray<Diagnostic> = includeBindAndCheckDiagnostics ? sourceFile.bindDiagnostics : emptyArray;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you instead use concatenate on the diagnostics instead of using empty array assignment to sub arrays to be concatenated, that way multiple emptyArray concatenate wont create new array with length 0 but use empty array in the end?

Copy link
Author

@ghost ghost May 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emptyArray is a const and not a function call -- it doesn't create a new empty array, it just refers to a global empty array. So shouldn't be any issue of too many allocations.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ghost ghost force-pushed the diagnosticWithLocation branch from 4f09020 to 919cfc0 Compare May 18, 2018 20:09
@ghost
Copy link
Author

ghost commented May 18, 2018

@sheetalkamat Good to go?

@ghost ghost merged commit 7106a58 into master May 22, 2018
@ghost ghost deleted the diagnosticWithLocation branch May 22, 2018 18:01
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
This pull request was closed.
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.

4 participants