Skip to content

refactor(compiler-cli): implement extended template diagnostics in preprocessor - #71316

Merged
crisbeto merged 2 commits into
angular:mainfrom
JeanMeche:ngp-extended-diagnostics
Oct 23, 2026
Merged

crisbeto merged 2 commits into
angular:mainfrom
JeanMeche:ngp-extended-diagnostics

Conversation

@JeanMeche

@JeanMeche JeanMeche commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

Stacked on top of #71313.

Summary

Implements AST-only and scope-aware extended template diagnostics in the NGP (packages/compiler-cli/preprocessor) pipeline:

  • Extended Template Diagnostics (ExtendedTemplateChecker):
    • deferTriggerMisconfiguration (NG8021)
    • invalidBananaInBox (NG8101)
    • missingControlFlowDirective (NG8103)
    • textAttributeNotBinding (NG8104)
    • missingNgForOfLet (NG8105)
    • suffixNotSupported (NG8106)
    • skipHydrationNotStatic (NG8108)
    • uninvokedFunctionInEventBinding (NG8111 — inline arrow/function expressions)
    • unusedLetDeclaration (NG8112)
    • unparenthesizedNullishCoalescing (NG8114)
    • missingStructuralDirective (NG8116)
  • Template Variable Write Semantics (checkTemplateVariableWrites):
    • Detects illegal writes to @let, @for, and template #ref / let- variables (NG8005).
  • Unused Standalone Imports (checkUnusedStandaloneImports):
    • Tracks resolved @Component.imports entries and spans in ng-analyze (Rust) and emits NG8113 diagnostics for unused standalone imports in tcb.ts.
  • Configuration Validation (verifyExtendedDiagnosticsOptions):
    • Validates extendedDiagnostics option compatibility (strictTemplates requirement, defaultCategory, and checks names/categories).
  • Shared Test Suite Updates:
    • Updates ngp_typecheck_testing.ts and removes newly passing tests from NGP_KNOWN_EXTENDED_CHECK_FAILURES and NGP_KNOWN_NGTSC_FAILURES.

@angular-robot angular-robot Bot added detected: feature PR contains a feature commit area: compiler Issues related to `ngc`, Angular's template compiler labels Oct 13, 2026
@ngbot ngbot Bot added this to the Backlog milestone Oct 13, 2026
@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch from dc273d2 to b771ec2 Compare October 13, 2026 01:31
@JeanMeche JeanMeche changed the title feat(compiler-cli): implement extended template diagnostics in preprocessor refactor(compiler-cli): implement extended template diagnostics in preprocessor Oct 13, 2026
@JeanMeche
JeanMeche requested a review from mattrbeck October 13, 2026 01:33
@JeanMeche
JeanMeche marked this pull request as ready for review October 13, 2026 01:33
@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch 6 times, most recently from 4f48156 to 79be6d0 Compare October 13, 2026 03:11

@mattrbeck mattrbeck left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'll review this more closely in the morning! I just wanted to add what Jetski came up with now in case it's actionable. Please take these comments with a grain of salt, though :p

@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch from 79be6d0 to 29a805f Compare October 13, 2026 13:10
@pullapprove
pullapprove Bot requested a review from josephperrott October 13, 2026 13:10
@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch 2 times, most recently from 9f7b427 to f0895b0 Compare October 13, 2026 14:26

@stewartmcgown stewartmcgown left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi! I've been working on our internal fork of oxc-angular-compiler so have a few suggestions here.

We'll be moving to your ngp + ngtsc setup soon, and have already had success with it! Great work.

Happy to share more on this if helpful cc @JeanMeche @mattlewis92

A few parity fixes and regression tests from our OXC/tsgo work. Longer term, I would share the TypeScript-independent check logic between ngtsc and NGP. The remaining type-dependent checks need semantic queries.

Comment on lines +121 to +142
function deduplicateDiagnostics(diagnostics: Diagnostic[]): Diagnostic[] {
const result: Diagnostic[] = [];

for (const diag of diagnostics) {
const isDuplicate = result.some((existingDiag) => areDiagnosticsEqual(diag, existingDiag));

if (!isDuplicate) {
result.push(diag);
}
}

return result;
}

