Skip to content

Commit f39cb06

Browse files
pmvaldthePunderWoman
authored andcommittedFeb 6, 2024·
fix(compiler-cli): show specific error for unresolved @Directive.exportAs in local compilation mode (#54230)
Currently the error is a generic error "exportAs must be a string ...". This commit makes the error more specific to local compilation and adds some action items. PR Close #54230
1 parent f3851b5 commit f39cb06

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
 

‎packages/compiler-cli/src/ngtsc/annotations/directive/src/shared.ts

+8
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ export function extractDirectiveMetadata(
161161
if (directive.has('exportAs')) {
162162
const expr = directive.get('exportAs')!;
163163
const resolved = evaluator.evaluate(expr);
164+
165+
assertLocalCompilationUnresolvedConst(compilationMode, resolved, null,
166+
'Unresolved identifier found for exportAs field! Did you import this ' +
167+
'identifier from a file outside of the compilation unit? This is not ' +
168+
'allowed when Angular compiler runs in local mode. Possible solutions: ' +
169+
'1) Move the declarations into a file within the compilation unit, ' +
170+
'2) Inline the selector');
171+
164172
if (typeof resolved !== 'string') {
165173
throw createValueHasWrongTypeError(expr, resolved, `exportAs must be a string`);
166174
}

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

+29
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,35 @@ runInEachFileSystem(
14431443
`Unresolved identifier found for @HostBinding's argument! Did you import this identifier from a file outside of the compilation unit? This is not allowed when Angular compiler runs in local mode. Possible solutions: 1) Move the declaration into a file within the compilation unit, 2) Inline the argument`
14441444
);
14451445
});
1446+
1447+
it('should show correct error message when using an external symbol for @Directive.exportAs argument',
1448+
() => {
1449+
env.write('test.ts', `
1450+
import {Directive} from '@angular/core';
1451+
import {ExternalString} from './some-where';
1452+
1453+
@Directive({selector: '[test]', exportAs: ExternalString})
1454+
export class Main {
1455+
}
1456+
`);
1457+
1458+
const errors = env.driveDiagnostics();
1459+
1460+
expect(errors.length).toBe(1);
1461+
1462+
const {code, messageText, relatedInformation, length} = errors[0];
1463+
1464+
expect(code).toBe(
1465+
ngErrorCode(ErrorCode.LOCAL_COMPILATION_UNRESOLVED_CONST));
1466+
expect(length).toBe(14),
1467+
expect(relatedInformation).toBeUndefined();
1468+
1469+
const text = ts.flattenDiagnosticMessageText(messageText, '\n');
1470+
1471+
expect(text).toEqual(
1472+
`Unresolved identifier found for exportAs field! Did you import this identifier from a file outside of the compilation unit? This is not allowed when Angular compiler runs in local mode. Possible solutions: 1) Move the declarations into a file within the compilation unit, 2) Inline the selector`
1473+
);
1474+
});
14461475
});
14471476

14481477
describe('ng module bootstrap def', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.