Skip to content

Commit

Permalink
refactor(compiler-cli): add a helper to generate error `LOCAL_COMPILA…
Browse files Browse the repository at this point in the history
…TION_UNRESOLVED_CONST` in common scenarios (#54230)

A helper `validateLocalCompilationUnresolvedConst` is added to encapsulate a common pattern which leads to the error `LOCAL_COMPILATION_UNRESOLVED_CONST`.

PR Close #54230
  • Loading branch information
pmvald authored and thePunderWoman committed Feb 6, 2024
1 parent a3de5ba commit acc98a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {identifierOfNode, isFromDtsFile} from '../../../util/src/typescript';

import {InjectableClassRegistry} from './injectable_registry';
import {isAbstractClassDeclaration, readBaseClass} from './util';
import { CompilationMode } from '../../../transform';


/**
Expand Down Expand Up @@ -403,3 +404,19 @@ function getInheritedUndecoratedCtorDiagnostic(
baseNeedsDecorator} decorator ` +
`to ${baseClassName}, or add an explicit constructor to ${node.name.text}.`);
}

/**
* Throws `FatalDiagnosticError` with error code `LOCAL_COMPILATION_UNRESOLVED_CONST`
* if the compilation mode is local and the value is not resolved due to being imported
* from external files. This is a common scenario for errors in local compilation mode,
* and so this helper can be used to quickly generate the relevant errors.
*/
export function assertLocalCompilationUnresolvedConst(compilationMode: CompilationMode, value: ResolvedValue, nodeToHighlight: ts.Node, errorMessage: string): void {
if (compilationMode === CompilationMode.LOCAL && value instanceof DynamicValue &&
value.isFromUnknownIdentifier()) {
throw new FatalDiagnosticError(
ErrorCode.LOCAL_COMPILATION_UNRESOLVED_CONST,
nodeToHighlight,
errorMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {DynamicValue, PartialEvaluator, traceDynamicValue} from '../../../partia
import {ClassDeclaration, DeclarationNode, Decorator} from '../../../reflection';
import {CompilationMode} from '../../../transform';
import {TemplateSourceMapping} from '../../../typecheck/api';
import {createValueHasWrongTypeError, isStringArray, ResourceLoader} from '../../common';
import {createValueHasWrongTypeError, isStringArray, ResourceLoader, assertLocalCompilationUnresolvedConst} from '../../common';

/**
* The literal style url extracted from the decorator, along with metadata for diagnostics.
Expand Down Expand Up @@ -151,14 +151,13 @@ export function extractTemplate(
const resolvedTemplate = evaluator.evaluate(template.expression);

// The identifier used for @Component.template cannot be resolved in local compilation mode. An error specific to this situation is generated.
if (compilationMode === CompilationMode.LOCAL && resolvedTemplate instanceof DynamicValue &&
resolvedTemplate.isFromUnknownIdentifier()) {

throw new FatalDiagnosticError(
ErrorCode.LOCAL_COMPILATION_UNRESOLVED_CONST,
template.expression,
'Unresolved identifier found for @Component.template 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 declaration into a file within the compilation unit, 2) Inline the template, 3) Move the template into a separate .html file and include it using @Component.templateUrl');
}
assertLocalCompilationUnresolvedConst(
compilationMode, resolvedTemplate, template.expression,
'Unresolved identifier found for @Component.template 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 declaration into a file within the ' +
'compilation unit, 2) Inline the template, 3) Move the template into ' +
'a separate .html file and include it using @Component.templateUrl');

if (typeof resolvedTemplate !== 'string') {
throw createValueHasWrongTypeError(
Expand Down

0 comments on commit acc98a1

Please sign in to comment.