-
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
Changes from all commits
0abae8b
3aa479a
285f1b8
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,12 @@ 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
176
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( | ||
|
|
@@ -198,6 +209,19 @@ function visitComponentMetadata( | |
| ) as ts.StringLiteralLike, | ||
| ]; | ||
| } else if (ts.isArrayLiteralExpression(node.initializer)) { | ||
| if (!isInlineStyle) { | ||
| // Validate each element is a string literal for styleUrls | ||
| for (const element of node.initializer.elements) { | ||
| if (!ts.isStringLiteralLike(element)) { | ||
| const sourceFile = node.getSourceFile(); | ||
| const { line } = sourceFile.getLineAndCharacterOfPosition(element.getStart()); | ||
|
|
||
| throw new Error( | ||
| `Component '${className}' in '${sourceFile.fileName}' contains a non-string literal '${name}' value at line ${line + 1}. The '${name}' property must contain string literals. Expressions, variables, or other dynamic values are not supported.`, | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| styles = ts.visitNodes(node.initializer.elements, (node) => | ||
| transformInlineStyleLiteral( | ||
| node, | ||
|
|
@@ -208,6 +232,14 @@ function visitComponentMetadata( | |
| moduleKind, | ||
| ), | ||
| ) as ts.NodeArray<ts.Expression>; | ||
| } else if (!isInlineStyle) { | ||
| // styleUrl or styleUrls with a non-string, non-array initializer | ||
| const sourceFile = node.getSourceFile(); | ||
| const { line } = sourceFile.getLineAndCharacterOfPosition(node.initializer.getStart()); | ||
|
|
||
| throw new Error( | ||
| `Component '${className}' in '${sourceFile.fileName}' contains a non-string literal '${name}' value at line ${line + 1}. The '${name}' property must be a string literal. Expressions, variables, or other dynamic values are not supported.`, | ||
| ); | ||
| } else { | ||
| return node; | ||
| } | ||
|
|
||
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.