Skip to content

Commit 33fe252

Browse files
crisbetoalxhub
authored andcommitted
fix(compiler-cli): do not report unused declarations coming from an imported array (#57940)
Some apps follow a pattern where they have an array of common declarations which is imported in most standalone components, but only some of the declarations are used. Such cases will currently raise the unused imports diagnostic but can be hard to fix, because it would require either removing declarations from the common array which can break other components, or copying only the necessary declarations from the array. Since neither of these solutions is great, this commit tweaks the logic for the diagnostic so that unused imports coming from _exported_ arrays are not reported (either from the same file or another one). PR Close #57940
1 parent 2545743 commit 33fe252

2 files changed

Lines changed: 350 additions & 4 deletions

File tree

‎packages/compiler-cli/src/ngtsc/validation/src/rules/unused_standalone_imports_rule.ts‎

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,24 @@ export class UnusedStandaloneImportsRule implements SourceFileValidatorRule {
105105
usedDirectives: Set<ts.ClassDeclaration>,
106106
usedPipes: Set<string>,
107107
) {
108-
if (metadata.imports === null || metadata.rawImports === null) {
108+
const {imports, rawImports} = metadata;
109+
110+
if (imports === null || rawImports === null) {
109111
return null;
110112
}
111113

112114
let unused: [ref: Reference, type: string, name: string][] | null = null;
113115

114-
for (const current of metadata.imports) {
116+
for (const current of imports) {
115117
const currentNode = current.node as ts.ClassDeclaration;
116118
const dirMeta = this.templateTypeChecker.getDirectiveMetadata(currentNode);
117119

118120
if (dirMeta !== null) {
119-
if (dirMeta.isStandalone && (usedDirectives === null || !usedDirectives.has(currentNode))) {
121+
if (
122+
dirMeta.isStandalone &&
123+
!usedDirectives.has(currentNode) &&
124+
!this.isPotentialSharedReference(current, rawImports)
125+
) {
120126
unused ??= [];
121127
unused.push([current, dirMeta.isComponent ? 'Component' : 'Directive', dirMeta.name]);
122128
}
@@ -128,7 +134,8 @@ export class UnusedStandaloneImportsRule implements SourceFileValidatorRule {
128134
if (
129135
pipeMeta !== null &&
130136
pipeMeta.isStandalone &&
131-
(usedPipes === null || !usedPipes.has(pipeMeta.name))
137+
!usedPipes.has(pipeMeta.name) &&
138+
!this.isPotentialSharedReference(current, rawImports)
132139
) {
133140
unused ??= [];
134141
unused.push([current, 'Pipe', pipeMeta.ref.node.name.text]);
@@ -137,4 +144,35 @@ export class UnusedStandaloneImportsRule implements SourceFileValidatorRule {
137144

138145
return unused;
139146
}
147+
148+
/**
149+
* Determines if an import reference *might* be coming from a shared imports array.
150+
* @param reference Reference to be checked.
151+
* @param rawImports AST node that defines the `imports` array.
152+
*/
153+
private isPotentialSharedReference(reference: Reference, rawImports: ts.Expression): boolean {
154+
// If the reference is defined directly in the `imports` array, it cannot be shared.
155+
if (reference.getIdentityInExpression(rawImports) !== null) {
156+
return false;
157+
}
158+
159+
// The reference might be shared if it comes from an exported array. If the variable is local
160+
/// to the file, then it likely isn't shared. Note that this has the potential for false
161+
// positives if a non-exported array of imports is shared between components in the same
162+
// file. This scenario is unlikely and even if we report the diagnostic for it, it would be
163+
// okay since the user only has to refactor components within the same file, rather than the
164+
// entire application.
165+
let current: ts.Node | null = reference.getIdentityIn(rawImports.getSourceFile());
166+
167+
while (current !== null) {
168+
if (ts.isVariableStatement(current)) {
169+
return !!current.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
170+
}
171+
current = current.parent;
172+
}
173+
174+
// Otherwise the reference likely comes from an imported
175+
// symbol like an array of shared common components.
176+
return true;
177+
}
140178
}

‎packages/compiler-cli/test/ngtsc/template_typecheck_spec.ts‎

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7765,6 +7765,314 @@ suppress
77657765
'Pipe "PercentPipe" is not used within the template',
77667766
);
77677767
});
7768+
7769+
it('should report unused imports coming from a nested array from the same file', () => {
7770+
env.write(
7771+
'used.ts',
7772+
`
7773+
import {Directive} from '@angular/core';
7774+
7775+
@Directive({selector: '[used]', standalone: true})
7776+
export class UsedDir {}
7777+
`,
7778+
);
7779+
7780+
env.write(
7781+
'other-used.ts',
7782+
`
7783+
import {Directive} from '@angular/core';
7784+
7785+
@Directive({selector: '[other-used]', standalone: true})
7786+
export class OtherUsedDir {}
7787+
`,
7788+
);
7789+
7790+
env.write(
7791+
'unused.ts',
7792+
`
7793+
import {Directive} from '@angular/core';
7794+
7795+
@Directive({selector: '[unused]', standalone: true})
7796+
export class UnusedDir {}
7797+
`,
7798+
);
7799+
7800+
env.write(
7801+
'test.ts',
7802+
`
7803+
import {Component} from '@angular/core';
7804+
import {UsedDir} from './used';
7805+
import {OtherUsedDir} from './other-used';
7806+
import {UnusedDir} from './unused';
7807+
7808+
const COMMON = [OtherUsedDir, UnusedDir];
7809+
7810+
@Component({
7811+
template: \`
7812+
<section>
7813+
<div other-used></div>
7814+
<span used></span>
7815+
</section>
7816+
\`,
7817+
standalone: true,
7818+
imports: [UsedDir, COMMON]
7819+
})
7820+
export class MyComp {}
7821+
`,
7822+
);
7823+
7824+
const diags = env.driveDiagnostics();
7825+
expect(diags.length).toBe(1);
7826+
expect(diags[0].messageText).toBe('Imports array contains unused imports');
7827+
expect(diags[0].relatedInformation?.length).toBe(1);
7828+
expect(diags[0].relatedInformation![0].messageText).toBe(
7829+
'Directive "UnusedDir" is not used within the template',
7830+
);
7831+
});
7832+
7833+
it('should report unused imports coming from an array used as the `imports` initializer', () => {
7834+
env.write(
7835+
'used.ts',
7836+
`
7837+
import {Directive} from '@angular/core';
7838+
7839+
@Directive({selector: '[used]', standalone: true})
7840+
export class UsedDir {}
7841+
`,
7842+
);
7843+
7844+
env.write(
7845+
'unused.ts',
7846+
`
7847+
import {Directive} from '@angular/core';
7848+
7849+
@Directive({selector: '[unused]', standalone: true})
7850+
export class UnusedDir {}
7851+
`,
7852+
);
7853+
7854+
env.write(
7855+
'test.ts',
7856+
`
7857+
import {Component} from '@angular/core';
7858+
import {UsedDir} from './used';
7859+
import {UnusedDir} from './unused';
7860+
7861+
const IMPORTS = [UsedDir, UnusedDir];
7862+
7863+
@Component({
7864+
template: \`
7865+
<section>
7866+
<div></div>
7867+
<span used></span>
7868+
</section>
7869+
\`,
7870+
standalone: true,
7871+
imports: IMPORTS
7872+
})
7873+
export class MyComp {}
7874+
`,
7875+
);
7876+
7877+
const diags = env.driveDiagnostics();
7878+
expect(diags.length).toBe(1);
7879+
expect(diags[0].messageText).toBe('Imports array contains unused imports');
7880+
expect(diags[0].relatedInformation?.length).toBe(1);
7881+
expect(diags[0].relatedInformation![0].messageText).toBe(
7882+
'Directive "UnusedDir" is not used within the template',
7883+
);
7884+
});
7885+
7886+
it('should not report unused imports coming from an array through a spread expression from a different file', () => {
7887+
env.write(
7888+
'used.ts',
7889+
`
7890+
import {Directive} from '@angular/core';
7891+
7892+
@Directive({selector: '[used]', standalone: true})
7893+
export class UsedDir {}
7894+
`,
7895+
);
7896+
7897+
env.write(
7898+
'other-used.ts',
7899+
`
7900+
import {Directive} from '@angular/core';
7901+
7902+
@Directive({selector: '[other-used]', standalone: true})
7903+
export class OtherUsedDir {}
7904+
`,
7905+
);
7906+
7907+
env.write(
7908+
'unused.ts',
7909+
`
7910+
import {Directive} from '@angular/core';
7911+
7912+
@Directive({selector: '[unused]', standalone: true})
7913+
export class UnusedDir {}
7914+
`,
7915+
);
7916+
7917+
env.write(
7918+
'common.ts',
7919+
`
7920+
import {OtherUsedDir} from './other-used';
7921+
import {UnusedDir} from './unused';
7922+
7923+
export const COMMON = [OtherUsedDir, UnusedDir];
7924+
`,
7925+
);
7926+
7927+
env.write(
7928+
'test.ts',
7929+
`
7930+
import {Component} from '@angular/core';
7931+
import {UsedDir} from './used';
7932+
import {COMMON} from './common';
7933+
7934+
@Component({
7935+
template: \`
7936+
<section>
7937+
<div other-used></div>
7938+
<span used></span>
7939+
</section>
7940+
\`,
7941+
standalone: true,
7942+
imports: [UsedDir, ...COMMON]
7943+
})
7944+
export class MyComp {}
7945+
`,
7946+
);
7947+
7948+
const diags = env.driveDiagnostics();
7949+
expect(diags.length).toBe(0);
7950+
});
7951+
7952+
it('should not report unused imports coming from a nested array from a different file', () => {
7953+
env.write(
7954+
'used.ts',
7955+
`
7956+
import {Directive} from '@angular/core';
7957+
7958+
@Directive({selector: '[used]', standalone: true})
7959+
export class UsedDir {}
7960+
`,
7961+
);
7962+
7963+
env.write(
7964+
'other-used.ts',
7965+
`
7966+
import {Directive} from '@angular/core';
7967+
7968+
@Directive({selector: '[other-used]', standalone: true})
7969+
export class OtherUsedDir {}
7970+
`,
7971+
);
7972+
7973+
env.write(
7974+
'unused.ts',
7975+
`
7976+
import {Directive} from '@angular/core';
7977+
7978+
@Directive({selector: '[unused]', standalone: true})
7979+
export class UnusedDir {}
7980+
`,
7981+
);
7982+
7983+
env.write(
7984+
'common.ts',
7985+
`
7986+
import {OtherUsedDir} from './other-used';
7987+
import {UnusedDir} from './unused';
7988+
7989+
export const COMMON = [OtherUsedDir, UnusedDir];
7990+
`,
7991+
);
7992+
7993+
env.write(
7994+
'test.ts',
7995+
`
7996+
import {Component} from '@angular/core';
7997+
import {UsedDir} from './used';
7998+
import {COMMON} from './common';
7999+
8000+
@Component({
8001+
template: \`
8002+
<section>
8003+
<div other-used></div>
8004+
<span used></span>
8005+
</section>
8006+
\`,
8007+
standalone: true,
8008+
imports: [UsedDir, COMMON]
8009+
})
8010+
export class MyComp {}
8011+
`,
8012+
);
8013+
8014+
const diags = env.driveDiagnostics();
8015+
expect(diags.length).toBe(0);
8016+
});
8017+
8018+
it('should not report unused imports coming from an exported array in the same file', () => {
8019+
env.write(
8020+
'used.ts',
8021+
`
8022+
import {Directive} from '@angular/core';
8023+
8024+
@Directive({selector: '[used]', standalone: true})
8025+
export class UsedDir {}
8026+
`,
8027+
);
8028+
8029+
env.write(
8030+
'other-used.ts',
8031+
`
8032+
import {Directive} from '@angular/core';
8033+
8034+
@Directive({selector: '[other-used]', standalone: true})
8035+
export class OtherUsedDir {}
8036+
`,
8037+
);
8038+
8039+
env.write(
8040+
'unused.ts',
8041+
`
8042+
import {Directive} from '@angular/core';
8043+
8044+
@Directive({selector: '[unused]', standalone: true})
8045+
export class UnusedDir {}
8046+
`,
8047+
);
8048+
8049+
env.write(
8050+
'test.ts',
8051+
`
8052+
import {Component} from '@angular/core';
8053+
import {UsedDir} from './used';
8054+
import {OtherUsedDir} from './other-used';
8055+
import {UnusedDir} from './unused';
8056+
8057+
export const COMMON = [OtherUsedDir, UnusedDir];
8058+
8059+
@Component({
8060+
template: \`
8061+
<section>
8062+
<div other-used></div>
8063+
<span used></span>
8064+
</section>
8065+
\`,
8066+
standalone: true,
8067+
imports: [UsedDir, COMMON]
8068+
})
8069+
export class MyComp {}
8070+
`,
8071+
);
8072+
8073+
const diags = env.driveDiagnostics();
8074+
expect(diags.length).toBe(0);
8075+
});
77688076
});
77698077
});
77708078
});

0 commit comments

Comments
 (0)