-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
185 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
projects/cdk/schematics/ng-update/v4/steps/constants/functions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type {ReplacementType} from '../../../interfaces/replacement-type'; | ||
|
||
export const REPLACE_FUNCTIONS: readonly ReplacementType[] = [ | ||
{ | ||
from: 'tuiDocLanguageSwitcher', | ||
to: 'tuiLanguageSwitcher', | ||
moduleSpecifier: ['@taiga-ui/i18n'], | ||
}, | ||
]; |
25 changes: 25 additions & 0 deletions
25
projects/cdk/schematics/ng-update/v4/steps/replace-functions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {Node} from 'ng-morph'; | ||
|
||
import {getNamedImportReferences} from '../../../utils/get-named-import-references'; | ||
import type {ReplacementType} from '../../interfaces/replacement-type'; | ||
|
||
export function replaceFunctions(functions: readonly ReplacementType[]): void { | ||
functions.forEach(({from, to, moduleSpecifier}) => { | ||
getNamedImportReferences(from, moduleSpecifier).forEach((ref) => { | ||
if (ref.wasForgotten()) { | ||
return; | ||
} | ||
|
||
const parent = ref.getParent(); | ||
|
||
if (Node.isImportSpecifier(parent) || Node.isCallExpression(parent)) { | ||
parent?.replaceWithText( | ||
parent | ||
?.getText({includeJsDocComments: false}) | ||
.trim() | ||
.replace(from, to ?? from), | ||
); | ||
} | ||
}); | ||
}); | ||
} |
84 changes: 84 additions & 0 deletions
84
projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-i18n.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import {join} from 'node:path'; | ||
|
||
import {HostTree} from '@angular-devkit/schematics'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import type {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema'; | ||
import { | ||
createProject, | ||
createSourceFile, | ||
resetActiveProject, | ||
saveActiveProject, | ||
setActiveProject, | ||
} from 'ng-morph'; | ||
|
||
import {createAngularJson} from '../../../utils/create-angular-json'; | ||
|
||
const collectionPath = join(__dirname, '../../../migration.json'); | ||
|
||
const COMPONENT_BEFORE = ` | ||
import {tuiDocLanguageSwitcher} from '@taiga-ui/i18n'; | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
providers: [ | ||
tuiDocLanguageSwitcher(async (language): Promise<unknown> => {}) | ||
] | ||
}) | ||
export class Test { | ||
}`; | ||
|
||
const COMPONENT_AFTER = ` | ||
import {tuiLanguageSwitcher} from '@taiga-ui/i18n'; | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
providers: [ | ||
tuiLanguageSwitcher(async (language): Promise<unknown> => {}) | ||
] | ||
}) | ||
export class Test { | ||
}`; | ||
|
||
describe('ng-update', () => { | ||
let host: UnitTestTree; | ||
let runner: SchematicTestRunner; | ||
|
||
beforeEach(() => { | ||
host = new UnitTestTree(new HostTree()); | ||
runner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
setActiveProject(createProject(host)); | ||
|
||
createMainFiles(); | ||
|
||
saveActiveProject(); | ||
}); | ||
|
||
it('should migrate i18n references in ts files', async () => { | ||
const tree = await runner.runSchematic( | ||
'updateToV4', | ||
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>, | ||
host, | ||
); | ||
|
||
expect(tree.readContent('test/app/test.component.ts')).toEqual(COMPONENT_AFTER); | ||
}); | ||
|
||
afterEach(() => { | ||
resetActiveProject(); | ||
}); | ||
}); | ||
|
||
function createMainFiles(): void { | ||
createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE); | ||
|
||
createSourceFile('test/app/test.template.html', ''); | ||
|
||
createAngularJson(); | ||
createSourceFile( | ||
'package.json', | ||
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/i18n": "~3.42.0"}}', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters