Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ivy): don't crash on an unknown localref target (#33454)
Previously the template binder would crash when encountering an unknown localref (# reference) such as `<div #ref="foo">` when no directive has `exportAs: "foo"`. With this commit, the compiler instead generates a template diagnostic error informing the user about the invalid reference. PR Close #33454
- Loading branch information
Showing
9 changed files
with
161 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @license | ||
* Copyright Google Inc. All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {TmplAstReference} from '@angular/compiler'; | ||
import * as ts from 'typescript'; | ||
|
||
import {ErrorCode, ngErrorCode} from '../../diagnostics'; | ||
|
||
import {TcbSourceResolver, makeTemplateDiagnostic} from './diagnostics'; | ||
|
||
/** | ||
* Collects `ts.Diagnostic`s on problems which occur in the template which aren't directly sourced | ||
* from Type Check Blocks. | ||
* | ||
* During the creation of a Type Check Block, the template is traversed and the | ||
* `OutOfBandDiagnosticRecorder` is called to record cases when a correct interpretation for the | ||
* template cannot be found. These operations create `ts.Diagnostic`s which are stored by the | ||
* recorder for later display. | ||
*/ | ||
export interface OutOfBandDiagnosticRecorder { | ||
readonly diagnostics: ReadonlyArray<ts.Diagnostic>; | ||
|
||
/** | ||
* Reports a `#ref="target"` expression in the template for which a target directive could not be | ||
* found. | ||
* | ||
* @param templateId the template type-checking ID of the template which contains the broken | ||
* reference. | ||
* @param ref the `TmplAstReference` which could not be matched to a directive. | ||
*/ | ||
missingReferenceTarget(templateId: string, ref: TmplAstReference): void; | ||
} | ||
|
||
export class OutOfBandDiagnosticRecorderImpl implements OutOfBandDiagnosticRecorder { | ||
private _diagnostics: ts.Diagnostic[] = []; | ||
|
||
constructor(private resolver: TcbSourceResolver) {} | ||
|
||
get diagnostics(): ReadonlyArray<ts.Diagnostic> { return this._diagnostics; } | ||
|
||
missingReferenceTarget(templateId: string, ref: TmplAstReference): void { | ||
const mapping = this.resolver.getSourceMapping(templateId); | ||
const value = ref.value.trim(); | ||
|
||
const errorMsg = `No directive found with exportAs '${value}'.`; | ||
this._diagnostics.push(makeTemplateDiagnostic( | ||
mapping, ref.valueSpan || ref.sourceSpan, ts.DiagnosticCategory.Error, | ||
ngErrorCode(ErrorCode.MISSING_REFERENCE_TARGET), errorMsg)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters