-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@ngtools/webpack): emit diagnostic for unsupported conditional templateUrl expressions #32888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,7 @@ export function replaceResources( | |
| resourceImportDeclarations, | ||
| moduleKind, | ||
| inlineStyleFileExtension, | ||
| node, | ||
| ), | ||
| ), | ||
| ...(ts.getModifiers(node) ?? []), | ||
|
|
@@ -87,6 +88,7 @@ function visitDecorator( | |
| resourceImportDeclarations: ts.ImportDeclaration[], | ||
| moduleKind?: ts.ModuleKind, | ||
| inlineStyleFileExtension?: string, | ||
| classDeclaration?: ts.ClassDeclaration, | ||
| ): ts.Decorator { | ||
| if (!isComponentDecorator(node, typeChecker)) { | ||
| return node; | ||
|
|
@@ -106,6 +108,8 @@ function visitDecorator( | |
| const objectExpression = args[0]; | ||
| const styleReplacements: ts.Expression[] = []; | ||
|
|
||
| const className = classDeclaration?.name?.text ?? 'Unknown'; | ||
|
|
||
| // visit all properties | ||
| let properties = ts.visitNodes(objectExpression.properties, (node) => | ||
| ts.isObjectLiteralElementLike(node) | ||
|
|
@@ -116,6 +120,7 @@ function visitDecorator( | |
| resourceImportDeclarations, | ||
| moduleKind, | ||
| inlineStyleFileExtension, | ||
| className, | ||
| ) | ||
| : node, | ||
| ) as ts.NodeArray<ts.ObjectLiteralElementLike>; | ||
|
|
@@ -148,6 +153,7 @@ function visitComponentMetadata( | |
| resourceImportDeclarations: ts.ImportDeclaration[], | ||
| moduleKind: ts.ModuleKind = ts.ModuleKind.ES2015, | ||
| inlineStyleFileExtension?: string, | ||
| className?: string, | ||
| ): ts.ObjectLiteralElementLike | undefined { | ||
| if (!ts.isPropertyAssignment(node) || ts.isComputedPropertyName(node.name)) { | ||
| return node; | ||
|
|
@@ -161,7 +167,14 @@ function visitComponentMetadata( | |
| case 'templateUrl': { | ||
| const url = getResourceUrl(node.initializer); | ||
| if (!url) { | ||
| return node; | ||
| const sourceFile = node.getSourceFile(); | ||
| const { line } = sourceFile.getLineAndCharacterOfPosition(node.initializer.getStart()); | ||
|
|
||
| throw new Error( | ||
| `Component '${className}' in '${sourceFile.fileName}' contains a non-string literal ` + | ||
| `'templateUrl' value at line ${line + 1}. The 'templateUrl' property must be a ` + | ||
| `string literal. Expressions, variables, or other dynamic values are not supported.`, | ||
| ); | ||
| } | ||
|
Comment on lines
169
to
178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change correctly adds a build-time error for dynamic To ensure consistency and prevent these issues, a similar error should be thrown for non-string literal expressions in This would likely involve:
Additionally, please add tests for these |
||
|
|
||
| const importName = createResourceImport( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved readability and maintainability, consider using a template literal for this error message instead of string concatenation.