@@ -28,7 +28,6 @@ import {
28
28
} from '@schematics/angular/utility/ast-utils' ;
29
29
import { InsertChange } from '@schematics/angular/utility/change' ;
30
30
import { getWorkspace } from '@schematics/angular/utility/config' ;
31
- import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils' ;
32
31
import { WorkspaceProject } from '@schematics/angular/utility/workspace-models' ;
33
32
import chalk from 'chalk' ;
34
33
import { readFileSync } from 'fs' ;
@@ -37,6 +36,7 @@ import * as ts from 'typescript';
37
36
38
37
import { getProjectFromProgram } from './cli-workspace' ;
39
38
import { findHammerScriptImportElements } from './find-hammer-script-tags' ;
39
+ import { findMainModuleExpression } from './find-main-module' ;
40
40
import { isHammerJsUsedInTemplate } from './hammer-template-check' ;
41
41
import { getImportOfIdentifier , Import } from './identifier-imports' ;
42
42
import { ImportManager } from './import-manager' ;
@@ -53,6 +53,9 @@ const HAMMER_MODULE_SPECIFIER = 'hammerjs';
53
53
const CANNOT_REMOVE_REFERENCE_ERROR =
54
54
`Cannot remove reference to "GestureConfig". Please remove manually.` ;
55
55
56
+ const CANNOT_SETUP_APP_MODULE_ERROR = `Could not setup HammerJS gesture in module. Please ` +
57
+ `manually ensure that the Hammer gesture config is set up.` ;
58
+
56
59
interface IdentifierReference {
57
60
node : ts . Identifier ;
58
61
importData : Import ;
@@ -198,21 +201,8 @@ export class HammerGesturesRule extends MigrationRule<null> {
198
201
this . _gestureConfigReferences . forEach (
199
202
i => this . _replaceGestureConfigReference ( i , gestureConfigPath ) ) ;
200
203
201
- const appModulePath = getAppModulePath ( this . tree , getProjectMainFile ( project ) ) ;
202
- const sourceFile = this . program . getSourceFile ( join ( this . basePath , appModulePath ) ) ;
203
-
204
- if ( ! sourceFile ) {
205
- this . failures . push ( {
206
- filePath : appModulePath ,
207
- message : `Could not setup HammerJS gesture in module. Please manually ensure that ` +
208
- `the Hammer gesture config is set up.` ,
209
- position : { character : 0 , line : 0 }
210
- } ) ;
211
- return ;
212
- }
213
-
214
- // Setup the gesture config provider in the project app module if not done.
215
- this . _setupGestureConfigProviderIfNeeded ( sourceFile , appModulePath , gestureConfigPath ) ;
204
+ // Setup the gesture config provider in the project app module if not done already.
205
+ this . _setupGestureConfigInAppModule ( project , gestureConfigPath ) ;
216
206
}
217
207
218
208
/**
@@ -531,12 +521,38 @@ export class HammerGesturesRule extends MigrationRule<null> {
531
521
} ) ;
532
522
}
533
523
534
- /**
535
- * Sets up the Hammer gesture config provider in the given app module
536
- * if needed.
537
- */
538
- private _setupGestureConfigProviderIfNeeded (
539
- sourceFile : ts . SourceFile , appModulePath : string , configPath : string ) {
524
+ /** Sets up the Hammer gesture config provider in the app module if needed. */
525
+ private _setupGestureConfigInAppModule ( project : WorkspaceProject , configPath : string ) {
526
+ const mainFilePath = join ( this . basePath , getProjectMainFile ( project ) ) ;
527
+ const mainFile = this . program . getSourceFile ( mainFilePath ) ;
528
+ if ( ! mainFile ) {
529
+ this . failures . push ( {
530
+ filePath : mainFilePath ,
531
+ message : CANNOT_SETUP_APP_MODULE_ERROR ,
532
+ } ) ;
533
+ return ;
534
+ }
535
+
536
+ const appModuleExpr = findMainModuleExpression ( mainFile ) ;
537
+ if ( ! appModuleExpr ) {
538
+ this . failures . push ( {
539
+ filePath : mainFilePath ,
540
+ message : CANNOT_SETUP_APP_MODULE_ERROR ,
541
+ } ) ;
542
+ return ;
543
+ }
544
+
545
+ const appModuleSymbol = this . _getDeclarationSymbolOfNode ( unwrapExpression ( appModuleExpr ) ) ;
546
+ if ( ! appModuleSymbol || ! appModuleSymbol . valueDeclaration ) {
547
+ this . failures . push ( {
548
+ filePath : mainFilePath ,
549
+ message : CANNOT_SETUP_APP_MODULE_ERROR ,
550
+ } ) ;
551
+ return ;
552
+ }
553
+
554
+ const sourceFile = appModuleSymbol . valueDeclaration . getSourceFile ( ) ;
555
+ const relativePath = relative ( this . basePath , sourceFile . fileName ) ;
540
556
const hammerConfigTokenExpr = this . _importManager . addImportToSourceFile (
541
557
sourceFile , HAMMER_CONFIG_TOKEN_NAME , HAMMER_CONFIG_TOKEN_MODULE ) ;
542
558
const gestureConfigExpr = this . _importManager . addImportToSourceFile (
@@ -574,8 +590,8 @@ export class HammerGesturesRule extends MigrationRule<null> {
574
590
return ;
575
591
}
576
592
577
- const changeActions = addSymbolToNgModuleMetadata (
578
- sourceFile , appModulePath , 'providers' , this . _printNode ( newProviderNode , sourceFile ) , null ) ;
593
+ const changeActions = addSymbolToNgModuleMetadata ( sourceFile , relativePath , 'providers' ,
594
+ this . _printNode ( newProviderNode , sourceFile ) , null ) ;
579
595
580
596
changeActions . forEach ( change => {
581
597
if ( change instanceof InsertChange ) {
@@ -668,7 +684,7 @@ export class HammerGesturesRule extends MigrationRule<null> {
668
684
}
669
685
670
686
context . logger . info ( chalk . yellow (
671
- ' ⚠ The HammerJS v9 migration for Angular components is not able to migrate tests. ' +
687
+ '⚠ The HammerJS v9 migration for Angular components is not able to migrate tests. ' +
672
688
'Please manually clean up tests in your project if they rely on HammerJS.' ) ) ;
673
689
674
690
// Clean global state once the workspace has been migrated. This is technically
0 commit comments