File tree Expand file tree Collapse file tree 4 files changed +28
-20
lines changed
adev/src/content/reference/migrations
migrations/ngclass-to-class-migration
ng-generate/ngclass-to-class-migration Expand file tree Collapse file tree 4 files changed +28
-20
lines changed Original file line number Diff line number Diff line change 1
- # Migration to ngclass to class
1
+ # Migration from NgClass to class bindings
2
2
3
- This schematic migrates the ngClass to class in your application.
3
+ This schematic migrates NgClass directive usages to class bindings in your application.
4
+ It will only migrate usages that are considered safe to migrate.
4
5
5
6
Run the schematic using the following command:
6
7
@@ -19,4 +20,4 @@ ng generate @angular/core:ngclass-to-class
19
20
#### After
20
21
21
22
``` html
22
- <div [class.admin ] =" isAdmin" [class. dense] = " density === 'high'" >
23
+ <div [class] =" {admin: isAdmin, dense: density === 'high'} " >
Original file line number Diff line number Diff line change @@ -25,16 +25,22 @@ describe('ngClass migrator', () => {
25
25
await verifyDeclarationNoChange ( `<div [class.active]="isActive"></div>` ) ;
26
26
} ) ;
27
27
28
- it ( 'should not change empty ngClass binding' , async ( ) => {
29
- await verifyDeclarationNoChange ( `<div [ngClass]="{}"></div>` ) ;
28
+ it ( 'should change empty ngClass binding' , async ( ) => {
29
+ await verifyDeclaration ( {
30
+ before : `<div [ngClass]="{}"></div>` ,
31
+ after : `<div [class]=""></div>` ,
32
+ } ) ;
30
33
} ) ;
31
34
32
35
it ( 'should not change ngClass with empty string key' , async ( ) => {
33
36
await verifyDeclarationNoChange ( `<div [ngClass]="{'': condition}"></div>` ) ;
34
37
} ) ;
35
38
36
- it ( 'should not change ngClass with empty array' , async ( ) => {
37
- await verifyDeclarationNoChange ( `<div [ngClass]="[]"></div>` ) ;
39
+ it ( 'should change ngClass with empty array' , async ( ) => {
40
+ await verifyDeclaration ( {
41
+ before : `<div [ngClass]="[]"></div>` ,
42
+ after : `<div [class]=""></div>` ,
43
+ } ) ;
38
44
} ) ;
39
45
} ) ;
40
46
Original file line number Diff line number Diff line change @@ -200,10 +200,14 @@ export class NgClassCollector extends RecursiveVisitor {
200
200
201
201
const staticMatch = tryParseStaticObjectLiteral ( expr ) ;
202
202
203
- if ( staticMatch && staticMatch . length > 0 ) {
204
- const replacement = staticMatch
205
- . map ( ( { key, value} ) => `[class.${ key } ]="${ value } "` )
206
- . join ( ' ' ) ;
203
+ if ( staticMatch !== null ) {
204
+ let replacement : string ;
205
+
206
+ if ( staticMatch . length === 0 ) {
207
+ replacement = '[class]=""' ;
208
+ } else {
209
+ replacement = staticMatch . map ( ( { key, value} ) => `[class.${ key } ]="${ value } "` ) . join ( ' ' ) ;
210
+ }
207
211
208
212
this . replacements . push ( {
209
213
start : attr . sourceSpan . start . offset ,
@@ -221,14 +225,12 @@ export class NgClassCollector extends RecursiveVisitor {
221
225
function tryParseStaticObjectLiteral ( expr : string ) : { key : string ; value : string } [ ] | null {
222
226
const trimmedExpr = expr . trim ( ) ;
223
227
224
- // Early validation
225
- if ( ! isObjectLiteralSyntax ( trimmedExpr ) ) {
226
- return null ;
228
+ if ( trimmedExpr === '{}' || trimmedExpr === '[]' ) {
229
+ return [ ] ;
227
230
}
228
231
229
- // Handle empty object
230
- if ( trimmedExpr === '{}' ) {
231
- return [ ] ;
232
+ if ( ! isObjectLiteralSyntax ( trimmedExpr ) ) {
233
+ return null ;
232
234
}
233
235
234
236
try {
Original file line number Diff line number Diff line change 1
1
2
2
# ngClass to class migration
3
- This schematic helps developers to convert ngClass to class attributes.
4
- This is a purely aesthetic change and does not affect the behavior of the application.
3
+ This schematic helps developers to convert ngClass directive usages to class bindings where possible.
5
4
6
5
## How to run this migration?
7
6
The migration can be run using the following command:
@@ -26,5 +25,5 @@ Example:
26
25
<div [ngClass] =" {admin: isAdmin, dense: density === 'high'}" >
27
26
28
27
<!-- After -->
29
- <div [class.admin ] =" isAdmin" [class. dense] = " density === 'high'" >
28
+ <div [class] =" {admin: isAdmin, dense: density === 'high'} " >
30
29
```
You can’t perform that action at this time.
0 commit comments