function areDiagnosticsEqual(first: Diagnostic, second: Diagnostic): boolean {
return (
first.typeCheckId === second.typeCheckId &&
first.start === second.start &&
first.end === second.end &&
first.code === second.code
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This drops distinct warnings at the same span. For @defer (on immediate; on timer(100ms); prefetch on idle) { <div></div> }, ngtsc returns two NG8021 warnings, but this keeps only one. Include the message and category in the key, and use a Set to avoid the quadratic scan. Please add this combined-trigger regression case.

Suggested change
function deduplicateDiagnostics(diagnostics: Diagnostic[]): Diagnostic[] {
const result: Diagnostic[] = [];
for (const diag of diagnostics) {
const isDuplicate = result.some((existingDiag) => areDiagnosticsEqual(diag, existingDiag));
if (!isDuplicate) {
result.push(diag);
}
}
return result;
}
function areDiagnosticsEqual(first: Diagnostic, second: Diagnostic): boolean {
return (
first.typeCheckId === second.typeCheckId &&
first.start === second.start &&
first.end === second.end &&
first.code === second.code
);
}
function deduplicateDiagnostics(diagnostics: Diagnostic[]): Diagnostic[] {
const seen = new Set<string>();
return diagnostics.filter((diag) => {
const key = JSON.stringify([
diag.typeCheckId,
diag.sourceFilePath,
diag.start,
diag.end,
diag.code,
diag.category,
diag.message,
]);
if (seen.has(key)) {
return false;
}
seen.add(key);
return true;
});
}

}
const defaultCategory = options.extendedDiagnostics.defaultCategory;
const allCategories: readonly DiagnosticCategoryLabel[] = ['warning', 'error', 'suppress'];
if (defaultCategory && !allCategories.includes(defaultCategory)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

defaultCategory: "" passes this validation and then throws in diagnosticLabelToCategory. Validate every supplied value and return NG4004. Please cover empty strings and invalid JSON value types.

Suggested change
if (defaultCategory && !allCategories.includes(defaultCategory)) {
if (defaultCategory !== undefined && !allCategories.includes(defaultCategory)) {

Comment on lines +107 to +109
const unusedStandaloneImportsCat = diagnosticLabelToCategory(
options?.checks?.unusedStandaloneImports ?? options?.defaultCategory ?? 'warning',
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pass the resolved config.unusedStandaloneImports from tcb.ts into this function and convert that value here. Recomputing it from extendedDiagnostics ignores a direct TCB config override such as unusedStandaloneImports: 'suppress'.

Add a DiagnosticCategoryLabel parameter, forward config.unusedStandaloneImports at the call site, and replace this expression with:

const unusedStandaloneImportsCat = diagnosticLabelToCategory(unusedStandaloneImports);

Please test direct warning, error, and suppress overrides.

readonly message: string;
readonly start: number;
readonly end: number;
readonly sourceFilePath?: string;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

NG8005 currently loses ngtsc's "'variable' is declared here" related location. Keep it as owned data, then thread it through makeTemplateDiagnostic, the semantics checker, processor mapping, and diagnostic serialization/conversion. An omitted source path should mean template-relative offsets; an explicit path should mean source-file offsets.

Suggested change
readonly sourceFilePath?: string;
readonly sourceFilePath?: string;
readonly relatedInformation?: readonly {
readonly message: string;
readonly start: number;
readonly end: number;
readonly sourceFilePath?: string;
}[];

);
});

it('NG8113: unusedStandaloneImports reports all unused or individual unused imports', async () => {

@stewartmcgown stewartmcgown Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

maybe needs an edit/fix parity test that reuses the compiler across used -> unused -> used imports, then compares each result with fresh compilation. Also cover selector changes, barrel exports, and diagnostic configuration changes. Include inline/external templates and Unicode before the reported span. Assert code, category, message, file, span, and related information.

@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch 9 times, most recently from e141aec to 7e61f3e Compare October 19, 2026 18:19
@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch from 7e61f3e to eddb0c6 Compare October 21, 2026 22:42
@JeanMeche
JeanMeche requested a review from mattrbeck October 21, 2026 22:42
@JeanMeche
JeanMeche requested review from crisbeto and removed request for josephperrott October 21, 2026 22:42
Comment on lines +816 to +848
Entry::Evaluated(_, evaluated_in_file) => {
if *evaluated_in_file == reference_id.file {
let mut found = None;
for (idx, imp) in
component.parsed_imports[parsed_cursor..].iter().enumerate()
{
let matches = if imp.import_source.is_none() {
reference.file == reference_id.file
&& imp.local_name == class_info.class_name
} else {
reference.aliases.get(&reference_id.file)
== Some(&imp.local_name)
};
if matches {
parsed_cursor += idx + 1;
found = Some(imp);
break;
}
}
found
} else {
None
}
}
};

let (is_potential_shared, usage_span) = match matched_import_info {
Some(imp) if imp.in_decorator => (false, Some(imp.span)),
Some(imp) if imp.import_source.is_some() && !imp.in_exported_variable => {
(false, Some(imp.span))
}
_ => (true, None),
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

✨ Could we use ValueReference.identifiers (identity_in_expression) directly here instead of matching evaluated imports back against component.parsed_imports with parsed_cursor?

The evaluator already records identifier spans on ValueReference the same way ngtsc does, whereas matching against parsed_imports (ImportsCollector) diverges from ngtsc in a couple of cases:

  1. Single-directive variables (const Alias = UnusedDir; imports: [UsedDir, Alias]): ngtsc reports NG8113 on Alias in imports (even if Alias is exported or UnusedDir is in the same file), whereas ImportsCollector unwraps Alias to its const declaration and suppresses NG8113 when exported or same-file.
  2. Expressions evaluated by the interpreter but skipped by ImportsCollector (imports: [cond ? UsedDir : OtherDir, UnusedDir, UsedDir]): parsed_cursor scans past UnusedDir to match the second UsedDir, causing UnusedDir to be skipped.

…eprocessor

Implements AST-only and scope-aware extended template diagnostics,
template variable write checks, and unused standalone import checks
in the NGP preprocessor.
@JeanMeche
JeanMeche force-pushed the ngp-extended-diagnostics branch from eddb0c6 to 02f45c8 Compare October 23, 2026 00:18
@JeanMeche
JeanMeche removed the request for review from crisbeto October 23, 2026 00:46
@JeanMeche JeanMeche added action: merge The PR is ready for merge by the caretaker target: minor This PR is targeted for the next minor release labels Oct 23, 2026
@crisbeto
crisbeto merged commit 970bbc7 into angular:main Oct 23, 2026
25 of 27 checks passed
@crisbeto

Copy link
Copy Markdown
Member

This PR was merged into the repository. The changes were merged into the following branches:

@JeanMeche
JeanMeche deleted the ngp-extended-diagnostics branch October 25, 2026 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: compiler Issues related to `ngc`, Angular's template compiler detected: feature PR contains a feature commit target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants